1
|
/**
|
2
|
* Copyright (C) 2007 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.berlinModel.in;
|
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.commons.lang.StringUtils;
|
19
|
import org.apache.logging.log4j.LogManager;
|
20
|
import org.apache.logging.log4j.Logger;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelNameStatusImportValidator;
|
25
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
26
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
27
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.common.Marker;
|
30
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
31
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
|
32
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
35
|
|
36
|
/**
|
37
|
* @author a.mueller
|
38
|
* @since 20.03.2008
|
39
|
*/
|
40
|
@Component
|
41
|
public class BerlinModelNameStatusImport extends BerlinModelImportBase {
|
42
|
|
43
|
private static final long serialVersionUID = 6984893930082868489L;
|
44
|
private static final Logger logger = LogManager.getLogger();
|
45
|
|
46
|
private int modCount = 5000;
|
47
|
private static final String pluralString = "nomenclatural status";
|
48
|
private static final String dbTableName = "NomStatusRel";
|
49
|
|
50
|
|
51
|
public BerlinModelNameStatusImport(){
|
52
|
super(dbTableName, pluralString);
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
protected String getIdQuery(BerlinModelImportState state) {
|
57
|
String result = " SELECT RIdentifier FROM " + getTableName();
|
58
|
|
59
|
if (StringUtils.isNotEmpty(state.getConfig().getNameIdTable())){
|
60
|
result += " WHERE nameFk IN (SELECT NameId FROM " + state.getConfig().getNameIdTable() + ")";
|
61
|
}
|
62
|
return result;
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
67
|
String strQuery =
|
68
|
" SELECT NomStatusRel.*, NomStatus.NomStatus, RefDetail.Details " +
|
69
|
" FROM NomStatusRel INNER JOIN " +
|
70
|
" NomStatus ON NomStatusRel.NomStatusFk = NomStatus.NomStatusId " +
|
71
|
" LEFT OUTER JOIN RefDetail ON NomStatusRel.NomStatusRefDetailFk = RefDetail.RefDetailId AND " +
|
72
|
" NomStatusRel.NomStatusRefFk = RefDetail.RefFk " +
|
73
|
" WHERE (RIdentifier IN (" + ID_LIST_TOKEN + "))";
|
74
|
return strQuery;
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner,BerlinModelImportState state) {
|
79
|
boolean success = true;
|
80
|
String dbAttrName;
|
81
|
String cdmAttrName;
|
82
|
|
83
|
Set<TaxonName> namesToSave = new HashSet<>();
|
84
|
BerlinModelImportConfigurator config = state.getConfig();
|
85
|
@SuppressWarnings("unchecked")
|
86
|
Map<String, TaxonName> nameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
|
87
|
|
88
|
ResultSet rs = partitioner.getResultSet();
|
89
|
try {
|
90
|
//get data from database
|
91
|
|
92
|
int i = 0;
|
93
|
//for each reference
|
94
|
while (rs.next()){
|
95
|
|
96
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("NomStatus handled: " + (i-1));}
|
97
|
|
98
|
int nomStatusRelId;
|
99
|
try {
|
100
|
nomStatusRelId = rs.getInt("RIdentifier");
|
101
|
} catch (Exception e) { //RIdentifier does not exist in BM database
|
102
|
nomStatusRelId = -1;
|
103
|
}
|
104
|
int nomStatusFk = rs.getInt("NomStatusFk");
|
105
|
int nameId = rs.getInt("nameFk");
|
106
|
|
107
|
boolean doubtful = rs.getBoolean("DoubtfulFlag");
|
108
|
String nomStatusLabel = rs.getString("NomStatus");
|
109
|
|
110
|
TaxonName taxonName = nameMap.get(String.valueOf(nameId));
|
111
|
//TODO doubtful
|
112
|
|
113
|
if (taxonName != null ){
|
114
|
try{
|
115
|
NomenclaturalStatus nomStatus;
|
116
|
if (state.getConfig().isMcl()) {
|
117
|
nomStatus = BerlinModelTransformer.nomStatusFkToNomStatusMedchecklist(nomStatusFk, nomStatusLabel);
|
118
|
}else {
|
119
|
nomStatus = BerlinModelTransformer.nomStatusFkToNomStatus(nomStatusFk, nomStatusLabel);
|
120
|
}
|
121
|
if (nomStatus == null){
|
122
|
String message = "Nomenclatural status could not be defined for %s ; %s";
|
123
|
message = String.format(message, nomStatusFk, nomStatusLabel);
|
124
|
logger.warn(message);
|
125
|
success = false;
|
126
|
continue;
|
127
|
}else{
|
128
|
if (nomStatus.getType() == null){
|
129
|
String message = "Nomenclatural status type could not be defined for %s ; %s";
|
130
|
message = String.format(message, nomStatusFk, nomStatusLabel);
|
131
|
logger.warn(message);
|
132
|
success = false;
|
133
|
continue;
|
134
|
}else if(nomStatus.getType().getId() == 0){
|
135
|
getTermService().save(nomStatus.getType());
|
136
|
}
|
137
|
}
|
138
|
|
139
|
//reference
|
140
|
makeReference(config, nomStatus, nameId, rs, partitioner);
|
141
|
|
142
|
//Details
|
143
|
dbAttrName = "details";
|
144
|
cdmAttrName = "citationMicroReference";
|
145
|
success &= ImportHelper.addStringValue(rs, nomStatus, dbAttrName, cdmAttrName, true);
|
146
|
|
147
|
//doubtful
|
148
|
if (doubtful){
|
149
|
nomStatus.addMarker(Marker.NewInstance(MarkerType.IS_DOUBTFUL(), true));
|
150
|
}
|
151
|
taxonName.addStatus(nomStatus);
|
152
|
namesToSave.add(taxonName);
|
153
|
}catch (UnknownCdmTypeException e) {
|
154
|
logger.warn("NomStatusType " + nomStatusFk + " not yet implemented");
|
155
|
success = false;
|
156
|
}
|
157
|
//TODO
|
158
|
//ID
|
159
|
//etc.
|
160
|
}else{
|
161
|
logger.warn("TaxonName for NomStatusRel (" + nomStatusRelId + ") does not exist in store");
|
162
|
success = false;
|
163
|
}
|
164
|
}
|
165
|
logger.info("TaxonNames to save: " + namesToSave.size());
|
166
|
getNameService().save(namesToSave);
|
167
|
|
168
|
return success;
|
169
|
} catch (SQLException e) {
|
170
|
logger.error("SQLException:" + e);
|
171
|
return false;
|
172
|
}
|
173
|
|
174
|
}
|
175
|
|
176
|
@Override
|
177
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
178
|
|
179
|
String nameSpace;
|
180
|
Set<String> idSet;
|
181
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
|
182
|
|
183
|
try{
|
184
|
Set<String> nameIdSet = new HashSet<>();
|
185
|
Set<String> referenceIdSet = new HashSet<>();
|
186
|
Set<String> refDetailIdSet = new HashSet<>();
|
187
|
while (rs.next()){
|
188
|
handleForeignKey(rs, nameIdSet, "nameFk");
|
189
|
handleForeignKey(rs, referenceIdSet, "NomStatusRefFk");
|
190
|
handleForeignKey(rs, refDetailIdSet, "NomStatusRefDetailFk");
|
191
|
}
|
192
|
|
193
|
//name map
|
194
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
195
|
idSet = nameIdSet;
|
196
|
Map<String, TaxonName> nameMap = getCommonService().getSourcedObjectsByIdInSourceC(TaxonName.class, idSet, nameSpace);
|
197
|
result.put(nameSpace, nameMap);
|
198
|
|
199
|
//reference map
|
200
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
201
|
idSet = referenceIdSet;
|
202
|
Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
|
203
|
result.put(nameSpace, referenceMap);
|
204
|
|
205
|
//refDetail map
|
206
|
nameSpace = BerlinModelRefDetailImport.REFDETAIL_NAMESPACE;
|
207
|
idSet = refDetailIdSet;
|
208
|
Map<String, Reference> refDetailMap= getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
|
209
|
result.put(nameSpace, refDetailMap);
|
210
|
|
211
|
} catch (SQLException e) {
|
212
|
throw new RuntimeException(e);
|
213
|
}
|
214
|
return result;
|
215
|
}
|
216
|
|
217
|
private boolean makeReference(BerlinModelImportConfigurator config, NomenclaturalStatus nomStatus,
|
218
|
int nameId, ResultSet rs, @SuppressWarnings("rawtypes") ResultSetPartitioner partitioner)
|
219
|
throws SQLException{
|
220
|
|
221
|
@SuppressWarnings("unchecked")
|
222
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
223
|
@SuppressWarnings("unchecked")
|
224
|
Map<String, Reference> refDetailMap = partitioner.getObjectMap(BerlinModelRefDetailImport.REFDETAIL_NAMESPACE);
|
225
|
|
226
|
Object nomRefFkObj = rs.getObject("NomStatusRefFk");
|
227
|
Object nomRefDetailFkObj = rs.getObject("NomStatusRefDetailFk");
|
228
|
//TODO
|
229
|
// boolean refDetailPrelim = rs.getBoolean("RefDetailPrelim");
|
230
|
|
231
|
boolean success = true;
|
232
|
//nomenclatural Reference
|
233
|
if (refMap != null){
|
234
|
if (nomRefFkObj != null){
|
235
|
String nomRefFk = String.valueOf(nomRefFkObj);
|
236
|
String nomRefDetailFk = String.valueOf(nomRefDetailFkObj);
|
237
|
Reference ref = getReferenceFromMaps(refDetailMap, refMap, nomRefDetailFk, nomRefFk);
|
238
|
|
239
|
//setRef
|
240
|
if (ref == null ){
|
241
|
//TODO
|
242
|
if (! config.isIgnoreNull()){logger.warn("Reference (refFk = " + nomRefFk + ") for NomStatus of TaxonName (nameId = " + nameId + ")"+
|
243
|
" was not found in reference store. Nomenclatural status reference was not set!!");}
|
244
|
}else{
|
245
|
nomStatus.setCitation(ref);
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
return success;
|
250
|
}
|
251
|
|
252
|
@Override
|
253
|
protected boolean doCheck(BerlinModelImportState state){
|
254
|
IOValidator<BerlinModelImportState> validator = new BerlinModelNameStatusImportValidator();
|
255
|
return validator.validate(state);
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
protected boolean isIgnore(BerlinModelImportState state){
|
260
|
return ! state.getConfig().isDoNameStatus();
|
261
|
}
|
262
|
}
|