Project

General

Profile

Download (6.37 KB) Statistics
| Branch: | Tag: | 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.cdm2cdm;
10

    
11
import java.util.List;
12
import java.util.Set;
13
import java.util.UUID;
14

    
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.api.application.ICdmRepository;
21
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
22
import eu.etaxonomy.cdm.filter.VocabularyFilter;
23
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
24
import eu.etaxonomy.cdm.model.term.TermNode;
25
import eu.etaxonomy.cdm.model.term.TermTree;
26
import eu.etaxonomy.cdm.model.term.TermVocabulary;
27

    
28
/**
29
 * @author a.mueller
30
 * @since 17.09.2021
31
 * IN WORK
32
 */
33
@Component
34
public class Cdm2CdmVocabularyImport
35
        extends Cdm2CdmImportBase {
36

    
37
    private static final long serialVersionUID = 3995116783196060465L;
38
    private static final Logger logger = LogManager.getLogger();
39

    
40
    @Override
41
    protected void doInvoke(Cdm2CdmImportState state) {
42
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
43
        doData(state);
44
    }
45

    
46
    private void doData(Cdm2CdmImportState state){
47

    
48
        //term uuids laden
49
        //gegen existierende Terme abgleichen
50
        //fehlende Terme importieren
51

    
52
        List<String> propertyPaths = null;
53
        int totalCount = getTotalCount();
54
        int count = 0;
55

    
56
        //vocabularies
57
        VocabularyFilter vocFilter =
58
                state.getConfig().getVocabularyFilter();
59
        for (UUID vocUuid : getVocabularyService().uuidList(vocFilter)){
60
            TransactionStatus tx = startTransaction();
61
            doSingleVocabulary(state, vocUuid);
62
            commitTransaction(tx);
63
        }
64

    
65
        //graphs
66
        Set<UUID> graphUuids = state.getConfig().getGraphFilter();
67
        for (UUID graphUuid : graphUuids){
68
            TransactionStatus tx = startTransaction();
69
            doSingleGraph(state, graphUuid);
70
            commitTransaction(tx);
71
        }
72
    }
73

    
74
    private int getTotalCount() {
75
        // TODO to be implemented
76
        return 100;
77
    }
78

    
79
    private void doSingleVocabulary(Cdm2CdmImportState state, UUID vocUuid) {
80
        ICdmRepository source = sourceRepo(state);
81
        TransactionStatus otherTx = source.startTransaction(true);
82
        TermVocabulary<DefinedTermBase> otherVoc = source.getVocabularyService().find(vocUuid);
83
        TermVocabulary<DefinedTermBase> thisVoc = null;
84
        try {
85
            thisVoc = detach(otherVoc, state);
86
            if (thisVoc != otherVoc){ //voc already existed
87
                for (DefinedTermBase<?> term: otherVoc.getTerms()){
88
                    doSingleTerm(state, term, thisVoc);
89
                }
90
            }
91
        } catch (Exception e) {
92
            logger.warn("Exception during detache vocabulary " + otherVoc.getUuid());
93
            e.printStackTrace();
94
        }
95
        try {
96
            if (thisVoc != null){
97
                source.commitTransaction(otherTx);
98
                getVocabularyService().saveOrUpdate(thisVoc);
99
                getCommonService().saveOrUpdate(state.getToSave());
100
                state.clearToSave();
101
            }
102
        } catch (Exception e) {
103
            logger.warn("Exception during save vocabulary " + otherVoc.getUuid());
104
             e.printStackTrace();
105
        }
106
    }
107

    
108
    private void doSingleTerm(Cdm2CdmImportState state, DefinedTermBase<?> otherTerm, TermVocabulary<DefinedTermBase> thisVoc) {
109
        DefinedTermBase<?> thisTerm = null;
110
        if (logger.isInfoEnabled()){logger.info(otherTerm.getTitleCache());}
111
        try {
112
            if (!thisVoc.getTerms().contains(otherTerm)){
113
                thisTerm = detach(otherTerm, state);
114
//                if(thisTerm == otherTerm){ //term does not yet exist
115
                thisVoc.addTerm(thisTerm);
116
                state.addToSave(thisTerm);
117
//                }
118
            }
119
        } catch (Exception e) {
120
            logger.warn("Exception during detache node " + otherTerm.getUuid());
121
            e.printStackTrace();
122
        }
123
    }
124

    
125
    private void doSingleGraph(Cdm2CdmImportState state, UUID graphUuid) {
126
        ICdmRepository source = sourceRepo(state);
127
        TransactionStatus otherTx = source.startTransaction(true);
128
        TermTree<DefinedTermBase> otherGraph = source.getTermTreeService().find(graphUuid);
129
        TermTree<DefinedTermBase> thisGraph = null;
130
        try {
131
            thisGraph = detach(otherGraph, state);
132
            if (thisGraph != otherGraph){ //voc already existed
133
                for (TermNode<DefinedTermBase> node: otherGraph.getRootChildren()){
134
                    doSingleNode(state, node, thisGraph.getRoot());
135
                }
136
            }
137
        } catch (Exception e) {
138
            logger.warn("Exception during detache term graph " + otherGraph.getUuid());
139
            e.printStackTrace();
140
        }
141
        try {
142
            if (thisGraph != null){
143
                source.commitTransaction(otherTx);
144
                getTermTreeService().saveOrUpdate(thisGraph);
145
                getCommonService().saveOrUpdate(state.getToSave());
146
                state.clearToSave();
147
            }
148
        } catch (Exception e) {
149
            logger.warn("Exception during save vocabulary " + otherGraph.getUuid());
150
             e.printStackTrace();
151
        }
152
    }
153

    
154
    private void doSingleNode(Cdm2CdmImportState state, TermNode<DefinedTermBase> otherNode, TermNode<DefinedTermBase> thisRoot) {
155
//        TermNode<DefinedTermBase> thisTerm = null;
156
////        try {
157
////            if (!thisRoot.getChilcontains(otherNode)){
158
////                thisTerm = detache(otherNode, state);
159
//////                if(thisTerm == otherTerm){ //term does not yet exist
160
////                thisGraph.addTerm(thisTerm);
161
////                state.addToSave(thisTerm);
162
//////                }
163
////            }
164
//        } catch (Exception e) {
165
//            logger.warn("Exception during detache node " + otherNode.getUuid());
166
//            e.printStackTrace();
167
//        }
168
    }
169

    
170
    @Override
171
    protected boolean doCheck(Cdm2CdmImportState state) {
172
        return true;
173
    }
174

    
175
    @Override
176
    protected boolean isIgnore(Cdm2CdmImportState state) {
177
        return !state.getConfig().isDoVocabularies();
178
    }
179
}
(6-6/7)