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