Project

General

Profile

Revision 77281e25

ID77281e25a336701ba883d3f166c3d1850f2ccace
Parent b37495b9
Child 25516d86

Added by Andreas Müller about 2 years ago

ref #8689 Cdm2Cdm import from app-import to cdmlib-io

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdm2cdm/Cdm2CdmDescriptionImport.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.cdm2cdm;
10

  
11
import java.util.HashSet;
12
import java.util.Set;
13

  
14
import org.apache.log4j.Logger;
15
import org.springframework.stereotype.Component;
16
import org.springframework.transaction.TransactionStatus;
17

  
18
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
19
import eu.etaxonomy.cdm.io.common.ITaxonNodeOutStreamPartitioner;
20
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
21
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitionerConcurrent;
22
import eu.etaxonomy.cdm.model.description.TaxonDescription;
23
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
24

  
25
/**
26
 * @author a.mueller
27
 * @since 17.08.2019
28
 */
29
@Component
30
public class Cdm2CdmDescriptionImport
31
        extends Cdm2CdmImportBase {
32

  
33
    private static final long serialVersionUID = -2111102574346601573L;
34
    private static final Logger logger = Logger.getLogger(Cdm2CdmDescriptionImport.class);
35

  
36

  
37
    @Override
38
    protected void doInvoke(Cdm2CdmImportState state) {
39
        setState(state);
40
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
41

  
42
        Cdm2CdmImportConfigurator config = state.getConfig();
43

  
44
        ITaxonNodeOutStreamPartitioner partitioner = getPartitioner(state, monitor, config);
45
        monitor.subTask("Start partitioning");
46
        doData(state, partitioner);
47
    }
48

  
49
    private void doData(Cdm2CdmImportState state, ITaxonNodeOutStreamPartitioner partitioner){
50
        TaxonNode node = partitioner.next();
51
        int partitionSize = 100;
52
        int count = 0;
53
        TransactionStatus tx = startTransaction();
54
        while (node != null) {
55
            doSingleNode(state, node);
56
            count++;
57
            if (count>=partitionSize){
58
                clearCache();
59
                try {
60
                    commitTransaction(tx);
61
                } catch (Exception e) {
62
                    logger.warn("Exception during commit node " + node.treeIndex());
63
                    e.printStackTrace();
64
                }
65
                tx = startTransaction();
66
                count=0;
67
            }
68
            node = partitioner.next();
69
        }
70
        commitTransaction(tx);
71
        partitioner.close();
72
    }
73

  
74
    private void doSingleNode(Cdm2CdmImportState state, TaxonNode node) {
75
        Set<TaxonDescription> result = new HashSet<>();
76
        logger.info(node.treeIndex());
77
        try {
78
            for (TaxonDescription desc : node.getTaxon().getDescriptions()){
79
                result.add(detache(desc));
80
            }
81
        } catch (Exception e) {
82
            logger.warn("Exception during detache node " + node.treeIndex());
83
            e.printStackTrace();
84
        }
85
        try {
86
            if (!result.isEmpty()){
87
                getDescriptionService().saveOrUpdate((Set)result);
88
                getCommonService().saveOrUpdate(toSave);
89
                toSave.clear();
90
            }
91
        } catch (Exception e) {
92
            logger.warn("Exception during save node " + node.treeIndex());
93
             e.printStackTrace();
94
        }
95
    }
96

  
97
    private ITaxonNodeOutStreamPartitioner getPartitioner(Cdm2CdmImportState state, IProgressMonitor monitor,
98
            Cdm2CdmImportConfigurator config) {
99
        ITaxonNodeOutStreamPartitioner partitioner = config.getPartitioner();
100
        if (partitioner == null){
101
            if(!config.isConcurrent()){
102
                partitioner = TaxonNodeOutStreamPartitioner.NewInstance(sourceRepo(state), state,
103
                        state.getConfig().getTaxonNodeFilter(), 100,
104
                        monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
105
                ((TaxonNodeOutStreamPartitioner)partitioner).setLastCommitManually(true);
106
            }else{
107
                partitioner = TaxonNodeOutStreamPartitionerConcurrent
108
                        .NewInstance(state.getConfig().getSource(), state.getConfig().getTaxonNodeFilter(),
109
                                1000, monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
110
            }
111
        }
112
        return partitioner;
113
    }
114

  
115

  
116
    @Override
117
    protected boolean doDescriptions(Cdm2CdmImportState state) {
118
        return true;
119
    }
120

  
121
    @Override
122
    protected boolean doCheck(Cdm2CdmImportState state) {
123
        return false;
124
    }
125

  
126
    @Override
127
    protected boolean isIgnore(Cdm2CdmImportState state) {
128
        return !state.getConfig().isDoDescriptions();
129
    }
130

  
131
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdm2cdm/Cdm2CdmImportBase.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.cdm2cdm;
10

  
11
import java.lang.reflect.Field;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.util.ArrayList;
15
import java.util.Collection;
16
import java.util.HashMap;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21
import java.util.UUID;
22

  
23
import org.apache.log4j.Logger;
24

  
25
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
26
import eu.etaxonomy.cdm.api.application.ICdmRepository;
27
import eu.etaxonomy.cdm.database.DbSchemaValidation;
28
import eu.etaxonomy.cdm.io.common.CdmImportBase;
29
import eu.etaxonomy.cdm.model.agent.AgentBase;
30
import eu.etaxonomy.cdm.model.agent.Contact;
31
import eu.etaxonomy.cdm.model.agent.Institution;
32
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
33
import eu.etaxonomy.cdm.model.agent.Person;
34
import eu.etaxonomy.cdm.model.agent.Team;
35
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
36
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
37
import eu.etaxonomy.cdm.model.common.Annotation;
38
import eu.etaxonomy.cdm.model.common.CdmBase;
39
import eu.etaxonomy.cdm.model.common.Credit;
40
import eu.etaxonomy.cdm.model.common.Extension;
41
import eu.etaxonomy.cdm.model.common.ExtensionType;
42
import eu.etaxonomy.cdm.model.common.IIntextReferencable;
43
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
44
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
45
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
46
import eu.etaxonomy.cdm.model.common.Identifier;
47
import eu.etaxonomy.cdm.model.common.IntextReference;
48
import eu.etaxonomy.cdm.model.common.Language;
49
import eu.etaxonomy.cdm.model.common.LanguageString;
50
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
51
import eu.etaxonomy.cdm.model.common.Marker;
52
import eu.etaxonomy.cdm.model.common.MarkerType;
53
import eu.etaxonomy.cdm.model.common.ReferencedEntityBase;
54
import eu.etaxonomy.cdm.model.common.RelationshipBase;
55
import eu.etaxonomy.cdm.model.common.SourcedEntityBase;
56
import eu.etaxonomy.cdm.model.common.VersionableEntity;
57
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
58
import eu.etaxonomy.cdm.model.description.DescriptionBase;
59
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
60
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
61
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
62
import eu.etaxonomy.cdm.model.description.Distribution;
63
import eu.etaxonomy.cdm.model.description.FeatureState;
64
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
65
import eu.etaxonomy.cdm.model.description.State;
66
import eu.etaxonomy.cdm.model.description.TaxonDescription;
67
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
68
import eu.etaxonomy.cdm.model.description.TextData;
69
import eu.etaxonomy.cdm.model.location.Country;
70
import eu.etaxonomy.cdm.model.location.NamedArea;
71
import eu.etaxonomy.cdm.model.media.ExternalLink;
72
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
73
import eu.etaxonomy.cdm.model.media.Media;
74
import eu.etaxonomy.cdm.model.media.Rights;
75
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
76
import eu.etaxonomy.cdm.model.name.HybridRelationship;
77
import eu.etaxonomy.cdm.model.name.NameRelationship;
78
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
79
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
80
import eu.etaxonomy.cdm.model.name.Registration;
81
import eu.etaxonomy.cdm.model.name.TaxonName;
82
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
83
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
84
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
85
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
86
import eu.etaxonomy.cdm.model.permission.User;
87
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
88
import eu.etaxonomy.cdm.model.reference.Reference;
89
import eu.etaxonomy.cdm.model.taxon.Classification;
90
import eu.etaxonomy.cdm.model.taxon.Synonym;
91
import eu.etaxonomy.cdm.model.taxon.Taxon;
92
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
93
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
94
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
95
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
96
import eu.etaxonomy.cdm.model.term.DefinedTerm;
97
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
98
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
99
import eu.etaxonomy.cdm.model.term.Representation;
100
import eu.etaxonomy.cdm.model.term.TermBase;
101
import eu.etaxonomy.cdm.model.term.TermCollection;
102
import eu.etaxonomy.cdm.model.term.TermNode;
103
import eu.etaxonomy.cdm.model.term.TermRelationBase;
104
import eu.etaxonomy.cdm.model.term.TermVocabulary;
105
import eu.etaxonomy.cdm.persistence.query.MatchMode;
106

  
107
/**
108
 * Base class for migrating data from one CDM instance to another.
109
 *
110
 * @author a.mueller
111
 * @since 17.08.2019
112
 */
113
public abstract class Cdm2CdmImportBase
114
        extends CdmImportBase<Cdm2CdmImportConfigurator, Cdm2CdmImportState> {
115

  
116
    private static final long serialVersionUID = 1344722304369624443L;
117
    private static final Logger logger = Logger.getLogger(Cdm2CdmImportBase.class);
118

  
119
    //quick and dirty
120
    private Cdm2CdmImportState state;
121

  
122
    //TODO move to state
123
    private Map<UUID, CdmBase> sessionCache = new HashMap<>();
124
    private Map<Class,Set<UUID>> existingObjects = new HashMap<>();
125
    protected Set<CdmBase> toSave = new HashSet<>();
126

  
127
    protected ICdmRepository sourceRepo(Cdm2CdmImportState state){
128
        ICdmRepository repo = state.getSourceRepository();
129
        if (repo == null){
130
            System.out.println("start source repo");
131
            boolean omitTermLoading = true;
132
            repo = CdmApplicationController.NewInstance(state.getConfig().getSource(),
133
                    DbSchemaValidation.VALIDATE, omitTermLoading);
134
            state.setSourceRepository(repo);
135
            System.out.println("end source repo");
136
        }
137
        return repo;
138
    }
139

  
140
    protected  Contact detache(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
141
        contact = CdmBase.deproxy(contact);
142
        if (contact == null){
143
            return contact;
144
        }else{
145
            return handlePersistedContact(contact);
146
        }
147
    }
148

  
149
    protected  IIntextReferencable detache(IIntextReferencable cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
150
        return (IIntextReferencable)detache((CdmBase)cdmBase);
151
    }
152
    protected  IIntextReferenceTarget detache(IIntextReferenceTarget cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
153
        return (IIntextReferenceTarget)detache((CdmBase)cdmBase);
154
    }
155

  
156
    protected <T extends CdmBase> T detache(T cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
157
        return detache(cdmBase, false);
158
    }
159

  
160
    protected <T extends CdmBase> T detache(T cdmBase, boolean notFromSource) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
161
        cdmBase = CdmBase.deproxy(cdmBase);
162
        if (cdmBase == null ){
163
            return cdmBase;
164
        }else if(isInCache(cdmBase)){
165
            return getCached(cdmBase);
166
        }else {
167
            if (existingObjects.get(cdmBase.getClass()) == null){
168
                loadExistingUuids(cdmBase.getClass());
169
            }
170
            boolean exists = existingObjects.get(cdmBase.getClass()).contains(cdmBase.getUuid());
171
            if (exists){
172
                Class<T> clazz = (Class<T>)cdmBase.getClass();
173
                T existingObj = getCommonService().find(clazz, cdmBase.getUuid());
174
                if (existingObj != null){
175
                    cache(existingObj);
176
                    return existingObj;
177
                }else{
178
                    logger.warn("Object should exist already but does not exist in target. This should not happen: " + cdmBase.getClass().getSimpleName() + "/" + cdmBase.getUuid());
179
                }
180
            }
181
        }
182
        if ( !cdmBase.isPersited()){
183
            logger.warn("Non persisted object not in cache and not in target DB. This should not happen: " + cdmBase.getUuid());
184
            return cdmBase; //should not happen anymore; either in cache or in target or persisted in source
185
        }else{
186
            return notFromSource? null : (T)handlePersisted(cdmBase);
187
        }
188
    }
189

  
190
    /**
191
     * @param class1
192
     * @return
193
     */
194
    private Set<UUID> loadExistingUuids(Class<? extends CdmBase> clazz) {
195
        List<UUID> list = getCommonService().listUuid(clazz);
196
        Set<UUID> result = new HashSet<>(list);
197
        existingObjects.put(clazz, result);
198
        return result;
199
    }
200

  
201
    protected <A extends CdmBase> CdmBase handlePersisted(A cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
202
        if(cdmBase instanceof TaxonNode){
203
            return handlePersistedTaxonNode((TaxonNode)cdmBase);
204
        }else if(cdmBase instanceof Taxon){
205
            return handlePersistedTaxon((Taxon)cdmBase);
206
        }else if(cdmBase instanceof Synonym){
207
            return handlePersistedSynonym((Synonym)cdmBase);
208
        }else if(cdmBase instanceof TaxonName){
209
            return handlePersistedTaxonName((TaxonName)cdmBase);
210
        }else if(cdmBase instanceof Team){
211
            return handlePersistedTeam((Team)cdmBase);
212
        }else if(cdmBase instanceof Person){
213
            return handlePersistedPerson((Person)cdmBase);
214
        }else if(cdmBase instanceof Classification){
215
            return handlePersistedClassification((Classification)cdmBase);
216
        }else if(cdmBase instanceof Reference){
217
            return handlePersistedReference((Reference)cdmBase);
218
        }else if(cdmBase instanceof SpecimenOrObservationBase){
219
            return handlePersistedSpecimenOrObservationBase((SpecimenOrObservationBase)cdmBase);
220
        }else if(cdmBase instanceof IdentifiableSource){
221
            return handlePersistedIdentifiableSource((IdentifiableSource)cdmBase);
222
        }else if(cdmBase instanceof DescriptionElementSource){
223
            return handlePersistedDescriptionElementSource((DescriptionElementSource)cdmBase);
224
        }else if(cdmBase instanceof CommonTaxonName){
225
            return handlePersistedCommonTaxonName((CommonTaxonName)cdmBase);
226
        }else if(cdmBase instanceof Distribution){
227
            return handlePersistedDistribution((Distribution)cdmBase);
228
        }else if(cdmBase instanceof TextData){
229
            return handlePersistedTextData((TextData)cdmBase);
230
        }else if(cdmBase instanceof HomotypicalGroup){
231
            return handlePersistedHomotypicalGroup((HomotypicalGroup)cdmBase);
232
        }else if(cdmBase instanceof TypeDesignationBase){
233
            return handlePersistedTypeDesignationBase((TypeDesignationBase)cdmBase);
234
        }else if(cdmBase instanceof TaxonDescription){
235
            return handlePersistedTaxonDescription((TaxonDescription)cdmBase);
236
        }else if(cdmBase instanceof NomenclaturalStatus){
237
            return handlePersistedNomenclaturalStatus((NomenclaturalStatus)cdmBase);
238
        }else if(cdmBase instanceof TaxonNameDescription){
239
            return handlePersistedTaxonNameDescription((TaxonNameDescription)cdmBase);
240
        }else if(cdmBase instanceof TaxonRelationship){
241
            return handlePersistedTaxonRelationship((TaxonRelationship)cdmBase);
242
        }else if(cdmBase instanceof HybridRelationship){
243
            return handlePersistedHybridRelationship((HybridRelationship)cdmBase);
244
        }else if(cdmBase instanceof NameRelationship){
245
            return handlePersistedNameRelationship((NameRelationship)cdmBase);
246
        }else if(cdmBase instanceof TaxonNodeAgentRelation){
247
            return handlePersistedTaxonNodeAgentRelation((TaxonNodeAgentRelation)cdmBase);
248
        }else if(cdmBase instanceof User){
249
            return handlePersistedUser((User)cdmBase);
250
        }else if(cdmBase instanceof Extension){
251
            return handlePersistedExtension((Extension)cdmBase);
252
        }else if(cdmBase instanceof Marker){
253
            return handlePersistedMarker((Marker)cdmBase);
254
        }else if(cdmBase instanceof Annotation){
255
            return handlePersistedAnnotation((Annotation)cdmBase);
256
        }else if(cdmBase instanceof LanguageString){
257
            return handlePersistedLanguageString((LanguageString)cdmBase);
258
        }else if(cdmBase instanceof TermVocabulary){
259
            return handlePersistedVocabulary((TermVocabulary<?>)cdmBase);
260
        }else if(cdmBase instanceof NamedArea){
261
            return handlePersistedNamedArea((NamedArea)cdmBase);
262
        }else if(cdmBase instanceof TermNode){
263
            return handlePersistedTermNode((TermNode)cdmBase);
264
        }else if(cdmBase instanceof Representation){
265
            return handlePersistedRepresentation((Representation)cdmBase);
266
        }else if(cdmBase instanceof InstitutionalMembership){
267
            return handlePersistedInstitutionalMembership((InstitutionalMembership)cdmBase);
268
        }else if(cdmBase instanceof Institution){
269
            return handlePersistedInstitution((Institution)cdmBase);
270
        }else if(cdmBase instanceof IntextReference){
271
            return handlePersistedIntextReference((IntextReference)cdmBase);
272
        }else if(cdmBase instanceof ExtensionType){
273
            return handlePersistedExtensionType((ExtensionType)cdmBase);
274
        }else if(cdmBase instanceof NomenclaturalStatusType){
275
            return handlePersistedNomenclaturalStatusType((NomenclaturalStatusType)cdmBase);
276
        }else if(cdmBase instanceof MarkerType){
277
            return handlePersistedMarkerType((MarkerType)cdmBase);
278
        }else if(cdmBase instanceof Rights){
279
            return handlePersistedRights((Rights)cdmBase);
280
        }else if(cdmBase instanceof DefinedTerm){
281
            return handlePersistedDefinedTerm((DefinedTerm)cdmBase);
282
        }else if(cdmBase instanceof DefinedTermBase){
283
            return handlePersistedTerm((DefinedTermBase<?>)cdmBase);
284
        }else {
285
            throw new RuntimeException("Type not yet supported: " + cdmBase.getClass().getCanonicalName());
286
        }
287
    }
288

  
289

  
290
    protected TaxonNode handlePersistedTaxonNode(TaxonNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
291

  
292
        TaxonNode result = handlePersisted((AnnotatableEntity)node);
293
        if (result ==null){
294
            return result;
295
        }
296
        //complete
297
        handleCollection(result, TaxonNode.class, "agentRelations", TaxonNodeAgentRelation.class);
298
        result.setTaxon(detache(result.getTaxon()));
299
        result.setReference(detache(node.getReference()));
300
        result.setSynonymToBeUsed(detache(result.getSynonymToBeUsed()));
301
        handleMap(result, TaxonNode.class, "excludedNote", Language.class, LanguageString.class);
302
        //classification, parent, children
303
        this.setInvisible(node, "classification", detache(node.getClassification()));
304
        handleParentTaxonNode(result);
305
        setNewCollection(node, TaxonNode.class, "childNodes", TaxonNode.class);
306
        return result;
307
    }
308

  
309
    private void handleParentTaxonNode(TaxonNode childNode) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
310
        TaxonNode parent = detache(childNode.getParent(), true);
311
        //TODO
312
        String microReference = null;
313
        Reference reference = null;
314
        if (parent == null && childNode.getClassification().getRootNode().equals(childNode)){
315
            //do nothing
316
        }else if (parent == null ){
317
            childNode.getClassification().addChildNode(childNode, reference, microReference) ;
318
        }else{
319
            parent.addChildNode(childNode, reference, microReference);
320
        }
321
    }
322

  
323
    protected Taxon handlePersistedTaxon(Taxon taxon) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
324
        Taxon result = handlePersisted((TaxonBase)taxon);
325
        //complete
326
        handleCollection(result, Taxon.class, "synonyms", Synonym.class);
327
//        handleCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
328
        setNewCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
329
        handleCollection(result, Taxon.class, "relationsFromThisTaxon", TaxonRelationship.class);
330
        handleCollection(result, Taxon.class, "relationsToThisTaxon", TaxonRelationship.class);
331
        if (this.doDescriptions(state)){
332
            handleCollection(result, Taxon.class, "descriptions", TaxonDescription.class);
333
        }else{
334
            setNewCollection(result, Taxon.class, "descriptions", TaxonDescription.class);
335
        }
336
        return result;
337
    }
338

  
339
    protected boolean doDescriptions(Cdm2CdmImportState state) {
340
        return false;
341
    }
342

  
343
    protected Synonym handlePersistedSynonym(Synonym synonym) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
344
        Synonym result = handlePersisted((TaxonBase)synonym);
345
        //complete
346
        setInvisible(result, "acceptedTaxon", detache(result.getAcceptedTaxon()));
347
        result.setType(detache(result.getType()));
348
        return result;
349
    }
350

  
351
    protected TaxonRelationship handlePersistedTaxonRelationship(TaxonRelationship taxRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
352
        TaxonRelationship result = handlePersisted((RelationshipBase)taxRel);
353
        //complete
354
        result.setFromTaxon(detache(result.getFromTaxon()));
355
        result.setToTaxon(detache(result.getToTaxon()));
356
        result.setType(detache(result.getType()));
357
        return result;
358
    }
359

  
360
    protected NameRelationship handlePersistedNameRelationship(NameRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
361
        NameRelationship result = handlePersisted((RelationshipBase)rel);
362
        //complete
363
        setInvisible(result, "relatedFrom", detache(result.getFromName()));
364
        setInvisible(result, "relatedTo", detache(result.getToName()));
365
//        result.setFromName(detache(result.getFromName()));
366
//        result.setToName(detache(result.getToName()));
367
        result.setType(detache(result.getType()));
368
        return result;
369
    }
370

  
371
    protected HybridRelationship handlePersistedHybridRelationship(HybridRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
372
        HybridRelationship result = handlePersisted((RelationshipBase)rel);
373
        //complete
374
        setInvisible(result, "relatedFrom", detache(result.getParentName()));
375
        setInvisible(result, "relatedTo", detache(result.getHybridName()));
376
//        result.setFromName(detache(result.getFromName()));
377
//        result.setToName(detache(result.getToName()));
378
        result.setType(detache(result.getType()));
379
        return result;
380
    }
381

  
382
    protected NomenclaturalStatus handlePersistedNomenclaturalStatus(NomenclaturalStatus status) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
383
        NomenclaturalStatus result = handlePersisted((ReferencedEntityBase)status);
384
        //complete
385
        result.setType(detache(result.getType()));
386
        return result;
387
    }
388

  
389
    protected TypeDesignationBase handlePersistedTypeDesignationBase(TypeDesignationBase<?> designation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
390
        TypeDesignationBase result = handlePersisted((SourcedEntityBase)designation);
391
        //complete
392
        result.setCitation(detache(result.getCitation()));
393
        handleCollection(result, TypeDesignationBase.class, "registrations", Registration.class);
394
        handleCollection(result, TypeDesignationBase.class, "typifiedNames", TaxonName.class);
395
        result.setTypeStatus(detache(result.getTypeStatus()));
396
        return result;
397
    }
398

  
399
    protected InstitutionalMembership handlePersistedInstitutionalMembership(InstitutionalMembership institutionalMembership) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
400
        InstitutionalMembership result = handlePersisted((VersionableEntity)institutionalMembership);
401
        //complete
402
//        result.setPerson(detache(result.getPerson()));
403
        setInvisible(result, "person", detache(result.getPerson()));
404
        result.setInstitute(detache(result.getInstitute()));
405
        return result;
406
    }
407

  
408
    protected Institution handlePersistedInstitution(Institution institution) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
409
        Institution result = handlePersisted((AgentBase)institution);
410
        //complete
411
        result.setIsPartOf(detache(result.getIsPartOf()));
412
        handleCollection(result, Institution.class, "types", DefinedTerm.class);
413
        return result;
414
    }
415

  
416
    protected TaxonNodeAgentRelation handlePersistedTaxonNodeAgentRelation(TaxonNodeAgentRelation nodeAgentRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
417
        TaxonNodeAgentRelation result = handlePersisted((AnnotatableEntity)nodeAgentRel);
418
        //complete
419
        result.setAgent(detache(result.getAgent()));
420
        result.setType(detache(result.getType()));
421
        setInvisible(result, "taxonNode", detache(result.getTaxonNode()));
422
        return result;
423
    }
424

  
425

  
426
    protected TaxonName handlePersistedTaxonName(TaxonName taxonName) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
427
        @SuppressWarnings("rawtypes")
428
        TaxonName result = handlePersisted((IdentifiableEntity)taxonName);
429
        //complete
430
        result.setRank(detache(result.getRank()));
431
        result.setCombinationAuthorship(detache(result.getCombinationAuthorship()));
432
        result.setExCombinationAuthorship(detache(result.getExCombinationAuthorship()));
433
        result.setBasionymAuthorship(detache(result.getBasionymAuthorship()));
434
        result.setExBasionymAuthorship(detache(result.getExBasionymAuthorship()));
435
        result.setInBasionymAuthorship(detache(result.getInBasionymAuthorship()));
436
        result.setInCombinationAuthorship(detache(result.getInCombinationAuthorship()));
437

  
438
        result.setNomenclaturalReference(detache(result.getNomenclaturalReference()));
439
        result.setNomenclaturalSource(detache(result.getNomenclaturalSource()));
440
        result.setHomotypicalGroup(detache(result.getHomotypicalGroup()));
441
        handleCollection(result, TaxonName.class, "descriptions", TaxonNameDescription.class);
442
        handleCollection(result, TaxonName.class, "hybridChildRelations", HybridRelationship.class);
443
        handleCollection(result, TaxonName.class, "hybridParentRelations", HybridRelationship.class);
444
        handleCollection(result, TaxonName.class, "relationsFromThisName", NameRelationship.class);
445
        handleCollection(result, TaxonName.class, "relationsToThisName", NameRelationship.class);
446
        handleCollection(result, TaxonName.class, "status", NomenclaturalStatus.class);
447

  
448
        handleCollection(result, TaxonName.class, "registrations", Registration.class);
449
        handleCollection(result, TaxonName.class, "typeDesignations", TypeDesignationBase.class);
450

  
451
        handleCollection(result, TaxonName.class, "taxonBases", TaxonBase.class);
452

  
453
        return result;
454
    }
455

  
456
    protected HomotypicalGroup handlePersistedHomotypicalGroup(HomotypicalGroup group) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
457
        HomotypicalGroup result = handlePersisted((AnnotatableEntity)group);
458
        //complete
459
        handleCollection(result, HomotypicalGroup.class, "typifiedNames", TaxonName.class);
460
        return result;
461
    }
462

  
463
    protected Annotation handlePersistedAnnotation(Annotation annotation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
464
        Annotation result = handlePersisted((AnnotatableEntity)annotation);
465
        //complete
466
        result.setAnnotationType(detache(annotation.getAnnotationType()));
467
        result.setCommentator(detache(result.getCommentator()));
468
        handleCollection(result, Annotation.class, "intextReferences", IntextReference.class);
469
        return result;
470
    }
471

  
472
    protected Extension handlePersistedExtension(Extension extension) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
473
        Extension result = handlePersisted((VersionableEntity)extension);
474
        //complete
475
        result.setType(detache(extension.getType()));
476
        return result;
477
    }
478

  
479
    protected Marker handlePersistedMarker(Marker marker) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
480
        Marker result = handlePersisted((VersionableEntity)marker);
481
        //complete
482
        result.setMarkerType(detache(marker.getMarkerType()));
483
        return result;
484
    }
485

  
486
    protected Team handlePersistedTeam(Team team) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
487
        Team result = handlePersisted((TeamOrPersonBase)team);
488
        //complete
489
        handleCollection(result, Team.class, "teamMembers", Person.class);
490
        return result;
491
    }
492

  
493
    protected Contact handlePersistedContact(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
494
        Contact result = contact; // getTarget(contact);
495
        if (result ==null){
496
            return result;
497
        }
498
        if (!contact.getAddresses().isEmpty() || !contact.getEmailAddresses().isEmpty()
499
               || !contact.getFaxNumbers().isEmpty() ||!contact.getPhoneNumbers().isEmpty()
500
               ||!contact.getUrls().isEmpty()){
501
            logger.warn("Addresses not yet implemented");
502
        }
503
        setInvisible(result, "addresses", new HashSet<>());
504
//        handleCollection(result, Contact.class, "", Address.class);
505
        setInvisible(result, "faxNumbers", new ArrayList<>());
506
        setInvisible(result, "phoneNumbers", new ArrayList<>());
507
        setInvisible(result, "emailAddresses", new ArrayList<>());
508
        setInvisible(result, "urls", new ArrayList<>());
509
        return result;
510
    }
511

  
512
    protected Person handlePersistedPerson(Person person) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
513
        Person result = handlePersisted((TeamOrPersonBase)person);
514
        //complete
515
        handleCollection(result, Person.class, "institutionalMemberships", InstitutionalMembership.class);
516
        return result;
517
    }
518

  
519
    protected NamedArea handlePersistedNamedArea(NamedArea area) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
520
        NamedArea result = handlePersisted((OrderedTermBase)area);
521
        //complete
522
        handleCollection(result, NamedArea.class, "countries", Country.class);
523
        result.setLevel(detache(result.getLevel()));
524
        result.setType(detache(result.getType()));
525
        result.setShape(detache(result.getShape()));
526
        return result;
527
    }
528

  
529
    protected Classification handlePersistedClassification(Classification classification) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
530
        Classification result = handlePersisted((IdentifiableEntity)classification);
531
        //complete
532
        result.setName(detache(classification.getName()));
533
        result.setReference(detache(classification.getReference()));
534
        result.setRootNode(detache(classification.getRootNode()));
535
        handleCollection(result, Classification.class, "geoScopes", NamedArea.class);
536
        handleMap(result, Classification.class, "description", Language.class, LanguageString.class);
537
        return result;
538
    }
539

  
540
    protected Reference handlePersistedReference(Reference reference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
541
        Reference result = handlePersisted((IdentifiableMediaEntity)reference);
542
        result.setAuthorship(detache(result.getAuthorship()));
543
        result.setInstitution(detache(result.getInstitution()));
544
        result.setSchool(detache(result.getSchool()));
545
        result.setInReference(detache(result.getInReference()));
546
        return result;
547
    }
548

  
549
    protected SpecimenOrObservationBase<?> handlePersistedSpecimenOrObservationBase(SpecimenOrObservationBase specimen) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
550
        //TODO implement for classes
551
        SpecimenOrObservationBase<?> result = handlePersisted((IdentifiableEntity)specimen);
552
        //complete
553
        result.setSex(detache(result.getSex()));
554
        result.setLifeStage(detache(result.getLifeStage()));
555
        result.setKindOfUnit(detache(result.getKindOfUnit()));
556
        handleCollection(result, SpecimenOrObservationBase.class, "determinations", DeterminationEvent.class);
557
        handleCollection(result, SpecimenOrObservationBase.class, "descriptions", SpecimenDescription.class);
558
        handleCollection(result, SpecimenOrObservationBase.class, "derivationEvents", DerivationEvent.class);
559
        handleMap(result, SpecimenOrObservationBase.class, "definition", Language.class, LanguageString.class);
560
        return result;
561
    }
562

  
563
    protected IdentifiableSource handlePersistedIdentifiableSource(IdentifiableSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
564
        IdentifiableSource result = handlePersisted((OriginalSourceBase)source);
565
        //complete
566
        return result;
567
    }
568

  
569
    protected DescriptionElementSource handlePersistedDescriptionElementSource(DescriptionElementSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
570
        DescriptionElementSource result = handlePersisted((OriginalSourceBase)source);
571
        //complete
572
        result.setNameUsedInSource(detache(result.getNameUsedInSource()));
573
        return result;
574
    }
575

  
576
    protected <T extends CommonTaxonName> T  handlePersistedCommonTaxonName(CommonTaxonName element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
577
        T result = handlePersisted((DescriptionElementBase)element);
578
        //complete
579
        result.setLanguage(detache(result.getLanguage()));
580
        result.setArea(detache(result.getArea()));
581
        return result;
582
    }
583

  
584
    protected <T extends TextData> T  handlePersistedTextData(TextData element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
585
        T result = handlePersisted((DescriptionElementBase)element);
586
        //complete
587
        result.setFormat(detache(result.getFormat()));
588
        handleMap(result, TextData.class, "multilanguageText", Language.class, LanguageString.class);
589
        return result;
590
    }
591

  
592
    protected <T extends Distribution> T  handlePersistedDistribution(Distribution element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
593
        T result = handlePersisted((DescriptionElementBase)element);
594
        //complete
595
        result.setArea(detache(result.getArea()));
596
        result.setStatus(detache(result.getStatus()));
597
        return result;
598
    }
599

  
600
    protected ExtensionType handlePersistedExtensionType(ExtensionType term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
601
        ExtensionType result = handlePersisted((DefinedTermBase)term);
602
        //complete
603
        return result;
604
    }
605

  
606
    protected MarkerType handlePersistedMarkerType(MarkerType term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
607
        MarkerType result = handlePersisted((DefinedTermBase)term);
608
        //complete
609
        return result;
610
    }
611

  
612
    protected NomenclaturalStatusType handlePersistedNomenclaturalStatusType(NomenclaturalStatusType term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
613
        NomenclaturalStatusType result = handlePersisted((OrderedTermBase)term);
614
        //complete
615
        return result;
616
    }
617

  
618
    protected DefinedTerm handlePersistedDefinedTerm(DefinedTerm term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
619
        DefinedTerm result = handlePersisted((DefinedTermBase)term);
620
        //complete
621
        return result;
622
    }
623

  
624
    //placeholder for not implemented methods for subclasses
625
    protected DefinedTermBase<?> handlePersistedTerm(DefinedTermBase term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
626
        DefinedTermBase<?> result = handlePersisted(term);
627
        logger.warn("Class not yet handled: " + term.getClass().getSimpleName());
628
        return result;
629
    }
630

  
631
    protected TermVocabulary<?> handlePersistedVocabulary(TermVocabulary voc) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
632
        TermVocabulary<?> result = (TermVocabulary<?>)handlePersisted((TermCollection)voc);
633
        handleCollection(result, TermVocabulary.class, "terms", DefinedTermBase.class);
634
        return result;
635
    }
636

  
637
    protected TermNode<?> handlePersistedTermNode(TermNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
638
        TermNode<?> result = (TermNode<?>)handlePersisted((TermRelationBase)node);
639
        //complete
640
        setInvisible(result, "parent", detache(result.getParent()));
641
        handleCollection(result, TermNode.class, "inapplicableIf", FeatureState.class);
642
        handleCollection(result, TermNode.class, "onlyApplicableIf", FeatureState.class);
643
        handleCollection(result, TermNode.class, "inapplicableIf_old", State.class);
644
        handleCollection(result, TermNode.class, "onlyApplicableIf_old", State.class);
645
        handleCollection(result, TermNode.class, "children", TermNode.class);
646

  
647
        return result;
648
    }
649

  
650
    protected Representation handlePersistedRepresentation(Representation representation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
651
        Representation result = (Representation)handlePersisted((LanguageStringBase)representation);
652
        return result;
653
    }
654

  
655
    protected <T extends TermBase> T  handlePersisted(TermBase termBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
656
        T result = handlePersisted((IdentifiableEntity)termBase);
657
        //complete
658
        handleCollection(result, TermBase.class, "representations", Representation.class);
659
        return result;
660
    }
661

  
662
    protected <T extends TermCollection> T  handlePersisted(TermCollection termCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
663
        T result = handlePersisted((TermBase)termCollection);
664
        //complete
665
        handleCollection(result, TermCollection.class, "termRelations", TermRelationBase.class);
666
        return result;
667
    }
668

  
669
    protected <T extends TermRelationBase> T  handlePersisted(TermRelationBase termRelationBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
670
        T result = handlePersisted((VersionableEntity)termRelationBase);
671
        result.setTerm(detache(result.getTerm()));
672
        setInvisible(result, TermRelationBase.class, "graph", detache(result.getGraph()));
673
        return result;
674
    }
675

  
676
    protected User handlePersistedUser(User user) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
677
        User result = (User)handlePersistedCdmBase(user);
678
        if (result.getUsername().equals("admin")){
679
            result = getUserService().listByUsername("admin", MatchMode.EXACT, null, null, null, null, null).iterator().next();
680
            getState().putPermanent(user.getUuid(), result);
681
            cache(result); //necessary?
682
            toSave.add(result);
683
            toSave.remove(user);
684
        }
685
        if (!result.isPersited()){
686
            result.setAuthorities(new HashSet<>());
687
            result.setGrantedAuthorities(new HashSet<>());
688
            setInvisible(result, "groups", new HashSet<>());
689
        }
690
        return result;
691
    }
692

  
693

  
694
    protected LanguageString handlePersistedLanguageString(LanguageString languageString) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
695
        LanguageString result = handlePersisted((LanguageStringBase)languageString);
696
        //complete
697
        handleCollection(result, LanguageString.class, "intextReferences", IntextReference.class);
698
        return result;
699
    }
700

  
701
    protected Rights handlePersistedRights(Rights rights) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
702
        Rights result = handlePersisted((LanguageStringBase)rights);
703
        result.setAgent(detache(rights.getAgent()));
704
        result.setType(rights.getType());
705
        //complete
706
        return result;
707
    }
708

  
709
    protected IntextReference handlePersistedIntextReference(IntextReference intextReference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
710
        IntextReference result = handlePersisted((VersionableEntity)intextReference);
711
        result.setReferencedEntity(detache(result.getReferencedEntity(), false));
712
        Method targetMethod = IntextReference.class.getDeclaredMethod("setTarget", IIntextReferenceTarget.class);
713
        targetMethod.setAccessible(true);
714
        targetMethod.invoke(result, detache(result.getTarget(), false));
715
        return result;
716
    }
717

  
718
    protected <T extends TaxonDescription> T  handlePersistedTaxonDescription(TaxonDescription taxDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
719
        T result = handlePersisted((DescriptionBase)taxDescription);
720
        //complete
721
        setInvisible(taxDescription, "taxon", detache(taxDescription.getTaxon()));
722
        handleCollection(taxDescription, TaxonDescription.class, "geoScopes", NamedArea.class);
723
        handleCollection(taxDescription, TaxonDescription.class, "scopes", DefinedTerm.class);
724
        return result;
725
    }
726

  
727
    protected <T extends TaxonDescription> T  handlePersistedTaxonNameDescription(TaxonNameDescription nameDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
728
        T result = handlePersisted((DescriptionBase)nameDescription);
729
        //complete
730
        setInvisible(nameDescription, "taxonName", detache(nameDescription.getTaxonName()));
731
        return result;
732
    }
733

  
734

  
735
// ***************************** BASE CLASSES ********************************************/
736

  
737
    protected <T extends CdmBase> T handlePersistedCdmBase(CdmBase cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
738
        T result = (T)getTarget(cdmBase);
739
        //complete
740
        cdmBase.setCreatedBy(detache(cdmBase.getCreatedBy()));
741
        return result;
742
    }
743

  
744
    protected <T extends VersionableEntity> T handlePersisted(VersionableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
745
        T result = (T)handlePersistedCdmBase((CdmBase)entity);
746
        //complete
747
        entity.setUpdatedBy(detache(entity.getUpdatedBy()));
748
        return result;
749
    }
750

  
751
    protected <T extends AnnotatableEntity> T handlePersisted(AnnotatableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
752
        T result = handlePersisted((VersionableEntity)entity);
753
        //complete
754
        handleCollection(result, AnnotatableEntity.class, "annotations", Annotation.class);
755
        handleCollection(result, AnnotatableEntity.class, "markers", Marker.class);
756
        return result;
757
    }
758

  
759
    protected <T extends SourcedEntityBase> T  handlePersisted(SourcedEntityBase sourcedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
760
        int originalId = sourcedEntity.getId();
761
        T result = handlePersisted((AnnotatableEntity)sourcedEntity);
762
        if (!result.isPersited() && getState().getConfig().isAddSources()){
763
            Reference sourceRef = getSourceReference(getState());
764
//            OriginalSourceBase<?> newSource = result.addImportSource(String.valueOf(originalId), sourcedEntity.getClass().getSimpleName(),
765
//                    sourceRef, null);
766
//            getCommonService().save(newSource);
767
        }
768
        //complete
769
        handleCollection(result, SourcedEntityBase.class, "sources", OriginalSourceBase.class);
770
        return result;
771
    }
772

  
773
    protected <T extends IdentifiableEntity> T  handlePersisted(IdentifiableEntity identifiableEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
774
        T result = handlePersisted((SourcedEntityBase)identifiableEntity);
775
        //complete
776
        handleCollection(result, IdentifiableEntity.class, "credits", Credit.class);
777
        handleCollection(result, IdentifiableEntity.class, "extensions", Extension.class);
778
        handleCollection(result, IdentifiableEntity.class, "identifiers", Identifier.class);
779
        handleCollection(result, IdentifiableEntity.class, "rights", Rights.class);
780
        return result;
781
    }
782

  
783
    protected <T extends DefinedTermBase> T  handlePersisted(DefinedTermBase definedTermBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
784
        T result = handlePersisted((TermBase)definedTermBase);
785
        //complete
786
        handleCollection(result, DefinedTermBase.class, "media", Media.class);
787
        handleCollection(result, DefinedTermBase.class, "generalizationOf", DefinedTermBase.class);
788
        handleCollection(result, DefinedTermBase.class, "includes", DefinedTermBase.class);
789
        result.setKindOf(detache(result.getKindOf()));
790
        result.setPartOf(detache(result.getPartOf()));
791
        setInvisible(result, DefinedTermBase.class, "vocabulary", detache(result.getVocabulary()));
792

  
793
        return result;
794
    }
795

  
796
    protected <T extends OriginalSourceBase> T  handlePersisted(OriginalSourceBase source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
797
        T result = handlePersisted((ReferencedEntityBase)source);
798
        //complete
799
        handleCollection(result, OriginalSourceBase.class, "links", ExternalLink.class);
800
        return result;
801
    }
802

  
803
    protected <T extends LanguageStringBase> T  handlePersisted(LanguageStringBase lsBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
804
        T result = handlePersisted((AnnotatableEntity)lsBase);
805
        //complete
806
        result.setLanguage(detache(lsBase.getLanguage()));
807
        return result;
808
    }
809

  
810
    protected <T extends TeamOrPersonBase> T  handlePersisted(TeamOrPersonBase teamOrPerson) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
811
        T result = handlePersisted((AgentBase)teamOrPerson);
812
        //complete
813
        return result;
814
    }
815

  
816
    protected <T extends AgentBase> T  handlePersisted(AgentBase agent) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
817
        T result = handlePersisted((IdentifiableMediaEntity)agent);
818
        result.setContact(detache(result.getContact()));
819
        //complete
820
        return result;
821
    }
822

  
823
    protected <T extends TaxonBase> T  handlePersisted(TaxonBase taxonBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
824
        T result = handlePersisted((IdentifiableEntity)taxonBase);
825
        //complete
826
        result.setName(detache(taxonBase.getName()));
827
        result.setSec(detache(taxonBase.getSec()));
828
        return result;
829
    }
830

  
831
    protected <T extends IdentifiableMediaEntity> T  handlePersisted(IdentifiableMediaEntity mediaEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
832
        T result = handlePersisted((IdentifiableEntity)mediaEntity);
833
        //complete
834
        handleCollection(result, IdentifiableMediaEntity.class, "media", Media.class);
835
        return result;
836
    }
837

  
838
    protected <T extends ReferencedEntityBase> T  handlePersisted(ReferencedEntityBase referencedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
839
        T result = handlePersisted((AnnotatableEntity)referencedEntity);
840
        //complete
841
        result.setCitation(detache(result.getCitation()));
842
        return result;
843
    }
844

  
845
    protected <T extends DescriptionBase> T  handlePersisted(DescriptionBase descriptionBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
846
        T result = handlePersisted((IdentifiableEntity)descriptionBase);
847
        //complete
848
        handleCollection(result, DescriptionBase.class, "descriptionElements", DescriptionElementBase.class);
849
        handleCollection(result, DescriptionBase.class, "descriptiveDataSets", DescriptiveDataSet.class);
850
        handleCollection(result, DescriptionBase.class, "descriptionSources", Reference.class);
851
        result.setDescribedSpecimenOrObservation(detache(descriptionBase.getDescribedSpecimenOrObservation()));
852
        return result;
853
    }
854

  
855
    protected <T extends DescriptionElementBase> T  handlePersisted(DescriptionElementBase element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
856
        T result = handlePersisted((AnnotatableEntity)element);
857
        //complete
858
        result.setFeature(detache(result.getFeature()));
859
        setInvisible(result, DescriptionElementBase.class, "inDescription", detache(result.getInDescription()));
860
        handleCollection(result, DescriptionElementBase.class, "sources", DescriptionElementSource.class);
861
        handleCollection(result, DescriptionElementBase.class, "media", Media.class);
862
        handleCollection(result, DescriptionElementBase.class, "modifiers", DefinedTerm.class);
863
        handleMap(result, DescriptionElementBase.class, "modifyingText", Language.class, LanguageString.class);
864

  
865
        return result;
866
    }
867

  
868
    protected <T extends RelationshipBase> T  handlePersisted(RelationshipBase relBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
869
        T result = handlePersisted((ReferencedEntityBase)relBase);
870
        return result;
871
    }
872

  
873

  
874
//************************** COLLECTIONS / MAPS ****************************************/
875

  
876
    protected <HOLDER extends CdmBase, ITEM extends CdmBase> void handleCollection(
877
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter, Class<ITEM> itemClass)
878
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
879

  
880
        Collection<ITEM> oldCollection = setNewCollection(holder, declaringClass, parameter, itemClass);
881
        Collection<ITEM> newCollection = getTargetCollection(itemClass, oldCollection);
882
        Field field = declaringClass.getDeclaredField(parameter);
883
        field.setAccessible(true);
884
        field.set(holder, newCollection);
885
    }
886

  
887
    protected <HOLDER extends CdmBase, KEY extends CdmBase, ITEM extends CdmBase>
888
            void handleMap(
889
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter,
890
            Class<KEY> keyClass, Class<ITEM> itemClass)
891
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
892

  
893
        //TODO we do not need to set the new map 2x
894
        Map<KEY,ITEM> oldMap = setNewMap(holder, declaringClass, parameter, keyClass, itemClass);
895
        Map<KEY,ITEM> newMap = getTargetMap(oldMap);
896
        Field field = declaringClass.getDeclaredField(parameter);
897
        field.setAccessible(true);
898
        field.set(holder, newMap);
899
    }
900

  
901
    protected <T extends CdmBase> Collection<T> setNewCollection(CdmBase obj, Class<?> holderClass,
902
            String parameter, Class<T> entityClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
903
        Field field = holderClass.getDeclaredField(parameter);
904
        field.setAccessible(true);
905
        Collection<T> oldValue = (Collection<T>)field.get(obj);
906
        Collection<T> newValue = null;
907
        if (Set.class.isAssignableFrom(field.getType())){
908
            newValue = new HashSet<>();
909
        }else if (List.class.isAssignableFrom(field.getType())){
910
            newValue = new ArrayList<>();
911
        }else{
912
            throw new RuntimeException("Unsupported collection type: " + field.getType().getCanonicalName());
913
        }
914
        field.set(obj, newValue);
915
        return oldValue;
916
    }
917

  
918
    protected <KEY extends CdmBase, ITEM extends CdmBase> Map<KEY,ITEM> setNewMap(CdmBase obj, Class<?> holderClass,
919
            String parameter, Class<KEY> keyClass, Class<ITEM> itemClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
920
        Field field = holderClass.getDeclaredField(parameter);
921
        field.setAccessible(true);
922
        Map<KEY,ITEM> oldValue = (Map<KEY,ITEM>)field.get(obj);
923
        Map<KEY,ITEM> newValue = null;
924
        if (Map.class.isAssignableFrom(field.getType())){
925
            newValue = new HashMap<>();
926
        }else{
927
            throw new RuntimeException("Unsupported map type: " + field.getType().getCanonicalName());
928
        }
929
        field.set(obj, newValue);
930
        return oldValue;
931
    }
932

  
933

  
934
    private <T extends Collection<S>, S extends CdmBase> Collection<S> getTargetCollection(Class<S> clazz, T sourceCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
935
        Collection<S> result =  new ArrayList<>();
936
        if (Set.class.isAssignableFrom(sourceCollection.getClass())){
937
            result = new HashSet<>();
938
        }
939
        for (S entity : sourceCollection){
940
            S target = detache(entity);
941
            result.add(target);
942
        }
943
        return result;
944
    }
945

  
946
    private <K extends CdmBase, V extends CdmBase> Map<K,V> getTargetMap(Map<K,V> sourceMap) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
947
        Map<K,V> result = new HashMap<>();
948
        for (K key : sourceMap.keySet()){
949
            K targetKey = detache(key);
950
            V targetValue = detache(sourceMap.get(key));
951
            result.put(targetKey, targetValue);
952
        }
953
        return result;
954
    }
955

  
956
// ****************************** INVISIBLE **************************************/
957

  
958
    protected void setInvisible(Object holder, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
959
        setInvisible(holder, holder.getClass(), fieldName, value);
960
    }
961
    protected void setInvisible(Object holder, Class<?> holderClazz, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
962
        Field field = holderClazz.getDeclaredField(fieldName);
963
        field.setAccessible(true);
964
        field.set(holder, value);
965
    }
966

  
967
// ************************* GET TARGET ******************************************/
968

  
969

  
970
    //TODO this should be cached for partition
971
    private <T extends CdmBase> T getTarget(T source) {
972
        if (source == null){
973
            return null;
974
        }
975
        T result = getCached(source);
976
//        if (result == null){
977
//            Class<T> clazz = (Class<T>)source.getClass();
978
//            result = getCommonService().find(clazz, source.getUuid());
979
//        }
980
        if (result == null){
981
            //Alternative: clone?
982
            result = CdmBase.deproxy(source);
983
            result.setId(0);
984
            cache(result);
985
            toSave.add(result);
986
        }
987
        return result;
988
    }
989

  
990
// ******************* CACHE *******************************************************/
991

  
992

  
993
    protected void cache(CdmBase cdmBase) {
994
       if (cdmBase instanceof User || cdmBase instanceof DefinedTermBase){
995
           getState().putPermanent(cdmBase.getUuid(), cdmBase);
996
       }else{
997
           sessionCache.put(cdmBase.getUuid(), cdmBase);
998
       }
999
       addExistingObject(cdmBase);
1000

  
1001
    }
1002

  
1003
    private void addExistingObject(CdmBase cdmBase) {
1004
        cdmBase = CdmBase.deproxy(cdmBase);
1005
        Set<UUID> set = existingObjects.get(cdmBase.getClass());
1006
        if (set == null){
1007
            set = loadExistingUuids(cdmBase.getClass());
1008
//            set = new HashSet<>();
1009
//            existingObjects.put(cdmBase.getClass(), set);
1010
        }
1011
        set.add(cdmBase.getUuid());
1012
    }
1013

  
1014
    protected boolean isInCache(CdmBase cdmBase) {
1015
        return getCached(cdmBase) != null;
1016
    }
1017

  
1018
    protected <T extends CdmBase> T getCached(T cdmBase) {
1019
        T result = (T)sessionCache.get(cdmBase.getUuid());
1020
        if (result == null){
1021
            result = (T)getState().getPermanent(cdmBase.getUuid());
1022
        }
1023
        return result;
1024
    }
1025

  
1026
    protected void clearCache() {
1027
        sessionCache.clear();
1028
    }
1029

  
1030
    private Reference getSourceReference(Cdm2CdmImportState state) {
1031
        UUID uuid = state.getConfig().getSourceRefUuid();
1032
        if (uuid == null){
1033
            uuid = state.getConfig().getSourceReference().getUuid();
1034
            state.getConfig().setSourceRefUuid(uuid);
1035
        }
1036
        Reference result = (Reference)sessionCache.get(uuid);
1037
        if (result == null){
1038
            result = (Reference)getState().getPermanent(uuid);
1039
        }
1040
        if (result == null){
1041
            result = getReferenceService().find(uuid);
1042
        }
1043
        if (result == null){
1044
            result = state.getConfig().getSourceReference();
1045
            getReferenceService().save(result);
1046
            sessionCache.put(result.getUuid(), result);
1047
        }
1048
        return result;
1049
    }
1050

  
1051
    public Cdm2CdmImportState getState() {
1052
        return state;
1053
    }
1054

  
1055
    public void setState(Cdm2CdmImportState state) {
1056
        this.state = state;
1057
    }
1058

  
1059
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdm2cdm/Cdm2CdmImportConfigurator.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.cdm2cdm;
10

  
11
import eu.etaxonomy.cdm.database.ICdmDataSource;
12
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
13
import eu.etaxonomy.cdm.io.common.ITaxonNodeOutStreamPartitioner;
14
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
15
import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
16
import eu.etaxonomy.cdm.model.reference.Reference;
17

  
18
/**
19
 * Configuration base class for Cdm2Cdm migration.
20
 *
21
 * @author a.mueller
22
 * @since 17.08.2019
23
 */
24
public  class Cdm2CdmImportConfigurator
25
        extends ImportConfiguratorBase<Cdm2CdmImportState, ICdmDataSource>{
26

  
27
    private static final long serialVersionUID = 5454400624983256935L;
28

  
29
    private static IInputTransformer myTransformer = null;
30

  
31
    private TaxonNodeFilter taxonNodeFilter = new TaxonNodeFilter();
32
    private ITaxonNodeOutStreamPartitioner partitioner;
33
    private boolean concurrent = false;
34

  
35
    private boolean doTaxa = true;
36
    private boolean doDescriptions = true;
37
    private boolean addSources = true;
38

  
39
//***************************** NewInstance ************************/
40

  
41
    public static Cdm2CdmImportConfigurator NewInstace(ICdmDataSource source, ICdmDataSource destination){
42
        return new Cdm2CdmImportConfigurator(source, destination);
43
    }
44

  
45
// ***************************** CONSTRUCTOR **********************/
46

  
47
    public Cdm2CdmImportConfigurator(ICdmDataSource source, ICdmDataSource destination) {
48
        super(myTransformer);
49
        this.setSource(source);
50
        this.setDestination(destination);
51
    }
52

  
53
// ****************************** METHODS *********************/
54

  
55
    @Override
56
    public Cdm2CdmImportState getNewState() {
57
        return new Cdm2CdmImportState(this);
58
    }
59

  
60
    @SuppressWarnings("unchecked")
61
    @Override
62
    protected void makeIoClassList() {
63
        ioClassList = new Class[]{
64
                Cdm2CdmTaxonNodeImport.class ,
65
                Cdm2CdmDescriptionImport.class ,
66
        };
67
    }
68

  
69
    @Override
70
    @Deprecated
71
    public Reference getSourceReference() {
72
        return sourceReference;
73
    }
74

  
75
// ******************* GETTER / SETTER ***************************/
76

  
77
    public TaxonNodeFilter getTaxonNodeFilter() {
78
        return taxonNodeFilter;
79
    }
80
    public void setTaxonNodeFilter(TaxonNodeFilter taxonNodeFilter) {
81
        this.taxonNodeFilter = taxonNodeFilter;
82
    }
83

  
84
    public ITaxonNodeOutStreamPartitioner getPartitioner() {
85
        return partitioner;
86
    }
87
    public void setPartitioner(ITaxonNodeOutStreamPartitioner partitioner) {
88
        this.partitioner = partitioner;
89
    }
90

  
91
    public boolean isConcurrent() {
92
        return concurrent;
93
    }
94
    public void setConcurrent(boolean concurrent) {
95
        this.concurrent = concurrent;
96
    }
97

  
98
    public boolean isDoDescriptions() {
99
        return doDescriptions;
100
    }
101
    public void setDoDescriptions(boolean doDescriptions) {
102
        this.doDescriptions = doDescriptions;
103
    }
104

  
105
    public boolean isDoTaxa() {
106
        return doTaxa;
107
    }
108
    public void setDoTaxa(boolean doTaxa) {
109
        this.doTaxa = doTaxa;
110
    }
111

  
112
    public boolean isAddSources() {
113
        return addSources;
114
    }
115
    public void setAddSources(boolean addSources) {
116
        this.addSources = addSources;
117
    }
118
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdm2cdm/Cdm2CdmImportState.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.cdm2cdm;
10

  
11
import java.util.HashMap;
12
import java.util.Map;
13
import java.util.UUID;
14

  
15
import eu.etaxonomy.cdm.api.application.ICdmRepository;
16
import eu.etaxonomy.cdm.io.common.ImportStateBase;
17
import eu.etaxonomy.cdm.model.common.CdmBase;
18

  
19
/**
20
 * Import state base class for migrating data from one CDM instance to another.
21
 *
22
 * @author a.mueller
23
 * @since 17.08.2019
24
 */
25
public class Cdm2CdmImportState
26
        extends ImportStateBase<Cdm2CdmImportConfigurator,Cdm2CdmImportBase>{
27

  
28
    private ICdmRepository sourceRepository;
29

  
30
    private Map<UUID, CdmBase> permanentCache = new HashMap<>();
31

  
32
//************************ CONSTRUCTOR **********************************/
33

  
34
    protected Cdm2CdmImportState(Cdm2CdmImportConfigurator config) {
35
        super(config);
36
    }
37

  
38
// ************************* GETTER / SETTER ****************************/
39

  
40
    public ICdmRepository getSourceRepository() {
41
        return sourceRepository;
42
    }
43
    public void setSourceRepository(ICdmRepository sourceRepository) {
44
        this.sourceRepository = sourceRepository;
45
    }
46

  
47
    public CdmBase getPermanent(UUID uuid) {
48
        return permanentCache.get(uuid);
49
    }
50
    public void putPermanent(UUID uuid, CdmBase cdmBase) {
51
        permanentCache.put(uuid, cdmBase);
52
    }
53
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/cdm2cdm/Cdm2CdmTaxonNodeImport.java
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.cdm2cdm;
10

  
11
import org.apache.log4j.Logger;
12
import org.springframework.stereotype.Component;
13
import org.springframework.transaction.TransactionStatus;
14

  
15
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
16
import eu.etaxonomy.cdm.io.common.ITaxonNodeOutStreamPartitioner;
17
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
18
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitionerConcurrent;
19
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
20

  
21
/**
22
 * @author a.mueller
23
 * @since 17.08.2019
24
 */
25
@Component
26
public class Cdm2CdmTaxonNodeImport
27
        extends Cdm2CdmImportBase {
28

  
29
    private static final long serialVersionUID = -2111102574346601573L;
30
    private static final Logger logger = Logger.getLogger(Cdm2CdmTaxonNodeImport.class);
31

  
32

  
33
    @Override
34
    protected void doInvoke(Cdm2CdmImportState state) {
35
        setState(state);
36
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
37

  
38
        Cdm2CdmImportConfigurator config = state.getConfig();
39

  
40
        ITaxonNodeOutStreamPartitioner partitioner = getPartitioner(state, monitor, config);
41
        monitor.subTask("Start partitioning");
42
        doData(state, partitioner);
43
    }
44

  
45
    private void doData(Cdm2CdmImportState state, ITaxonNodeOutStreamPartitioner partitioner){
46
        TaxonNode node = partitioner.next();
47
        int partitionSize = 100;
48
        int count = 0;
49
        TransactionStatus tx = startTransaction();
50
        while (node != null) {
51
            node = doSingleNode(state, node);
52
            count++;
53
            if (count>=partitionSize){
54
                clearCache();
55
                try {
56
                    commitTransaction(tx);
57
                } catch (Exception e) {
58
                    logger.warn("Exception during commit node " + node.treeIndex());
59
                    e.printStackTrace();
60
                }
61
                tx = startTransaction();
62
                count=0;
63
            }
64
            node = partitioner.next();
65
        }
66
        commitTransaction(tx);
67
        partitioner.close();
68
    }
69

  
70
    private TaxonNode doSingleNode(Cdm2CdmImportState state, TaxonNode node) {
71
        TaxonNode result = null;
72
        logger.info(node.treeIndex());
73
        try {
74
            result = detache(node);
75
        } catch (Exception e) {
76
            logger.warn("Exception during detache node " + node.treeIndex());
77
            e.printStackTrace();
78
        }
79
        try {
80
            if (result != null){
81
                getTaxonNodeService().saveOrUpdate(node);
82
                getCommonService().saveOrUpdate(toSave);
83
                toSave.clear();
84
            }
85
        } catch (Exception e) {
86
            logger.warn("Exception during save node " + node.treeIndex());
87
             e.printStackTrace();
88
        }
89

  
90
        return result;
91
    }
92

  
93
    private ITaxonNodeOutStreamPartitioner getPartitioner(Cdm2CdmImportState state, IProgressMonitor monitor,
94
            Cdm2CdmImportConfigurator config) {
95
        ITaxonNodeOutStreamPartitioner partitioner = config.getPartitioner();
96
        if (partitioner == null){
97
            if(!config.isConcurrent()){
98
                partitioner = TaxonNodeOutStreamPartitioner.NewInstance(sourceRepo(state), state,
99
                        state.getConfig().getTaxonNodeFilter(), 100,
100
                        monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
101
                ((TaxonNodeOutStreamPartitioner)partitioner).setLastCommitManually(true);
102
            }else{
103
                partitioner = TaxonNodeOutStreamPartitionerConcurrent
104
                        .NewInstance(state.getConfig().getSource(), state.getConfig().getTaxonNodeFilter(),
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)