|
1 |
/**
|
|
2 |
* Copyright (C) 2020 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.caryo;
|
|
10 |
|
|
11 |
import java.util.Map;
|
|
12 |
import java.util.UUID;
|
|
13 |
import java.util.stream.Collectors;
|
|
14 |
|
|
15 |
import org.apache.logging.log4j.LogManager;
|
|
16 |
import org.apache.logging.log4j.Logger;
|
|
17 |
import org.springframework.stereotype.Component;
|
|
18 |
|
|
19 |
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
|
|
20 |
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
|
21 |
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
|
|
22 |
import eu.etaxonomy.cdm.model.name.TaxonName;
|
|
23 |
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
|
24 |
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
|
25 |
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
|
26 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
|
27 |
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
|
28 |
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* @author a.mueller
|
|
32 |
* @since 02.02.2023
|
|
33 |
*/
|
|
34 |
@Component
|
|
35 |
public class CaryoSileneaeSynonymSynonymCleanupImport extends CaryoSileneaeImportBase {
|
|
36 |
|
|
37 |
private static final long serialVersionUID = -3721982716275962061L;
|
|
38 |
private static final Logger logger = LogManager.getLogger();
|
|
39 |
|
|
40 |
private static final UUID uuidSileneaeInfoNameIdType = UUID.fromString("95ecbf6d-521d-447f-bae5-d82585ff3617");
|
|
41 |
|
|
42 |
private static final String NOMTAX_ID = "NomTax_ID";
|
|
43 |
private static final String NOMEN_LINK = "Nomen_link";
|
|
44 |
private static final String TAXON_LINK = "Taxon_link";
|
|
45 |
|
|
46 |
@Override
|
|
47 |
protected String getWorksheetName(CaryoSileneaeImportConfigurator config) {
|
|
48 |
return "Synonyms";
|
|
49 |
}
|
|
50 |
|
|
51 |
@Override
|
|
52 |
protected void firstPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
|
53 |
int line = state.getCurrentLine();
|
|
54 |
if ((line % 500) == 0){
|
|
55 |
// newTransaction(state);
|
|
56 |
System.out.println(line);
|
|
57 |
}
|
|
58 |
|
|
59 |
Map<String, String> record = state.getOriginalRecord();
|
|
60 |
|
|
61 |
Integer nomTaxId = Integer.valueOf(getValue(record, NOMTAX_ID));
|
|
62 |
Integer nameLinkID = Integer.valueOf(getValue(record, NOMEN_LINK));
|
|
63 |
Integer taxonLinkId = Integer.valueOf(getValue(record, TAXON_LINK));
|
|
64 |
|
|
65 |
|
|
66 |
String row = String.valueOf(line) + "("+nomTaxId+"): ";
|
|
67 |
|
|
68 |
//find synonym name
|
|
69 |
TaxonName synonymName = getNameFromDb(state, nameLinkID, row);
|
|
70 |
if (synonymName == null) {
|
|
71 |
logger.warn(row + "Name does not exist");
|
|
72 |
return;
|
|
73 |
}
|
|
74 |
|
|
75 |
Integer accNameId = accIdMap.get(taxonLinkId);
|
|
76 |
if (accNameId == null) {
|
|
77 |
logger.warn(row + "Taxon not found in taxon map: " + taxonLinkId);
|
|
78 |
return;
|
|
79 |
}
|
|
80 |
TaxonName accName = getNameFromDb(state, accNameId, row);
|
|
81 |
if (accName == null) {
|
|
82 |
logger.warn(row + "Accepeted taxon name for synonym " + synonymName.getTitleCache() + " not found. Keep as unresolved.");
|
|
83 |
return;
|
|
84 |
}
|
|
85 |
|
|
86 |
Taxon newAccTaxon;
|
|
87 |
//add as synonym
|
|
88 |
if (accName.getTaxa().size() != 1) {
|
|
89 |
if (accName.getTaxa().isEmpty()){
|
|
90 |
logger.warn(row + "Accepted taxon name "+accName.getTitleCache()+" has no taxon attached. Keep synonynm " + synonymName.getTitleCache() + "as unresolved");
|
|
91 |
return;
|
|
92 |
}else {
|
|
93 |
String names = accName.getTaxa().stream().map(t->t.getTitleCache()).collect(Collectors.toList()).toString();
|
|
94 |
logger.warn(row + "Acc taxon name has "+accName.getTaxa().size()+" taxa attached: n=" + accName.getTaxa().size() + " (" + names +")" );
|
|
95 |
newAccTaxon = accName.getTaxa().iterator().next();
|
|
96 |
logger.warn(row + " used: " + newAccTaxon.getTitleCache());
|
|
97 |
}
|
|
98 |
}else {
|
|
99 |
newAccTaxon = accName.getTaxa().iterator().next();
|
|
100 |
}
|
|
101 |
|
|
102 |
Synonym synonym = Synonym.NewInstance(synonymName, getSecRef(state));
|
|
103 |
newAccTaxon.addSynonym(synonym, SynonymType.SYNONYM_OF);
|
|
104 |
|
|
105 |
getTaxonService().saveOrUpdate(newAccTaxon);
|
|
106 |
|
|
107 |
|
|
108 |
//remove from parent
|
|
109 |
if (synonymName.getTaxa().size() != 1) {
|
|
110 |
logger.warn(row + "syn taxon name "+ synonymName.getTitleCache() + " has "+synonymName.getTaxa().size()+" taxa attached");
|
|
111 |
return;
|
|
112 |
}
|
|
113 |
Taxon accTaxon = synonymName.getTaxa().iterator().next();
|
|
114 |
if (accTaxon.getTaxonNodes().size() != 1) {
|
|
115 |
logger.warn(row + "syn taxon has "+accTaxon.getTaxonNodes().size()+" taxon nodes attached");
|
|
116 |
return;
|
|
117 |
}
|
|
118 |
TaxonNode node = accTaxon.getTaxonNodes().iterator().next();
|
|
119 |
|
|
120 |
//boolean success = node.getParent().deleteChildNode(node);
|
|
121 |
node.setTaxon(null);
|
|
122 |
// if (!success) {
|
|
123 |
// logger.warn(row + "deleteChildNode not successful");
|
|
124 |
// }
|
|
125 |
|
|
126 |
accTaxon.setName(null);
|
|
127 |
getTaxonService().delete(accTaxon);
|
|
128 |
// getTaxonNodeService().delete(node);
|
|
129 |
}
|
|
130 |
|
|
131 |
|
|
132 |
private TaxonName getNameFromDb(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state, Integer nameId, String row) {
|
|
133 |
try {
|
|
134 |
DefinedTerm sileneaeInfoNameIdType = getIdentiferType(state,
|
|
135 |
uuidSileneaeInfoNameIdType, null, null, null, null);
|
|
136 |
Pager<IdentifiedEntityDTO<TaxonName>> list = getNameService().findByIdentifier(TaxonName.class, nameId.toString(),
|
|
137 |
sileneaeInfoNameIdType, MatchMode.EXACT, true, null, null, null);
|
|
138 |
return list.getRecords().get(0).getCdmEntity().getEntity();
|
|
139 |
} catch (Exception e) {
|
|
140 |
logger.warn(row + "Error when getting name from DB. nameID = " + nameId);
|
|
141 |
e.printStackTrace();
|
|
142 |
return null;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
boolean first = true;
|
|
147 |
@Override
|
|
148 |
protected void secondPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
|
149 |
// if (first) {
|
|
150 |
// for (TaxonName taxonName : orphanedNameMap.values()) {
|
|
151 |
// Taxon taxon = Taxon.NewInstance(taxonName, getSecRef(state));
|
|
152 |
// TaxonNode node = getUnresolvedNode(state).addChildTaxon(taxon, null);
|
|
153 |
// getTaxonNodeService().saveOrUpdate(node);
|
|
154 |
// }
|
|
155 |
// first = false;
|
|
156 |
// }
|
|
157 |
}
|
|
158 |
//
|
|
159 |
// private TaxonNode unresolvedNode;
|
|
160 |
// private TaxonNode getUnresolvedNode(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
|
|
161 |
// if (unresolvedNode == null) {
|
|
162 |
// unresolvedNode = getTaxonNodeService().find(state.getConfig().getUnresolvedNodeUuid());
|
|
163 |
// }
|
|
164 |
// return unresolvedNode;
|
|
165 |
// }
|
|
166 |
|
|
167 |
}
|
ref #10242 synonym cleanup for Sileneae