ABCD & SYNTHESYS import - minor changes
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / abcd206 / AbcdIO.java
1 package eu.etaxonomy.cdm.io.abcd206;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10
11 import org.apache.log4j.Logger;
12 import org.springframework.transaction.TransactionStatus;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17
18 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
19 import eu.etaxonomy.cdm.database.DbSchemaValidation;
20 import eu.etaxonomy.cdm.io.common.ICdmIO;
21 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
22 import eu.etaxonomy.cdm.model.agent.Institution;
23 import eu.etaxonomy.cdm.model.location.NamedArea;
24 import eu.etaxonomy.cdm.model.media.Media;
25 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
26 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
27 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
28 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29 import eu.etaxonomy.cdm.model.occurrence.Collection;
30 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
31 import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
32 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
33 import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
34 import eu.etaxonomy.cdm.model.occurrence.LivingBeing;
35 import eu.etaxonomy.cdm.model.occurrence.Observation;
36 import eu.etaxonomy.cdm.model.occurrence.Specimen;
37 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
38 import eu.etaxonomy.cdm.model.reference.Generic;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40 import eu.etaxonomy.cdm.model.taxon.Taxon;
41 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
42
43 public class AbcdIO extends SpecimenIoBase implements ICdmIO {
44
45
46 private static final Logger logger = Logger.getLogger(AbcdIO.class);
47
48 protected String fullScientificNameString;
49 protected String nomenclatureCode;
50 protected String institutionCode;
51 protected String collectionCode;
52 protected String unitID;
53 protected String recordBasis;
54 protected String accessionNumber;
55 protected String collectorsNumber;
56 protected String fieldNumber;
57 protected Double longitude;
58 protected Double latitude;
59 protected String locality;
60 protected String languageIso = null;
61 protected String country;
62 protected String isocountry;
63 protected int depth;
64 protected int altitude;
65 protected ArrayList<String> gatheringAgentList;
66 protected ArrayList<String> identificationList;
67 protected ArrayList<String> namedAreaList;
68 protected ArrayList<String> referenceList;
69 protected ArrayList<String> multimediaObjects;
70
71
72 protected ArrayList<String> knownABCDelements = new ArrayList<String>();
73
74
75 public AbcdIO() {
76 super();
77 knownABCDelements.add("Identifications");
78 knownABCDelements.add("Identification");
79 knownABCDelements.add("Result");
80 knownABCDelements.add("TaxonIdentified");
81 knownABCDelements.add("ScientificName");
82 knownABCDelements.add("FullScientificNameString");
83 knownABCDelements.add("NameAtomised");
84 knownABCDelements.add("SourceInstitutionID");
85 knownABCDelements.add("SourceID");
86 knownABCDelements.add("UnitID");
87 knownABCDelements.add("RecordBasis");
88 knownABCDelements.add("AccessionNumber");
89 knownABCDelements.add("LocalityText");
90 knownABCDelements.add("LongitudeDecimal");
91 knownABCDelements.add("Country");
92 knownABCDelements.add("ISO3166Code");
93 knownABCDelements.add("CollectorsFieldNumber");
94 knownABCDelements.add("CollectorsNumber");
95 knownABCDelements.add("AccessionNumber");
96 knownABCDelements.add("Altitude_MeasurementOrFactText");
97 knownABCDelements.add("Depth");
98 knownABCDelements.add("NamedArea_AreaName");
99 knownABCDelements.add("GatheringAgent_Person_FullName");
100 }
101
102 /*
103 * Return the list of root nodes for an ABCD 2.06 XML file
104 * @param fileName: the file's location
105 * @return the list of root nodes ("Unit")
106 */
107 private static NodeList getUnitsNodeList(String fileName){
108 NodeList unitList = null;
109 try {
110 DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
111 DocumentBuilder constructeur = fabrique.newDocumentBuilder();
112 File xml = new File(fileName);
113 Document document = constructeur.parse(xml);
114 Element racine = document.getDocumentElement();
115 unitList = racine.getElementsByTagName("Unit");
116 }catch(Exception e){
117 System.out.println(e);
118 }
119 return unitList;
120 }
121
122
123 public void afficherInfos(Node noeud, int niv) {
124 short type = noeud.getNodeType();
125 String nom = noeud.getNodeName();
126 String valeur = noeud.getNodeValue();
127
128 indenter(niv, type == Node.TEXT_NODE);
129 if(!knownABCDelements.contains(nom)){
130 System.out.print(nom + " (" + type + ") = '");
131 if(valeur != null && !valeur.matches("^\\s+$")){
132 System.out.print(valeur);
133 System.out.println("'");
134 }
135 }
136 if ((type == Node.DOCUMENT_NODE
137 || type == Node.ELEMENT_NODE)
138 && noeud.hasChildNodes()) {
139 NodeList liste = noeud.getChildNodes();
140 for(int i = 0; i < liste.getLength(); i++)
141 afficherInfos(liste.item(i), niv + 1);
142 }
143 }
144 public void indenter(int n, boolean texte){
145 String tab = "\t";
146 for(int i = 0; i < n; i++){
147 System.out.print(tab);
148 }
149 if(texte){
150 System.out.print(" - ");
151 }
152 else
153 System.out.print(" + ");
154 }
155
156 /*
157 * Store the unit's properties into variables
158 * Look which unit is the preferred one
159 * Look what kind of name it is supposed to be, for the parsing (Botanical, Zoological)
160 * @param racine: the root node for a single unit
161 */
162 private void setUnitPropertiesXML(Element racine){
163 try{
164 NodeList group,childs,person;
165
166 // try{afficherInfos(racine, 0);}catch (Exception e) {System.out.println(e);}
167 group = racine.getChildNodes();
168 // logger.info("ABCD ELEMENT not stored: "+group.item(i).getNodeName().toString()+" - value: "+group.item(i).getTextContent());
169 for (int i=0; i< group.getLength(); i++){
170 if (group.item(i).getNodeName() == "Identifications"){
171 group = group.item(i).getChildNodes();
172 break;
173 }
174 }
175 this.identificationList = new ArrayList<String>();
176 this.referenceList = new ArrayList<String>();
177 this.multimediaObjects = new ArrayList<String>();
178
179 this.getScientificNames(group);
180
181 // System.out.println("this.identificationList "+this.identificationList.toString());
182 this.getIDs(racine);
183 this.getRecordBasis(racine);
184 this.getMultimedia(racine);
185 this.getNumbers(racine);
186 this.getGeolocation(racine);
187 this.getGatheringPeople(racine);
188
189 } catch (Exception e) {
190 logger.info("Error occured while parsing XML file"+e);
191 }
192 }
193
194
195 private void getScientificNames(NodeList group){
196 NodeList identifications,results;
197 String tmpName = null;
198 for (int j=0; j< group.getLength(); j++){
199 if(group.item(j).getNodeName() == "Identification"){
200 this.nomenclatureCode ="";
201 identifications = group.item(j).getChildNodes();
202 for (int m=0; m<identifications.getLength();m++){
203 if(identifications.item(m).getNodeName() == "Result"){
204 results = identifications.item(m).getChildNodes();
205 for(int k=0; k<results.getLength();k++)
206 if (results.item(k).getNodeName() == "TaxonIdentified")
207 tmpName=this.getScientificName(results.item(k));
208 }
209 else if(identifications.item(m).getNodeName() == "PreferredFlag")
210 this.identificationList.add(tmpName+"_preferred_"+identifications.item(m).getTextContent()+"_code_"+this.nomenclatureCode);
211
212 else if (identifications.item(m).getNodeName() == "References")
213 this.getReferences(identifications.item(m));
214 else
215 if (tmpName != null)
216 this.identificationList.add(tmpName+"_preferred_"+"0"+"_code_"+this.nomenclatureCode);
217 }
218 }
219 }
220 }
221
222 private void getReferences(Node result){
223 NodeList results,reference;
224 results = result.getChildNodes();
225 for(int k=0; k<results.getLength();k++){
226 if (results.item(k).getNodeName() == "Reference"){
227 reference = results.item(k).getChildNodes();
228 for(int l=0;l<reference.getLength();l++){
229 if (reference.item(l).getNodeName()=="TitleCitation")
230 referenceList.add(reference.item(l).getTextContent());
231 }
232 }
233 }
234 }
235
236 private String getScientificName(Node result){
237 NodeList taxonsIdentified, scnames;
238 String tmpName = "";
239 taxonsIdentified = result.getChildNodes();
240 for (int l=0; l<taxonsIdentified.getLength(); l++){
241 if (taxonsIdentified.item(l).getNodeName() == "ScientificName"){
242 scnames = taxonsIdentified.item(l).getChildNodes();
243 for (int n=0;n<scnames.getLength();n++){
244 if (scnames.item(n).getNodeName() == "FullScientificNameString")
245 tmpName = scnames.item(n).getTextContent();
246 if (scnames.item(n).getNodeName() == "NameAtomised"){
247 try {
248 if (scnames.item(n).hasChildNodes()){
249
250 this.nomenclatureCode = scnames.item(n).getChildNodes().item(1).getNodeName();
251 }
252 } catch (Exception e) {
253 this.nomenclatureCode ="";
254 }
255 }
256 }
257 }
258 }
259 return tmpName;
260 }
261 private void getIDs(Element racine){
262 NodeList group;
263 try {
264 group = racine.getElementsByTagName("SourceInstitutionID");
265 this.institutionCode = group.item(0).getTextContent();
266 } catch (NullPointerException e) {
267 this.institutionCode= "";
268 }
269 try {
270 group = racine.getElementsByTagName("SourceID");
271 this.collectionCode = group.item(0).getTextContent();
272 } catch (NullPointerException e) {
273 this.collectionCode = "";
274 }
275 try {
276 group = racine.getElementsByTagName("UnitID");
277 this.unitID = group.item(0).getTextContent();
278 } catch (NullPointerException e) {
279 this.unitID = "";
280 }
281 }
282
283 private void getRecordBasis(Element racine){
284 NodeList group;
285 try {
286 group = racine.getElementsByTagName("RecordBasis");
287 this.recordBasis = group.item(0).getTextContent();
288 } catch (NullPointerException e) {
289 this.recordBasis = "";
290 }
291 }
292
293 private void getMultimedia(Element racine){
294 NodeList group, multimedias, multimedia;
295 try {
296 group = racine.getElementsByTagName("MultiMediaObjects");
297 for(int i=0;i<group.getLength();i++){
298 multimedias = group.item(i).getChildNodes();
299 for (int j=0;j<multimedias.getLength();j++){
300 if (multimedias.item(j).getNodeName() == "MultiMediaObject"){
301 multimedia = multimedias.item(j).getChildNodes();
302 for (int k=0;k<multimedia.getLength();k++){
303 if(multimedia.item(k).getNodeName() == "FileURI")
304 this.multimediaObjects.add(multimedia.item(k).getTextContent());
305 }
306 }
307 }
308 }
309 } catch (NullPointerException e) {
310 System.out.println(e);
311 }
312 }
313
314 private void getNumbers(Element racine){
315 NodeList group;
316 try {
317 group = racine.getElementsByTagName("AccessionNumber");
318 this.accessionNumber = group.item(0).getTextContent();
319 } catch (NullPointerException e) {
320 this.accessionNumber = "";
321 }
322 try {
323 group = racine.getElementsByTagName("CollectorsFieldNumber");
324 this.fieldNumber = group.item(0).getTextContent();
325 } catch (NullPointerException e) {
326 this.fieldNumber = "";
327 }
328
329 try {
330 group = racine.getElementsByTagName("CollectorsNumber");
331 this.collectorsNumber = group.item(0).getTextContent();
332 } catch (NullPointerException e) {
333 this.collectorsNumber = "";
334 }
335
336 try {
337 group = racine.getElementsByTagName("AccessionNumber");
338 this.accessionNumber = group.item(0).getTextContent();
339 } catch (NullPointerException e) {
340 this.accessionNumber = "";
341 }
342 }
343
344 private void getGeolocation(Element racine){
345 NodeList group, childs;
346 try {
347 group = racine.getElementsByTagName("LocalityText");
348 this.locality = group.item(0).getTextContent();
349 if (group.item(0).hasAttributes())
350 if (group.item(0).getAttributes().getNamedItem("lang") != null)
351 this.languageIso = group.item(0).getAttributes().getNamedItem("lang").getTextContent();
352 } catch (NullPointerException e) {
353 this.locality = "";
354 }
355 try {
356 group = racine.getElementsByTagName("LongitudeDecimal");
357 this.longitude = Double.valueOf(group.item(0).getTextContent());
358 } catch (NullPointerException e) {
359 this.longitude=0.0;
360 }
361 try {
362 group = racine.getElementsByTagName("LatitudeDecimal");
363 this.latitude = Double.valueOf(group.item(0).getTextContent());
364 } catch (NullPointerException e) {
365 this.latitude=0.0;
366 }
367 try {
368 group = racine.getElementsByTagName("Country");
369 this.country = group.item(0).getTextContent();
370 } catch (NullPointerException e) {
371 this.country = "";
372 }
373 try {
374 group = racine.getElementsByTagName("ISO3166Code");
375 this.isocountry = group.item(0).getTextContent();
376 } catch (NullPointerException e) {
377 this.isocountry = "";
378 }
379 try {
380 group = racine.getElementsByTagName("Altitude");
381 for (int i=0;i<group.getLength();i++){
382 childs = group.item(i).getChildNodes();
383 for (int j=0;j<childs.getLength();j++){
384 if (childs.item(j).getNodeName() == "MeasurementOrFactText")
385 this.altitude = Integer.valueOf(childs.item(j).getTextContent());
386 }
387 }
388 } catch (NullPointerException e) {
389 this.altitude = -9999;
390 }
391
392 try {
393 group = racine.getElementsByTagName("Depth");
394 this.depth = Integer.valueOf(group.item(0).getTextContent());
395 } catch (NullPointerException e) {
396 this.depth = -9999;
397 }
398
399 try{
400 group = racine.getElementsByTagName("NamedArea");
401 this.namedAreaList = new ArrayList<String>();
402 for (int i=0;i<group.getLength();i++){
403 childs = group.item(i).getChildNodes();
404 for (int j=0; j<childs.getLength();j++){
405 if (childs.item(j).getNodeName() == "AreaName")
406 this.namedAreaList.add(childs.item(j).getTextContent());
407 }
408 }
409 }catch(NullPointerException e){
410 this.namedAreaList = new ArrayList<String>();
411 }
412 }
413
414 private void getGatheringPeople(Element racine){
415 NodeList group, childs, person;
416 try {
417 group = racine.getElementsByTagName("GatheringAgent");
418 this.gatheringAgentList = new ArrayList<String>();
419 for (int i=0; i< group.getLength(); i++){
420 childs = group.item(i).getChildNodes();
421 for (int j=0; j<childs.getLength();j++){
422 if (childs.item(j).getNodeName() == "Person"){
423 person = childs.item(j).getChildNodes();
424 for (int k=0; k<person.getLength(); k++)
425 if (person.item(k).getNodeName() == "FullName")
426 this.gatheringAgentList.add(person.item(k).getTextContent());
427 }
428
429 }
430 }
431 } catch (NullPointerException e) {
432 this.gatheringAgentList = new ArrayList<String>();
433 }
434 }
435
436 private Institution getInstitution(String institutionCode, SpecimenImportConfigurator config){
437 Institution institution;
438 List<Institution> institutions;
439 try{
440 System.out.println(this.institutionCode);
441 institutions= config.getCdmAppController().getAgentService().searchInstitutionByCode(this.institutionCode);
442 }catch(Exception e){
443 System.out.println("BLI "+e);
444 institutions=new ArrayList<Institution>();
445 }
446 if (institutions.size() ==0 || !config.getReUseExistingMetadata()){
447 System.out.println("Institution (agent) unknown or not allowed to reuse existing metadata");
448 //create institution
449 institution = Institution.NewInstance();
450 institution.setCode(this.institutionCode);
451 }
452 else{
453 System.out.println("Institution (agent) already in the db");
454 institution = institutions.get(0);
455 }
456 return institution;
457 }
458
459 /*
460 * Look if the Collection does already exists
461 * @param collectionCode: a string
462 * @param institution: the current Institution
463 * @param app
464 * @return the Collection (existing or new)
465 */
466 private Collection getCollection(String collectionCode, Institution institution, SpecimenImportConfigurator config){
467 Collection collection = Collection.NewInstance();
468 List<Collection> collections;
469 try{
470 collections = config.getCdmAppController().getOccurrenceService().searchCollectionByCode(this.collectionCode);
471 }catch(Exception e){
472 collections=new ArrayList<Collection>();
473 }
474 if (collections.size() ==0 || !config.getReUseExistingMetadata()){
475 System.out.println("Collection not found or do not reuse existing metadata "+this.collectionCode);
476 //create new collection
477 collection.setCode(this.collectionCode);
478 collection.setCodeStandard("GBIF");
479 collection.setInstitute(institution);
480 }
481 else{
482 boolean collectionFound=false;
483 for (int i=0; i<collections.size(); i++){
484 collection = collections.get(i);
485 try {
486 if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){
487 //found a collection with the same code and the same institution
488 collectionFound=true;
489 }
490 } catch (NullPointerException e) {}
491 }
492 if (!collectionFound){
493 collection.setCode(this.collectionCode);
494 collection.setCodeStandard("GBIF");
495 collection.setInstitute(institution);
496 }
497
498 }
499 return collection;
500 }
501
502 /*
503 *
504 * @param app
505 * @param derivedThing
506 * @param sec
507 */
508 private void setTaxonNameBase(SpecimenImportConfigurator config, DerivedUnitBase derivedThing, ReferenceBase sec){
509 TaxonNameBase taxonName = null;
510 String fullScientificNameString;
511 Taxon taxon = null;
512 DeterminationEvent determinationEvent = null;
513 List<TaxonNameBase> names = null;
514
515 String scientificName="";
516 boolean preferredFlag=false;
517
518 for (int i = 0; i < this.identificationList.size(); i++) {
519 fullScientificNameString = this.identificationList.get(i);
520 fullScientificNameString = fullScientificNameString.replaceAll(" et ", " & ");
521 if (fullScientificNameString.indexOf("_preferred_") != -1){
522 scientificName = fullScientificNameString.split("_preferred_")[0];
523 String pTmp = fullScientificNameString.split("_preferred_")[1].split("_code_")[0];
524 if (pTmp == "1" || pTmp.toLowerCase().indexOf("true") != -1)
525 preferredFlag=true;
526 else
527 preferredFlag=false;
528 }
529 else scientificName = fullScientificNameString;
530
531 if (fullScientificNameString.indexOf("_code_") != -1)
532 this.nomenclatureCode = fullScientificNameString.split("_code_")[1];
533
534 if (config.getDoAutomaticParsing())
535 taxonName = this.parseScientificName(scientificName);
536 else taxonName.setTitleCache(scientificName);
537
538 if (true){
539 names = config.getCdmAppController().getNameService().getNamesByName(scientificName);
540 if (names.size() == 0){
541 System.out.println("Name not found: " + scientificName);
542 }else{
543 if (names.size() > 1){
544 System.out.println("More then 1 name found: " + scientificName);
545 }
546 System.out.println("Name found");
547 taxonName = names.get(0);
548 }
549 }
550
551 config.getCdmAppController().getNameService().saveTaxonName(taxonName);
552 taxon = Taxon.NewInstance(taxonName, sec); //sec set null
553
554 determinationEvent = DeterminationEvent.NewInstance();
555 determinationEvent.setTaxon(taxon);
556 determinationEvent.setPreferredFlag(preferredFlag);
557 for (int l=0;l<this.referenceList.size();l++){
558 ReferenceBase reference = new Generic();
559 reference.setTitleCache(this.referenceList.get(l));
560 determinationEvent.addReference(reference);
561 }
562 derivedThing.addDetermination(determinationEvent);
563 }
564
565 }
566
567 private TaxonNameBase parseScientificName(String scientificName){
568 System.out.println("scientificName");
569 TaxonNameBase taxonName = null;
570 NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
571
572 System.out.println("nomenclature: "+this.nomenclatureCode);
573 if (this.nomenclatureCode == "Zoological"){
574 taxonName = nvnpi.parseFullName(scientificName,NomenclaturalCode.ICZN(),null);
575 if (taxonName.hasProblem())
576 System.out.println("pb ICZN");}
577 if (this.nomenclatureCode == "Botanical"){
578 taxonName = nvnpi.parseFullName(scientificName,NomenclaturalCode.ICBN(),null);
579 if (taxonName.hasProblem())
580 System.out.println("pb ICBN");}
581 if (this.nomenclatureCode == "Bacterial"){
582 taxonName = nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNB(), null);
583 if (taxonName.hasProblem())
584 System.out.println("pb ICNB");
585 }
586 if (this.nomenclatureCode == "Cultivar"){
587 taxonName = nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNCP(), null);
588 if (taxonName.hasProblem())
589 System.out.println("pb ICNCP");
590 }
591 if (this.nomenclatureCode == "Viral"){
592 taxonName = nvnpi.parseFullName(scientificName,NomenclaturalCode.ICVCN(), null);
593 if (taxonName.hasProblem())
594 System.out.println("pb ICVCN");
595 }
596 try{taxonName.hasProblem();}
597 catch (Exception e) {
598 taxonName = nvnpi.parseFullName(scientificName);
599 }
600 if (taxonName.hasProblem())
601 taxonName = nvnpi.parseFullName(scientificName);
602 return taxonName;
603 }
604 /*
605 * Store the unit with its Gathering informations in the CDM
606 */
607 public boolean start(SpecimenImportConfigurator config){
608 boolean result = true;
609 boolean withCdm = true;
610 CdmApplicationController app = null;
611 TransactionStatus tx = null;
612
613 app = config.getCdmAppController();
614 // try {
615 // app = CdmApplicationController.NewInstance(config.getDestination(), config.getDbSchemaValidation());
616 // } catch (DataSourceNotFoundException e1) {
617 // e1.printStackTrace();
618 // System.out.println("DataSourceNotFoundException "+e1);
619 // } catch (TermNotFoundException e1) {
620 // e1.printStackTrace();
621 // System.out.println("TermNotFoundException " +e1);
622 // }
623
624 tx = app.startTransaction();
625 try {
626 // ReferenceBase sec = Database.NewInstance();
627 // sec.setTitleCache("XML DATA");
628 ReferenceBase sec = null;
629
630 /**
631 * SPECIMEN OR OBSERVATION OR LIVING
632 */
633 DerivedUnitBase derivedThing = null;
634 //create specimen
635 if (this.recordBasis != null){
636 if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
637 derivedThing = Specimen.NewInstance();
638 }
639 else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
640 derivedThing = Observation.NewInstance();
641 }
642 else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
643 derivedThing = LivingBeing.NewInstance();
644 }
645 }
646 if (derivedThing == null)
647 derivedThing = Observation.NewInstance();
648
649 this.setTaxonNameBase(config, derivedThing, sec);
650
651
652 //set catalogue number (unitID)
653 derivedThing.setCatalogNumber(this.unitID);
654 derivedThing.setAccessionNumber(this.accessionNumber);
655 derivedThing.setCollectorsNumber(this.collectorsNumber);
656
657
658 /**
659 * INSTITUTION & COLLECTION
660 */
661 //manage institution
662 Institution institution = this.getInstitution(this.institutionCode,config);
663 //manage collection
664 Collection collection = this.getCollection(this.collectionCode, institution, config);
665 //link specimen & collection
666 derivedThing.setCollection(collection);
667
668 /**
669 * GATHERING EVENT
670 */
671
672 UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(config, this.locality, this.languageIso, this.longitude,
673 this.latitude, this.gatheringAgentList);
674 UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea(this.isocountry, this.country, config);
675 NamedArea areaCountry = unitsGatheringArea.getArea();
676 unitsGatheringEvent.addArea(areaCountry);
677 unitsGatheringArea = new UnitsGatheringArea(this.namedAreaList);
678 ArrayList<NamedArea> nas = unitsGatheringArea.getAreas();
679 for (int i=0; i<nas.size();i++)
680 unitsGatheringEvent.addArea(nas.get(i));
681
682
683 //create field/observation
684 FieldObservation fieldObservation = FieldObservation.NewInstance();
685 //add fieldNumber
686 fieldObservation.setFieldNumber(this.fieldNumber);
687 //join gatheringEvent to fieldObservation
688 fieldObservation.setGatheringEvent(unitsGatheringEvent.getGatheringEvent());
689 // //add Multimedia URLs
690 if(this.multimediaObjects.size()>0){
691 MediaRepresentationPart part;
692 MediaRepresentation representation;
693 Media media;
694 for (int i=0;i<this.multimediaObjects.size();i++){
695 part= MediaRepresentationPart.NewInstance(this.multimediaObjects.get(i),0);
696 //TODO update the Multimedia Object without size :)
697 representation = MediaRepresentation.NewInstance();
698 representation.addRepresentationPart(part);
699 media = Media.NewInstance();
700 media.addRepresentation(representation);
701 fieldObservation.addMedia(media);
702 }
703 }
704 // //link fieldObservation and specimen
705 DerivationEvent derivationEvent = DerivationEvent.NewInstance();
706 derivationEvent.addOriginal(fieldObservation);
707 derivedThing.addDerivationEvent(derivationEvent);
708
709 /**
710 * SAVE AND STORE DATA
711 */
712
713 app.getTermService().saveTerm(areaCountry);//save it sooner
714 for (int i=0; i<nas.size();i++)
715 app.getTermService().saveTerm(nas.get(i));//save it sooner (foreach area)
716 app.getTermService().saveLanguageData(unitsGatheringEvent.getLocality());//save it sooner
717 app.getOccurrenceService().saveSpecimenOrObservationBase(derivedThing);
718
719 logger.info("saved new specimen ...");
720
721
722 } catch (Exception e) {
723 logger.warn("Error when reading record!!");
724 e.printStackTrace();
725 result = false;
726 }
727 app.commitTransaction(tx);
728 System.out.println("commit done");
729 app.close();
730 return result;
731 }
732
733
734 public boolean invoke(SpecimenImportConfigurator config){
735 System.out.println("INVOKE Specimen Import from ABCD2.06 XML File");
736 AbcdIO test = new AbcdIO();
737 String sourceName = config.getSourceNameString();
738 NodeList unitsList = getUnitsNodeList(sourceName);
739 if (unitsList != null)
740 {
741 System.out.println("nb units to insert: "+unitsList.getLength());
742 for (int i=0;i<unitsList.getLength();i++){
743 test.setUnitPropertiesXML((Element)unitsList.item(i));
744 test.start(config);
745 config.setDbSchemaValidation(DbSchemaValidation.UPDATE);
746 }
747 }
748
749 return false;
750
751 }
752
753
754 public boolean invoke(IImportConfigurator config, Map stores) {
755 System.out.println("invoke de ABCDio");
756 invoke((SpecimenImportConfigurator)config);
757 return false;
758 }
759
760
761
762
763 }