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.globis;
|
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.log4j.Logger;
|
20
|
import org.springframework.stereotype.Component;
|
21
|
|
22
|
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
|
23
|
import eu.etaxonomy.cdm.model.agent.Person;
|
24
|
import eu.etaxonomy.cdm.model.agent.Team;
|
25
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.common.Language;
|
28
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
29
|
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
30
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
31
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
32
|
import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
|
33
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
34
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
35
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
37
|
|
38
|
|
39
|
/**
|
40
|
* @author a.mueller
|
41
|
* @created 20.02.2010
|
42
|
* @version 1.0
|
43
|
*/
|
44
|
@Component
|
45
|
public class GlobisCommonNameImport extends GlobisImportBase<Taxon> {
|
46
|
private static final Logger logger = Logger.getLogger(GlobisCommonNameImport.class);
|
47
|
|
48
|
private int modCount = 10000;
|
49
|
private static final String pluralString = "common names";
|
50
|
private static final String dbTableName = "species_language";
|
51
|
private static final Class cdmTargetClass = Taxon.class; //not needed
|
52
|
|
53
|
public GlobisCommonNameImport(){
|
54
|
super(pluralString, dbTableName, cdmTargetClass);
|
55
|
}
|
56
|
|
57
|
//dirty but acceptable for globis environment
|
58
|
private Map<Integer,Reference> refMap = new HashMap<Integer,Reference>();
|
59
|
|
60
|
|
61
|
/* (non-Javadoc)
|
62
|
* @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
|
63
|
*/
|
64
|
@Override
|
65
|
protected String getIdQuery() {
|
66
|
String strRecordQuery =
|
67
|
" SELECT ID " +
|
68
|
" FROM " + dbTableName;
|
69
|
return strRecordQuery;
|
70
|
}
|
71
|
|
72
|
|
73
|
|
74
|
|
75
|
/* (non-Javadoc)
|
76
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
|
77
|
*/
|
78
|
@Override
|
79
|
protected String getRecordQuery(GlobisImportConfigurator config) {
|
80
|
String strRecordQuery =
|
81
|
" SELECT * " +
|
82
|
" FROM " + getTableName() + " sl " +
|
83
|
" WHERE ( sl.ID IN (" + ID_LIST_TOKEN + ") )";
|
84
|
return strRecordQuery;
|
85
|
}
|
86
|
|
87
|
|
88
|
|
89
|
/* (non-Javadoc)
|
90
|
* @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
|
91
|
*/
|
92
|
@Override
|
93
|
public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
|
94
|
boolean success = true;
|
95
|
|
96
|
Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
|
97
|
|
98
|
Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
|
99
|
// Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
|
100
|
|
101
|
ResultSet rs = partitioner.getResultSet();
|
102
|
|
103
|
try {
|
104
|
|
105
|
int i = 0;
|
106
|
|
107
|
//for each common name
|
108
|
while (rs.next()){
|
109
|
|
110
|
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
|
111
|
|
112
|
Integer idTaxon = nullSafeInt(rs,"IDCurrentSpec");
|
113
|
|
114
|
try {
|
115
|
|
116
|
//source ref
|
117
|
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
118
|
|
119
|
//common names
|
120
|
Integer id = nullSafeInt(rs,"ID");
|
121
|
String isoLang = rs.getString("ISO");
|
122
|
String strCommonName = rs.getString("commonname");
|
123
|
Integer refID = nullSafeInt(rs,"ReferenceID");
|
124
|
String strCountryCode = rs.getString("Code2");
|
125
|
|
126
|
|
127
|
Taxon taxon = taxonMap.get(String.valueOf(idTaxon));
|
128
|
if (taxon == null){
|
129
|
logger.warn("No taxon found for taxonId " + idTaxon);
|
130
|
}else if (isBlank(strCommonName)){
|
131
|
logger.warn("No common name string defined for common name ID: " + id);
|
132
|
}else{
|
133
|
Language language = getLanguage(isoLang);
|
134
|
if (language == null){
|
135
|
logger.warn("No language found for common name ID: " + id);
|
136
|
}
|
137
|
NamedArea area = WaterbodyOrCountry.getWaterbodyOrCountryByIso3166A2(strCountryCode);
|
138
|
if (area == null){
|
139
|
logger.warn("No country found for common name ID: " + id);
|
140
|
}
|
141
|
|
142
|
TaxonDescription taxonDescription = getTaxonDescription(taxon, sourceRef, ! IMAGE_GALLERY, CREATE);
|
143
|
CommonTaxonName commonName = CommonTaxonName.NewInstance(strCommonName, language, area);
|
144
|
taxonDescription.addElement(commonName);
|
145
|
|
146
|
Reference<?> ref = handleReference(state, refID);
|
147
|
if (ref == null && refID != null){
|
148
|
logger.warn("No reference found for common name ID: " + id);
|
149
|
}else{
|
150
|
commonName.addSource(String.valueOf(refID), "reference", sourceRef, null);
|
151
|
}
|
152
|
|
153
|
objectsToSave.add(taxon);
|
154
|
}
|
155
|
|
156
|
} catch (Exception e) {
|
157
|
logger.warn("Exception in current_species: IDcurrentspec " + idTaxon + ". " + e.getMessage());
|
158
|
e.printStackTrace();
|
159
|
}
|
160
|
|
161
|
}
|
162
|
|
163
|
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
|
164
|
|
165
|
logger.warn(pluralString + " to save: " + objectsToSave.size());
|
166
|
getTaxonService().save(objectsToSave);
|
167
|
|
168
|
return success;
|
169
|
} catch (SQLException e) {
|
170
|
logger.error("SQLException:" + e);
|
171
|
return false;
|
172
|
}
|
173
|
}
|
174
|
|
175
|
|
176
|
|
177
|
private Map<String,Language> languageMap = new HashMap<String,Language>();
|
178
|
private Language getLanguage(String isoLang) {
|
179
|
Language result = languageMap.get(isoLang);
|
180
|
if (result == null){
|
181
|
|
182
|
result = getTermService().getLanguageByIso(isoLang);
|
183
|
if (result == null){
|
184
|
logger.warn("No language found for iso code: " + isoLang);
|
185
|
}
|
186
|
}
|
187
|
return result;
|
188
|
|
189
|
}
|
190
|
|
191
|
private Reference<?> handleReference(GlobisImportState state, Integer refId){
|
192
|
Reference<?> result = refMap.get(refId);
|
193
|
|
194
|
if (result == null){
|
195
|
try {
|
196
|
String sql = "SELECT * FROM references WHERE ReferenceID = " + refId;
|
197
|
ResultSet rs = state.getConfig().getSource().getResultSet(sql);
|
198
|
rs.next();
|
199
|
|
200
|
String authors = rs.getString("Author(s)");
|
201
|
String title = rs.getString("Title");
|
202
|
String details = rs.getString("Details");
|
203
|
Integer year = nullSafeInt(rs, "year");
|
204
|
result = ReferenceFactory.newGeneric();
|
205
|
result.setTitleCache(details);
|
206
|
result.setTitle(title);
|
207
|
result.setDatePublished(TimePeriod.NewInstance(year));
|
208
|
|
209
|
TeamOrPersonBase<?> author;
|
210
|
String[] authorSplit = authors.split("&");
|
211
|
if (authorSplit.length > 1){
|
212
|
Team team = Team.NewInstance();
|
213
|
author = team;
|
214
|
for (String singleAuthor : authorSplit){
|
215
|
Person person = makeSingleAuthor(singleAuthor);
|
216
|
team.addTeamMember(person);
|
217
|
}
|
218
|
}else{
|
219
|
author = makeSingleAuthor(authors);
|
220
|
}
|
221
|
|
222
|
result.setAuthorTeam(author);
|
223
|
refMap.put(refId,result);
|
224
|
rs.close();
|
225
|
} catch (SQLException e) {
|
226
|
e.printStackTrace();
|
227
|
}
|
228
|
|
229
|
|
230
|
}
|
231
|
|
232
|
return result;
|
233
|
}
|
234
|
|
235
|
|
236
|
|
237
|
|
238
|
private Person makeSingleAuthor(String authors) {
|
239
|
Person result = Person.NewTitledInstance(authors);
|
240
|
return result;
|
241
|
}
|
242
|
|
243
|
|
244
|
|
245
|
|
246
|
/* (non-Javadoc)
|
247
|
* @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
|
248
|
*/
|
249
|
public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
|
250
|
String nameSpace;
|
251
|
Class cdmClass;
|
252
|
Set<String> idSet;
|
253
|
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
|
254
|
try{
|
255
|
Set<String> taxonIdSet = new HashSet<String>();
|
256
|
|
257
|
while (rs.next()){
|
258
|
handleForeignKey(rs, taxonIdSet, "IDCurrentSpec");
|
259
|
}
|
260
|
|
261
|
//taxon map
|
262
|
nameSpace = TAXON_NAMESPACE;
|
263
|
cdmClass = Taxon.class;
|
264
|
idSet = taxonIdSet;
|
265
|
Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
|
266
|
result.put(nameSpace, objectMap);
|
267
|
|
268
|
|
269
|
} catch (SQLException e) {
|
270
|
throw new RuntimeException(e);
|
271
|
}
|
272
|
return result;
|
273
|
}
|
274
|
|
275
|
/* (non-Javadoc)
|
276
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
277
|
*/
|
278
|
@Override
|
279
|
protected boolean doCheck(GlobisImportState state){
|
280
|
// IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
|
281
|
return true;
|
282
|
}
|
283
|
|
284
|
|
285
|
/* (non-Javadoc)
|
286
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
|
287
|
*/
|
288
|
protected boolean isIgnore(GlobisImportState state){
|
289
|
return ! state.getConfig().isDoCommonNames();
|
290
|
}
|
291
|
|
292
|
|
293
|
|
294
|
|
295
|
|
296
|
}
|