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
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
30
|
|
31
|
/**
|
32
|
* Import class for synonym relationships in Edaphobase.
|
33
|
*
|
34
|
* @author a.mueller
|
35
|
* @date 21.12.2015
|
36
|
*
|
37
|
*/
|
38
|
@Component
|
39
|
public class EdaphobaseSynonymy2Import extends EdaphobaseImportBase {
|
40
|
private static final long serialVersionUID = 8968205268798472136L;
|
41
|
private static final Logger logger = Logger.getLogger(EdaphobaseSynonymy2Import.class);
|
42
|
|
43
|
private static final String tableName = "tax_synonym";
|
44
|
private static final String pluralString = "related unreal synonyms";
|
45
|
|
46
|
|
47
|
/**
|
48
|
* @param tableName
|
49
|
* @param pluralString
|
50
|
*/
|
51
|
public EdaphobaseSynonymy2Import() {
|
52
|
super(tableName, pluralString);
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
protected String getIdQuery(EdaphobaseImportState state) {
|
57
|
return " SELECT sr.tax_synonym_id "
|
58
|
+ " FROM tax_synonym sr "
|
59
|
+ " INNER JOIN tax_taxon s ON s.taxon_id = sr.a_taxon_fk_taxon_id "
|
60
|
+ " INNER JOIN tax_taxon t ON t.taxon_id = sr.b_taxon_fk_taxon_id "
|
61
|
+ " WHERE NOT (s.valid = false AND t.valid = true) OR sr.synonym_role <> 11614 "
|
62
|
+ " ORDER BY sr.a_taxon_fk_taxon_id ";
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
67
|
String result = "SELECT sr.* , t2.valid t2valid, t3.valid t3valid, t2.taxon_id t2taxon_id, t3.taxon_id t3taxon_id "
|
68
|
|
69
|
+ " FROM tax_taxon s "
|
70
|
+ " INNER JOIN tax_synonym sr ON s.taxon_id = sr.a_taxon_fk_taxon_id "
|
71
|
+ " INNER JOIN tax_taxon t ON t.taxon_id = sr.b_taxon_fk_taxon_id "
|
72
|
+ " LEFT JOIN tax_synonym sr2 ON t.taxon_id = sr2.a_taxon_fk_taxon_id "
|
73
|
+ " LEFT JOIN tax_taxon t2 ON t2.taxon_id = sr2.b_taxon_fk_taxon_id "
|
74
|
+ " LEFT JOIN tax_synonym sr3 ON t2.taxon_id = sr3.a_taxon_fk_taxon_id"
|
75
|
+ " LEFT JOIN tax_taxon t3 ON t3.taxon_id = sr3.b_taxon_fk_taxon_id "
|
76
|
+ " WHERE sr.tax_synonym_id IN (@IDSET)"
|
77
|
+ " ORDER BY sr.tax_synonym_id ";
|
78
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
79
|
return result;
|
80
|
}
|
81
|
|
82
|
|
83
|
@Override
|
84
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
85
|
ResultSet rs = partitioner.getResultSet();
|
86
|
Map<String, TaxonBase> map = partitioner.getObjectMap(TAXON_NAMESPACE);
|
87
|
Reference sourceReference = state.getTransactionalSourceReference();
|
88
|
|
89
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
90
|
try {
|
91
|
while (rs.next()){
|
92
|
int id = rs.getInt("tax_synonym_id");
|
93
|
//parentTaxonFk
|
94
|
// boolean isValid = rs.getBoolean("valid");
|
95
|
Integer synId = nullSafeInt(rs, "a_taxon_fk_taxon_id");
|
96
|
Integer accId = nullSafeInt(rs, "b_taxon_fk_taxon_id");
|
97
|
|
98
|
Integer role = nullSafeInt(rs, "synonym_role");
|
99
|
boolean isSynonym = role.equals(11614);
|
100
|
|
101
|
if (synId == null || accId == null){
|
102
|
logger.warn("Either a_taxon or b_taxon is NULL for tax_synonym " + id);
|
103
|
}else{
|
104
|
TaxonBase<?> synonymCandidate = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(synId), TaxonBase.class);
|
105
|
if (synonymCandidate == null){
|
106
|
logger.warn("Synonym " + synId + " not found for synonymRelations (tax_synonym): " + id);
|
107
|
}else if (isSynonym && synonymCandidate.isInstanceOf(Taxon.class)){
|
108
|
String message = "Synonym ("+synId+") is not synonym but accepted (valid). Can't add synonym for tax_synonym: "+id;
|
109
|
logger.warn(message);
|
110
|
}else if (!isSynonym && !synonymCandidate.isInstanceOf(Taxon.class)){
|
111
|
String message = "From taxon ("+synId+") in included in relation is not of type Taxon but Synonym (invalid). Can't add concept relation for tax_synonym: "+id;
|
112
|
logger.warn(message);
|
113
|
}else if(isSynonym){
|
114
|
handleSynonymToInvalid(state, rs, taxaToSave, synId, accId, synonymCandidate);
|
115
|
}else{
|
116
|
handleConceptRelationship(state, taxaToSave, id, accId, role, synonymCandidate);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
// //id
|
121
|
// String nameSpace = "tax_taxon";
|
122
|
// ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
|
123
|
// ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
|
124
|
|
125
|
|
126
|
}
|
127
|
} catch (SQLException e) {
|
128
|
e.printStackTrace();
|
129
|
}
|
130
|
|
131
|
getTaxonService().saveOrUpdate(taxaToSave);
|
132
|
return true;
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* @param state
|
137
|
* @param taxaToSave
|
138
|
* @param id
|
139
|
* @param accId
|
140
|
* @param role
|
141
|
* @param synonymCandidate
|
142
|
*/
|
143
|
private void handleConceptRelationship(EdaphobaseImportState state, Set<TaxonBase> taxaToSave, int id,
|
144
|
Integer accId, Integer role, TaxonBase<?> fromTaxonBase) {
|
145
|
if (!role.equals(11613)){
|
146
|
String message = "Concept relation is not of type is included in ('11613'). This is not expected here. tax_synonym: " + id;
|
147
|
logger.warn(message);
|
148
|
}else{
|
149
|
Taxon fromTaxon = CdmBase.deproxy(fromTaxonBase, Taxon.class);
|
150
|
TaxonBase<?> toTaxonBase = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(accId), TaxonBase.class);
|
151
|
if (toTaxonBase == null){
|
152
|
logger.warn("Accepted(parent) taxon " + accId + " not found for tax_synonym " + id );
|
153
|
}else if(toTaxonBase.isInstanceOf(Synonym.class)){
|
154
|
String message = "Taxon ("+accId+") is not accepted but synonym. Can't add concept relation for tax_synonym: "+id;
|
155
|
logger.warn(message);
|
156
|
}else{
|
157
|
Taxon toTaxon = CdmBase.deproxy(toTaxonBase, Taxon.class);
|
158
|
TaxonRelationshipType relType = TaxonRelationshipType.INCLUDES();
|
159
|
toTaxon.addTaxonRelation(fromTaxon, relType, null, null);
|
160
|
taxaToSave.add(fromTaxon);
|
161
|
taxaToSave.add(toTaxon);
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* @param state
|
168
|
* @param taxaToSave
|
169
|
* @param synId
|
170
|
* @param accId
|
171
|
* @param synonymCandidate
|
172
|
* @throws SQLException
|
173
|
*/
|
174
|
private void handleSynonymToInvalid(EdaphobaseImportState state, ResultSet rs, Set<TaxonBase> taxaToSave, Integer synId,
|
175
|
Integer accId, TaxonBase<?> synonymCandidate) throws SQLException {
|
176
|
Synonym synonym = CdmBase.deproxy(synonymCandidate, Synonym.class);
|
177
|
if (state.hasAcceptedTaxon(synId)){
|
178
|
//TODO do some further homotypie checking
|
179
|
}else{
|
180
|
String message = "Synonym a("+synId+") of invalid taxon (b) has not accepted taxon yet. Automatically create relationship to valid taxon (c) of invalid taxon (b) ";
|
181
|
logger.warn(message);
|
182
|
Integer acc3 = nullSafeInt(rs, "t3taxon_id");
|
183
|
Boolean valid3 = nullSafeBoolean(rs, "t3valid");
|
184
|
Integer acc2 = nullSafeInt(rs, "t2taxon_id");
|
185
|
Boolean valid2 = nullSafeBoolean(rs, "t2valid");
|
186
|
|
187
|
Integer accC = acc2;
|
188
|
if (valid2 == null || valid2 == false){
|
189
|
accC = acc3;
|
190
|
if (valid3 == null || valid3 == false){
|
191
|
logger.warn("All unvalid not handled: " + synId);
|
192
|
}
|
193
|
}
|
194
|
TaxonBase<?> acceptedC = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(accC), TaxonBase.class);
|
195
|
if (acceptedC == null || acceptedC.isInstanceOf(Synonym.class)){
|
196
|
logger.warn("Taxon c does not exist or is not valid taxon.");
|
197
|
}else{
|
198
|
Taxon taxon = CdmBase.deproxy(acceptedC, Taxon.class);
|
199
|
//TODO
|
200
|
SynonymType synType = SynonymType.SYNONYM_OF();
|
201
|
taxon.addSynonym(synonym, synType);
|
202
|
taxaToSave.add(synonym);
|
203
|
taxaToSave.add(taxon);
|
204
|
}
|
205
|
}
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
210
|
EdaphobaseImportState state) {
|
211
|
String nameSpace;
|
212
|
Class<?> cdmClass;
|
213
|
Set<String> idSet;
|
214
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
215
|
|
216
|
try{
|
217
|
Set<String> taxonIdSet = new HashSet<>();
|
218
|
while (rs.next()){
|
219
|
handleForeignKey(rs, taxonIdSet, "a_taxon_fk_taxon_id");
|
220
|
handleForeignKey(rs, taxonIdSet, "b_taxon_fk_taxon_id");
|
221
|
handleForeignKey(rs, taxonIdSet, "t2taxon_id");
|
222
|
handleForeignKey(rs, taxonIdSet, "t3taxon_id");
|
223
|
}
|
224
|
|
225
|
//name map
|
226
|
nameSpace = TAXON_NAMESPACE;
|
227
|
cdmClass = TaxonBase.class;
|
228
|
idSet = taxonIdSet;
|
229
|
@SuppressWarnings("rawtypes")
|
230
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
231
|
result.put(nameSpace, taxonMap);
|
232
|
|
233
|
} catch (SQLException e) {
|
234
|
throw new RuntimeException(e);
|
235
|
}
|
236
|
|
237
|
return result;
|
238
|
}
|
239
|
|
240
|
|
241
|
@Override
|
242
|
protected boolean doCheck(EdaphobaseImportState state) {
|
243
|
return false;
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
248
|
return ! state.getConfig().isDoSynonyms();
|
249
|
}
|
250
|
|
251
|
}
|