2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.cdm2cdm
;
11 import java
.util
.List
;
13 import java
.util
.UUID
;
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
;
20 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplication
;
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
;
34 public class Cdm2CdmVocabularyImport
35 extends Cdm2CdmImportBase
{
37 private static final long serialVersionUID
= 3995116783196060465L;
38 private static final Logger logger
= LogManager
.getLogger();
41 protected void doInvoke(Cdm2CdmImportState state
) {
42 IProgressMonitor monitor
= state
.getConfig().getProgressMonitor();
46 private void doData(Cdm2CdmImportState state
){
48 ICdmApplication source
= sourceRepo(state
);
50 //gegen existierende Terme abgleichen
51 //fehlende Terme importieren
53 List
<String
> propertyPaths
= null;
54 int totalCount
= getTotalCount();
58 VocabularyFilter vocFilter
= state
.getConfig().getVocabularyFilter();
59 for (UUID vocUuid
: source
.getVocabularyService().uuidList(vocFilter
)){
60 TransactionStatus tx
= startTransaction();
61 doSingleVocabulary(state
, vocUuid
);
62 commitTransaction(tx
);
66 Set
<UUID
> graphUuids
= state
.getConfig().getGraphFilter();
67 for (UUID graphUuid
: graphUuids
){
68 TransactionStatus tx
= startTransaction();
69 doSingleGraph(state
, graphUuid
);
70 commitTransaction(tx
);
74 private int getTotalCount() {
75 // TODO to be implemented
79 private void doSingleVocabulary(Cdm2CdmImportState state
, UUID vocUuid
) {
80 ICdmApplication source
= sourceRepo(state
);
81 TransactionStatus otherTx
= source
.startTransaction(true);
82 TermVocabulary
<DefinedTermBase
> otherVoc
= source
.getVocabularyService().find(vocUuid
);
83 TermVocabulary
<DefinedTermBase
> thisVoc
= null;
85 thisVoc
= detach(otherVoc
, state
);
86 if (thisVoc
!= otherVoc
){ //voc already existed
87 for (DefinedTermBase
<?
> term
: otherVoc
.getTerms()){
88 doSingleTerm(state
, term
, thisVoc
);
91 } catch (Exception e
) {
92 logger
.warn("Exception during detache vocabulary " + otherVoc
.getUuid());
97 source
.commitTransaction(otherTx
);
98 getVocabularyService().saveOrUpdate(thisVoc
);
99 getCommonService().saveOrUpdate(state
.getToSave());
102 } catch (Exception e
) {
103 logger
.warn("Exception during save vocabulary " + otherVoc
.getUuid());
108 private void doSingleTerm(Cdm2CdmImportState state
, DefinedTermBase
<?
> otherTerm
, TermVocabulary
<DefinedTermBase
> thisVoc
) {
109 DefinedTermBase
<?
> thisTerm
= null;
110 if (logger
.isInfoEnabled()){logger
.info(otherTerm
.getTitleCache());}
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
);
119 } catch (Exception e
) {
120 logger
.warn("Exception during detache node " + otherTerm
.getUuid());
125 private void doSingleGraph(Cdm2CdmImportState state
, UUID graphUuid
) {
126 ICdmApplication source
= sourceRepo(state
);
127 TransactionStatus otherTx
= source
.startTransaction(true);
128 TermTree
<DefinedTermBase
> otherGraph
= source
.getTermTreeService().find(graphUuid
);
129 TermTree
<DefinedTermBase
> thisGraph
= null;
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());
137 } catch (Exception e
) {
138 logger
.warn("Exception during detache term graph " + otherGraph
.getUuid());
142 if (thisGraph
!= null){
143 source
.commitTransaction(otherTx
);
144 getTermTreeService().saveOrUpdate(thisGraph
);
145 getCommonService().saveOrUpdate(state
.getToSave());
148 } catch (Exception e
) {
149 logger
.warn("Exception during save vocabulary " + otherGraph
.getUuid());
154 private void doSingleNode(Cdm2CdmImportState state
, TermNode
<DefinedTermBase
> otherNode
, TermNode
<DefinedTermBase
> thisRoot
) {
155 // TermNode<DefinedTermBase> thisTerm = null;
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);
164 // } catch (Exception e) {
165 // logger.warn("Exception during detache node " + otherNode.getUuid());
166 // e.printStackTrace();
171 protected boolean doCheck(Cdm2CdmImportState state
) {
176 protected boolean isIgnore(Cdm2CdmImportState state
) {
177 return !state
.getConfig().isDoVocabularies();