2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.app
.synthesysImport
;
12 import java
.io
.FileNotFoundException
;
14 import java
.net
.URISyntaxException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
18 import java
.util
.ListIterator
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.transaction
.TransactionStatus
;
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
.TaxonNameBase
;
39 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
40 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
41 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
42 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
46 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
47 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
49 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
56 public class SynthesysCacheActivator
{
57 private static final Logger logger
= Logger
.getLogger(SynthesysCacheActivator
.class);
59 protected String fullScientificNameString
= null;
60 protected String institutionCode
= null;
61 protected String collectionCode
= null;
62 protected String unitID
= null;
63 protected String recordBasis
= null;
64 protected String accessionNumber
= null;
65 protected String collectorsNumber
= null;
66 protected String fieldNumber
= null;
67 protected Double longitude
= null;
68 protected Double latitude
= null;
69 protected String locality
= null;
70 protected String country
= null;
71 protected String isocountry
= null;
72 protected ArrayList
<String
> gatheringAgentList
= new ArrayList
<String
>();
73 protected ArrayList
<String
> identificationList
= new ArrayList
<String
>();
75 static DbSchemaValidation hbm2dll
= DbSchemaValidation
.CREATE
;
77 static ICdmDataSource desitination
= CdmDestinations
.localH2();
80 public void saveUnit(HashMap
<String
,String
> unit
){
81 String author
= unit
.get("author");
82 author
=author
.replaceAll("None","");
83 String taxonName
= unit
.get("taxonName");
84 taxonName
= taxonName
.replaceAll("None", "");
87 this.institutionCode
= unit
.get("institution").replaceAll("None", null);
88 } catch (Exception e
) {
91 try {this.collectionCode
= unit
.get("collection").replaceAll("None", null);
92 } catch (Exception e
) {
94 try {this.unitID
= unit
.get("unitID").replaceAll("None", null);
95 } catch (Exception e
) {
97 try {this.recordBasis
= unit
.get("recordBasis").replaceAll("None", null);
98 } catch (Exception e
) {
100 try {this.accessionNumber
= null;
101 } catch (Exception e
) {
103 try {this.locality
= unit
.get("locality").replaceAll("None", null);
104 } catch (Exception e
) {
106 try {this.longitude
= Double
.valueOf(unit
.get("longitude"));
107 } catch (Exception e
) {
109 try {this.latitude
= Double
.valueOf(unit
.get("latitude"));
110 } catch (Exception e
) {
112 try {this.country
= unit
.get("country").replaceAll("None", null);
113 } catch (Exception e
) {
115 try {this.isocountry
= unit
.get("isoCountry").replaceAll("None", null);
116 } catch (Exception e
) {
118 try {this.fieldNumber
= unit
.get("field number").replaceAll("None", null);
119 } catch (Exception e
) {
121 try {this.collectorsNumber
= unit
.get("collector number").replaceAll("None", null);
122 } catch (Exception e
) {
124 try {String coll
=unit
.get("collector");
125 coll
=coll
.replaceAll("None", null);
126 this.gatheringAgentList
.add(coll
);
127 } catch (Exception e
) {
129 try {this.identificationList
.add(taxonName
+" "+author
);
130 } catch (Exception e
) {System
.out
.println(e
);
134 public boolean invoke(){
135 boolean result
= true;
136 boolean withCdm
= true;
137 CdmApplicationController app
= null;
138 TransactionStatus tx
= null;
140 app
= CdmApplicationController
.NewInstance(desitination
, hbm2dll
);
142 tx
= app
.startTransaction();
144 Reference sec
= ReferenceFactory
.newDatabase();
145 sec
.setTitleCache("SYNTHESYS CACHE DATA", true);
148 * SPECIMEN OR OBSERVATION OR LIVING
150 DerivedUnit derivedThing
= null;
152 if (this.recordBasis
!= null){
153 if (this.recordBasis
.toLowerCase().startsWith("s")) {//specimen
154 derivedThing
= DerivedUnit
.NewPreservedSpecimenInstance();
156 else if (this.recordBasis
.toLowerCase().startsWith("o")) {//observation
157 derivedThing
= DerivedUnit
.NewInstance(SpecimenOrObservationType
.Observation
);
159 else if (this.recordBasis
.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
160 derivedThing
= DerivedUnit
.NewInstance(SpecimenOrObservationType
.LivingSpecimen
);
163 if (derivedThing
== null){
164 derivedThing
= DerivedUnit
.NewInstance(SpecimenOrObservationType
.Observation
);
167 TaxonNameBase
<?
,?
> taxonName
= null;
169 DeterminationEvent determinationEvent
= null;
170 List
<TaxonNameBase
> names
= null;
171 NonViralNameParserImpl nvnpi
= NonViralNameParserImpl
.NewInstance();
172 String scientificName
="";
173 boolean preferredFlag
=false;
174 System
.out
.println(this.identificationList
);
175 for (int i
= 0; i
< this.identificationList
.size(); i
++) {
176 this.fullScientificNameString
= this.identificationList
.get(i
);
177 this.fullScientificNameString
= this.fullScientificNameString
.replaceAll(" et ", " & ");
178 if (this.fullScientificNameString
.indexOf("_preferred_") != -1){
179 scientificName
= this.fullScientificNameString
.split("_preferred_")[0];
180 String pTmp
= this.fullScientificNameString
.split("_preferred_")[1];
181 if (pTmp
== "1" || pTmp
.toLowerCase().indexOf("true") != -1) {
187 scientificName
= this.fullScientificNameString
;
190 // taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICZN(),null);
191 // if (taxonName.hasProblem()){
192 // System.out.println("pb ICZN");
193 // taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICBN(),null);
194 // if (taxonName.hasProblem()){
195 // System.out.println("pb ICBN");
196 // taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNB(), null);
197 // if (taxonName.hasProblem()){
198 // System.out.println("pb ICNB");
199 // taxonName = nvnpi.parseFullName(this.fullScientificNameString,NomenclaturalCode.ICNCP(), null);
200 // if (taxonName.hasProblem()){
201 // System.out.println("pb ICNCP");
206 taxonName
= nvnpi
.parseFullName(scientificName
);
208 names
= app
.getNameService().getNamesByName(scientificName
);
209 if (names
.size() == 0){
210 System
.out
.println("Name not found: " + scientificName
);
212 if (names
.size() > 1){
213 System
.out
.println("More then 1 name found: " + scientificName
);
215 System
.out
.println("Name found");
216 taxonName
= names
.get(0);
221 // tx = app.startTransaction();
222 app
.getNameService().saveOrUpdate(taxonName
);
223 taxon
= Taxon
.NewInstance(taxonName
, sec
); //TODO use real reference for sec
224 // app.commitTransaction(tx);
227 determinationEvent
= DeterminationEvent
.NewInstance();
228 determinationEvent
.setTaxon(taxon
);
229 determinationEvent
.setPreferredFlag(preferredFlag
);
230 derivedThing
.addDetermination(determinationEvent
);
234 //set catalogue number (unitID)
235 derivedThing
.setCatalogNumber(this.unitID
);
236 derivedThing
.setAccessionNumber(this.accessionNumber
);
237 derivedThing
.setCollectorsNumber(this.collectorsNumber
);
241 * INSTITUTION & COLLECTION
244 Institution institution
;
245 List
<Institution
> institutions
;
247 System
.out
.println(this.institutionCode
);
248 institutions
= app
.getAgentService().searchInstitutionByCode(this.institutionCode
);
250 System
.out
.println("BLI "+e
);
251 institutions
=new ArrayList
<Institution
>();
253 if (institutions
.size() ==0){
254 System
.out
.println("Institution (agent) unknown");
256 institution
= Institution
.NewInstance();
257 institution
.setCode(this.institutionCode
);
260 System
.out
.println("Institution (agent) already in the db");
261 institution
= institutions
.get(0);
265 Collection collection
= Collection
.NewInstance();
266 List
<Collection
> collections
;
268 collections
= app
.getCollectionService().searchByCode(this.collectionCode
);
269 //.searchCollectionByCode(this.collectionCode);
271 System
.out
.println("BLA"+e
);
272 collections
=new ArrayList
<Collection
>();
274 if (collections
.size() ==0){
275 System
.out
.println("Collection not found "+this.collectionCode
);
276 //create new collection
277 collection
.setCode(this.collectionCode
);
278 collection
.setCodeStandard("GBIF");
279 collection
.setInstitute(institution
);
282 boolean collectionFound
=false;
283 for (int i
=0; i
<collections
.size(); i
++){
284 collection
= collections
.get(i
);
286 if (collection
.getInstitute().getCode().equalsIgnoreCase(institution
.getCode())){
287 //found a collection with the same code and the same institution
288 collectionFound
=true;
290 } catch (NullPointerException e
) {}
292 System
.out
.println("a trouvé la collection avec la meme institution? "+collectionFound
);
293 if (!collectionFound
){ //need to add a new collection with the pre-configured institution
294 collection
.setCode(this.collectionCode
);
295 collection
.setCodeStandard("GBIF");
296 collection
.setInstitute(institution
);
300 System
.out
.println("collection inserted");
301 //link specimen & collection
302 derivedThing
.setCollection(collection
);
307 //create gathering event
308 GatheringEvent gatheringEvent
= GatheringEvent
.NewInstance();
310 Language language
= Language
.DEFAULT();
311 LanguageString loc
= LanguageString
.NewInstance(this.locality
,language
);
312 gatheringEvent
.setLocality(loc
);
314 //create coordinates point
315 Point coordinates
= Point
.NewInstance();
317 coordinates
.setLatitude(this.latitude
);
318 coordinates
.setLongitude(this.longitude
);
319 gatheringEvent
.setExactLocation(coordinates
);
321 NamedArea area
= NamedArea
.NewInstance();
324 Country country
= null;
325 // System.out.println("isocountry "+this.isocountry);
326 if (this.isocountry
!= null) {
327 country
= app
.getOccurrenceService().getCountryByIso(this.isocountry
);
330 // System.out.println(country.getLabel());
331 // Set<Continent> cont = country.getContinents();
333 // System.out.println(cont.size());
334 // Iterator<Continent> iter = cont.iterator();
335 // while (iter.hasNext())
336 // System.out.println(iter.next().toString());
338 if (country
!= null){
339 area
.addCountry(country
);
340 System
.out
.println("country not null!");
343 // if (this.country != null){
344 // List<Country>countries = app.getOccurrenceService().getCountryByName(this.country);
345 // if (countries.size() >0)
346 // area.addCountry(countries.get(0));
348 // System.out.println("NO COUNTRY");//TODO need to add a new country!
351 // app.getTermService().saveTerm(area);
352 gatheringEvent
.addCollectingArea(area
);
356 ListIterator
<String
> collectors
= this.gatheringAgentList
.listIterator();
359 while (collectors
.hasNext()){
360 collName
= collectors
.next();
361 /*check if the collector does already exist*/
363 Pager
<AgentBase
> col
= app
.getAgentService().findByTitle(null, collName
, null, null, null, null, null, null);
364 collector
=col
.getRecords().get(0);
365 System
.out
.println("a trouve l'agent");
366 }catch (Exception e
) {
367 collector
= Person
.NewInstance();
368 collector
.setTitleCache(collName
, true);
370 gatheringEvent
.setCollector(collector
);
373 //create field/observation
374 FieldUnit fieldUnit
= FieldUnit
.NewInstance();
376 fieldUnit
.setFieldNumber(this.fieldNumber
);
378 //join gatheringEvent to fieldObservation
379 fieldUnit
.setGatheringEvent(gatheringEvent
);
382 // //link fieldObservation and specimen
383 DerivationEvent derivationEvent
= DerivationEvent
.NewInstance();
384 derivationEvent
.addOriginal(fieldUnit
);
385 derivedThing
.addDerivationEvent(derivationEvent
);
386 // derivationEvent.addDerivative(derivedThing);
389 * SAVE AND STORE DATA
391 //save the specimen data
392 // app.getOccurrenceService().saveSpecimenOrObservationBase(fieldObservation);
394 // tx = app.startTransaction();
395 app
.getTermService().saveOrUpdate(area
);//save it sooner
396 app
.getOccurrenceService().saveOrUpdate(derivedThing
);
397 // app.commitTransaction(tx);
399 } catch (Exception e
) {
400 // TODO Auto-generated catch block
401 System
.out
.println("PATATE "+e
);
405 logger
.info("saved new specimen ...");
409 } catch (Exception e
) {
410 logger
.warn("Error when reading record!!");
415 app
.commitTransaction(tx
);
416 System
.out
.println("commit done");
424 private DeterminationEvent
getDetermination(Taxon taxon
, String actor
){
425 logger
.info("Create determination event");
426 DeterminationEvent determinationEvent
= DeterminationEvent
.NewInstance();
427 determinationEvent
.setTaxon(taxon
);
428 Person person
= Person
.NewTitledInstance(actor
);
429 determinationEvent
.setActor(person
);
430 return determinationEvent
;
437 * @throws URISyntaxException
439 public static void main(String
[] args
) throws URISyntaxException
{
440 URI uri
= CdmImportSources
.SYNTHESYS_SPECIMEN();
441 logger
.info("main method");
442 SynthesysCacheActivator abcdAct
= new SynthesysCacheActivator();
444 ArrayList
<HashMap
<String
, String
>> units
;
446 units
= ExcelUtils
.parseXLS(uri
);
447 } catch (FileNotFoundException e
) {
448 logger
.error("FileNotFound: " + uri
);
451 HashMap
<String
,String
> unit
=null;
452 for (int i
=0; i
<units
.size();i
++){
455 abcdAct
.saveUnit(unit
);//and then invoke