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
|
* @author a.mueller
|
32
|
* @date 21.12.2015
|
33
|
*
|
34
|
*/
|
35
|
@Component
|
36
|
public class EdaphobaseSynonymyImport extends EdaphobaseImportBase {
|
37
|
private static final long serialVersionUID = 6641343927320994726L;
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(EdaphobaseSynonymyImport.class);
|
40
|
|
41
|
private static final String tableName = "tax_synonym";
|
42
|
|
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 tax_synonym_id "
|
57
|
+ " FROM tax_synonym "
|
58
|
+ " ORDER BY tax_synonym.a_taxon_fk_taxon_id ";
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
protected String getRecordQuery(EdaphobaseImportConfigurator config) {
|
63
|
String result = "SELECT sr.* "
|
64
|
+ " FROM tax_synonym sr"
|
65
|
+ " WHERE tax_synonym_id IN (@IDSET)";
|
66
|
result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
|
67
|
return result;
|
68
|
}
|
69
|
|
70
|
|
71
|
@Override
|
72
|
public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
|
73
|
ResultSet rs = partitioner.getResultSet();
|
74
|
Map<String, TaxonBase> map = partitioner.getObjectMap(TAXON_NAMESPACE);
|
75
|
Reference sourceReference = state.getTransactionalSourceReference();
|
76
|
|
77
|
Set<TaxonBase> taxaToSave = new HashSet<>();
|
78
|
try {
|
79
|
while (rs.next()){
|
80
|
int id = rs.getInt("tax_synonym_id");
|
81
|
//parentTaxonFk
|
82
|
// boolean isValid = rs.getBoolean("valid");
|
83
|
Integer synId = nullSafeInt(rs, "a_taxon_fk_taxon_id");
|
84
|
Integer accId = nullSafeInt(rs, "b_taxon_fk_taxon_id");
|
85
|
|
86
|
|
87
|
if (synId == null || accId == null){
|
88
|
logger.warn("Either a_taxon or b_taxon is NULL for tax_synonym " + id);
|
89
|
}else{
|
90
|
TaxonBase<?> synonymCandidate = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(synId), TaxonBase.class);
|
91
|
if (synonymCandidate == null){
|
92
|
logger.warn("Synonym " + synId + " not found for synonymRelations (tax_synonym): " + id);
|
93
|
}else if (synonymCandidate.isInstanceOf(Taxon.class)){
|
94
|
String message = "Synonym ("+synId+") is not synonym but accepted (valid). Can't add synonym for tax_synonym: "+id;
|
95
|
logger.warn(message);
|
96
|
}else{
|
97
|
Synonym synonym = CdmBase.deproxy(synonymCandidate, Synonym.class);
|
98
|
TaxonBase<?> accepted = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(accId), TaxonBase.class);
|
99
|
if (accepted == null){
|
100
|
logger.warn("Accepted(parent) taxon " + accId + " not found for tax_synonym " + id );
|
101
|
}else if(accepted.isInstanceOf(Synonym.class)){
|
102
|
String message = "Taxon ("+accId+") is not accepted but synonym. Can't add synonym for tax_synonym: "+id;
|
103
|
logger.warn(message);
|
104
|
}else{
|
105
|
Taxon taxon = CdmBase.deproxy(accepted, Taxon.class);
|
106
|
taxon.addSynonym(synonym, SynonymType.SYNONYM_OF());
|
107
|
taxaToSave.add(synonym);
|
108
|
taxaToSave.add(taxon);
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
// //id
|
114
|
// String nameSpace = "tax_taxon";
|
115
|
// ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
|
116
|
// ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
|
117
|
|
118
|
|
119
|
}
|
120
|
} catch (SQLException e) {
|
121
|
// TODO Auto-generated catch block
|
122
|
e.printStackTrace();
|
123
|
}
|
124
|
|
125
|
getTaxonService().saveOrUpdate(taxaToSave);
|
126
|
return true;
|
127
|
}
|
128
|
|
129
|
@Override
|
130
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
|
131
|
EdaphobaseImportState state) {
|
132
|
String nameSpace;
|
133
|
Class<?> cdmClass;
|
134
|
Set<String> idSet;
|
135
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
136
|
|
137
|
try{
|
138
|
Set<String> taxonIdSet = new HashSet<String>();
|
139
|
while (rs.next()){
|
140
|
handleForeignKey(rs, taxonIdSet, "a_taxon_fk_taxon_id");
|
141
|
handleForeignKey(rs, taxonIdSet, "b_taxon_fk_taxon_id");
|
142
|
}
|
143
|
|
144
|
//name map
|
145
|
nameSpace = TAXON_NAMESPACE;
|
146
|
cdmClass = TaxonBase.class;
|
147
|
idSet = taxonIdSet;
|
148
|
Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
149
|
result.put(nameSpace, taxonMap);
|
150
|
|
151
|
} catch (SQLException e) {
|
152
|
throw new RuntimeException(e);
|
153
|
}
|
154
|
|
155
|
return result;
|
156
|
}
|
157
|
|
158
|
|
159
|
@Override
|
160
|
protected boolean doCheck(EdaphobaseImportState state) {
|
161
|
return false;
|
162
|
}
|
163
|
|
164
|
@Override
|
165
|
protected boolean isIgnore(EdaphobaseImportState state) {
|
166
|
return ! state.getConfig().isDoSynonyms();
|
167
|
}
|
168
|
|
169
|
}
|