Project

General

Profile

« Previous | Next » 

Revision 5815f954

Added by Andreas Müller over 4 years ago

ref #8479 FauEu Cdm2Cdm import more or less running but still slow

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/common/CdmDestinations.java
39 39
		return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
40 40
	}
41 41

  
42
    public static ICdmDataSource cdm_test_local_pesi_leer(){
43
        DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
44
        String cdmServer = "127.0.0.1";
45
        String cdmDB = "cdm_pesi_test_leer";
46
        String cdmUserName = "edit";
47
        return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
48
    }
49

  
50
    public static ICdmDataSource cdm_test_local_pesi_leer2(){
51
        DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
52
        String cdmServer = "127.0.0.1";
53
        String cdmDB = "cdm_pesi_test_leer2";
54
        String cdmUserName = "edit";
55
        return makeDestination(dbType, cdmServer, cdmDB, -1, cdmUserName, null);
56
    }
57

  
42 58

  
43 59
	public static ICdmDataSource cdm_test_local_indexFungorum(){
44 60
		DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/FauEu2CdmActivator.java
8 8
*/
9 9
package eu.etaxonomy.cdm.app.pesi;
10 10

  
11
import java.util.UUID;
12

  
11 13
import org.apache.log4j.Logger;
12 14

  
13 15
import eu.etaxonomy.cdm.app.common.CdmDestinations;
......
15 17
import eu.etaxonomy.cdm.database.ICdmDataSource;
16 18
import eu.etaxonomy.cdm.io.common.CdmDefaultImport;
17 19
import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
18
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
19 20
import eu.etaxonomy.cdm.io.pesi.fauEu2Cdm.FauEu2CdmImportConfigurator;
20 21

  
21 22
/**
......
28 29
    private static final Logger logger = Logger.getLogger(FauEu2CdmActivator.class);
29 30

  
30 31
    static final ICdmDataSource fauEuSource = CdmDestinations.cdm_pesi_fauna_europaea();
31
    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_erms();
32
    static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_pesi_leer2();
32 33

  
33 34
    //check - import
34 35
    static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
......
37 38

  
38 39
// ***************** ALL ************************************************//
39 40

  
40
    //references
41
    static final DO_REFERENCES doReferences =  DO_REFERENCES.ALL;
42

  
43
    //taxa
44
    static final boolean doTaxa = true;
41
//    UUID uuidTaxonNodeFilter = UUID.fromString("0e8bc793-f434-47c4-ba82-650c3bbd83bf");
42
    UUID uuidTaxonNodeFilter = UUID.fromString("7ee4983b-78a3-44c5-9af2-beb0494b5fc8");
45 43

  
46 44

  
47 45
    private void doImport(ICdmDataSource source, ICdmDataSource destination, DbSchemaValidation hbm2dll){
......
53 51

  
54 52
//        config.setDoTaxa(doTaxa);
55 53
        config.setDbSchemaValidation(hbm2dll);
54
        config.getTaxonNodeFilter().orSubtree(uuidTaxonNodeFilter);
56 55

  
57 56
        config.setCheck(check);
58 57
//        config.setRecordsPerTransaction(partitionSize);
......
72 71
        ICdmDataSource cdmDB = CdmDestinations.chooseDestination(args) != null ? CdmDestinations.chooseDestination(args) : cdmDestination;
73 72
        FauEu2CdmActivator myImport = new FauEu2CdmActivator();
74 73
        myImport.doImport(fauEuSource, cdmDB, DbSchemaValidation.VALIDATE);
75

  
74
        System.exit(0);
76 75
    }
77 76
}
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;
11 14
import java.util.ArrayList;
12 15
import java.util.Collection;
16
import java.util.HashMap;
17
import java.util.HashSet;
13 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
import org.springframework.stereotype.Component;
25
import org.springframework.transaction.TransactionStatus;
14 26

  
15 27
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
16 28
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
17
import eu.etaxonomy.cdm.io.common.XmlExportState;
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;
18 36
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
19 37
import eu.etaxonomy.cdm.model.common.Annotation;
20 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;
21 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;
22 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;
23 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;
24 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;
25 104

  
26 105
/**
27 106
 * @author a.mueller
28 107
 * @since 17.08.2019
29 108
 */
109
@Component
30 110
public class FauEu2CdmImport
31 111
        extends FauEu2CdmImportBase {
32 112

  
33 113
    private static final long serialVersionUID = -2111102574346601573L;
114
    private static final Logger logger = Logger.getLogger(FauEu2CdmImport.class);
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<>();
120

  
121
    private Set<CdmBase> toSave = new HashSet<>();
34 122

  
35
    @Override
36
    protected boolean doCheck(FauEu2CdmImportState state) {
37
        return false;
38
    }
39 123

  
40 124
    @Override
41 125
    protected void doInvoke(FauEu2CdmImportState state) {
42 126
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
127
        System.out.println("start source repo");
128
        source(state);
129
        System.out.println("end source repo");
43 130
        FauEu2CdmImportConfigurator config = state.getConfig();
44
        if (config.getTaxonNodeFilter().getClassificationFilter() != null
45
                && !config.getTaxonNodeFilter().getClassificationFilter().isEmpty()) {
131
        if (config.getTaxonNodeFilter().hasClassificationFilter()) {
46 132
            Classification classification = getClassificationService()
47 133
                    .load(config.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
48 134
            state.setRootId(classification.getRootNode().getUuid());
49

  
50
        } else if (config.getTaxonNodeFilter().getSubtreeFilter() != null
51
                && !config.getTaxonNodeFilter().getSubtreeFilter().isEmpty()) {
135
        } else if (config.getTaxonNodeFilter().hasSubtreeFilter()) {
52 136
            state.setRootId(config.getTaxonNodeFilter().getSubtreeFilter().get(0).getUuid());
53 137
        }
54 138
        @SuppressWarnings("unchecked")
55
        TaxonNodeOutStreamPartitioner<XmlExportState> partitioner = TaxonNodeOutStreamPartitioner.NewInstance(this,
56
                state, state.getConfig().getTaxonNodeFilter(), 100, monitor, null);
57

  
139
        TaxonNodeOutStreamPartitioner<FauEu2CdmImportState> partitioner = TaxonNodeOutStreamPartitioner
140
                .NewInstance(source(state), state, state.getConfig().getTaxonNodeFilter(), 100, monitor, null);
58 141
        monitor.subTask("Start partitioning");
59 142

  
143
        doData(state, partitioner);
144
    }
145

  
146
    /**
147
     * @param state
148
     * @param partitioner
149
     * @throws IllegalAccessException
150
     * @throws InvocationTargetException
151
     */
152
    private void doData(FauEu2CdmImportState state, TaxonNodeOutStreamPartitioner<FauEu2CdmImportState> partitioner){
60 153
        TaxonNode node = partitioner.next();
154
        int partitionSize = 100;
155
        int count = 0;
156
        TransactionStatus tx = startTransaction();
61 157
        while (node != null) {
62
            handleTaxonNode(state, node);
158
            node = doSingleNode(state, node);
159
            count++;
160
            if (count>=partitionSize){
161
                clearCache();
162
                try {
163
                    commitTransaction(tx);
164
                } catch (Exception e) {
165
                    logger.warn("Exception during commit node " + node.treeIndex());
166
                    e.printStackTrace();
167
                }
168
                tx = startTransaction();
169
                count=0;
170
            }
63 171
            node = partitioner.next();
64 172
        }
173
        commitTransaction(tx);
174

  
65 175
    }
66 176

  
67 177
    /**
68 178
     * @param state
69 179
     * @param node
180
     * @return
70 181
     */
71
    private void handleTaxonNode(FauEu2CdmImportState state, TaxonNode node) {
72
//        Integer id = state.getTarget(createdBy.getUuid(), createdBy.getClass());
73
        handleAnnotatableEntity(node);
182
    private TaxonNode doSingleNode(FauEu2CdmImportState state, TaxonNode node) {
183
        TaxonNode result = null;
184
        logger.info(node.treeIndex());
185
        try {
186
            result = detache(node);
187
        } catch (Exception e) {
188
            logger.warn("Exception during detache node " + node.treeIndex());
189
            e.printStackTrace();
190
        }
191
        try {
192
            if (result != null){
193
                getTaxonNodeService().saveOrUpdate(node);
194
                getCommonService().saveOrUpdate(toSave);
195
                toSave.clear();
196
            }
197
        } catch (Exception e) {
198
            logger.warn("Exception during save node " + node.treeIndex());
199
             e.printStackTrace();
200
        }
74 201

  
202
        return result;
203
    }
75 204

  
205
    private void clearCache() {
206
        sessionCache.clear();
76 207
    }
77 208

  
78
    private void handleAnnotatableEntity(AnnotatableEntity entity) {
79
        handleVersionableEntity(entity);
80
        for (Annotation a : getTargetCollection(entity.getAnnotations())){
81
            entity.addAnnotation(a);
209
    private TaxonNode handlePersistedTaxonNode(TaxonNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
210

  
211
        TaxonNode result = handlePersisted((AnnotatableEntity)node);
212
        if (result ==null){
213
            return result;
82 214
        }
83
        for (Marker a : getTargetCollection(entity.getMarkers())){
84
            entity.addMarker(a);
215
        handleCollection(result, TaxonNode.class, "agentRelations", TaxonNodeAgentRelation.class);
216
        result.setTaxon(detache(result.getTaxon()));
217
        result.setReference(detache(node.getReference()));
218
        handleMap(result, TaxonNode.class, "excludedNote", Language.class, LanguageString.class);
219
        //classification, parent, children
220
        this.setInvisible(node, "classification", detache(node.getClassification()));
221
        handleParentTaxonNode(result);
222
        setNewCollection(node, TaxonNode.class, "childNodes", TaxonNode.class);
223
        return result;
224
    }
225

  
226
    private void handleParentTaxonNode(TaxonNode childNode) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
227
        TaxonNode parent = detache(childNode.getParent());
228
        if (parent == null){
229
            return;
230
        }
231
        //TODO
232
        String microReference = null;
233
        Reference reference = null;
234
        parent.addChildNode(childNode, reference, microReference);
235
    }
236

  
237
    private void setInvisible(Object holder, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
238
        setInvisible(holder, holder.getClass(), fieldName, value);
239
    }
240
    private void setInvisible(Object holder, Class<?> holderClazz, String fieldName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
241
        Field field = holderClazz.getDeclaredField(fieldName);
242
        field.setAccessible(true);
243
        field.set(holder, value);
244
    }
245

  
246
    private Classification handlePersistedClassification(Classification classification) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
247
        Classification result = handlePersisted((IdentifiableEntity)classification);
248
        if (result ==null){
249
            return result;
85 250
        }
86
//        entity.setUpdatedBy(getTarget(entity.getUpdatedBy()));
251
        result.setName(detache(classification.getName()));
252
        result.setReference(detache(classification.getReference()));
253
        result.setRootNode(detache(classification.getRootNode()));
254
        handleCollection(result, Classification.class, "geoScopes", NamedArea.class);
255
        handleMap(result, Classification.class, "description", Language.class, LanguageString.class);
256

  
257
        return result;
258
    }
259

  
260
    private Reference handlePersistedReference(Reference reference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
261
        Reference result = handlePersisted((IdentifiableMediaEntity)reference);
262
        result.setAuthorship(detache(result.getAuthorship()));
263
        result.setInstitution(detache(result.getInstitution()));
264
        result.setSchool(detache(result.getSchool()));
265
        result.setInReference(detache(result.getInReference()));
266
        return result;
267
    }
268

  
269
    private SpecimenOrObservationBase<?> handlePersistedSpecimenOrObservationBase(SpecimenOrObservationBase specimen) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
270
        SpecimenOrObservationBase<?> result = handlePersisted((IdentifiableEntity)specimen);
271
        result.setSex(detache(result.getSex()));
272
        result.setLifeStage(detache(result.getLifeStage()));
273
        result.setKindOfUnit(detache(result.getKindOfUnit()));
274
        //TODO implement for classes
275
        handleCollection(result, SpecimenOrObservationBase.class, "determinations", DeterminationEvent.class);
276
        handleCollection(result, SpecimenOrObservationBase.class, "descriptions", SpecimenDescription.class);
277
        handleCollection(result, SpecimenOrObservationBase.class, "derivationEvents", DerivationEvent.class);
278
        handleMap(result, SpecimenOrObservationBase.class, "definition", Language.class, LanguageString.class);
279
        return result;
280
    }
281

  
282
    private IdentifiableSource handlePersistedIdentifiableSource(IdentifiableSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
283
        IdentifiableSource result = handlePersisted((OriginalSourceBase)source);
284
        if (result ==null){
285
            return null;
286
        }
287
        return result;
288
    }
289

  
290
    private DescriptionElementSource handlePersistedDescriptionElementSource(DescriptionElementSource source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
291
        DescriptionElementSource result = handlePersisted((OriginalSourceBase)source);
292
        result.setNameUsedInSource(detache(result.getNameUsedInSource()));
293
        return result;
294
    }
295

  
296
    private <T extends OriginalSourceBase> T  handlePersisted(OriginalSourceBase source) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
297
        T result = handlePersisted((ReferencedEntityBase)source);
298
        handleCollection(result, OriginalSourceBase.class, "links", ExternalLink.class);
299

  
300
        return result;
301
    }
302

  
303
    private LanguageString handlePersistedLanguageString(LanguageString languageString) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
304
        LanguageString result = handlePersisted((LanguageStringBase)languageString);
305
        handleCollection(result, LanguageString.class, "intextReferences", IntextReference.class);
306
        return result;
307
    }
308

  
309
    private IntextReference handlePersistedIntextReference(IntextReference intextReference) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
310
        IntextReference result = handlePersisted((VersionableEntity)intextReference);
311
        result.setReferencedEntity(detache(result.getReferencedEntity(), false));
312
        Method targetMethod = IntextReference.class.getDeclaredMethod("setTarget", IIntextReferenceTarget.class);
313
        targetMethod.setAccessible(true);
314
        targetMethod.invoke(result, detache(result.getTarget(), false));
315
        return result;
316
    }
317

  
318
    private <T extends LanguageStringBase> T  handlePersisted(LanguageStringBase lsBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
319
        T result = handlePersisted((AnnotatableEntity)lsBase);
320
        if (result ==null){
321
            return null;
322
        }
323
        result.setLanguage(detache(lsBase.getLanguage()));
324
        return result;
87 325
    }
88 326

  
89 327
    /**
90
     * @param annotations
328
     * @param classification
91 329
     * @return
330
     * @throws InvocationTargetException
331
     * @throws IllegalAccessException
332
     * @throws IllegalArgumentException
333
     * @throws SecurityException
334
     * @throws NoSuchFieldException
335
     * @throws NoSuchMethodException
92 336
     */
93
    private <T extends Collection<S>, S extends CdmBase> List<S> getTargetCollection(T sourceCollection) {
94
        List<S> result = new ArrayList<>();
337
    private <T extends IdentifiableEntity> T  handlePersisted(IdentifiableEntity identifiableEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
338
        T result = handlePersisted((SourcedEntityBase)identifiableEntity);
339
        if (result ==null){
340
            return null;
341
        }
342
        handleCollection(result, IdentifiableEntity.class, "credits", Credit.class);
343
        handleCollection(result, IdentifiableEntity.class, "extensions", Extension.class);
344
        handleCollection(result, IdentifiableEntity.class, "identifiers", Identifier.class);
345
        handleCollection(result, IdentifiableEntity.class, "rights", Rights.class);
346
        return result;
347
    }
348

  
349
    private <T extends TeamOrPersonBase> T  handlePersisted(TeamOrPersonBase teamOrPerson) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
350
        T result = handlePersisted((AgentBase)teamOrPerson);
351
        if (result ==null){
352
            return null;
353
        }
354
        return result;
355
    }
356

  
357
    private <T extends AgentBase> T  handlePersisted(AgentBase agent) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
358
        T result = handlePersisted((IdentifiableMediaEntity)agent);
359
        result.setContact(detache(result.getContact()));
360
        return result;
361
    }
362

  
363
    private <T extends TaxonBase> T  handlePersisted(TaxonBase taxonBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
364
        T result = handlePersisted((IdentifiableEntity)taxonBase);
365
        if (result ==null){
366
            return null;
367
        }
368
        result.setName(detache(taxonBase.getName()));
369
        result.setSec(detache(taxonBase.getSec()));
370
//        handleCollection(result, IdentifiableEntity.class, "credits", Credit.class);
371
//        handleCollection(result, IdentifiableEntity.class, "extensions", Extension.class);
372
//        handleCollection(result, IdentifiableEntity.class, "identifiers", Identifier.class);
373
//        handleCollection(result, IdentifiableEntity.class, "rights", Rights.class);
374
        return result;
375
    }
376

  
377
    private <T extends SourcedEntityBase> T  handlePersisted(SourcedEntityBase sourcedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
378
        T result = handlePersisted((AnnotatableEntity)sourcedEntity);
379
        if (result ==null){
380
            return null;
381
        }
382
        handleCollection(result, SourcedEntityBase.class, "sources", OriginalSourceBase.class);
383
        return result;
384
    }
385

  
386
    private <T extends IdentifiableMediaEntity> T  handlePersisted(IdentifiableMediaEntity mediaEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
387
        T result = handlePersisted((IdentifiableEntity)mediaEntity);
388
        handleCollection(result, IdentifiableMediaEntity.class, "media", Media.class);
389
        return result;
390
    }
391

  
392
    private <T extends ReferencedEntityBase> T  handlePersisted(ReferencedEntityBase referencedEntity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
393
        T result = handlePersisted((AnnotatableEntity)referencedEntity);
394
        if (result ==null){
395
            return null;
396
        }
397
        result.setCitation(detache(result.getCitation()));
398
        return result;
399
    }
400

  
401
    private <T extends DescriptionBase> T  handlePersisted(DescriptionBase descriptionBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
402
        T result = handlePersisted((IdentifiableEntity)descriptionBase);
403
        handleCollection(result, DescriptionBase.class, "descriptionElements", DescriptionElementBase.class);
404
        handleCollection(result, DescriptionBase.class, "descriptiveDataSets", DescriptiveDataSet.class);
405
        handleCollection(result, DescriptionBase.class, "descriptionSources", Reference.class);
406
        result.setDescribedSpecimenOrObservation(detache(descriptionBase.getDescribedSpecimenOrObservation()));
407
        return result;
408
    }
409

  
410
    private <T extends DescriptionElementBase> T  handlePersisted(DescriptionElementBase element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
411
        T result = handlePersisted((AnnotatableEntity)element);
412
        result.setFeature(detache(result.getFeature()));
413
        setInvisible(result, DescriptionElementBase.class, "inDescription", detache(result.getInDescription()));
414
        handleCollection(result, DescriptionElementBase.class, "sources", DescriptionElementSource.class);
415
        handleCollection(result, DescriptionElementBase.class, "media", Media.class);
416
        handleCollection(result, DescriptionElementBase.class, "modifiers", DefinedTerm.class);
417
        handleMap(result, DescriptionElementBase.class, "modifyingText", Language.class, LanguageString.class);
418

  
419
        return result;
420
    }
421

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

  
429
    private <T extends TextData> T  handlePersistedTextData(TextData element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
430
        T result = handlePersisted((DescriptionElementBase)element);
431
        result.setFormat(detache(result.getFormat()));
432
        handleMap(result, TextData.class, "multilanguageText", Language.class, LanguageString.class);
433
        return result;
434
    }
435

  
436
    private <T extends Distribution> T  handlePersistedDistribution(Distribution element) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
437
        T result = handlePersisted((DescriptionElementBase)element);
438
        result.setArea(detache(result.getArea()));
439
        result.setStatus(detache(result.getStatus()));
440
        return result;
441
    }
442

  
443
    private <T extends RelationshipBase> T  handlePersisted(RelationshipBase relBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
444
        T result = handlePersisted((ReferencedEntityBase)relBase);
445
        return result;
446
    }
447

  
448
    private <T extends TaxonDescription> T  handlePersistedTaxonDescription(TaxonDescription taxDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
449
        T result = handlePersisted((DescriptionBase)taxDescription);
450
        setInvisible(taxDescription, "taxon", detache(taxDescription.getTaxon()));
451
        handleCollection(taxDescription, TaxonDescription.class, "geoScopes", NamedArea.class);
452
        handleCollection(taxDescription, TaxonDescription.class, "scopes", DefinedTerm.class);
453
        return result;
454
    }
455

  
456
    private <T extends TaxonDescription> T  handlePersistedTaxonNameDescription(TaxonNameDescription nameDescription) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
457
        T result = handlePersisted((DescriptionBase)nameDescription);
458
        setInvisible(nameDescription, "taxonName", detache(nameDescription.getTaxonName()));
459
        return result;
460
    }
461

  
462
    private <T extends AnnotatableEntity> T handlePersisted(AnnotatableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
463
        T result = handlePersisted((VersionableEntity)entity);
464
        if (result ==null){
465
            return null;
466
        }
467
        handleCollection(result, AnnotatableEntity.class, "annotations", Annotation.class);
468
        handleCollection(result, AnnotatableEntity.class, "markers", Marker.class);
469
        return result;
470
    }
471

  
472
    private <HOLDER extends CdmBase, ITEM extends CdmBase> void handleCollection(
473
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter, Class<ITEM> itemClass)
474
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
475

  
476
        Collection<ITEM> oldCollection = setNewCollection(holder, declaringClass, parameter, itemClass);
477
        Collection<ITEM> newCollection = getTargetCollection(itemClass, oldCollection);
478
        Field field = declaringClass.getDeclaredField(parameter);
479
        field.setAccessible(true);
480
        field.set(holder, newCollection);
481
//        EntityCollectionSetterAdapter<HOLDER,ITEM> adapter = new EntityCollectionSetterAdapter(declaringClass, itemClass, parameter);
482
//        if ("rights".equals(parameter)){
483
//            adapter.setAddMethodName("addRights");
484
//            adapter.setRemovMethodName("removeRights");
485
//        }
486
//        if ("media".equals(parameter)){
487
//            adapter.setAddMethodName("addMedia");
488
//            adapter.setRemovMethodName("removeMedia");
489
//        }
490
//        if ("media".equals(parameter)){
491
//            adapter.setAddMethodName("addMedia");
492
//            adapter.setRemovMethodName("removeMedia");
493
//        }
494
//        if ("media".equals(parameter)){
495
//            adapter.setAddMethodName("addMedia");
496
//            adapter.setRemovMethodName("removeMedia");
497
//        }
498
//        try {
499
//            adapter.setCollection(holder, newCollection);
500
//        } catch (SetterAdapterException e) {
501
//
502
//            throw new RuntimeException(e);
503
//        }
504
////        for (T a : getTargetCollection(itemClass, oldCollection)){
505
////
506
////            result.addAnnotation(a);
507
////        }
508
    }
509

  
510
    private <HOLDER extends CdmBase, KEY extends CdmBase, ITEM extends CdmBase>
511
            void handleMap(
512
            HOLDER holder, Class<? super HOLDER> declaringClass, String parameter,
513
            Class<KEY> keyClass, Class<ITEM> itemClass)
514
            throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
515

  
516
        //TODO we do not need to set the new map 2x
517
        Map<KEY,ITEM> oldMap = setNewMap(holder, declaringClass, parameter, keyClass, itemClass);
518
        Map<KEY,ITEM> newMap = getTargetMap(oldMap);
519
        Field field = declaringClass.getDeclaredField(parameter);
520
        field.setAccessible(true);
521
        field.set(holder, newMap);
522
    }
523

  
524
    private <T extends CdmBase> Collection<T> setNewCollection(CdmBase obj, Class<?> holderClass,
525
            String parameter, Class<T> entityClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
526
        Field field = holderClass.getDeclaredField(parameter);
527
        field.setAccessible(true);
528
        Collection<T> oldValue = (Collection<T>)field.get(obj);
529
        Collection<T> newValue = null;
530
        if (Set.class.isAssignableFrom(field.getType())){
531
            newValue = new HashSet<>();
532
        }else if (List.class.isAssignableFrom(field.getType())){
533
            newValue = new ArrayList<>();
534
        }else{
535
            throw new RuntimeException("Unsupported collection type: " + field.getType().getCanonicalName());
536
        }
537
        field.set(obj, newValue);
538
        return oldValue;
539
    }
540

  
541
    private <KEY extends CdmBase, ITEM extends CdmBase> Map<KEY,ITEM> setNewMap(CdmBase obj, Class<?> holderClass,
542
            String parameter, Class<KEY> keyClass, Class<ITEM> itemClass) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
543
        Field field = holderClass.getDeclaredField(parameter);
544
        field.setAccessible(true);
545
        Map<KEY,ITEM> oldValue = (Map<KEY,ITEM>)field.get(obj);
546
        Map<KEY,ITEM> newValue = null;
547
        if (Map.class.isAssignableFrom(field.getType())){
548
            newValue = new HashMap<>();
549
        }else{
550
            throw new RuntimeException("Unsupported map type: " + field.getType().getCanonicalName());
551
        }
552
        field.set(obj, newValue);
553
        return oldValue;
554
    }
555

  
556
    private <T extends Collection<S>, S extends CdmBase> Collection<S> getTargetCollection(Class<S> clazz, T sourceCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
557
        Collection<S> result =  new ArrayList<>();
558
        if (Set.class.isAssignableFrom(sourceCollection.getClass())){
559
            result = new HashSet<>();
560
        }
95 561
        for (S entity : sourceCollection){
96
            S target = getTarget(entity);
562
            S target = detache(entity);
97 563
            result.add(target);
98 564
        }
99 565
        return result;
100 566
    }
101 567

  
102
    private void handleVersionableEntity(VersionableEntity entity) {
103
        handleCdmBase(entity);
104
        entity.setUpdatedBy(getTarget(entity.getUpdatedBy()));
568
    private <K extends CdmBase, V extends CdmBase> Map<K,V> getTargetMap(Map<K,V> sourceMap) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
569
        Map<K,V> result = new HashMap<>();
570
        for (K key : sourceMap.keySet()){
571
            K targetKey = detache(key);
572
            V targetValue = detache(sourceMap.get(key));
573
            result.put(targetKey, targetValue);
574
        }
575
        return result;
576
    }
577

  
578
    private <T extends VersionableEntity> T handlePersisted(VersionableEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
579
        T result = (T)handlePersistedCdmBase((CdmBase)entity);
580
        if (result ==null){
581
            return null;
582
        }
583
        entity.setUpdatedBy(detache(entity.getUpdatedBy()));
584
        return result;
585
    }
586

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

  
669
    /**
670
     * @param cdmBase
671
     * @return
672
     * @throws NoSuchMethodException
673
     * @throws IllegalArgumentException
674
     * @throws SecurityException
675
     * @throws NoSuchFieldException
676
     * @throws InvocationTargetException
677
     * @throws IllegalAccessException
678
     */
679
    private Taxon handlePersistedTaxon(Taxon taxon) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
680
        Taxon result = handlePersisted((TaxonBase)taxon);
681
        handleCollection(result, Taxon.class, "synonyms", Synonym.class);
682
//        handleCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
683
        setNewCollection(result, Taxon.class, "taxonNodes", TaxonNode.class);
684
        handleCollection(result, Taxon.class, "relationsFromThisTaxon", TaxonRelationship.class);
685
        handleCollection(result, Taxon.class, "relationsToThisTaxon", TaxonRelationship.class);
686
        handleCollection(result, Taxon.class, "descriptions", TaxonDescription.class);
687
        return result;
688
    }
689

  
690
    private Synonym handlePersistedSynonym(Synonym synonym) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
691
        Synonym result = handlePersisted((TaxonBase)synonym);
692
        setInvisible(result, "acceptedTaxon", detache(result.getAcceptedTaxon()));
693
        result.setType(detache(result.getType()));
694
        return result;
695
    }
696

  
697
    private TaxonRelationship handlePersistedTaxonRelationship(TaxonRelationship taxRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
698
        TaxonRelationship result = handlePersisted((RelationshipBase)taxRel);
699
        result.setFromTaxon(detache(result.getFromTaxon()));
700
        result.setToTaxon(detache(result.getToTaxon()));
701
        result.setType(detache(result.getType()));
702
        return result;
703
    }
704

  
705
    private NameRelationship handlePersistedNameRelationship(NameRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
706
        NameRelationship result = handlePersisted((RelationshipBase)rel);
707
        setInvisible(result, "relatedFrom", detache(result.getFromName()));
708
        setInvisible(result, "relatedTo", detache(result.getToName()));
709
//        result.setFromName(detache(result.getFromName()));
710
//        result.setToName(detache(result.getToName()));
711
        result.setType(detache(result.getType()));
712
        return result;
713
    }
714

  
715
    private HybridRelationship handlePersistedHybridRelationship(HybridRelationship rel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
716
        HybridRelationship result = handlePersisted((RelationshipBase)rel);
717
        setInvisible(result, "relatedFrom", detache(result.getParentName()));
718
        setInvisible(result, "relatedTo", detache(result.getHybridName()));
719
//        result.setFromName(detache(result.getFromName()));
720
//        result.setToName(detache(result.getToName()));
721
        result.setType(detache(result.getType()));
722
        return result;
723
    }
724
    private NomenclaturalStatus handlePersistedNomenclaturalStatus(NomenclaturalStatus status) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
725
        NomenclaturalStatus result = handlePersisted((ReferencedEntityBase)status);
726
        result.setType(detache(result.getType()));
727
        return result;
728
    }
729

  
730
    private TypeDesignationBase handlePersistedTypeDesignationBase(TypeDesignationBase<?> designation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
731
        TypeDesignationBase result = handlePersisted((SourcedEntityBase)designation);
732
        result.setCitation(detache(result.getCitation()));
733
        handleCollection(result, TypeDesignationBase.class, "registrations", Registration.class);
734
        handleCollection(result, TypeDesignationBase.class, "typifiedNames", TaxonName.class);
735
        result.setTypeStatus(detache(result.getTypeStatus()));
736
        return result;
737
    }
738

  
739
    private InstitutionalMembership handlePersistedInstitutionalMembership(InstitutionalMembership institutionalMembership) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
740
        InstitutionalMembership result = handlePersisted((VersionableEntity)institutionalMembership);
741
//        result.setPerson(detache(result.getPerson()));
742
        setInvisible(result, "person", detache(result.getPerson()));
743
        result.setInstitute(detache(result.getInstitute()));
744
        return result;
745
    }
746

  
747
    private Institution handlePersistedInstitution(Institution institution) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
748
        Institution result = handlePersisted((AgentBase)institution);
749
        result.setIsPartOf(detache(result.getIsPartOf()));
750
        handleCollection(result, Institution.class, "types", DefinedTerm.class);
751
        return result;
752
    }
753

  
754
    private TaxonNodeAgentRelation handlePersistedTaxonNodeAgentRelation(TaxonNodeAgentRelation nodeAgentRel) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
755
        TaxonNodeAgentRelation result = handlePersisted((AnnotatableEntity)nodeAgentRel);
756
        if (result ==null){
757
            return result;
758
        }
759
        result.setAgent(detache(result.getAgent()));
760
        result.setType(detache(result.getType()));
761
        setInvisible(result, "taxonNode", detache(result.getTaxonNode()));
762
        return result;
763
    }
764

  
765

  
766
    private TaxonName handlePersistedTaxonName(TaxonName taxonName) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
767
        @SuppressWarnings("rawtypes")
768
        TaxonName result = handlePersisted((IdentifiableEntity)taxonName);
769
        if (result ==null){
770
            return result;
771
        }
772
        result.setCombinationAuthorship(detache(result.getCombinationAuthorship()));
773
        result.setExCombinationAuthorship(detache(result.getExCombinationAuthorship()));
774
        result.setBasionymAuthorship(detache(result.getBasionymAuthorship()));
775
        result.setExBasionymAuthorship(detache(result.getExBasionymAuthorship()));
776
        result.setInBasionymAuthorship(detache(result.getInBasionymAuthorship()));
777
        result.setInCombinationAuthorship(detache(result.getInCombinationAuthorship()));
778

  
779
        result.setNomenclaturalReference(detache(result.getNomenclaturalReference()));
780
        result.setHomotypicalGroup(detache(result.getHomotypicalGroup()));
781
        handleCollection(result, TaxonName.class, "descriptions", TaxonNameDescription.class);
782
        handleCollection(result, TaxonName.class, "hybridChildRelations", HybridRelationship.class);
783
        handleCollection(result, TaxonName.class, "hybridParentRelations", HybridRelationship.class);
784
        handleCollection(result, TaxonName.class, "relationsFromThisName", NameRelationship.class);
785
        handleCollection(result, TaxonName.class, "relationsToThisName", NameRelationship.class);
786
        handleCollection(result, TaxonName.class, "status", NomenclaturalStatus.class);
787

  
788
        handleCollection(result, TaxonName.class, "registrations", Registration.class);
789
        handleCollection(result, TaxonName.class, "typeDesignations", TypeDesignationBase.class);
790

  
791
        handleCollection(result, TaxonName.class, "taxonBases", TaxonBase.class);
792

  
793
        return result;
794
    }
795

  
796
    private HomotypicalGroup handlePersistedHomotypicalGroup(HomotypicalGroup group) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
797
        HomotypicalGroup result = handlePersisted((AnnotatableEntity)group);
798
        if (result ==null){
799
            return result;
800
        }
801
        handleCollection(result, HomotypicalGroup.class, "typifiedNames", TaxonName.class);
802
        return result;
803
    }
804

  
805
    private Annotation handlePersistedAnnotation(Annotation annotation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
806
        Annotation result = handlePersisted((AnnotatableEntity)annotation);
807
        if (result ==null){
808
            return result;
809
        }
810
        result.setAnnotationType(detache(annotation.getAnnotationType()));
811
        handleCollection(result, Annotation.class, "intextReferences", IntextReference.class);
812
        return result;
813
    }
814

  
815
    private Extension handlePersistedExtension(Extension extension) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
816
        Extension result = handlePersisted((VersionableEntity)extension);
817
        if (result ==null){
818
            return result;
819
        }
820
        result.setType(detache(extension.getType()));
821
        return result;
822
    }
823

  
824
    private Marker handlePersistedMarker(Marker marker) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
825
        Marker result = handlePersisted((VersionableEntity)marker);
826
        if (result ==null){
827
            return result;
828
        }
829
        result.setMarkerType(detache(marker.getMarkerType()));
830
        return result;
831
    }
832

  
833
    private Team handlePersistedTeam(Team team) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
834
        Team result = handlePersisted((TeamOrPersonBase)team);
835
        if (result ==null){
836
            return result;
837
        }
838
        handleCollection(result, Team.class, "teamMembers", Person.class);
839
        return result;
840
    }
841

  
842
    private Contact handlePersistedContact(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
843
        Contact result = contact; // getTarget(contact);
844
        if (result ==null){
845
            return result;
846
        }
847
        if (!contact.getAddresses().isEmpty() || !contact.getEmailAddresses().isEmpty()
848
               || !contact.getFaxNumbers().isEmpty() ||!contact.getPhoneNumbers().isEmpty()
849
               ||!contact.getUrls().isEmpty()){
850
            logger.warn("Addresses not yet implemented");
851
        }
852
        setInvisible(result, "addresses", new HashSet<>());
853
//        handleCollection(result, Contact.class, "", Address.class);
854
        setInvisible(result, "faxNumbers", new ArrayList<>());
855
        setInvisible(result, "phoneNumbers", new ArrayList<>());
856
        setInvisible(result, "emailAddresses", new ArrayList<>());
857
        setInvisible(result, "urls", new ArrayList<>());
858
        return result;
859
    }
860

  
861
    private Person handlePersistedPerson(Person person) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
862
        Person result = handlePersisted((TeamOrPersonBase)person);
863
        handleCollection(result, Person.class, "institutionalMemberships", InstitutionalMembership.class);
864
        return result;
865
    }
866

  
867
    private NamedArea handlePersistedNamedArea(NamedArea area) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
868
        NamedArea result = handlePersisted((OrderedTermBase)area);
869
        handleCollection(result, NamedArea.class, "countries", Country.class);
870
        result.setLevel(detache(result.getLevel()));
871
        result.setType(detache(result.getType()));
872
        result.setShape(detache(result.getShape()));
873
        return result;
874
    }
875

  
876
    private <T extends CdmBase> CdmBase handlePersistedCdmBase(CdmBase cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
877
        T result = (T)getTarget(cdmBase);
878
        if (result == null){
879
            return null;
880
        }
881
        cdmBase.setCreatedBy(detache(cdmBase.getCreatedBy()));
882
        return result;
883
    }
884

  
885
    private <T extends DefinedTermBase> T  handlePersisted(DefinedTermBase definedTermBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
886
        T result = handlePersisted((TermBase)definedTermBase);
887

  
888
        handleCollection(result, DefinedTermBase.class, "media", Media.class);
889
        handleCollection(result, DefinedTermBase.class, "generalizationOf", DefinedTermBase.class);
890
        handleCollection(result, DefinedTermBase.class, "includes", DefinedTermBase.class);
891
        setInvisible(result, DefinedTermBase.class, "vocabulary", detache(result.getVocabulary()));
892

  
893
//        getTermService().saveOrUpdate(result);
894

  
895
        return result;
896
    }
897

  
898
    private DefinedTerm handlePersistedDefinedTerm(DefinedTerm term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
899
        DefinedTerm result = handlePersisted((DefinedTermBase)term);
900
        return result;
901
    }
902

  
903
    private ExtensionType handlePersistedExtensionType(ExtensionType term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
904
        ExtensionType result = handlePersisted((DefinedTermBase)term);
905
        return result;
906
    }
907

  
908
    //placeholder for not implemented methods for subclasses
909
    private DefinedTermBase<?> handlePersistedTerm(DefinedTermBase term) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
910
        DefinedTermBase<?> result = handlePersisted(term);
911
        return result;
912
    }
913

  
914

  
915
    private TermVocabulary<?> handlePersistedVocabulary(TermVocabulary voc) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
916
        TermVocabulary<?> result = (TermVocabulary<?>)handlePersisted((TermCollection)voc);
917
        handleCollection(result, TermVocabulary.class, "terms", DefinedTermBase.class);
918
        return result;
919
    }
920

  
921
    private TermNode<?> handlePersistedTermNode(TermNode node) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
922
        TermNode<?> result = (TermNode<?>)handlePersisted((TermRelationBase)node);
923
        setInvisible(result, "parent", detache(result.getParent()));
924
        handleCollection(result, TermNode.class, "inapplicableIf", FeatureState.class);
925
        handleCollection(result, TermNode.class, "onlyApplicableIf", FeatureState.class);
926
        handleCollection(result, TermNode.class, "inapplicableIf_old", State.class);
927
        handleCollection(result, TermNode.class, "onlyApplicableIf_old", State.class);
928
        handleCollection(result, TermNode.class, "children", TermNode.class);
929

  
930
        return result;
931
    }
932

  
933
    private Representation handlePersistedRepresentation(Representation representation) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
934
        Representation result = (Representation)handlePersisted((LanguageStringBase)representation);
935
        return result;
936
    }
937

  
938
    private <T extends TermBase> T  handlePersisted(TermBase termBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
939
        T result = handlePersisted((IdentifiableEntity)termBase);
940
        if (result ==null){
941
            return null;
942
        }
943
        handleCollection(result, TermBase.class, "representations", Representation.class);
944
        return result;
945
    }
946

  
947
    private <T extends TermCollection> T  handlePersisted(TermCollection termCollection) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
948
        T result = handlePersisted((TermBase)termCollection);
949
        if (result ==null){
950
            return null;
951
        }
952
        handleCollection(result, TermCollection.class, "termRelations", TermRelationBase.class);
953
        return result;
954
    }
955

  
956
    private <T extends TermRelationBase> T  handlePersisted(TermRelationBase termRelationBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
957
        T result = handlePersisted((VersionableEntity)termRelationBase);
958
        result.setTerm(detache(result.getTerm()));
959
        setInvisible(result, TermRelationBase.class, "graph", detache(result.getGraph()));
960
        return result;
105 961
    }
106 962

  
107
    private void handleCdmBase(CdmBase cdmBase) {
108
        cdmBase.setCreatedBy(getTarget(cdmBase.getCreatedBy()));
963
    private User handlePersistedUser(User user) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
964
        User result = (User)handlePersistedCdmBase(user);
965
        if (result.getUsername().equals("admin")){
966
            result = getUserService().listByUsername("admin", MatchMode.EXACT, null, null, null, null, null).iterator().next();
967
            permanentCache.put(user.getUuid(), result);
968
            cache(result); //necessary?
969
            toSave.add(result);
970
            toSave.remove(user);
971
        }
972
        if (!result.isPersited()){
973
            result.setAuthorities(new HashSet<>());
974
            result.setGrantedAuthorities(new HashSet<>());
975
            setInvisible(result, "groups", new HashSet<>());
976
        }
977
        return result;
978
    }
979

  
980
    /**
981
     * @param result
982
     */
983
    private void cache(CdmBase cdmBase) {
984
       if (cdmBase instanceof User || cdmBase instanceof DefinedTermBase){
985
           permanentCache.put(cdmBase.getUuid(), cdmBase);
986
       }else{
987
           sessionCache.put(cdmBase.getUuid(), cdmBase);
988
       }
989
       movedObjects.add(cdmBase.getUuid());
990

  
991
    }
992

  
993
    private  Contact detache(Contact contact) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
994
        contact = CdmBase.deproxy(contact);
995
        if (contact == null){
996
            return contact;
997
        }else{
998
            return handlePersistedContact(contact);
999
        }
1000
    }
1001

  
1002
    private  IIntextReferencable detache(IIntextReferencable cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
1003
        return (IIntextReferencable)detache((CdmBase)cdmBase);
1004
    }
1005
    private  IIntextReferenceTarget detache(IIntextReferenceTarget cdmBase, boolean onlyForDefinedSignature) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
1006
        return (IIntextReferenceTarget)detache((CdmBase)cdmBase);
1007
    }
1008

  
1009

  
1010
    private <T extends CdmBase> T detache(T cdmBase) throws IllegalAccessException, InvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, NoSuchMethodException {
1011
        cdmBase = CdmBase.deproxy(cdmBase);
1012
        if (cdmBase == null ){
1013
            return cdmBase;
1014
        }else if(isInCache(cdmBase)){
1015
            return getCached(cdmBase);
1016
        }else {
1017
            //TODO better load all existing UUIDs just in case any of the objects is already in the DB
1018
            if (cdmBase instanceof TermBase || movedObjects.contains(cdmBase.getUuid())){
1019
                Class<T> clazz = (Class<T>)cdmBase.getClass();
1020
                T exists = getCommonService().find(clazz, cdmBase.getUuid());
1021
                if (exists != null){
1022
                    return exists;
1023
                }else if (movedObjects.contains(cdmBase.getUuid())){
1024
                    logger.warn("Object should be moved already but does not exist in target. This should not happen: " + cdmBase.getUuid());
1025
                }
1026
            }
1027
        }
1028
        if ( !cdmBase.isPersited()){
1029
            logger.warn("Non persisted object not in cache and not in target DB. This should not happen: " + cdmBase.getUuid());
1030
            return cdmBase; //should not happen anymore; either in cache or in target or persisted in source
1031
        }else{
1032
            return (T)handlePersisted(cdmBase);
1033
        }
1034
    }
1035

  
1036

  
1037
    /**
1038
     * @param cdmBase
1039
     * @return
1040
     */
1041
    private boolean isInCache(CdmBase cdmBase) {
1042
        return getCached(cdmBase) != null;
1043
    }
1044

  
1045
    /**
1046
     * @param cdmBase
1047
     * @return
1048
     */
1049
    private <T extends CdmBase> T getCached(T cdmBase) {
1050
        T result = (T)sessionCache.get(cdmBase.getUuid());
1051
        if (result == null){
1052
            result = (T)permanentCache.get(cdmBase.getUuid());
1053
        }
1054
        return result;
109 1055
    }
110 1056

  
111 1057
    //TODO this should be cached for partition
112 1058
    private <T extends CdmBase> T getTarget(T source) {
113
        Class<T> clazz = (Class<T>)source.getClass();
114
        T result = getCommonService().find(clazz, source.getUuid());
1059
        if (source == null){
1060
            return null;
1061
        }
1062
        T result = getCached(source);
1063
//        if (result == null){
1064
//            Class<T> clazz = (Class<T>)source.getClass();
1065
//            result = getCommonService().find(clazz, source.getUuid());
1066
//        }
115 1067
        if (result == null){
116
            //TODO recursive
117
            //TODO session evict
1068
            //Alternative: clone?
118 1069
            result = CdmBase.deproxy(source);
119 1070
            result.setId(0);
1071
            cache(result);
1072
            toSave.add(result);
120 1073
        }
121 1074
        return result;
122 1075
    }
123 1076

  
1077

  
1078

  
1079
    @Override
1080
    protected boolean doCheck(FauEu2CdmImportState state) {
1081
        return false;
1082
    }
1083

  
124 1084
    @Override
125 1085
    protected boolean isIgnore(FauEu2CdmImportState state) {
126 1086
        return false;

Also available in: Unified diff