Project

General

Profile

Download (6.27 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.model.term.DefinedTermBase;
23
import eu.etaxonomy.cdm.model.term.TermNode;
24
import eu.etaxonomy.cdm.model.term.TermTree;
25
import eu.etaxonomy.cdm.model.term.TermVocabulary;
26

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

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

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

    
45
    private void doData(Cdm2CdmImportState state){
46

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

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

    
55
        //vocabularies
56
        Set<UUID> vocUuids = state.getConfig().getVocabularyFilter();
57
        for (UUID vocUuid : vocUuids){
58
            TransactionStatus tx = startTransaction();
59
            doSingleVocabulary(state, vocUuid);
60
            commitTransaction(tx);
61
        }
62

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

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

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

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

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

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

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

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