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
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.io.common.IPartitionedIO;
|
23
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
24
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
25
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
26
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
27
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @date 21.12.2015
|
34
|
*
|
35
|
*/
|
36
|
@Component
|
37
|
public class EdaphobaseClassificationImport extends EdaphobaseImportBase {
|
38
|
private static final long serialVersionUID = -9138378836474086070L;
|
39
|
|
40
|
private static final Logger logger = Logger.getLogger(EdaphobaseClassificationImport.class);
|
41
|
|
42
|
private static final String tableName = "tax_taxon";
|
43
|
|
44
|
private static final String pluralString = "taxon relationships";
|
45
|
|
46
|
|
47
|
/**
|
48
|
* @param tableName
|
49
|
* @param pluralString
|
50
|
*/
|
51
|
public EdaphobaseClassificationImport() {
|
52
|
super(tableName, pluralString);
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
protected String getIdQuery(EdaphobaseImportState state) {
|
57
|
return "SELECT taxon_id FROM "
|
58
|
+ " (SELECT DISTINCT taxon_id, length(path_to_root) FROM tax_taxon t "
|
59
|
+ " ORDER BY length(path_to_root), taxon_id) as drvTbl ";
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
64
|
String result = "SELECT DISTINCT t.* "
|
65
|
+ " FROM tax_taxon t"
|
66
|
+ " WHERE taxon_id IN (@IDSET)";
|
67
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
68
|
return result;
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
protected void doInvoke(EdaphobaseImportState state) {
|
73
|
makeClassification(state);
|
74
|
super.doInvoke(state);
|
75
|
}
|
76
|
|
77
|
|
78
|
@Override
|
79
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
80
|
ResultSet rs = partitioner.getResultSet();
|
81
|
Map<String, Classification> map = partitioner.getObjectMap(CLASSIFICATION_NAMESPACE);
|
82
|
Classification classification = map.get(state.getConfig().getClassificationUuid().toString());
|
83
|
|
84
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
85
|
try {
|
86
|
while (rs.next()){
|
87
|
handleSingleRecord(state, rs, classification, taxaToSave);
|
88
|
}
|
89
|
} catch (SQLException e) {
|
90
|
e.printStackTrace();
|
91
|
}
|
92
|
|
93
|
getTaxonService().saveOrUpdate(taxaToSave);
|
94
|
return true;
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* @param state
|
99
|
* @param rs
|
100
|
* @param taxaToSave
|
101
|
* @param classification
|
102
|
* @throws SQLException
|
103
|
*/
|
104
|
private void handleSingleRecord(EdaphobaseImportState state, ResultSet rs, Classification classification, Set<TaxonBase> taxaToSave) throws SQLException {
|
105
|
Reference sourceReference = state.getTransactionalSourceReference();
|
106
|
|
107
|
int id = rs.getInt("taxon_id");
|
108
|
boolean isDeleted = rs.getBoolean("deleted");
|
109
|
if (isDeleted){
|
110
|
logger.warn("Deleted not handled according to mail Stephan 2018-03-07. ID: " + id );
|
111
|
return;
|
112
|
}
|
113
|
|
114
|
//parentTaxonFk
|
115
|
boolean isValid = rs.getBoolean("valid");
|
116
|
// boolean idDeleted = rs.getBoolean("deleted");
|
117
|
// String treeIndex = rs.getString("path_to_root");
|
118
|
// Integer rankFk = rs.getInt("tax_rank_fk");
|
119
|
// String officialRemark = rs.getString("official_remark");
|
120
|
// boolean isGroup = rs.getBoolean("taxonomic_group");
|
121
|
Integer parentTaxonFk = nullSafeInt(rs, "parent_taxon_fk");
|
122
|
|
123
|
if (parentTaxonFk != null){
|
124
|
TaxonBase<?> parent = state.getRelatedObject(TAXON_NAMESPACE, parentTaxonFk.toString(), TaxonBase.class);
|
125
|
if (parent == null){
|
126
|
logger.warn("Parent taxon " + parentTaxonFk + " not found for taxon " + id );
|
127
|
}else{
|
128
|
|
129
|
TaxonBase<?> child = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(id), TaxonBase.class);
|
130
|
|
131
|
if (isValid){
|
132
|
if (parent.isInstanceOf(Synonym.class)){
|
133
|
logger.warn("Parent taxon (" + parentTaxonFk + " is not valid for valid child " + id + ")");
|
134
|
}else{
|
135
|
Taxon accParent = CdmBase.deproxy(parent, Taxon.class);
|
136
|
if (child == null){
|
137
|
logger.warn("Child not found. ID= " + id);
|
138
|
}
|
139
|
classification.addParentChild(accParent, (Taxon)child, sourceReference, null);
|
140
|
taxaToSave.add(accParent);
|
141
|
}
|
142
|
}else{
|
143
|
// Synonym synonym = CdmBase.deproxy(child, Synonym.class);
|
144
|
// if (synonym == null){
|
145
|
// logger.warn("Synonym " + id + " not found for taxon ");
|
146
|
// }
|
147
|
// if(parent.isInstanceOf(Synonym.class)){
|
148
|
// String message = "Taxon ("+parentTaxonFk+") is not accepted but synonym. Can't add synonym ("+id+")";
|
149
|
// logger.warn(message);
|
150
|
// }else{
|
151
|
// Taxon accepted = CdmBase.deproxy(parent, Taxon.class);
|
152
|
//// accepted.addSynonym(synonym, SynonymType.SYNONYM_OF());
|
153
|
// taxaToSave.add(accepted);
|
154
|
// }
|
155
|
}
|
156
|
}
|
157
|
}
|
158
|
|
159
|
// //id
|
160
|
// String nameSpace = "tax_taxon";
|
161
|
// ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
|
162
|
// ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
|
163
|
|
164
|
|
165
|
|
166
|
}
|
167
|
|
168
|
// /**
|
169
|
// * @param childName
|
170
|
// * @param parentName
|
171
|
// */
|
172
|
// private void handleMissingNameParts(INonViralName childName, INonViralName parentName) {
|
173
|
// if (childName.getGenusOrUninomial())
|
174
|
// }
|
175
|
|
176
|
@Override
|
177
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
178
|
EdaphobaseImportState state) {
|
179
|
String nameSpace;
|
180
|
Class<?> cdmClass;
|
181
|
Set<String> idSet;
|
182
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
183
|
|
184
|
try{
|
185
|
Set<String> taxonIdSet = new HashSet<String>();
|
186
|
while (rs.next()){
|
187
|
handleForeignKey(rs, taxonIdSet, "taxon_id");
|
188
|
handleForeignKey(rs, taxonIdSet, "parent_taxon_fk");
|
189
|
}
|
190
|
|
191
|
//name map
|
192
|
nameSpace = TAXON_NAMESPACE;
|
193
|
cdmClass = TaxonBase.class;
|
194
|
idSet = taxonIdSet;
|
195
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
196
|
result.put(nameSpace, taxonMap);
|
197
|
|
198
|
//Classification
|
199
|
Map<String, Classification> classificationMap = new HashMap<>();
|
200
|
UUID classificationUuid = state.getConfig().getClassificationUuid();
|
201
|
Classification classification = getClassificationService().find(state.getConfig().getClassificationUuid());
|
202
|
classificationMap.put(classificationUuid.toString(), classification);
|
203
|
result.put(CLASSIFICATION_NAMESPACE, classificationMap);
|
204
|
|
205
|
} catch (SQLException e) {
|
206
|
throw new RuntimeException(e);
|
207
|
}
|
208
|
|
209
|
return result;
|
210
|
}
|
211
|
|
212
|
|
213
|
/**
|
214
|
* @param state
|
215
|
*/
|
216
|
private void makeClassification(EdaphobaseImportState state) {
|
217
|
Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
|
218
|
classification.setUuid(state.getConfig().getClassificationUuid());
|
219
|
getClassificationService().save(classification);
|
220
|
}
|
221
|
|
222
|
|
223
|
@Override
|
224
|
protected boolean doCheck(EdaphobaseImportState state) {
|
225
|
return true;
|
226
|
}
|
227
|
|
228
|
@Override
|
229
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
230
|
return ! state.getConfig().isDoTaxa();
|
231
|
}
|
232
|
|
233
|
}
|