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