Project

General

Profile

« Previous | Next » 

Revision c0238c65

Added by Andreas Müller over 4 years ago

ref #8479 move cdm2cdm methods down to base class

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/fauEu2Cdm/FauEu2CdmImport.java
8 8
*/
9 9
package eu.etaxonomy.cdm.io.pesi.fauEu2Cdm;
10 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 11
import org.apache.log4j.Logger;
24 12
import org.springframework.stereotype.Component;
25 13
import org.springframework.transaction.TransactionStatus;
......
28 16
import eu.etaxonomy.cdm.io.common.ITaxonNodeOutStreamPartitioner;
29 17
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
30 18
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitionerConcurrent;
31
import eu.etaxonomy.cdm.model.agent.AgentBase;
32
import eu.etaxonomy.cdm.model.agent.Contact;
33
import eu.etaxonomy.cdm.model.agent.Institution;
34
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
35
import eu.etaxonomy.cdm.model.agent.Person;
36
import eu.etaxonomy.cdm.model.agent.Team;
37
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
38
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
39
import eu.etaxonomy.cdm.model.common.Annotation;
40
import eu.etaxonomy.cdm.model.common.CdmBase;
41
import eu.etaxonomy.cdm.model.common.Credit;
42
import eu.etaxonomy.cdm.model.common.Extension;
43
import eu.etaxonomy.cdm.model.common.ExtensionType;
44
import eu.etaxonomy.cdm.model.common.IIntextReferencable;
45
import eu.etaxonomy.cdm.model.common.IIntextReferenceTarget;
46
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
47
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
48
import eu.etaxonomy.cdm.model.common.Identifier;
49
import eu.etaxonomy.cdm.model.common.IntextReference;
50
import eu.etaxonomy.cdm.model.common.Language;
51
import eu.etaxonomy.cdm.model.common.LanguageString;
52
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
53
import eu.etaxonomy.cdm.model.common.Marker;
54
import eu.etaxonomy.cdm.model.common.ReferencedEntityBase;
55
import eu.etaxonomy.cdm.model.common.RelationshipBase;
56
import eu.etaxonomy.cdm.model.common.SourcedEntityBase;
57
import eu.etaxonomy.cdm.model.common.VersionableEntity;
58
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
59
import eu.etaxonomy.cdm.model.description.DescriptionBase;
60
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
61
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
62
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
63
import eu.etaxonomy.cdm.model.description.Distribution;
64
import eu.etaxonomy.cdm.model.description.FeatureState;
65
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
66
import eu.etaxonomy.cdm.model.description.State;
67
import eu.etaxonomy.cdm.model.description.TaxonDescription;
68
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
69
import eu.etaxonomy.cdm.model.description.TextData;
70
import eu.etaxonomy.cdm.model.location.Country;
71
import eu.etaxonomy.cdm.model.location.NamedArea;
72
import eu.etaxonomy.cdm.model.media.ExternalLink;
73
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
74
import eu.etaxonomy.cdm.model.media.Media;
75
import eu.etaxonomy.cdm.model.media.Rights;
76
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
77
import eu.etaxonomy.cdm.model.name.HybridRelationship;
78
import eu.etaxonomy.cdm.model.name.NameRelationship;
79
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
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 19
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 20
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 21

  
107 22
/**
108 23
 * @author a.mueller
......
115 30
    private static final long serialVersionUID = -2111102574346601573L;
116 31
    private static final Logger logger = Logger.getLogger(FauEu2CdmImport.class);
117 32

  
118
    //TODO move to state
119
    private Map<UUID, CdmBase> sessionCache = new HashMap<>();
120
    private Map<UUID, CdmBase> permanentCache = new HashMap<>();
121
    private Set<UUID> movedObjects = new HashSet<>();
122

  
123
    private Set<CdmBase> toSave = new HashSet<>();
124

  
125 33

  
126 34
    @Override
127 35
    protected void doInvoke(FauEu2CdmImportState state) {
......
130 38
        FauEu2CdmImportConfigurator config = state.getConfig();
131 39
        setRootId(state, config); //necessary?
132 40

  
133
        ITaxonNodeOutStreamPartitioner partitioner = config.getPartitioner();
134
        if (partitioner == null){
135
//        @SuppressWarnings("unchecked")
136
//        TaxonNodeOutStreamPartitioner<FauEu2CdmImportState> partitioner = TaxonNodeOutStreamPartitioner
137
//                .NewInstance(source(state), state, state.getConfig().getTaxonNodeFilter(), 100, monitor, null);
138
//        partitioner.setLastCommitManually(true);
139

  
140
            partitioner = TaxonNodeOutStreamPartitionerConcurrent
141
                    .NewInstance(state.getConfig().getSource(), state.getConfig().getTaxonNodeFilter(),
142
                            1000, monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
143

  
144
        }
41
        ITaxonNodeOutStreamPartitioner partitioner = getPartitioner(state, monitor, config);
145 42
        monitor.subTask("Start partitioning");
146 43
        doData(state, partitioner);
147 44
    }
148 45

  
149
    /**
150
     * @param state
151
     * @param config
152
     */
153
    private void setRootId(FauEu2CdmImportState state, FauEu2CdmImportConfigurator config) {
154
        if (config.getTaxonNodeFilter().hasClassificationFilter()) {
155
            Classification classification = getClassificationService()
156
                    .load(config.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
157
            state.setRootId(classification.getRootNode().getUuid());
158
        } else if (config.getTaxonNodeFilter().hasSubtreeFilter()) {
159
            state.setRootId(config.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
160
        }
161
    }
162

  
163 46
    private void doData(FauEu2CdmImportState state, ITaxonNodeOutStreamPartitioner partitioner){
164 47
        TaxonNode node = partitioner.next();
165 48
        int partitionSize = 100;
......
208 91
        return result;
209 92
    }
210 93

  
211
    private void clearCache() {
212
        sessionCache.clear();
213
    }
214

  
215
    private TaxonNode handlePersistedTaxonNode(TaxonNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
216

  
217
        TaxonNode result = handlePersisted((AnnotatableEntity)node);
218
        if (result ==null){
219
            return result;
220
        }
221
        handleCollection(result, TaxonNode.class, "agentRelations", TaxonNodeAgentRelation.class);
222
        result.setTaxon(detache(result.getTaxon()));
223
        result.setReference(detache(node.getReference()));
224
        handleMap(result, TaxonNode.class, "excludedNote", Language.class, LanguageString.class);
225
        //classification, parent, children
226
        this.setInvisible(node, "classification", detache(node.getClassification()));
227
        handleParentTaxonNode(result);
228
        setNewCollection(node, TaxonNode.class, "childNodes", TaxonNode.class);
229
        return result;
230
    }
231

  
232
    private void handleParentTaxonNode(TaxonNode childNode) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
233
        TaxonNode parent = detache(childNode.getParent(), true);
234
        //TODO
235
        String microReference = null;
236
        Reference reference = null;
237
        if (parent == null && childNode.getClassification().getRootNode().equals(childNode)){
238
            //do nothing
239
        }else if (parent == null ){
240
            childNode.getClassification().addChildNode(childNode, reference, microReference) ;
241
        }else{
242
            parent.addChildNode(childNode, reference, microReference);
243
        }
244
    }
245

  
246
    private void setInvisible(Object holder, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
247
        setInvisible(holder, holder.getClass(), fieldName, value);
248
    }
249
    private void setInvisible(Object holder, Class<?> holderClazz, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
250
        Field field = holderClazz.getDeclaredField(fieldName);
251
        field.setAccessible(true);
252
        field.set(holder, value);
253
    }
254

  
255
    private Classification handlePersistedClassification(Classification classification) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
256
        Classification result = handlePersisted((IdentifiableEntity)classification);
257
        if (result ==null){
258
            return result;
259
        }
260
        result.setName(detache(classification.getName()));
261
        result.setReference(detache(classification.getReference()));
262
        result.setRootNode(detache(classification.getRootNode()));
263
        handleCollection(result, Classification.class, "geoScopes", NamedArea.class);
264
        handleMap(result, Classification.class, "description", Language.class, LanguageString.class);
265

  
266
        return result;
267
    }
268

  
269
    private Reference handlePersistedReference(Reference reference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
270
        Reference result = handlePersisted((IdentifiableMediaEntity)reference);
271
        result.setAuthorship(detache(result.getAuthorship()));
272
        result.setInstitution(detache(result.getInstitution()));
273
        result.setSchool(detache(result.getSchool()));
274
        result.setInReference(detache(result.getInReference()));
275
        return result;
276
    }
277

  
278
    private SpecimenOrObservationBase<?> handlePersistedSpecimenOrObservationBase(SpecimenOrObservationBase specimen) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
279
        SpecimenOrObservationBase<?> result = handlePersisted((IdentifiableEntity)specimen);
280
        result.setSex(detache(result.getSex()));
281
        result.setLifeStage(detache(result.getLifeStage()));
282
        result.setKindOfUnit(detache(result.getKindOfUnit()));
283
        //TODO implement for classes
284
        handleCollection(result, SpecimenOrObservationBase.class, "determinations", DeterminationEvent.class);
285
        handleCollection(result, SpecimenOrObservationBase.class, "descriptions", SpecimenDescription.class);
286
        handleCollection(result, SpecimenOrObservationBase.class, "derivationEvents", DerivationEvent.class);
287
        handleMap(result, SpecimenOrObservationBase.class, "definition", Language.class, LanguageString.class);
288
        return result;
289
    }
290

  
291
    private IdentifiableSource handlePersistedIdentifiableSource(IdentifiableSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
292
        IdentifiableSource result = handlePersisted((OriginalSourceBase)source);
293
        if (result ==null){
294
            return null;
295
        }
296
        return result;
297
    }
298

  
299
    private DescriptionElementSource handlePersistedDescriptionElementSource(DescriptionElementSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
300
        DescriptionElementSource result = handlePersisted((OriginalSourceBase)source);
301
        result.setNameUsedInSource(detache(result.getNameUsedInSource()));
302
        return result;
303
    }
304

  
305
    private <T extends OriginalSourceBase> T  handlePersisted(OriginalSourceBase source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
306
        T result = handlePersisted((ReferencedEntityBase)source);
307
        handleCollection(result, OriginalSourceBase.class, "links", ExternalLink.class);
308

  
309
        return result;
310
    }
311

  
312
    private LanguageString handlePersistedLanguageString(LanguageString languageString) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
313
        LanguageString result = handlePersisted((LanguageStringBase)languageString);
314
        handleCollection(result, LanguageString.class, "intextReferences", IntextReference.class);
315
        return result;
316
    }
317

  
318
    private IntextReference handlePersistedIntextReference(IntextReference intextReference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
319
        IntextReference result = handlePersisted((VersionableEntity)intextReference);
320
        result.setReferencedEntity(detache(result.getReferencedEntity(), false));
321
        Method targetMethod = IntextReference.class.getDeclaredMethod("setTarget", IIntextReferenceTarget.class);
322
        targetMethod.setAccessible(true);
323
        targetMethod.invoke(result, detache(result.getTarget(), false));
324
        return result;
325
    }
326

  
327
    private <T extends LanguageStringBase> T  handlePersisted(LanguageStringBase lsBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
328
        T result = handlePersisted((AnnotatableEntity)lsBase);
329
        result.setLanguage(detache(lsBase.getLanguage()));
330
        return result;
331
    }
332

  
333
    private <T extends IdentifiableEntity> T  handlePersisted(IdentifiableEntity identifiableEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
334
        T result = handlePersisted((SourcedEntityBase)identifiableEntity);
335
        if (result ==null){
336
            return null;
337
        }
338
        handleCollection(result, IdentifiableEntity.class, "credits", Credit.class);
339
        handleCollection(result, IdentifiableEntity.class, "extensions", Extension.class);
340
        handleCollection(result, IdentifiableEntity.class, "identifiers", Identifier.class);
341
        handleCollection(result, IdentifiableEntity.class, "rights", Rights.class);
342
        return result;
343
    }
344

  
345
    private <T extends TeamOrPersonBase> T  handlePersisted(TeamOrPersonBase teamOrPerson) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
346
        T result = handlePersisted((AgentBase)teamOrPerson);
347
        return result;
348
    }
349

  
350
    private <T extends AgentBase> T  handlePersisted(AgentBase agent) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
351
        T result = handlePersisted((IdentifiableMediaEntity)agent);
352
        result.setContact(detache(result.getContact()));
353
        return result;
354
    }
355

  
356
    private <T extends TaxonBase> T  handlePersisted(TaxonBase taxonBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
357
        T result = handlePersisted((IdentifiableEntity)taxonBase);
358
        result.setName(detache(taxonBase.getName()));
359
        result.setSec(detache(taxonBase.getSec()));
360
        return result;
361
    }
362

  
363
    private <T extends SourcedEntityBase> T  handlePersisted(SourcedEntityBase sourcedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
364
        T result = handlePersisted((AnnotatableEntity)sourcedEntity);
365
        if (result ==null){
366
            return null;
367
        }
368
        handleCollection(result, SourcedEntityBase.class, "sources", OriginalSourceBase.class);
369
        return result;
370
    }
371

  
372
    private <T extends IdentifiableMediaEntity> T  handlePersisted(IdentifiableMediaEntity mediaEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
373
        T result = handlePersisted((IdentifiableEntity)mediaEntity);
374
        handleCollection(result, IdentifiableMediaEntity.class, "media", Media.class);
375
        return result;
376
    }
377

  
378
    private <T extends ReferencedEntityBase> T  handlePersisted(ReferencedEntityBase referencedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
379
        T result = handlePersisted((AnnotatableEntity)referencedEntity);
380
        if (result ==null){
381
            return null;
382
        }
383
        result.setCitation(detache(result.getCitation()));
384
        return result;
385
    }
386

  
387
    private <T extends DescriptionBase> T  handlePersisted(DescriptionBase descriptionBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
388
        T result = handlePersisted((IdentifiableEntity)descriptionBase);
389
        handleCollection(result, DescriptionBase.class, "descriptionElements", DescriptionElementBase.class);
390
        handleCollection(result, DescriptionBase.class, "descriptiveDataSets", DescriptiveDataSet.class);
391
        handleCollection(result, DescriptionBase.class, "descriptionSources", Reference.class);
392
        result.setDescribedSpecimenOrObservation(detache(descriptionBase.getDescribedSpecimenOrObservation()));
393
        return result;
394
    }
395

  
396
    private <T extends DescriptionElementBase> T  handlePersisted(DescriptionElementBase element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
397
        T result = handlePersisted((AnnotatableEntity)element);
398
        result.setFeature(detache(result.getFeature()));
399
        setInvisible(result, DescriptionElementBase.class, "inDescription", detache(result.getInDescription()));
400
        handleCollection(result, DescriptionElementBase.class, "sources", DescriptionElementSource.class);
401
        handleCollection(result, DescriptionElementBase.class, "media", Media.class);
402
        handleCollection(result, DescriptionElementBase.class, "modifiers", DefinedTerm.class);
403
        handleMap(result, DescriptionElementBase.class, "modifyingText", Language.class, LanguageString.class);
404

  
405
        return result;
406
    }
407

  
408
    private <T extends CommonTaxonName> T  handlePersistedCommonTaxonName(CommonTaxonName element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
409
        T result = handlePersisted((DescriptionElementBase)element);
410
        result.setLanguage(detache(result.getLanguage()));
411
        result.setArea(detache(result.getArea()));
412
        return result;
413
    }
414

  
415
    private <T extends TextData> T  handlePersistedTextData(TextData element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
416
        T result = handlePersisted((DescriptionElementBase)element);
417
        result.setFormat(detache(result.getFormat()));
418
        handleMap(result, TextData.class, "multilanguageText", Language.class, LanguageString.class);
419
        return result;
420
    }
421

  
422
    private <T extends Distribution> T  handlePersistedDistribution(Distribution element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
423
        T result = handlePersisted((DescriptionElementBase)element);
424
        result.setArea(detache(result.getArea()));
425
        result.setStatus(detache(result.getStatus()));
426
        return result;
427
    }
428

  
429
    private <T extends RelationshipBase> T  handlePersisted(RelationshipBase relBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
430
        T result = handlePersisted((ReferencedEntityBase)relBase);
431
        return result;
432
    }
433

  
434
    private <T extends TaxonDescription> T  handlePersistedTaxonDescription(TaxonDescription taxDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
435
        T result = handlePersisted((DescriptionBase)taxDescription);
436
        setInvisible(taxDescription, "taxon", detache(taxDescription.getTaxon()));
437
        handleCollection(taxDescription, TaxonDescription.class, "geoScopes", NamedArea.class);
438
        handleCollection(taxDescription, TaxonDescription.class, "scopes", DefinedTerm.class);
439
        return result;
440
    }
441

  
442
    private <T extends TaxonDescription> T  handlePersistedTaxonNameDescription(TaxonNameDescription nameDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
443
        T result = handlePersisted((DescriptionBase)nameDescription);
444
        setInvisible(nameDescription, "taxonName", detache(nameDescription.getTaxonName()));
445
        return result;
446
    }
447

  
448
    private <T extends AnnotatableEntity> T handlePersisted(AnnotatableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
449
        T result = handlePersisted((VersionableEntity)entity);
450
        if (result ==null){
451
            return null;
452
        }
453
        handleCollection(result, AnnotatableEntity.class, "annotations", Annotation.class);
454
        handleCollection(result, AnnotatableEntity.class, "markers", Marker.class);
455
        return result;
456
    }
457

  
458
    private <HOLDER extends CdmBase, ITEM extends CdmBase> void handleCollection(
459
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter, Class<ITEM> itemClass)
460
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
461

  
462
        Collection<ITEM> oldCollection = setNewCollection(holder, declaringClass, parameter, itemClass);
463
        Collection<ITEM> newCollection = getTargetCollection(itemClass, oldCollection);
464
        Field field = declaringClass.getDeclaredField(parameter);
465
        field.setAccessible(true);
466
        field.set(holder, newCollection);
467
//        EntityCollectionSetterAdapter<HOLDER,ITEM> adapter = new EntityCollectionSetterAdapter(declaringClass, itemClass, parameter);
468
//        if ("rights".equals(parameter)){
469
//            adapter.setAddMethodName("addRights");
470
//            adapter.setRemovMethodName("removeRights");
471
//        }
472
//        if ("media".equals(parameter)){
473
//            adapter.setAddMethodName("addMedia");
474
//            adapter.setRemovMethodName("removeMedia");
475
//        }
476
//        if ("media".equals(parameter)){
477
//            adapter.setAddMethodName("addMedia");
478
//            adapter.setRemovMethodName("removeMedia");
479
//        }
480
//        if ("media".equals(parameter)){
481
//            adapter.setAddMethodName("addMedia");
482
//            adapter.setRemovMethodName("removeMedia");
483
//        }
484
//        try {
485
//            adapter.setCollection(holder, newCollection);
486
//        } catch (SetterAdapterException e) {
487
//
488
//            throw new RuntimeException(e);
489
//        }
490
////        for (T a : getTargetCollection(itemClass, oldCollection)){
491
////
492
////            result.addAnnotation(a);
493
////        }
494
    }
495

  
496
    private <HOLDER extends CdmBase, KEY extends CdmBase, ITEM extends CdmBase>
497
            void handleMap(
498
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter,
499
            Class<KEY> keyClass, Class<ITEM> itemClass)
500
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
501

  
502
        //TODO we do not need to set the new map 2x
503
        Map<KEY,ITEM> oldMap = setNewMap(holder, declaringClass, parameter, keyClass, itemClass);
504
        Map<KEY,ITEM> newMap = getTargetMap(oldMap);
505
        Field field = declaringClass.getDeclaredField(parameter);
506
        field.setAccessible(true);
507
        field.set(holder, newMap);
508
    }
509

  
510
    private <T extends CdmBase> Collection<T> setNewCollection(CdmBase obj, Class<?> holderClass,
511
            String parameter, Class<T> entityClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
512
        Field field = holderClass.getDeclaredField(parameter);
513
        field.setAccessible(true);
514
        Collection<T> oldValue = (Collection<T>)field.get(obj);
515
        Collection<T> newValue = null;
516
        if (Set.class.isAssignableFrom(field.getType())){
517
            newValue = new HashSet<>();
518
        }else if (List.class.isAssignableFrom(field.getType())){
519
            newValue = new ArrayList<>();
520
        }else{
521
            throw new RuntimeException("Unsupported collection type: " + field.getType().getCanonicalName());
522
        }
523
        field.set(obj, newValue);
524
        return oldValue;
525
    }
526

  
527
    private <KEY extends CdmBase, ITEM extends CdmBase> Map<KEY,ITEM> setNewMap(CdmBase obj, Class<?> holderClass,
528
            String parameter, Class<KEY> keyClass, Class<ITEM> itemClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
529
        Field field = holderClass.getDeclaredField(parameter);
530
        field.setAccessible(true);
531
        Map<KEY,ITEM> oldValue = (Map<KEY,ITEM>)field.get(obj);
532
        Map<KEY,ITEM> newValue = null;
533
        if (Map.class.isAssignableFrom(field.getType())){
534
            newValue = new HashMap<>();
535
        }else{
536
            throw new RuntimeException("Unsupported map type: " + field.getType().getCanonicalName());
537
        }
538
        field.set(obj, newValue);
539
        return oldValue;
540
    }
541

  
542
    private <T extends Collection<S>, S extends CdmBase> Collection<S> getTargetCollection(Class<S> clazz, T sourceCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
543
        Collection<S> result =  new ArrayList<>();
544
        if (Set.class.isAssignableFrom(sourceCollection.getClass())){
545
            result = new HashSet<>();
546
        }
547
        for (S entity : sourceCollection){
548
            S target = detache(entity);
549
            result.add(target);
550
        }
551
        return result;
552
    }
553

  
554
    private <K extends CdmBase, V extends CdmBase> Map<K,V> getTargetMap(Map<K,V> sourceMap) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
555
        Map<K,V> result = new HashMap<>();
556
        for (K key : sourceMap.keySet()){
557
            K targetKey = detache(key);
558
            V targetValue = detache(sourceMap.get(key));
559
            result.put(targetKey, targetValue);
560
        }
561
        return result;
562
    }
563

  
564
    private <T extends VersionableEntity> T handlePersisted(VersionableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
565
        T result = (T)handlePersistedCdmBase((CdmBase)entity);
566
        if (result ==null){
567
            return null;
568
        }
569
        entity.setUpdatedBy(detache(entity.getUpdatedBy()));
570
        return result;
571
    }
572

  
573
    private <A extends CdmBase> CdmBase handlePersisted(A cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
574
        if(cdmBase instanceof TaxonNode){
575
            return handlePersistedTaxonNode((TaxonNode)cdmBase);
576
        }else if(cdmBase instanceof Taxon){
577
            return handlePersistedTaxon((Taxon)cdmBase);
578
        }else if(cdmBase instanceof Synonym){
579
            return handlePersistedSynonym((Synonym)cdmBase);
580
        }else if(cdmBase instanceof TaxonName){
581
            return handlePersistedTaxonName((TaxonName)cdmBase);
582
        }else if(cdmBase instanceof Team){
583
            return handlePersistedTeam((Team)cdmBase);
584
        }else if(cdmBase instanceof Person){
585
            return handlePersistedPerson((Person)cdmBase);
586
        }else if(cdmBase instanceof Classification){
587
            return handlePersistedClassification((Classification)cdmBase);
588
        }else if(cdmBase instanceof Reference){
589
            return handlePersistedReference((Reference)cdmBase);
590
        }else if(cdmBase instanceof SpecimenOrObservationBase){
591
            return handlePersistedSpecimenOrObservationBase((SpecimenOrObservationBase)cdmBase);
592
        }else if(cdmBase instanceof IdentifiableSource){
593
            return handlePersistedIdentifiableSource((IdentifiableSource)cdmBase);
594
        }else if(cdmBase instanceof DescriptionElementSource){
595
            return handlePersistedDescriptionElementSource((DescriptionElementSource)cdmBase);
596
        }else if(cdmBase instanceof CommonTaxonName){
597
            return handlePersistedCommonTaxonName((CommonTaxonName)cdmBase);
598
        }else if(cdmBase instanceof Distribution){
599
            return handlePersistedDistribution((Distribution)cdmBase);
600
        }else if(cdmBase instanceof TextData){
601
            return handlePersistedTextData((TextData)cdmBase);
602
        }else if(cdmBase instanceof HomotypicalGroup){
603
            return handlePersistedHomotypicalGroup((HomotypicalGroup)cdmBase);
604
        }else if(cdmBase instanceof TypeDesignationBase){
605
            return handlePersistedTypeDesignationBase((TypeDesignationBase)cdmBase);
606
        }else if(cdmBase instanceof TaxonDescription){
607
            return handlePersistedTaxonDescription((TaxonDescription)cdmBase);
608
        }else if(cdmBase instanceof NomenclaturalStatus){
609
            return handlePersistedNomenclaturalStatus((NomenclaturalStatus)cdmBase);
610
        }else if(cdmBase instanceof TaxonNameDescription){
611
            return handlePersistedTaxonNameDescription((TaxonNameDescription)cdmBase);
612
        }else if(cdmBase instanceof TaxonRelationship){
613
            return handlePersistedTaxonRelationship((TaxonRelationship)cdmBase);
614
        }else if(cdmBase instanceof HybridRelationship){
615
            return handlePersistedHybridRelationship((HybridRelationship)cdmBase);
616
        }else if(cdmBase instanceof NameRelationship){
617
            return handlePersistedNameRelationship((NameRelationship)cdmBase);
618
        }else if(cdmBase instanceof TaxonNodeAgentRelation){
619
            return handlePersistedTaxonNodeAgentRelation((TaxonNodeAgentRelation)cdmBase);
620
        }else if(cdmBase instanceof User){
621
            return handlePersistedUser((User)cdmBase);
622
        }else if(cdmBase instanceof Extension){
623
            return handlePersistedExtension((Extension)cdmBase);
624
        }else if(cdmBase instanceof Marker){
625
            return handlePersistedMarker((Marker)cdmBase);
626
        }else if(cdmBase instanceof Annotation){
627
            return handlePersistedAnnotation((Annotation)cdmBase);
628
        }else if(cdmBase instanceof LanguageString){
629
            return handlePersistedLanguageString((LanguageString)cdmBase);
630
        }else if(cdmBase instanceof TermVocabulary){
631
            return handlePersistedVocabulary((TermVocabulary<?>)cdmBase);
632
        }else if(cdmBase instanceof NamedArea){
633
            return handlePersistedNamedArea((NamedArea)cdmBase);
634
        }else if(cdmBase instanceof TermNode){
635
            return handlePersistedTermNode((TermNode)cdmBase);
636
        }else if(cdmBase instanceof Representation){
637
            return handlePersistedRepresentation((Representation)cdmBase);
638
        }else if(cdmBase instanceof InstitutionalMembership){
639
            return handlePersistedInstitutionalMembership((InstitutionalMembership)cdmBase);
640
        }else if(cdmBase instanceof Institution){
641
            return handlePersistedInstitution((Institution)cdmBase);
642
        }else if(cdmBase instanceof IntextReference){
643
            return handlePersistedIntextReference((IntextReference)cdmBase);
644
        }else if(cdmBase instanceof ExtensionType){
645
            return handlePersistedExtensionType((ExtensionType)cdmBase);
646
        }else if(cdmBase instanceof DefinedTerm){
647
            return handlePersistedDefinedTerm((DefinedTerm)cdmBase);
648
        }else if(cdmBase instanceof DefinedTermBase){
649
            return handlePersistedTerm((DefinedTermBase<?>)cdmBase);
650
        }else {
651
            throw new RuntimeException("Type not yet supported: " + cdmBase.getClass().getCanonicalName());
652
        }
653
    }
654

  
655
    /**
656
     * @param cdmBase
657
     * @return
658
     * @throws NoSuchMethodException
659
     * @throws IllegalArgumentException
660
     * @throws SecurityException
661
     * @throws NoSuchFieldException
662
     * @throws InvocationTargetException
663
     * @throws IllegalAccessException
664
     */
665
    private Taxon handlePersistedTaxon(Taxon taxon) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
666
        Taxon result = handlePersisted((TaxonBase)taxon);
667
        handleCollection(result, Taxon.class, "synonyms", Synonym.class);
668
//        handleCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
669
        setNewCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
670
        handleCollection(result, Taxon.class, "relationsFromThisTaxon", TaxonRelationship.class);
671
        handleCollection(result, Taxon.class, "relationsToThisTaxon", TaxonRelationship.class);
672
        handleCollection(result, Taxon.class, "descriptions", TaxonDescription.class);
673
        return result;
674
    }
675

  
676
    private Synonym handlePersistedSynonym(Synonym synonym) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
677
        Synonym result = handlePersisted((TaxonBase)synonym);
678
        setInvisible(result, "acceptedTaxon", detache(result.getAcceptedTaxon()));
679
        result.setType(detache(result.getType()));
680
        return result;
681
    }
682

  
683
    private TaxonRelationship handlePersistedTaxonRelationship(TaxonRelationship taxRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
684
        TaxonRelationship result = handlePersisted((RelationshipBase)taxRel);
685
        result.setFromTaxon(detache(result.getFromTaxon()));
686
        result.setToTaxon(detache(result.getToTaxon()));
687
        result.setType(detache(result.getType()));
688
        return result;
689
    }
690

  
691
    private NameRelationship handlePersistedNameRelationship(NameRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
692
        NameRelationship result = handlePersisted((RelationshipBase)rel);
693
        setInvisible(result, "relatedFrom", detache(result.getFromName()));
694
        setInvisible(result, "relatedTo", detache(result.getToName()));
695
//        result.setFromName(detache(result.getFromName()));
696
//        result.setToName(detache(result.getToName()));
697
        result.setType(detache(result.getType()));
698
        return result;
699
    }
700

  
701
    private HybridRelationship handlePersistedHybridRelationship(HybridRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
702
        HybridRelationship result = handlePersisted((RelationshipBase)rel);
703
        setInvisible(result, "relatedFrom", detache(result.getParentName()));
704
        setInvisible(result, "relatedTo", detache(result.getHybridName()));
705
//        result.setFromName(detache(result.getFromName()));
706
//        result.setToName(detache(result.getToName()));
707
        result.setType(detache(result.getType()));
708
        return result;
709
    }
710
    private NomenclaturalStatus handlePersistedNomenclaturalStatus(NomenclaturalStatus status) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
711
        NomenclaturalStatus result = handlePersisted((ReferencedEntityBase)status);
712
        result.setType(detache(result.getType()));
713
        return result;
714
    }
715

  
716
    private TypeDesignationBase handlePersistedTypeDesignationBase(TypeDesignationBase<?> designation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
717
        TypeDesignationBase result = handlePersisted((SourcedEntityBase)designation);
718
        result.setCitation(detache(result.getCitation()));
719
        handleCollection(result, TypeDesignationBase.class, "registrations", Registration.class);
720
        handleCollection(result, TypeDesignationBase.class, "typifiedNames", TaxonName.class);
721
        result.setTypeStatus(detache(result.getTypeStatus()));
722
        return result;
723
    }
724

  
725
    private InstitutionalMembership handlePersistedInstitutionalMembership(InstitutionalMembership institutionalMembership) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
726
        InstitutionalMembership result = handlePersisted((VersionableEntity)institutionalMembership);
727
//        result.setPerson(detache(result.getPerson()));
728
        setInvisible(result, "person", detache(result.getPerson()));
729
        result.setInstitute(detache(result.getInstitute()));
730
        return result;
731
    }
732

  
733
    private Institution handlePersistedInstitution(Institution institution) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
734
        Institution result = handlePersisted((AgentBase)institution);
735
        result.setIsPartOf(detache(result.getIsPartOf()));
736
        handleCollection(result, Institution.class, "types", DefinedTerm.class);
737
        return result;
738
    }
739

  
740
    private TaxonNodeAgentRelation handlePersistedTaxonNodeAgentRelation(TaxonNodeAgentRelation nodeAgentRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
741
        TaxonNodeAgentRelation result = handlePersisted((AnnotatableEntity)nodeAgentRel);
742
        if (result ==null){
743
            return result;
744
        }
745
        result.setAgent(detache(result.getAgent()));
746
        result.setType(detache(result.getType()));
747
        setInvisible(result, "taxonNode", detache(result.getTaxonNode()));
748
        return result;
749
    }
750

  
751

  
752
    private TaxonName handlePersistedTaxonName(TaxonName taxonName) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
753
        @SuppressWarnings("rawtypes")
754
        TaxonName result = handlePersisted((IdentifiableEntity)taxonName);
755
        if (result ==null){
756
            return result;
757
        }
758
        result.setCombinationAuthorship(detache(result.getCombinationAuthorship()));
759
        result.setExCombinationAuthorship(detache(result.getExCombinationAuthorship()));
760
        result.setBasionymAuthorship(detache(result.getBasionymAuthorship()));
761
        result.setExBasionymAuthorship(detache(result.getExBasionymAuthorship()));
762
        result.setInBasionymAuthorship(detache(result.getInBasionymAuthorship()));
763
        result.setInCombinationAuthorship(detache(result.getInCombinationAuthorship()));
764

  
765
        result.setNomenclaturalReference(detache(result.getNomenclaturalReference()));
766
        result.setHomotypicalGroup(detache(result.getHomotypicalGroup()));
767
        handleCollection(result, TaxonName.class, "descriptions", TaxonNameDescription.class);
768
        handleCollection(result, TaxonName.class, "hybridChildRelations", HybridRelationship.class);
769
        handleCollection(result, TaxonName.class, "hybridParentRelations", HybridRelationship.class);
770
        handleCollection(result, TaxonName.class, "relationsFromThisName", NameRelationship.class);
771
        handleCollection(result, TaxonName.class, "relationsToThisName", NameRelationship.class);
772
        handleCollection(result, TaxonName.class, "status", NomenclaturalStatus.class);
773

  
774
        handleCollection(result, TaxonName.class, "registrations", Registration.class);
775
        handleCollection(result, TaxonName.class, "typeDesignations", TypeDesignationBase.class);
776

  
777
        handleCollection(result, TaxonName.class, "taxonBases", TaxonBase.class);
778

  
779
        return result;
780
    }
781

  
782
    private HomotypicalGroup handlePersistedHomotypicalGroup(HomotypicalGroup group) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
783
        HomotypicalGroup result = handlePersisted((AnnotatableEntity)group);
784
        if (result ==null){
785
            return result;
786
        }
787
        handleCollection(result, HomotypicalGroup.class, "typifiedNames", TaxonName.class);
788
        return result;
789
    }
790

  
791
    private Annotation handlePersistedAnnotation(Annotation annotation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
792
        Annotation result = handlePersisted((AnnotatableEntity)annotation);
793
        if (result ==null){
794
            return result;
795
        }
796
        result.setAnnotationType(detache(annotation.getAnnotationType()));
797
        handleCollection(result, Annotation.class, "intextReferences", IntextReference.class);
798
        return result;
799
    }
800

  
801
    private Extension handlePersistedExtension(Extension extension) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
802
        Extension result = handlePersisted((VersionableEntity)extension);
803
        if (result ==null){
804
            return result;
805
        }
806
        result.setType(detache(extension.getType()));
807
        return result;
808
    }
809

  
810
    private Marker handlePersistedMarker(Marker marker) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
811
        Marker result = handlePersisted((VersionableEntity)marker);
812
        if (result ==null){
813
            return result;
814
        }
815
        result.setMarkerType(detache(marker.getMarkerType()));
816
        return result;
817
    }
818

  
819
    private Team handlePersistedTeam(Team team) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
820
        Team result = handlePersisted((TeamOrPersonBase)team);
821
        if (result ==null){
822
            return result;
823
        }
824
        handleCollection(result, Team.class, "teamMembers", Person.class);
825
        return result;
826
    }
827

  
828
    private Contact handlePersistedContact(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
829
        Contact result = contact; // getTarget(contact);
830
        if (result ==null){
831
            return result;
832
        }
833
        if (!contact.getAddresses().isEmpty() || !contact.getEmailAddresses().isEmpty()
834
               || !contact.getFaxNumbers().isEmpty() ||!contact.getPhoneNumbers().isEmpty()
835
               ||!contact.getUrls().isEmpty()){
836
            logger.warn("Addresses not yet implemented");
837
        }
838
        setInvisible(result, "addresses", new HashSet<>());
839
//        handleCollection(result, Contact.class, "", Address.class);
840
        setInvisible(result, "faxNumbers", new ArrayList<>());
841
        setInvisible(result, "phoneNumbers", new ArrayList<>());
842
        setInvisible(result, "emailAddresses", new ArrayList<>());
843
        setInvisible(result, "urls", new ArrayList<>());
844
        return result;
845
    }
846

  
847
    private Person handlePersistedPerson(Person person) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
848
        Person result = handlePersisted((TeamOrPersonBase)person);
849
        handleCollection(result, Person.class, "institutionalMemberships", InstitutionalMembership.class);
850
        return result;
851
    }
852

  
853
    private NamedArea handlePersistedNamedArea(NamedArea area) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
854
        NamedArea result = handlePersisted((OrderedTermBase)area);
855
        handleCollection(result, NamedArea.class, "countries", Country.class);
856
        result.setLevel(detache(result.getLevel()));
857
        result.setType(detache(result.getType()));
858
        result.setShape(detache(result.getShape()));
859
        return result;
860
    }
861

  
862
    private <T extends CdmBase> CdmBase handlePersistedCdmBase(CdmBase cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
863
        T result = (T)getTarget(cdmBase);
864
        if (result == null){
865
            return null;
866
        }
867
        cdmBase.setCreatedBy(detache(cdmBase.getCreatedBy()));
868
        return result;
869
    }
870

  
871
    private <T extends DefinedTermBase> T  handlePersisted(DefinedTermBase definedTermBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
872
        T result = handlePersisted((TermBase)definedTermBase);
873

  
874
        handleCollection(result, DefinedTermBase.class, "media", Media.class);
875
        handleCollection(result, DefinedTermBase.class, "generalizationOf", DefinedTermBase.class);
876
        handleCollection(result, DefinedTermBase.class, "includes", DefinedTermBase.class);
877
        setInvisible(result, DefinedTermBase.class, "vocabulary", detache(result.getVocabulary()));
878

  
879
//        getTermService().saveOrUpdate(result);
880

  
881
        return result;
882
    }
883

  
884
    private DefinedTerm handlePersistedDefinedTerm(DefinedTerm term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
885
        DefinedTerm result = handlePersisted((DefinedTermBase)term);
886
        return result;
887
    }
888

  
889
    private ExtensionType handlePersistedExtensionType(ExtensionType term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
890
        ExtensionType result = handlePersisted((DefinedTermBase)term);
891
        return result;
892
    }
893

  
894
    //placeholder for not implemented methods for subclasses
895
    private DefinedTermBase<?> handlePersistedTerm(DefinedTermBase term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
896
        DefinedTermBase<?> result = handlePersisted(term);
897
        return result;
898
    }
899

  
900

  
901
    private TermVocabulary<?> handlePersistedVocabulary(TermVocabulary voc) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
902
        TermVocabulary<?> result = (TermVocabulary<?>)handlePersisted((TermCollection)voc);
903
        handleCollection(result, TermVocabulary.class, "terms", DefinedTermBase.class);
904
        return result;
905
    }
906

  
907
    private TermNode<?> handlePersistedTermNode(TermNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
908
        TermNode<?> result = (TermNode<?>)handlePersisted((TermRelationBase)node);
909
        setInvisible(result, "parent", detache(result.getParent()));
910
        handleCollection(result, TermNode.class, "inapplicableIf", FeatureState.class);
911
        handleCollection(result, TermNode.class, "onlyApplicableIf", FeatureState.class);
912
        handleCollection(result, TermNode.class, "inapplicableIf_old", State.class);
913
        handleCollection(result, TermNode.class, "onlyApplicableIf_old", State.class);
914
        handleCollection(result, TermNode.class, "children", TermNode.class);
915

  
916
        return result;
917
    }
918

  
919
    private Representation handlePersistedRepresentation(Representation representation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
920
        Representation result = (Representation)handlePersisted((LanguageStringBase)representation);
921
        return result;
922
    }
923

  
924
    private <T extends TermBase> T  handlePersisted(TermBase termBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
925
        T result = handlePersisted((IdentifiableEntity)termBase);
926
        if (result ==null){
927
            return null;
928
        }
929
        handleCollection(result, TermBase.class, "representations", Representation.class);
930
        return result;
931
    }
932

  
933
    private <T extends TermCollection> T  handlePersisted(TermCollection termCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
934
        T result = handlePersisted((TermBase)termCollection);
935
        if (result ==null){
936
            return null;
937
        }
938
        handleCollection(result, TermCollection.class, "termRelations", TermRelationBase.class);
939
        return result;
940
    }
941

  
942
    private <T extends TermRelationBase> T  handlePersisted(TermRelationBase termRelationBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
943
        T result = handlePersisted((VersionableEntity)termRelationBase);
944
        result.setTerm(detache(result.getTerm()));
945
        setInvisible(result, TermRelationBase.class, "graph", detache(result.getGraph()));
946
        return result;
947
    }
948

  
949
    private User handlePersistedUser(User user) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
950
        User result = (User)handlePersistedCdmBase(user);
951
        if (result.getUsername().equals("admin")){
952
            result = getUserService().listByUsername("admin", MatchMode.EXACT, null, null, null, null, null).iterator().next();
953
            permanentCache.put(user.getUuid(), result);
954
            cache(result); //necessary?
955
            toSave.add(result);
956
            toSave.remove(user);
957
        }
958
        if (!result.isPersited()){
959
            result.setAuthorities(new HashSet<>());
960
            result.setGrantedAuthorities(new HashSet<>());
961
            setInvisible(result, "groups", new HashSet<>());
962
        }
963
        return result;
964
    }
965

  
966
    /**
967
     * @param result
968
     */
969
    private void cache(CdmBase cdmBase) {
970
       if (cdmBase instanceof User || cdmBase instanceof DefinedTermBase){
971
           permanentCache.put(cdmBase.getUuid(), cdmBase);
972
       }else{
973
           sessionCache.put(cdmBase.getUuid(), cdmBase);
974
       }
975
       movedObjects.add(cdmBase.getUuid());
976

  
977
    }
978

  
979
    private  Contact detache(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
980
        contact = CdmBase.deproxy(contact);
981
        if (contact == null){
982
            return contact;
983
        }else{
984
            return handlePersistedContact(contact);
985
        }
986
    }
987

  
988
    private  IIntextReferencable detache(IIntextReferencable cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
989
        return (IIntextReferencable)detache((CdmBase)cdmBase);
990
    }
991
    private  IIntextReferenceTarget detache(IIntextReferenceTarget cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
992
        return (IIntextReferenceTarget)detache((CdmBase)cdmBase);
993
    }
994

  
995
    private <T extends CdmBase> T detache(T cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
996
        return detache(cdmBase, false);
997
    }
998

  
999
    private <T extends CdmBase> T detache(T cdmBase, boolean notFromSource) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
1000
        cdmBase = CdmBase.deproxy(cdmBase);
1001
        if (cdmBase == null ){
1002
            return cdmBase;
1003
        }else if(isInCache(cdmBase)){
1004
            return getCached(cdmBase);
1005
        }else {
1006
            //TODO better load all existing UUIDs just in case any of the objects is already in the DB
1007
            if (cdmBase instanceof TermBase || movedObjects.contains(cdmBase.getUuid())){
1008
                Class<T> clazz = (Class<T>)cdmBase.getClass();
1009
                T exists = getCommonService().find(clazz, cdmBase.getUuid());
1010
                if (exists != null){
1011
                    return exists;
1012
                }else if (movedObjects.contains(cdmBase.getUuid())){
1013
                    logger.warn("Object should be moved already but does not exist in target. This should not happen: " + cdmBase.getUuid());
1014
                }
94
    private ITaxonNodeOutStreamPartitioner getPartitioner(FauEu2CdmImportState state, IProgressMonitor monitor,
95
            FauEu2CdmImportConfigurator config) {
96
        ITaxonNodeOutStreamPartitioner partitioner = config.getPartitioner();
97
        if (partitioner == null){
98
            if(!config.isConcurrent()){
99
                partitioner = TaxonNodeOutStreamPartitioner.NewInstance(sourceRepo(state), state,
100
                        state.getConfig().getTaxonNodeFilter(), 100,
101
                        monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
102
                ((TaxonNodeOutStreamPartitioner)partitioner).setLastCommitManually(true);
103
            }else{
104
                partitioner = TaxonNodeOutStreamPartitionerConcurrent
105
                        .NewInstance(state.getConfig().getSource(), state.getConfig().getTaxonNodeFilter(),
106
                                1000, monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
1015 107
            }
1016 108
        }
1017
        if ( !cdmBase.isPersited()){
1018
            logger.warn("Non persisted object not in cache and not in target DB. This should not happen: " + cdmBase.getUuid());
1019
            return cdmBase; //should not happen anymore; either in cache or in target or persisted in source
1020
        }else{
1021
            return notFromSource? null : (T)handlePersisted(cdmBase);
1022
        }
109
        return partitioner;
1023 110
    }
1024 111

  
1025

  
1026
    /**
1027
     * @param cdmBase
1028
     * @return
1029
     */
1030
    private boolean isInCache(CdmBase cdmBase) {
1031
        return getCached(cdmBase) != null;
1032
    }
1033

  
1034
    /**
1035
     * @param cdmBase
1036
     * @return
1037
     */
1038
    private <T extends CdmBase> T getCached(T cdmBase) {
1039
        T result = (T)sessionCache.get(cdmBase.getUuid());
1040
        if (result == null){
1041
            result = (T)permanentCache.get(cdmBase.getUuid());
1042
        }
1043
        return result;
1044
    }
1045

  
1046
    //TODO this should be cached for partition
1047
    private <T extends CdmBase> T getTarget(T source) {
1048
        if (source == null){
1049
            return null;
1050
        }
1051
        T result = getCached(source);
1052
//        if (result == null){
1053
//            Class<T> clazz = (Class<T>)source.getClass();
1054
//            result = getCommonService().find(clazz, source.getUuid());
1055
//        }
1056
        if (result == null){
1057
            //Alternative: clone?
1058
            result = CdmBase.deproxy(source);
1059
            result.setId(0);
1060
            cache(result);
1061
            toSave.add(result);
112
    private void setRootId(FauEu2CdmImportState state, FauEu2CdmImportConfigurator config) {
113
        if (config.getTaxonNodeFilter().hasClassificationFilter()) {
114
            Classification classification = getClassificationService()
115
                    .load(config.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
116
            state.setRootId(classification.getRootNode().getUuid());
117
        } else if (config.getTaxonNodeFilter().hasSubtreeFilter()) {
118
            state.setRootId(config.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
1062 119
        }
1063
        return result;
1064 120
    }
1065 121

  
1066

  
1067

  
1068 122
    @Override
1069 123
    protected boolean doCheck(FauEu2CdmImportState state) {
1070 124
        return false;
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/fauEu2Cdm/FauEu2CdmImportBase.java
8 8
*/
9 9
package eu.etaxonomy.cdm.io.pesi.fauEu2Cdm;
10 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

  
11 25
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
12 26
import eu.etaxonomy.cdm.api.application.ICdmRepository;
13 27
import eu.etaxonomy.cdm.database.DbSchemaValidation;
14 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.ReferencedEntityBase;
53
import eu.etaxonomy.cdm.model.common.RelationshipBase;
54
import eu.etaxonomy.cdm.model.common.SourcedEntityBase;
55
import eu.etaxonomy.cdm.model.common.VersionableEntity;
56
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
57
import eu.etaxonomy.cdm.model.description.DescriptionBase;
58
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
59
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
60
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
61
import eu.etaxonomy.cdm.model.description.Distribution;
62
import eu.etaxonomy.cdm.model.description.FeatureState;
63
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
64
import eu.etaxonomy.cdm.model.description.State;
65
import eu.etaxonomy.cdm.model.description.TaxonDescription;
66
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
67
import eu.etaxonomy.cdm.model.description.TextData;
68
import eu.etaxonomy.cdm.model.location.Country;
69
import eu.etaxonomy.cdm.model.location.NamedArea;
70
import eu.etaxonomy.cdm.model.media.ExternalLink;
71
import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
72
import eu.etaxonomy.cdm.model.media.Media;
73
import eu.etaxonomy.cdm.model.media.Rights;
74
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
75
import eu.etaxonomy.cdm.model.name.HybridRelationship;
76
import eu.etaxonomy.cdm.model.name.NameRelationship;
77
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
78
import eu.etaxonomy.cdm.model.name.Registration;
79
import eu.etaxonomy.cdm.model.name.TaxonName;
80
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
81
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
82
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
83
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
84
import eu.etaxonomy.cdm.model.permission.User;
85
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
86
import eu.etaxonomy.cdm.model.reference.Reference;
87
import eu.etaxonomy.cdm.model.taxon.Classification;
88
import eu.etaxonomy.cdm.model.taxon.Synonym;
89
import eu.etaxonomy.cdm.model.taxon.Taxon;
90
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
91
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
92
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation;
93
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
94
import eu.etaxonomy.cdm.model.term.DefinedTerm;
95
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
96
import eu.etaxonomy.cdm.model.term.OrderedTermBase;
97
import eu.etaxonomy.cdm.model.term.Representation;
98
import eu.etaxonomy.cdm.model.term.TermBase;
99
import eu.etaxonomy.cdm.model.term.TermCollection;
100
import eu.etaxonomy.cdm.model.term.TermNode;
101
import eu.etaxonomy.cdm.model.term.TermRelationBase;
102
import eu.etaxonomy.cdm.model.term.TermVocabulary;
103
import eu.etaxonomy.cdm.persistence.query.MatchMode;
15 104

  
16 105
/**
17 106
 * @author a.mueller
......
21 110
        extends CdmImportBase<FauEu2CdmImportConfigurator, FauEu2CdmImportState> {
22 111

  
23 112
    private static final long serialVersionUID = 8917991155285743172L;
113
    private static final Logger logger = Logger.getLogger(FauEu2CdmImportBase.class);
114

  
115

  
116
    //TODO move to state
117
    private Map<UUID, CdmBase> sessionCache = new HashMap<>();
118
    private Map<UUID, CdmBase> permanentCache = new HashMap<>();
119
    private Set<UUID> movedObjects = new HashSet<>();
24 120

  
25
    protected ICdmRepository source(FauEu2CdmImportState state){
121
    protected Set<CdmBase> toSave = new HashSet<>();
122

  
123
    protected ICdmRepository sourceRepo(FauEu2CdmImportState state){
26 124
        ICdmRepository repo = state.getSourceRepository();
27 125
        if (repo == null){
28 126
            System.out.println("start source repo");
......
35 133
        return repo;
36 134
    }
37 135

  
136
    protected  Contact detache(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
137
        contact = CdmBase.deproxy(contact);
138
        if (contact == null){
139
            return contact;
140
        }else{
141
            return handlePersistedContact(contact);
142
        }
143
    }
144

  
145
    protected  IIntextReferencable detache(IIntextReferencable cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
146
        return (IIntextReferencable)detache((CdmBase)cdmBase);
147
    }
148
    protected  IIntextReferenceTarget detache(IIntextReferenceTarget cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
149
        return (IIntextReferenceTarget)detache((CdmBase)cdmBase);
150
    }
151

  
152
    protected <T extends CdmBase> T detache(T cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
153
        return detache(cdmBase, false);
154
    }
155

  
156

  
157
    protected <T extends CdmBase> T detache(T cdmBase, boolean notFromSource) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
158
        cdmBase = CdmBase.deproxy(cdmBase);
159
        if (cdmBase == null ){
160
            return cdmBase;
161
        }else if(isInCache(cdmBase)){
162
            return getCached(cdmBase);
163
        }else {
164
            //TODO better load all existing UUIDs just in case any of the objects is already in the DB
165
            if (cdmBase instanceof TermBase || movedObjects.contains(cdmBase.getUuid())){
166
                Class<T> clazz = (Class<T>)cdmBase.getClass();
167
                T exists = getCommonService().find(clazz, cdmBase.getUuid());
168
                if (exists != null){
169
                    return exists;
170
                }else if (movedObjects.contains(cdmBase.getUuid())){
171
                    logger.warn("Object should be moved already but does not exist in target. This should not happen: " + cdmBase.getUuid());
172
                }
173
            }
174
        }
175
        if ( !cdmBase.isPersited()){
176
            logger.warn("Non persisted object not in cache and not in target DB. This should not happen: " + cdmBase.getUuid());
177
            return cdmBase; //should not happen anymore; either in cache or in target or persisted in source
178
        }else{
179
            return notFromSource? null : (T)handlePersisted(cdmBase);
180
        }
181
    }
182

  
183
    protected <A extends CdmBase> CdmBase handlePersisted(A cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
184
        if(cdmBase instanceof TaxonNode){
185
            return handlePersistedTaxonNode((TaxonNode)cdmBase);
186
        }else if(cdmBase instanceof Taxon){
187
            return handlePersistedTaxon((Taxon)cdmBase);
188
        }else if(cdmBase instanceof Synonym){
189
            return handlePersistedSynonym((Synonym)cdmBase);
190
        }else if(cdmBase instanceof TaxonName){
191
            return handlePersistedTaxonName((TaxonName)cdmBase);
192
        }else if(cdmBase instanceof Team){
193
            return handlePersistedTeam((Team)cdmBase);
194
        }else if(cdmBase instanceof Person){
195
            return handlePersistedPerson((Person)cdmBase);
196
        }else if(cdmBase instanceof Classification){
197
            return handlePersistedClassification((Classification)cdmBase);
198
        }else if(cdmBase instanceof Reference){
199
            return handlePersistedReference((Reference)cdmBase);
200
        }else if(cdmBase instanceof SpecimenOrObservationBase){
201
            return handlePersistedSpecimenOrObservationBase((SpecimenOrObservationBase)cdmBase);
202
        }else if(cdmBase instanceof IdentifiableSource){
203
            return handlePersistedIdentifiableSource((IdentifiableSource)cdmBase);
204
        }else if(cdmBase instanceof DescriptionElementSource){
205
            return handlePersistedDescriptionElementSource((DescriptionElementSource)cdmBase);
206
        }else if(cdmBase instanceof CommonTaxonName){
207
            return handlePersistedCommonTaxonName((CommonTaxonName)cdmBase);
208
        }else if(cdmBase instanceof Distribution){
209
            return handlePersistedDistribution((Distribution)cdmBase);
210
        }else if(cdmBase instanceof TextData){
211
            return handlePersistedTextData((TextData)cdmBase);
212
        }else if(cdmBase instanceof HomotypicalGroup){
213
            return handlePersistedHomotypicalGroup((HomotypicalGroup)cdmBase);
214
        }else if(cdmBase instanceof TypeDesignationBase){
215
            return handlePersistedTypeDesignationBase((TypeDesignationBase)cdmBase);
216
        }else if(cdmBase instanceof TaxonDescription){
217
            return handlePersistedTaxonDescription((TaxonDescription)cdmBase);
218
        }else if(cdmBase instanceof NomenclaturalStatus){
219
            return handlePersistedNomenclaturalStatus((NomenclaturalStatus)cdmBase);
220
        }else if(cdmBase instanceof TaxonNameDescription){
221
            return handlePersistedTaxonNameDescription((TaxonNameDescription)cdmBase);
222
        }else if(cdmBase instanceof TaxonRelationship){
223
            return handlePersistedTaxonRelationship((TaxonRelationship)cdmBase);
224
        }else if(cdmBase instanceof HybridRelationship){
225
            return handlePersistedHybridRelationship((HybridRelationship)cdmBase);
226
        }else if(cdmBase instanceof NameRelationship){
227
            return handlePersistedNameRelationship((NameRelationship)cdmBase);
228
        }else if(cdmBase instanceof TaxonNodeAgentRelation){
229
            return handlePersistedTaxonNodeAgentRelation((TaxonNodeAgentRelation)cdmBase);
230
        }else if(cdmBase instanceof User){
231
            return handlePersistedUser((User)cdmBase);
232
        }else if(cdmBase instanceof Extension){
233
            return handlePersistedExtension((Extension)cdmBase);
234
        }else if(cdmBase instanceof Marker){
235
            return handlePersistedMarker((Marker)cdmBase);
236
        }else if(cdmBase instanceof Annotation){
237
            return handlePersistedAnnotation((Annotation)cdmBase);
238
        }else if(cdmBase instanceof LanguageString){
239
            return handlePersistedLanguageString((LanguageString)cdmBase);
240
        }else if(cdmBase instanceof TermVocabulary){
241
            return handlePersistedVocabulary((TermVocabulary<?>)cdmBase);
242
        }else if(cdmBase instanceof NamedArea){
243
            return handlePersistedNamedArea((NamedArea)cdmBase);
244
        }else if(cdmBase instanceof TermNode){
245
            return handlePersistedTermNode((TermNode)cdmBase);
246
        }else if(cdmBase instanceof Representation){
247
            return handlePersistedRepresentation((Representation)cdmBase);
248
        }else if(cdmBase instanceof InstitutionalMembership){
249
            return handlePersistedInstitutionalMembership((InstitutionalMembership)cdmBase);
250
        }else if(cdmBase instanceof Institution){
251
            return handlePersistedInstitution((Institution)cdmBase);
252
        }else if(cdmBase instanceof IntextReference){
253
            return handlePersistedIntextReference((IntextReference)cdmBase);
254
        }else if(cdmBase instanceof ExtensionType){
255
            return handlePersistedExtensionType((ExtensionType)cdmBase);
256
        }else if(cdmBase instanceof DefinedTerm){
257
            return handlePersistedDefinedTerm((DefinedTerm)cdmBase);
258
        }else if(cdmBase instanceof DefinedTermBase){
259
            return handlePersistedTerm((DefinedTermBase<?>)cdmBase);
260
        }else {
261
            throw new RuntimeException("Type not yet supported: " + cdmBase.getClass().getCanonicalName());
262
        }
263
    }
264

  
265

  
266
    protected TaxonNode handlePersistedTaxonNode(TaxonNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
267

  
268
        TaxonNode result = handlePersisted((AnnotatableEntity)node);
269
        if (result ==null){
270
            return result;
271
        }
272
        handleCollection(result, TaxonNode.class, "agentRelations", TaxonNodeAgentRelation.class);
273
        result.setTaxon(detache(result.getTaxon()));
274
        result.setReference(detache(node.getReference()));
275
        handleMap(result, TaxonNode.class, "excludedNote", Language.class, LanguageString.class);
276
        //classification, parent, children
277
        this.setInvisible(node, "classification", detache(node.getClassification()));
278
        handleParentTaxonNode(result);
279
        setNewCollection(node, TaxonNode.class, "childNodes", TaxonNode.class);
280
        return result;
281
    }
282

  
283
    private void handleParentTaxonNode(TaxonNode childNode) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
284
        TaxonNode parent = detache(childNode.getParent(), true);
285
        //TODO
286
        String microReference = null;
287
        Reference reference = null;
288
        if (parent == null && childNode.getClassification().getRootNode().equals(childNode)){
289
            //do nothing
290
        }else if (parent == null ){
291
            childNode.getClassification().addChildNode(childNode, reference, microReference) ;
292
        }else{
293
            parent.addChildNode(childNode, reference, microReference);
294
        }
295
    }
296

  
297
    protected Taxon handlePersistedTaxon(Taxon taxon) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
298
        Taxon result = handlePersisted((TaxonBase)taxon);
299
        handleCollection(result, Taxon.class, "synonyms", Synonym.class);
300
//        handleCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
301
        setNewCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
302
        handleCollection(result, Taxon.class, "relationsFromThisTaxon", TaxonRelationship.class);
303
        handleCollection(result, Taxon.class, "relationsToThisTaxon", TaxonRelationship.class);
304
        handleCollection(result, Taxon.class, "descriptions", TaxonDescription.class);
305
        return result;
306
    }
307

  
308
    protected Synonym handlePersistedSynonym(Synonym synonym) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
309
        Synonym result = handlePersisted((TaxonBase)synonym);
310
        setInvisible(result, "acceptedTaxon", detache(result.getAcceptedTaxon()));
311
        result.setType(detache(result.getType()));
312
        return result;
313
    }
314

  
315
    protected TaxonRelationship handlePersistedTaxonRelationship(TaxonRelationship taxRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
316
        TaxonRelationship result = handlePersisted((RelationshipBase)taxRel);
317
        result.setFromTaxon(detache(result.getFromTaxon()));
318
        result.setToTaxon(detache(result.getToTaxon()));
319
        result.setType(detache(result.getType()));
320
        return result;
321
    }
322

  
323
    protected NameRelationship handlePersistedNameRelationship(NameRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
324
        NameRelationship result = handlePersisted((RelationshipBase)rel);
325
        setInvisible(result, "relatedFrom", detache(result.getFromName()));
326
        setInvisible(result, "relatedTo", detache(result.getToName()));
327
//        result.setFromName(detache(result.getFromName()));
328
//        result.setToName(detache(result.getToName()));
329
        result.setType(detache(result.getType()));
330
        return result;
331
    }
332

  
333
    protected HybridRelationship handlePersistedHybridRelationship(HybridRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
334
        HybridRelationship result = handlePersisted((RelationshipBase)rel);
335
        setInvisible(result, "relatedFrom", detache(result.getParentName()));
336
        setInvisible(result, "relatedTo", detache(result.getHybridName()));
337
//        result.setFromName(detache(result.getFromName()));
338
//        result.setToName(detache(result.getToName()));
339
        result.setType(detache(result.getType()));
340
        return result;
341
    }
342

  
343
    protected NomenclaturalStatus handlePersistedNomenclaturalStatus(NomenclaturalStatus status) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
344
        NomenclaturalStatus result = handlePersisted((ReferencedEntityBase)status);
345
        result.setType(detache(result.getType()));
346
        return result;
347
    }
348

  
349
    protected TypeDesignationBase handlePersistedTypeDesignationBase(TypeDesignationBase<?> designation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
350
        TypeDesignationBase result = handlePersisted((SourcedEntityBase)designation);
351
        result.setCitation(detache(result.getCitation()));
352
        handleCollection(result, TypeDesignationBase.class, "registrations", Registration.class);
353
        handleCollection(result, TypeDesignationBase.class, "typifiedNames", TaxonName.class);
354
        result.setTypeStatus(detache(result.getTypeStatus()));
355
        return result;
356
    }
357

  
358
    protected InstitutionalMembership handlePersistedInstitutionalMembership(InstitutionalMembership institutionalMembership) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
359
        InstitutionalMembership result = handlePersisted((VersionableEntity)institutionalMembership);
360
//        result.setPerson(detache(result.getPerson()));
361
        setInvisible(result, "person", detache(result.getPerson()));
362
        result.setInstitute(detache(result.getInstitute()));
363
        return result;
364
    }
365

  
366
    protected Institution handlePersistedInstitution(Institution institution) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
367
        Institution result = handlePersisted((AgentBase)institution);
368
        result.setIsPartOf(detache(result.getIsPartOf()));
369
        handleCollection(result, Institution.class, "types", DefinedTerm.class);
370
        return result;
371
    }
372

  
373
    protected TaxonNodeAgentRelation handlePersistedTaxonNodeAgentRelation(TaxonNodeAgentRelation nodeAgentRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
374
        TaxonNodeAgentRelation result = handlePersisted((AnnotatableEntity)nodeAgentRel);
375
        if (result ==null){
376
            return result;
377
        }
378
        result.setAgent(detache(result.getAgent()));
379
        result.setType(detache(result.getType()));
380
        setInvisible(result, "taxonNode", detache(result.getTaxonNode()));
381
        return result;
382
    }
383

  
384

  
385
    protected TaxonName handlePersistedTaxonName(TaxonName taxonName) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
386
        @SuppressWarnings("rawtypes")
387
        TaxonName result = handlePersisted((IdentifiableEntity)taxonName);
388
        if (result ==null){
389
            return result;
390
        }
391
        result.setCombinationAuthorship(detache(result.getCombinationAuthorship()));
392
        result.setExCombinationAuthorship(detache(result.getExCombinationAuthorship()));
393
        result.setBasionymAuthorship(detache(result.getBasionymAuthorship()));
394
        result.setExBasionymAuthorship(detache(result.getExBasionymAuthorship()));
395
        result.setInBasionymAuthorship(detache(result.getInBasionymAuthorship()));
396
        result.setInCombinationAuthorship(detache(result.getInCombinationAuthorship()));
397

  
398
        result.setNomenclaturalReference(detache(result.getNomenclaturalReference()));
399
        result.setHomotypicalGroup(detache(result.getHomotypicalGroup()));
400
        handleCollection(result, TaxonName.class, "descriptions", TaxonNameDescription.class);
401
        handleCollection(result, TaxonName.class, "hybridChildRelations", HybridRelationship.class);
402
        handleCollection(result, TaxonName.class, "hybridParentRelations", HybridRelationship.class);
403
        handleCollection(result, TaxonName.class, "relationsFromThisName", NameRelationship.class);
404
        handleCollection(result, TaxonName.class, "relationsToThisName", NameRelationship.class);
405
        handleCollection(result, TaxonName.class, "status", NomenclaturalStatus.class);
406

  
407
        handleCollection(result, TaxonName.class, "registrations", Registration.class);
408
        handleCollection(result, TaxonName.class, "typeDesignations", TypeDesignationBase.class);
409

  
410
        handleCollection(result, TaxonName.class, "taxonBases", TaxonBase.class);
411

  
412
        return result;
413
    }
414

  
415
    protected HomotypicalGroup handlePersistedHomotypicalGroup(HomotypicalGroup group) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
416
        HomotypicalGroup result = handlePersisted((AnnotatableEntity)group);
417
        if (result ==null){
418
            return result;
419
        }
420
        handleCollection(result, HomotypicalGroup.class, "typifiedNames", TaxonName.class);
421
        return result;
422
    }
423

  
424
    protected Annotation handlePersistedAnnotation(Annotation annotation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
425
        Annotation result = handlePersisted((AnnotatableEntity)annotation);
426
        if (result ==null){
427
            return result;
428
        }
429
        result.setAnnotationType(detache(annotation.getAnnotationType()));
430
        handleCollection(result, Annotation.class, "intextReferences", IntextReference.class);
431
        return result;
432
    }
433

  
434
    protected Extension handlePersistedExtension(Extension extension) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
435
        Extension result = handlePersisted((VersionableEntity)extension);
436
        if (result ==null){
437
            return result;
438
        }
439
        result.setType(detache(extension.getType()));
440
        return result;
441
    }
442

  
443
    protected Marker handlePersistedMarker(Marker marker) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
444
        Marker result = handlePersisted((VersionableEntity)marker);
445
        if (result ==null){
446
            return result;
447
        }
448
        result.setMarkerType(detache(marker.getMarkerType()));
449
        return result;
450
    }
451

  
452
    protected Team handlePersistedTeam(Team team) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
453
        Team result = handlePersisted((TeamOrPersonBase)team);
454
        if (result ==null){
455
            return result;
456
        }
457
        handleCollection(result, Team.class, "teamMembers", Person.class);
458
        return result;
459
    }
460

  
461
    protected Contact handlePersistedContact(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff