Project

General

Profile

Download (4.31 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2019 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.pesi.fauEu2Cdm;
10

    
11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.List;
14

    
15
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
16
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
17
import eu.etaxonomy.cdm.io.common.XmlExportState;
18
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
19
import eu.etaxonomy.cdm.model.common.Annotation;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.Marker;
22
import eu.etaxonomy.cdm.model.common.VersionableEntity;
23
import eu.etaxonomy.cdm.model.taxon.Classification;
24
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
25

    
26
/**
27
 * @author a.mueller
28
 * @since 17.08.2019
29
 */
30
public class FauEu2CdmImport
31
        extends FauEu2CdmImportBase {
32

    
33
    private static final long serialVersionUID = -2111102574346601573L;
34

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

    
40
    @Override
41
    protected void doInvoke(FauEu2CdmImportState state) {
42
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
43
        FauEu2CdmImportConfigurator config = state.getConfig();
44
        if (config.getTaxonNodeFilter().getClassificationFilter() != null
45
                && !config.getTaxonNodeFilter().getClassificationFilter().isEmpty()) {
46
            Classification classification = getClassificationService()
47
                    .load(config.getTaxonNodeFilter().getClassificationFilter().get(0).getUuid());
48
            state.setRootId(classification.getRootNode().getUuid());
49

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

    
58
        monitor.subTask("Start partitioning");
59

    
60
        TaxonNode node = partitioner.next();
61
        while (node != null) {
62
            handleTaxonNode(state, node);
63
            node = partitioner.next();
64
        }
65
    }
66

    
67
    /**
68
     * @param state
69
     * @param node
70
     */
71
    private void handleTaxonNode(FauEu2CdmImportState state, TaxonNode node) {
72
//        Integer id = state.getTarget(createdBy.getUuid(), createdBy.getClass());
73
        handleAnnotatableEntity(node);
74

    
75

    
76
    }
77

    
78
    private void handleAnnotatableEntity(AnnotatableEntity entity) {
79
        handleVersionableEntity(entity);
80
        for (Annotation a : getTargetCollection(entity.getAnnotations())){
81
            entity.addAnnotation(a);
82
        }
83
        for (Marker a : getTargetCollection(entity.getMarkers())){
84
            entity.addMarker(a);
85
        }
86
//        entity.setUpdatedBy(getTarget(entity.getUpdatedBy()));
87
    }
88

    
89
    /**
90
     * @param annotations
91
     * @return
92
     */
93
    private <T extends Collection<S>, S extends CdmBase> List<S> getTargetCollection(T sourceCollection) {
94
        List<S> result = new ArrayList<>();
95
        for (S entity : sourceCollection){
96
            S target = getTarget(entity);
97
            result.add(target);
98
        }
99
        return result;
100
    }
101

    
102
    private void handleVersionableEntity(VersionableEntity entity) {
103
        handleCdmBase(entity);
104
        entity.setUpdatedBy(getTarget(entity.getUpdatedBy()));
105
    }
106

    
107
    private void handleCdmBase(CdmBase cdmBase) {
108
        cdmBase.setCreatedBy(getTarget(cdmBase.getCreatedBy()));
109
    }
110

    
111
    //TODO this should be cached for partition
112
    private <T extends CdmBase> T getTarget(T source) {
113
        Class<T> clazz = (Class<T>)source.getClass();
114
        T result = getCommonService().find(clazz, source.getUuid());
115
        if (result == null){
116
            //TODO recursive
117
            //TODO session evict
118
            result = CdmBase.deproxy(source);
119
            result.setId(0);
120
        }
121
        return result;
122
    }
123

    
124
    @Override
125
    protected boolean isIgnore(FauEu2CdmImportState state) {
126
        return false;
127
    }
128

    
129
}
(1-1/4)