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