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.app.synthesysImport;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.net.URI;
|
14
|
import java.net.URISyntaxException;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
import java.util.ListIterator;
|
18
|
import java.util.Map;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.springframework.transaction.TransactionStatus;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
24
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
25
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
26
|
import eu.etaxonomy.cdm.app.common.CdmImportSources;
|
27
|
import eu.etaxonomy.cdm.common.ExcelUtils;
|
28
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
29
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
30
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
31
|
import eu.etaxonomy.cdm.model.agent.Institution;
|
32
|
import eu.etaxonomy.cdm.model.agent.Person;
|
33
|
import eu.etaxonomy.cdm.model.common.Language;
|
34
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
35
|
import eu.etaxonomy.cdm.model.location.Country;
|
36
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
37
|
import eu.etaxonomy.cdm.model.location.Point;
|
38
|
import eu.etaxonomy.cdm.model.name.ITaxonNameBase;
|
39
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
40
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
41
|
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
|
45
|
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
|
46
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
47
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
48
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
49
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
50
|
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
|
51
|
|
52
|
/**
|
53
|
* @author PK
|
54
|
* @since 19.09.2008
|
55
|
* @version 1.0
|
56
|
*/
|
57
|
public class SynthesysCacheActivator {
|
58
|
private static final Logger logger = Logger.getLogger(SynthesysCacheActivator.class);
|
59
|
|
60
|
protected String fullScientificNameString = null;
|
61
|
protected String institutionCode = null;
|
62
|
protected String collectionCode = null;
|
63
|
protected String unitID = null;
|
64
|
protected String recordBasis = null;
|
65
|
protected String accessionNumber = null;
|
66
|
protected String collectorsNumber = null;
|
67
|
protected String fieldNumber = null;
|
68
|
protected Double longitude = null;
|
69
|
protected Double latitude = null;
|
70
|
protected String locality = null;
|
71
|
protected String country = null;
|
72
|
protected String isocountry = null;
|
73
|
protected ArrayList<String> gatheringAgentList = new ArrayList<String>();
|
74
|
protected ArrayList<String> identificationList = new ArrayList<String>();
|
75
|
|
76
|
static DbSchemaValidation hbm2dll = DbSchemaValidation.CREATE;
|
77
|
|
78
|
static ICdmDataSource desitination = CdmDestinations.localH2();
|
79
|
|
80
|
|
81
|
public void saveUnit(Map<String,String> unit){
|
82
|
String author = unit.get("author");
|
83
|
author=author.replaceAll("None","");
|
84
|
String taxonName = unit.get("taxonName");
|
85
|
taxonName = taxonName.replaceAll("None", "");
|
86
|
|
87
|
try {
|
88
|
this.institutionCode = unit.get("institution").replaceAll("None", null);
|
89
|
} catch (Exception e) {
|
90
|
}
|
91
|
|
92
|
try {this.collectionCode = unit.get("collection").replaceAll("None", null);
|
93
|
} catch (Exception e) {
|
94
|
}
|
95
|
try {this.unitID = unit.get("unitID").replaceAll("None", null);
|
96
|
} catch (Exception e) {
|
97
|
}
|
98
|
try {this.recordBasis = unit.get("recordBasis").replaceAll("None", null);
|
99
|
} catch (Exception e) {
|
100
|
}
|
101
|
try {this.accessionNumber = null;
|
102
|
} catch (Exception e) {
|
103
|
}
|
104
|
try {this.locality = unit.get("locality").replaceAll("None", null);
|
105
|
} catch (Exception e) {
|
106
|
}
|
107
|
try {this.longitude = Double.valueOf(unit.get("longitude"));
|
108
|
} catch (Exception e) {
|
109
|
}
|
110
|
try {this.latitude = Double.valueOf(unit.get("latitude"));
|
111
|
} catch (Exception e) {
|
112
|
}
|
113
|
try {this.country = unit.get("country").replaceAll("None", null);
|
114
|
} catch (Exception e) {
|
115
|
}
|
116
|
try {this.isocountry = unit.get("isoCountry").replaceAll("None", null);
|
117
|
} catch (Exception e) {
|
118
|
}
|
119
|
try {this.fieldNumber = unit.get("field number").replaceAll("None", null);
|
120
|
} catch (Exception e) {
|
121
|
}
|
122
|
try {this.collectorsNumber = unit.get("collector number").replaceAll("None", null);
|
123
|
} catch (Exception e) {
|
124
|
}
|
125
|
try {String coll =unit.get("collector");
|
126
|
coll=coll.replaceAll("None", null);
|
127
|
this.gatheringAgentList.add(coll);
|
128
|
} catch (Exception e) {
|
129
|
}
|
130
|
try {this.identificationList.add(taxonName+" "+author);
|
131
|
} catch (Exception e) {System.out.println(e);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
public boolean invoke(){
|
136
|
boolean result = true;
|
137
|
boolean withCdm = true;
|
138
|
CdmApplicationController app = null;
|
139
|
TransactionStatus tx = null;
|
140
|
|
141
|
app = CdmApplicationController.NewInstance(desitination, hbm2dll);
|
142
|
|
143
|
tx = app.startTransaction();
|
144
|
try {
|
145
|
Reference sec = ReferenceFactory.newDatabase();
|
146
|
sec.setTitleCache("SYNTHESYS CACHE DATA", true);
|
147
|
|
148
|
/**
|
149
|
* SPECIMEN OR OBSERVATION OR LIVING
|
150
|
*/
|
151
|
DerivedUnit derivedThing = null;
|
152
|
//create specimen
|
153
|
if (this.recordBasis != null){
|
154
|
if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
|
155
|
derivedThing = DerivedUnit.NewPreservedSpecimenInstance();
|
156
|
}
|
157
|
else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
|
158
|
derivedThing = DerivedUnit.NewInstance(SpecimenOrObservationType.Observation);
|
159
|
}
|
160
|
else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
|
161
|
derivedThing = DerivedUnit.NewInstance(SpecimenOrObservationType.LivingSpecimen);
|
162
|
}
|
163
|
}
|
164
|
if (derivedThing == null){
|
165
|
derivedThing = DerivedUnit.NewInstance(SpecimenOrObservationType.Observation);
|
166
|
}
|
167
|
|
168
|
ITaxonNameBase taxonName = null;
|
169
|
Taxon taxon = null;
|
170
|
DeterminationEvent determinationEvent = null;
|
171
|
List<TaxonName> names = null;
|
172
|
NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
|
173
|
String scientificName="";
|
174
|
boolean preferredFlag=false;
|
175
|
// System.out.println(this.identificationList);
|
176
|
for (int i = 0; i < this.identificationList.size(); i++) {
|
177
|
this.fullScientificNameString = this.identificationList.get(i);
|
178
|
this.fullScientificNameString = this.fullScientificNameString.replaceAll(" et ", " & ");
|
179
|
if (this.fullScientificNameString.indexOf("_preferred_") != -1){
|
180
|
scientificName = this.fullScientificNameString.split("_preferred_")[0];
|
181
|
String pTmp = this.fullScientificNameString.split("_preferred_")[1];
|
182
|
if (pTmp == "1" || pTmp.toLowerCase().indexOf("true") != -1) {
|
183
|
preferredFlag=true;
|
184
|
} else {
|
185
|
preferredFlag=false;
|
186
|
}
|
187
|
} else {
|
188
|
scientificName = this.fullScientificNameString;
|
189
|
}
|
190
|
|
191
|
taxonName = nvnpi.parseFullName(scientificName);
|
192
|
if (withCdm){
|
193
|
names = app.getNameService().findByName(null, scientificName, null, null, null, null, null, null).getRecords();
|
194
|
if (names.size() == 0){
|
195
|
System.out.println("Name not found: " + scientificName);
|
196
|
}else{
|
197
|
if (names.size() > 1){
|
198
|
System.out.println("More then 1 name found: " + scientificName);
|
199
|
}
|
200
|
System.out.println("Name found");
|
201
|
taxonName = names.get(0);
|
202
|
}
|
203
|
}
|
204
|
|
205
|
|
206
|
// tx = app.startTransaction();
|
207
|
app.getNameService().saveOrUpdate(TaxonName.castAndDeproxy(taxonName));
|
208
|
taxon = Taxon.NewInstance(taxonName, sec); //TODO use real reference for sec
|
209
|
// app.commitTransaction(tx);
|
210
|
|
211
|
|
212
|
determinationEvent = DeterminationEvent.NewInstance();
|
213
|
determinationEvent.setTaxon(taxon);
|
214
|
determinationEvent.setPreferredFlag(preferredFlag);
|
215
|
derivedThing.addDetermination(determinationEvent);
|
216
|
}
|
217
|
|
218
|
|
219
|
//set catalogue number (unitID)
|
220
|
derivedThing.setCatalogNumber(this.unitID);
|
221
|
derivedThing.setAccessionNumber(this.accessionNumber);
|
222
|
derivedThing.setCollectorsNumber(this.collectorsNumber);
|
223
|
|
224
|
|
225
|
/**
|
226
|
* INSTITUTION & COLLECTION
|
227
|
*/
|
228
|
//manage institution
|
229
|
Institution institution;
|
230
|
List<Institution> institutions;
|
231
|
try{
|
232
|
System.out.println(this.institutionCode);
|
233
|
institutions= app.getAgentService().searchInstitutionByCode(this.institutionCode);
|
234
|
}catch(Exception e){
|
235
|
System.out.println("BLI "+e);
|
236
|
institutions=new ArrayList<Institution>();
|
237
|
}
|
238
|
if (institutions.size() ==0){
|
239
|
System.out.println("Institution (agent) unknown");
|
240
|
//create institution
|
241
|
institution = Institution.NewInstance();
|
242
|
institution.setCode(this.institutionCode);
|
243
|
}
|
244
|
else{
|
245
|
System.out.println("Institution (agent) already in the db");
|
246
|
institution = institutions.get(0);
|
247
|
}
|
248
|
|
249
|
//manage collection
|
250
|
Collection collection = Collection.NewInstance();
|
251
|
List<Collection> collections;
|
252
|
try{
|
253
|
collections = app.getCollectionService().searchByCode(this.collectionCode);
|
254
|
//.searchCollectionByCode(this.collectionCode);
|
255
|
}catch(Exception e){
|
256
|
System.out.println("BLA"+e);
|
257
|
collections=new ArrayList<>();
|
258
|
}
|
259
|
if (collections.size() ==0){
|
260
|
System.out.println("Collection not found "+this.collectionCode);
|
261
|
//create new collection
|
262
|
collection.setCode(this.collectionCode);
|
263
|
collection.setCodeStandard("GBIF");
|
264
|
collection.setInstitute(institution);
|
265
|
}
|
266
|
else{
|
267
|
boolean collectionFound=false;
|
268
|
for (int i=0; i<collections.size(); i++){
|
269
|
collection = collections.get(i);
|
270
|
try {
|
271
|
if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){
|
272
|
//found a collection with the same code and the same institution
|
273
|
collectionFound=true;
|
274
|
}
|
275
|
} catch (NullPointerException e) {}
|
276
|
}
|
277
|
System.out.println("a trouvé la collection avec la meme institution? "+collectionFound);
|
278
|
if (!collectionFound){ //need to add a new collection with the pre-configured institution
|
279
|
collection.setCode(this.collectionCode);
|
280
|
collection.setCodeStandard("GBIF");
|
281
|
collection.setInstitute(institution);
|
282
|
}
|
283
|
|
284
|
}
|
285
|
System.out.println("collection inserted");
|
286
|
//link specimen & collection
|
287
|
derivedThing.setCollection(collection);
|
288
|
|
289
|
/**
|
290
|
* GATHERING EVENT
|
291
|
*/
|
292
|
//create gathering event
|
293
|
GatheringEvent gatheringEvent = GatheringEvent.NewInstance();
|
294
|
//add locality
|
295
|
Language language = Language.DEFAULT();
|
296
|
LanguageString loc = LanguageString.NewInstance(this.locality,language);
|
297
|
gatheringEvent.setLocality(loc);
|
298
|
|
299
|
//create coordinates point
|
300
|
Point coordinates = Point.NewInstance();
|
301
|
//add coordinates
|
302
|
coordinates.setLatitude(this.latitude);
|
303
|
coordinates.setLongitude(this.longitude);
|
304
|
gatheringEvent.setExactLocation(coordinates);
|
305
|
|
306
|
NamedArea area = NamedArea.NewInstance();
|
307
|
|
308
|
|
309
|
Country country = null;
|
310
|
// System.out.println("isocountry "+this.isocountry);
|
311
|
if (this.isocountry != null) {
|
312
|
country = app.getOccurrenceService().getCountryByIso(this.isocountry);
|
313
|
}
|
314
|
|
315
|
// System.out.println(country.getLabel());
|
316
|
// Set<Continent> cont = country.getContinents();
|
317
|
//
|
318
|
// System.out.println(cont.size());
|
319
|
// Iterator<Continent> iter = cont.iterator();
|
320
|
// while (iter.hasNext())
|
321
|
// System.out.println(iter.next().toString());
|
322
|
|
323
|
if (country != null){
|
324
|
area.addCountry(country);
|
325
|
System.out.println("country not null!");
|
326
|
}
|
327
|
// else{
|
328
|
// if (this.country != null){
|
329
|
// List<Country>countries = app.getOccurrenceService().getCountryByName(this.country);
|
330
|
// if (countries.size() >0)
|
331
|
// area.addCountry(countries.get(0));
|
332
|
// else
|
333
|
// System.out.println("NO COUNTRY");//TODO need to add a new country!
|
334
|
// }
|
335
|
// }
|
336
|
// app.getTermService().saveTerm(area);
|
337
|
gatheringEvent.addCollectingArea(area);
|
338
|
|
339
|
//create collector
|
340
|
AgentBase collector;
|
341
|
ListIterator<String> collectors = this.gatheringAgentList.listIterator();
|
342
|
//add the collectors
|
343
|
String collName;
|
344
|
while (collectors.hasNext()){
|
345
|
collName = collectors.next();
|
346
|
/*check if the collector does already exist*/
|
347
|
try{
|
348
|
Pager<AgentBase> col = app.getAgentService().findByTitle(null, collName, null, null, null, null, null, null);
|
349
|
collector=col.getRecords().get(0);
|
350
|
System.out.println("a trouve l'agent");
|
351
|
}catch (Exception e) {
|
352
|
collector = Person.NewInstance();
|
353
|
collector.setTitleCache(collName, true);
|
354
|
}
|
355
|
gatheringEvent.setCollector(collector);
|
356
|
}
|
357
|
|
358
|
//create field/observation
|
359
|
FieldUnit fieldUnit = FieldUnit.NewInstance();
|
360
|
//add fieldNumber
|
361
|
fieldUnit.setFieldNumber(this.fieldNumber);
|
362
|
|
363
|
//join gatheringEvent to fieldObservation
|
364
|
fieldUnit.setGatheringEvent(gatheringEvent);
|
365
|
|
366
|
|
367
|
// //link fieldObservation and specimen
|
368
|
DerivationEvent derivationEvent = DerivationEvent.NewInstance();
|
369
|
derivationEvent.addOriginal(fieldUnit);
|
370
|
derivedThing.addDerivationEvent(derivationEvent);
|
371
|
// derivationEvent.addDerivative(derivedThing);
|
372
|
|
373
|
/**
|
374
|
* SAVE AND STORE DATA
|
375
|
*/
|
376
|
//save the specimen data
|
377
|
// app.getOccurrenceService().saveSpecimenOrObservationBase(fieldObservation);
|
378
|
try {
|
379
|
// tx = app.startTransaction();
|
380
|
app.getTermService().saveOrUpdate(area);//save it sooner
|
381
|
app.getOccurrenceService().saveOrUpdate(derivedThing);
|
382
|
// app.commitTransaction(tx);
|
383
|
// app.close();
|
384
|
} catch (Exception e) {
|
385
|
// TODO Auto-generated catch block
|
386
|
System.out.println("PATATE "+e);
|
387
|
}
|
388
|
|
389
|
|
390
|
logger.info("saved new specimen ...");
|
391
|
|
392
|
|
393
|
|
394
|
} catch (Exception e) {
|
395
|
logger.warn("Error when reading record!!");
|
396
|
e.printStackTrace();
|
397
|
result = false;
|
398
|
}
|
399
|
//
|
400
|
app.commitTransaction(tx);
|
401
|
System.out.println("commit done");
|
402
|
app.close();
|
403
|
|
404
|
return result;
|
405
|
}
|
406
|
|
407
|
|
408
|
|
409
|
private DeterminationEvent getDetermination(Taxon taxon, String actor){
|
410
|
logger.info("Create determination event");
|
411
|
DeterminationEvent determinationEvent = DeterminationEvent.NewInstance();
|
412
|
determinationEvent.setTaxon(taxon);
|
413
|
Person person = Person.NewTitledInstance(actor);
|
414
|
determinationEvent.setActor(person);
|
415
|
return determinationEvent;
|
416
|
}
|
417
|
|
418
|
/**
|
419
|
* @param args
|
420
|
* @throws URISyntaxException
|
421
|
*/
|
422
|
public static void main(String[] args) throws URISyntaxException {
|
423
|
URI uri = CdmImportSources.SYNTHESYS_SPECIMEN();
|
424
|
logger.info("main method");
|
425
|
SynthesysCacheActivator abcdAct = new SynthesysCacheActivator();
|
426
|
|
427
|
List<Map<String, String>> units;
|
428
|
try {
|
429
|
units = ExcelUtils.parseXLS(uri);
|
430
|
} catch (FileNotFoundException e) {
|
431
|
logger.error("FileNotFound: " + uri);
|
432
|
return;
|
433
|
}
|
434
|
Map<String,String> unit=null;
|
435
|
for (int i=0; i<units.size();i++){
|
436
|
unit = units.get(i);
|
437
|
logger.info(unit);
|
438
|
abcdAct.saveUnit(unit);//and then invoke
|
439
|
abcdAct.invoke();
|
440
|
|
441
|
}
|
442
|
}
|
443
|
|
444
|
|
445
|
|
446
|
}
|