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.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.springframework.stereotype.Component;
|
23
|
|
24
|
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
|
25
|
import eu.etaxonomy.cdm.io.berlinModel.in.validation.BerlinModelTypesImportValidator;
|
26
|
import eu.etaxonomy.cdm.io.common.IOValidator;
|
27
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
28
|
import eu.etaxonomy.cdm.io.common.Source;
|
29
|
import eu.etaxonomy.cdm.model.agent.Person;
|
30
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.common.Language;
|
33
|
import eu.etaxonomy.cdm.model.description.Feature;
|
34
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
35
|
import eu.etaxonomy.cdm.model.description.TextData;
|
36
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
37
|
import eu.etaxonomy.cdm.model.media.Media;
|
38
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
39
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
40
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
41
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
42
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
43
|
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
|
44
|
|
45
|
/**
|
46
|
* @author a.mueller
|
47
|
* @created 20.03.2008
|
48
|
*/
|
49
|
@Component
|
50
|
public class BerlinModelTypesImport extends BerlinModelImportBase /*implements IIO<BerlinModelImportConfigurator>*/ {
|
51
|
|
52
|
private static final long serialVersionUID = -8468807718014749046L;
|
53
|
private static final Logger logger = Logger.getLogger(BerlinModelTypesImport.class);
|
54
|
|
55
|
private static int modCount = 10000;
|
56
|
private static final String pluralString = "types";
|
57
|
private static final String dbTableName = "TypeDesignation";
|
58
|
|
59
|
|
60
|
public BerlinModelTypesImport(){
|
61
|
super(dbTableName, pluralString);
|
62
|
}
|
63
|
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
67
|
*/
|
68
|
@Override
|
69
|
protected String getRecordQuery(BerlinModelImportConfigurator config) {
|
70
|
String strRecordQuery =
|
71
|
" SELECT TypeDesignation.*, TypeStatus.Status " +
|
72
|
" FROM TypeDesignation LEFT OUTER JOIN " +
|
73
|
" TypeStatus ON TypeDesignation.TypeStatusFk = TypeStatus.TypeStatusId " +
|
74
|
" WHERE (TypeDesignationId IN ("+ ID_LIST_TOKEN + ") )";
|
75
|
return strRecordQuery;
|
76
|
}
|
77
|
|
78
|
/* (non-Javadoc)
|
79
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#doPartition(eu.etaxonomy.cdm.io.berlinModel.in.ResultSetPartitioner, eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportState)
|
80
|
*/
|
81
|
@Override
|
82
|
public boolean doPartition(ResultSetPartitioner partitioner, BerlinModelImportState state) {
|
83
|
boolean result = true;
|
84
|
Set<TaxonName> namesToSave = new HashSet<>();
|
85
|
Map<Integer, DerivedUnit> typeMap = new HashMap<Integer, DerivedUnit>();
|
86
|
|
87
|
Map<String, TaxonName> nameMap = partitioner.getObjectMap(BerlinModelTaxonNameImport.NAMESPACE);
|
88
|
Map<String, Reference> refMap = partitioner.getObjectMap(BerlinModelReferenceImport.REFERENCE_NAMESPACE);
|
89
|
|
90
|
BerlinModelImportConfigurator config = state.getConfig();
|
91
|
Source source = config.getSource();
|
92
|
|
93
|
ResultSet rs = partitioner.getResultSet();
|
94
|
|
95
|
try {
|
96
|
|
97
|
int i = 0;
|
98
|
//for each reference
|
99
|
while (rs.next()){
|
100
|
|
101
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Types handled: " + (i-1));}
|
102
|
|
103
|
int typeDesignationId = rs.getInt("typeDesignationId");
|
104
|
int nameId = rs.getInt("nameFk");
|
105
|
int typeStatusFk = rs.getInt("typeStatusFk");
|
106
|
Object refFkObj = rs.getObject("refFk");
|
107
|
String refDetail = rs.getString("refDetail");
|
108
|
String status = rs.getString("Status");
|
109
|
String typePhrase = rs.getString("typePhrase");
|
110
|
String notes = rs.getString("notes");
|
111
|
|
112
|
//TODO
|
113
|
boolean isNotDesignated = false;
|
114
|
|
115
|
|
116
|
//TODO
|
117
|
//TypeCache leer
|
118
|
//RejectedFlag false
|
119
|
//PublishFlag xxx
|
120
|
|
121
|
TaxonName taxonNameBase = nameMap.get(String.valueOf(nameId));
|
122
|
|
123
|
if (taxonNameBase != null){
|
124
|
try{
|
125
|
SpecimenTypeDesignationStatus typeDesignationStatus = BerlinModelTransformer.typeStatusId2TypeStatus(typeStatusFk);
|
126
|
Reference citation = null;
|
127
|
if (refFkObj != null){
|
128
|
String relRefFk = String.valueOf(refFkObj);
|
129
|
//get nomRef
|
130
|
citation = refMap.get(relRefFk);
|
131
|
}
|
132
|
|
133
|
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
134
|
specimen.putDefinition(Language.DEFAULT(), typePhrase);
|
135
|
|
136
|
if (typePhrase == null){
|
137
|
String message = "No type phrase available for type with typeDesignationId %s of taxon name %s";
|
138
|
message = String.format(message, typeDesignationId, taxonNameBase.getTitleCache());
|
139
|
logger.warn(message);
|
140
|
}else if (typePhrase.length()> 255){
|
141
|
typePhrase = typePhrase.substring(0, 255);
|
142
|
specimen.setTitleCache(typePhrase, true);
|
143
|
}
|
144
|
boolean addToAllNames = true;
|
145
|
String originalNameString = null;
|
146
|
SpecimenTypeDesignation type = taxonNameBase.addSpecimenTypeDesignation(specimen, typeDesignationStatus, citation, refDetail, originalNameString, isNotDesignated, addToAllNames);
|
147
|
this.doNotes(type, notes);
|
148
|
|
149
|
typeMap.put(typeDesignationId, specimen);
|
150
|
namesToSave.add(taxonNameBase);
|
151
|
|
152
|
}catch (UnknownCdmTypeException e) {
|
153
|
logger.warn("TypeStatus '" + status + "' not yet implemented");
|
154
|
result = false;
|
155
|
}catch (Exception e) {
|
156
|
logger.warn("Unexpected exception occurred while processing type with typeDesignationId " + String.valueOf(typeDesignationId));
|
157
|
result = false;
|
158
|
}
|
159
|
}else{
|
160
|
//TODO
|
161
|
logger.warn("TaxonName for TypeDesignation " + typeDesignationId + " does not exist in store");
|
162
|
result = false;
|
163
|
}
|
164
|
//put
|
165
|
}
|
166
|
|
167
|
result &= makeFigures(typeMap, source);
|
168
|
logger.info("Names to save: " + namesToSave.size());
|
169
|
getNameService().save(namesToSave);
|
170
|
return result;
|
171
|
} catch (SQLException e) {
|
172
|
logger.error("SQLException:" + e);
|
173
|
return false;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, BerlinModelImportState state) {
|
179
|
String nameSpace;
|
180
|
Class<?> cdmClass;
|
181
|
Set<String> idSet;
|
182
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
183
|
|
184
|
try{
|
185
|
Set<String> nameIdSet = new HashSet<String>();
|
186
|
Set<String> referenceIdSet = new HashSet<String>();
|
187
|
while (rs.next()){
|
188
|
handleForeignKey(rs, nameIdSet, "NameFk");
|
189
|
handleForeignKey(rs, referenceIdSet, "RefFk");
|
190
|
}
|
191
|
|
192
|
//name map
|
193
|
nameSpace = BerlinModelTaxonNameImport.NAMESPACE;
|
194
|
cdmClass = TaxonName.class;
|
195
|
idSet = nameIdSet;
|
196
|
Map<String, Person> objectMap = (Map<String, Person>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
197
|
result.put(nameSpace, objectMap);
|
198
|
|
199
|
//reference map
|
200
|
nameSpace = BerlinModelReferenceImport.REFERENCE_NAMESPACE;
|
201
|
cdmClass = Reference.class;
|
202
|
idSet = referenceIdSet;
|
203
|
Map<String, Reference> referenceMap = (Map<String, Reference>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
204
|
result.put(nameSpace, referenceMap);
|
205
|
|
206
|
} catch (SQLException e) {
|
207
|
throw new RuntimeException(e);
|
208
|
}
|
209
|
return result;
|
210
|
}
|
211
|
|
212
|
|
213
|
private boolean makeFigures(Map<Integer, DerivedUnit> typeMap, Source source){
|
214
|
boolean success = true;
|
215
|
try {
|
216
|
//get data from database
|
217
|
String strQuery =
|
218
|
" SELECT * " +
|
219
|
" FROM TypeFigure " +
|
220
|
" WHERE (1=1) ";
|
221
|
ResultSet rs = source.getResultSet(strQuery) ;
|
222
|
|
223
|
int i = 0;
|
224
|
//for each reference
|
225
|
while (rs.next()){
|
226
|
|
227
|
if ((i++ % modCount) == 0){ logger.info("TypesFigures handled: " + (i-1));}
|
228
|
|
229
|
Integer typeFigureId = rs.getInt("typeFigureId");
|
230
|
Integer typeDesignationFk = rs.getInt("typeDesignationFk");
|
231
|
Integer collectionFk = rs.getInt("collectionFk");
|
232
|
String filename = rs.getString("filename");
|
233
|
|
234
|
String figurePhrase = rs.getString("figurePhrase");
|
235
|
|
236
|
String mimeType = null; //"image/jpg";
|
237
|
String suffix = null; //"jpg";
|
238
|
java.net.URI uri = new URI(filename);
|
239
|
Media media = ImageFile.NewMediaInstance(null, null, uri, mimeType, suffix, null, null, null);
|
240
|
if (figurePhrase != null) {
|
241
|
media.addAnnotation(Annotation.NewDefaultLanguageInstance(figurePhrase));
|
242
|
}
|
243
|
DerivedUnit typeSpecimen = typeMap.get(typeDesignationFk);
|
244
|
if (typeSpecimen != null) {
|
245
|
SpecimenDescription desc = this.getSpecimenDescription(typeSpecimen, IMAGE_GALLERY, CREATE);
|
246
|
if (desc.getElements().isEmpty()){
|
247
|
desc.addElement(TextData.NewInstance(Feature.IMAGE()));
|
248
|
}
|
249
|
TextData textData = CdmBase.deproxy(desc.getElements().iterator().next(), TextData.class);
|
250
|
textData.addMedia(media);
|
251
|
// typeSpecimen.addMedia(media); #3597
|
252
|
}
|
253
|
|
254
|
//mimeType + suffix
|
255
|
//TODO
|
256
|
//RefFk
|
257
|
//RefDetail
|
258
|
//VerifiedBy
|
259
|
//VerifiedWhen
|
260
|
//PrefFigureFlag
|
261
|
//PublishedFlag
|
262
|
//etc.
|
263
|
}
|
264
|
} catch (SQLException e) {
|
265
|
logger.error("SQLException:" + e);
|
266
|
return false;
|
267
|
} catch (URISyntaxException e) {
|
268
|
logger.error("URISyntaxException:" + e);
|
269
|
return false;
|
270
|
}
|
271
|
|
272
|
return success;
|
273
|
}
|
274
|
|
275
|
/* (non-Javadoc)
|
276
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
277
|
*/
|
278
|
@Override
|
279
|
protected boolean doCheck(BerlinModelImportState state){
|
280
|
IOValidator<BerlinModelImportState> validator = new BerlinModelTypesImportValidator();
|
281
|
return validator.validate(state);
|
282
|
}
|
283
|
|
284
|
|
285
|
/* (non-Javadoc)
|
286
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
287
|
*/
|
288
|
@Override
|
289
|
protected boolean isIgnore(BerlinModelImportState state){
|
290
|
return ! state.getConfig().isDoTypes();
|
291
|
}
|
292
|
|
293
|
}
|