1
|
/**
|
2
|
* Copyright (C) 2015 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.edaphobase;
|
10
|
|
11
|
import java.sql.ResultSet;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.HashSet;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
|
22
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
25
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
26
|
import eu.etaxonomy.cdm.model.taxon.SynonymType;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
29
|
|
30
|
/**
|
31
|
* Import class for synonym relationships in Edaphobase.
|
32
|
*
|
33
|
* @author a.mueller
|
34
|
* @since 21.12.2015
|
35
|
*
|
36
|
*/
|
37
|
@Component
|
38
|
public class EdaphobaseSynonymyImport extends EdaphobaseImportBase {
|
39
|
private static final long serialVersionUID = 6641343927320994726L;
|
40
|
|
41
|
private static final Logger logger = Logger.getLogger(EdaphobaseSynonymyImport.class);
|
42
|
private static final String tableName = "tax_synonym";
|
43
|
private static final String pluralString = "related synonyms";
|
44
|
|
45
|
|
46
|
/**
|
47
|
* @param tableName
|
48
|
* @param pluralString
|
49
|
*/
|
50
|
public EdaphobaseSynonymyImport() {
|
51
|
super(tableName, pluralString);
|
52
|
}
|
53
|
|
54
|
@Override
|
55
|
protected String getIdQuery(EdaphobaseImportState state) {
|
56
|
return " SELECT sr.tax_synonym_id "
|
57
|
+ " FROM tax_synonym sr "
|
58
|
+ " INNER JOIN tax_taxon s ON s.taxon_id = sr.a_taxon_fk_taxon_id "
|
59
|
+ " INNER JOIN tax_taxon t ON t.taxon_id = sr.b_taxon_fk_taxon_id "
|
60
|
+ " WHERE s.valid = false AND t.valid = true AND sr.synonym_role = 11614 "
|
61
|
+ " ORDER BY sr.a_taxon_fk_taxon_id ";
|
62
|
}
|
63
|
|
64
|
@Override
|
65
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
66
|
String result = "SELECT sr.* "
|
67
|
+ " FROM tax_synonym sr"
|
68
|
+ " WHERE tax_synonym_id IN (@IDSET)";
|
69
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
70
|
return result;
|
71
|
}
|
72
|
|
73
|
|
74
|
@Override
|
75
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
76
|
ResultSet rs = partitioner.getResultSet();
|
77
|
Reference sourceReference = state.getTransactionalSourceReference();
|
78
|
|
79
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
80
|
try {
|
81
|
while (rs.next()){
|
82
|
int id = rs.getInt("tax_synonym_id");
|
83
|
//parentTaxonFk
|
84
|
// boolean isValid = rs.getBoolean("valid");
|
85
|
Integer synId = nullSafeInt(rs, "a_taxon_fk_taxon_id");
|
86
|
Integer accId = nullSafeInt(rs, "b_taxon_fk_taxon_id");
|
87
|
|
88
|
|
89
|
if (synId == null || accId == null){
|
90
|
logger.warn("Either a_taxon or b_taxon is NULL for tax_synonym " + id);
|
91
|
}else{
|
92
|
TaxonBase<?> synonymCandidate = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(synId), TaxonBase.class);
|
93
|
if (synonymCandidate == null){
|
94
|
logger.warn("Synonym " + synId + " not found for synonymRelations (tax_synonym): " + id);
|
95
|
}else if (synonymCandidate.isInstanceOf(Taxon.class)){
|
96
|
String message = "Synonym ("+synId+") is not synonym but accepted (valid). Can't add synonym for tax_synonym: "+id;
|
97
|
logger.warn(message);
|
98
|
}else{
|
99
|
Synonym synonym = CdmBase.deproxy(synonymCandidate, Synonym.class);
|
100
|
TaxonBase<?> accepted = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(accId), TaxonBase.class);
|
101
|
if (accepted == null){
|
102
|
logger.warn("Accepted(parent) taxon " + accId + " not found for tax_synonym " + id );
|
103
|
}else if(accepted.isInstanceOf(Synonym.class)){
|
104
|
String message = "Taxon ("+accId+") is not accepted but synonym. Can't add synonym for tax_synonym: "+id;
|
105
|
logger.warn(message);
|
106
|
}else{
|
107
|
Taxon taxon = CdmBase.deproxy(accepted, Taxon.class);
|
108
|
if (synonym.getAcceptedTaxon()!= null){
|
109
|
String message = "Synonym ("+synId+") already has an accepted taxon. Have to clone synonym. RelId: " + id;
|
110
|
logger.warn(message);
|
111
|
synonym = (Synonym)synonym.clone();
|
112
|
}
|
113
|
taxon.addSynonym(synonym, SynonymType.SYNONYM_OF());
|
114
|
state.addSynonymWithAcceptedTaxon(synId);
|
115
|
taxaToSave.add(synonym);
|
116
|
taxaToSave.add(taxon);
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
|
121
|
// //id
|
122
|
// String nameSpace = "tax_taxon";
|
123
|
// ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
|
124
|
// ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
|
125
|
|
126
|
|
127
|
}
|
128
|
} catch (SQLException e) {
|
129
|
e.printStackTrace();
|
130
|
}
|
131
|
|
132
|
getTaxonService().saveOrUpdate(taxaToSave);
|
133
|
return true;
|
134
|
}
|
135
|
|
136
|
@Override
|
137
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
138
|
EdaphobaseImportState state) {
|
139
|
String nameSpace;
|
140
|
Class<?> cdmClass;
|
141
|
Set<String> idSet;
|
142
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
143
|
|
144
|
try{
|
145
|
Set<String> taxonIdSet = new HashSet<String>();
|
146
|
while (rs.next()){
|
147
|
handleForeignKey(rs, taxonIdSet, "a_taxon_fk_taxon_id");
|
148
|
handleForeignKey(rs, taxonIdSet, "b_taxon_fk_taxon_id");
|
149
|
}
|
150
|
|
151
|
//name map
|
152
|
nameSpace = TAXON_NAMESPACE;
|
153
|
cdmClass = TaxonBase.class;
|
154
|
idSet = taxonIdSet;
|
155
|
@SuppressWarnings("rawtypes")
|
156
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
157
|
result.put(nameSpace, taxonMap);
|
158
|
|
159
|
} catch (SQLException e) {
|
160
|
throw new RuntimeException(e);
|
161
|
}
|
162
|
|
163
|
return result;
|
164
|
}
|
165
|
|
166
|
|
167
|
@Override
|
168
|
protected boolean doCheck(EdaphobaseImportState state) {
|
169
|
return true;
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
174
|
return ! state.getConfig().isDoSynonyms();
|
175
|
}
|
176
|
|
177
|
}
|