Committing large number of changes relating to versioning implementation (#108) ...
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / abcd206 / AbcdIO.java
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.abcd206;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.net.URL;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.parsers.ParserConfigurationException;
25
26 import org.apache.log4j.Logger;
27 import org.springframework.stereotype.Component;
28 import org.springframework.transaction.TransactionStatus;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.w3c.dom.Node;
32 import org.w3c.dom.NodeList;
33 import org.xml.sax.SAXException;
34
35 import eu.etaxonomy.cdm.common.MediaMetaData;
36 import eu.etaxonomy.cdm.common.MediaMetaData.ImageMetaData;
37 import eu.etaxonomy.cdm.database.DbSchemaValidation;
38 import eu.etaxonomy.cdm.io.common.ICdmIO;
39 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
40 import eu.etaxonomy.cdm.io.common.MapWrapper;
41 import eu.etaxonomy.cdm.model.agent.Institution;
42 import eu.etaxonomy.cdm.model.agent.Team;
43 import eu.etaxonomy.cdm.model.common.CdmBase;
44 import eu.etaxonomy.cdm.model.location.NamedArea;
45 import eu.etaxonomy.cdm.model.media.ImageFile;
46 import eu.etaxonomy.cdm.model.media.Media;
47 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
48 import eu.etaxonomy.cdm.model.name.BacterialName;
49 import eu.etaxonomy.cdm.model.name.BotanicalName;
50 import eu.etaxonomy.cdm.model.name.CultivarPlantName;
51 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
52 import eu.etaxonomy.cdm.model.name.NonViralName;
53 import eu.etaxonomy.cdm.model.name.Rank;
54 import eu.etaxonomy.cdm.model.name.ZoologicalName;
55 import eu.etaxonomy.cdm.model.occurrence.Collection;
56 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
57 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
58 import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
59 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
60 import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
61 import eu.etaxonomy.cdm.model.occurrence.LivingBeing;
62 import eu.etaxonomy.cdm.model.occurrence.Observation;
63 import eu.etaxonomy.cdm.model.occurrence.Specimen;
64 import eu.etaxonomy.cdm.model.reference.Generic;
65 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
66 import eu.etaxonomy.cdm.model.taxon.Taxon;
67 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
68 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
69
70
71 /**
72 * @author p.kelbert
73 * @created 20.10.2008
74 * @version 1.0
75 */
76 @Component
77 public class AbcdIO extends SpecimenIoBase implements ICdmIO<IImportConfigurator> {
78 private static final Logger logger = Logger.getLogger(AbcdIO.class);
79
80 protected String fullScientificNameString;
81 protected String atomisedStr;
82 protected String nomenclatureCode;
83 protected String institutionCode;
84 protected String collectionCode;
85 protected String unitID;
86 protected String recordBasis;
87 protected String accessionNumber;
88 protected String collectorsNumber;
89 protected String fieldNumber;
90 protected Double longitude;
91 protected Double latitude;
92 protected String locality;
93 protected String languageIso = null;
94 protected String country;
95 protected String isocountry;
96 protected int depth;
97 protected int altitude;
98 protected ArrayList<String> gatheringAgentList;
99 protected ArrayList<String> identificationList;
100 protected ArrayList<HashMap<String, String>> atomisedIdentificationList;
101 protected ArrayList<String> namedAreaList;
102 protected ArrayList<String> referenceList;
103 protected ArrayList<String> multimediaObjects;
104
105
106 protected ArrayList<String> knownABCDelements = new ArrayList<String>();
107 protected HashMap<String,String> allABCDelements = new HashMap<String,String>();
108
109
110 public AbcdIO() {
111 super();
112 }
113
114
115 @Override
116 protected boolean doCheck(IImportConfigurator config) {
117 logger.warn("Checking not yet implemented for AbcdIO.class");
118 return true;
119 }
120
121 /*
122 * Return the list of root nodes for an ABCD 2.06 XML file
123 * @param fileName: the file's location
124 * @return the list of root nodes ("Unit")
125 */
126 private static NodeList getUnitsNodeList(String urlFileName){
127 NodeList unitList = null;
128 try {
129 DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
130 DocumentBuilder constructeur = fabrique.newDocumentBuilder();
131 URL url = new URL(urlFileName);
132 Object o = url.getContent();
133 InputStream is = (InputStream)o;
134 Document document = constructeur.parse(is);
135 Element racine = document.getDocumentElement();
136 unitList = racine.getElementsByTagName("Unit");
137
138 }catch(Exception e){
139 logger.warn(e);
140 }
141 return unitList;
142 }
143
144
145 /*
146 * Store the unit's properties into variables
147 * Look which unit is the preferred one
148 * Look what kind of name it is supposed to be, for the parsing (Botanical, Zoological)
149 * @param racine: the root node for a single unit
150 */
151 private void setUnitPropertiesXML(Element racine){
152 try{
153 NodeList group;
154
155 // try{afficherInfos(racine, 0);}catch (Exception e) {logger.info(e);}
156 group = racine.getChildNodes();
157 // logger.info("ABCD ELEMENT not stored: "+group.item(i).getNodeName().toString()+" - value: "+group.item(i).getTextContent());
158 for (int i=0; i< group.getLength(); i++){
159 if (group.item(i).getNodeName().equals("Identifications")){
160 group = group.item(i).getChildNodes();
161 break;
162 }
163 }
164 this.identificationList = new ArrayList<String>();
165 this.atomisedIdentificationList = new ArrayList<HashMap<String, String>>();
166 this.referenceList = new ArrayList<String>();
167 this.multimediaObjects = new ArrayList<String>();
168
169 this.getScientificNames(group);
170
171 // logger.info("this.identificationList "+this.identificationList.toString());
172 this.getIDs(racine);
173 this.getRecordBasis(racine);
174 this.getMultimedia(racine);
175 this.getNumbers(racine);
176 this.getGeolocation(racine);
177 this.getGatheringPeople(racine);
178
179 } catch (Exception e) {
180 logger.info("Error occured while parsing XML file"+e);
181 }
182 }
183
184 String path= "";
185 private void getHierarchie(Node node){
186 while (node != null && node.getNodeName() != "DataSets"){
187 // logger.info(node.getParentNode().getNodeName());
188 path = node.getParentNode().getNodeName()+"/"+path;
189 node = node.getParentNode();
190 }
191 // logger.info("path: "+path);
192 }
193
194 private void getScientificNames(NodeList group){
195 NodeList identifications,results;
196 String tmpName = null;
197 for (int j=0; j< group.getLength(); j++){
198 if(group.item(j).getNodeName().equals("Identification")){
199 this.nomenclatureCode ="";
200 identifications = group.item(j).getChildNodes();
201 for (int m=0; m<identifications.getLength();m++){
202 if(identifications.item(m).getNodeName().equals("Result")){
203 results = identifications.item(m).getChildNodes();
204 for(int k=0; k<results.getLength();k++)
205 if (results.item(k).getNodeName().equals("TaxonIdentified")){
206 tmpName=this.getScientificName(results.item(k));
207 }
208 }
209 else if(identifications.item(m).getNodeName().equals("PreferredFlag")){
210 if (this.nomenclatureCode != null && this.nomenclatureCode !="")
211 this.identificationList.add(tmpName+"_preferred_"+identifications.item(m).getTextContent()+"_code_"+this.nomenclatureCode);
212 else
213 this.identificationList.add(tmpName+"_preferred_"+identifications.item(m).getTextContent());
214 path=identifications.item(m).getNodeName();
215 getHierarchie(identifications.item(m));
216 knownABCDelements.add(path);
217 path="";
218 }
219
220 else if (identifications.item(m).getNodeName().equals("References"))
221 this.getReferences(identifications.item(m));
222 }
223 }
224 }
225 boolean hasPref=false;
226 for (int j=0; j< group.getLength(); j++){
227 if(group.item(j).getNodeName().equals("Identification")){
228 this.nomenclatureCode ="";
229 identifications = group.item(j).getChildNodes();
230 for (int m=0; m<identifications.getLength();m++){
231 if(identifications.item(m).getNodeName().equals("Result")){
232 results = identifications.item(m).getChildNodes();
233 for(int k=0; k<results.getLength();k++)
234 if (results.item(k).getNodeName().equals("TaxonIdentified")){
235 tmpName=this.getScientificName(results.item(k));
236 }
237 }
238 if(identifications.item(m).getNodeName().equals("PreferredFlag")){
239 hasPref=true;
240 }
241 }
242 if ( !hasPref && tmpName != null)
243 if (this.nomenclatureCode != null && this.nomenclatureCode !="")
244 this.identificationList.add(tmpName+"_preferred_"+"0"+"_code_"+this.nomenclatureCode);
245 else
246 this.identificationList.add(tmpName+"_preferred_"+"0");
247 }
248 }
249
250 }
251
252
253
254 private void getReferences(Node result){
255 NodeList results,reference;
256 results = result.getChildNodes();
257 for(int k=0; k<results.getLength();k++){
258 if (results.item(k).getNodeName().equals("Reference")){
259 reference = results.item(k).getChildNodes();
260 for(int l=0;l<reference.getLength();l++){
261 if (reference.item(l).getNodeName().equals("TitleCitation")){
262 path = reference.item(l).getNodeName();
263 referenceList.add(reference.item(l).getTextContent());
264 getHierarchie(reference.item(l));
265 knownABCDelements.add(path);
266 path="";
267 }
268 }
269 }
270 }
271 }
272
273 private String getScientificName(Node result){
274 NodeList taxonsIdentified, scnames, atomised;
275 String tmpName = "";
276 atomisedStr="";
277 taxonsIdentified = result.getChildNodes();
278 for (int l=0; l<taxonsIdentified.getLength(); l++){
279 if (taxonsIdentified.item(l).getNodeName().equals("ScientificName")){
280 scnames = taxonsIdentified.item(l).getChildNodes();
281 for (int n=0;n<scnames.getLength();n++){
282 if (scnames.item(n).getNodeName().equals("FullScientificNameString")){
283 path=scnames.item(n).getNodeName();
284 tmpName = scnames.item(n).getTextContent();
285 getHierarchie(scnames.item(n));
286 knownABCDelements.add(path);
287 path="";
288 }
289 if (scnames.item(n).getNodeName().equals("NameAtomised")){
290 try {
291 if (scnames.item(n).hasChildNodes()){
292 this.nomenclatureCode = scnames.item(n).getChildNodes().item(1).getNodeName();
293 }
294 } catch (Exception e) {
295 this.nomenclatureCode ="";
296 }
297 atomised = scnames.item(n).getChildNodes().item(1).getChildNodes();
298 this.atomisedIdentificationList.add(this.getAtomisedNames(nomenclatureCode,atomised));
299 }
300 }
301 }
302 }
303 return tmpName;
304 }
305
306 private HashMap<String,String> getAtomisedNames(String code, NodeList atomised){
307 if (code.equals("Botanical"))
308 return this.getAtomisedBotanical(atomised);
309 if (code.equals("Bacterial"))
310 return this.getAtomisedBacterial(atomised);
311 if (code.equals("NameViral"))
312 return this.getAtomisedViral(atomised);
313 if (code.equals("NameZoological"))
314 return this.getAtomisedZoological(atomised);
315 return new HashMap<String,String>();
316 }
317
318 private HashMap<String,String> getAtomisedZoological(NodeList atomised){
319 HashMap<String,String> atomisedMap = new HashMap<String,String>();
320 for (int i=0;i<atomised.getLength();i++){
321 if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
322 atomisedMap.put("Genus",atomised.item(i).getTextContent());
323 path=atomised.item(i).getNodeName();
324 getHierarchie(atomised.item(i));
325 knownABCDelements.add(path);
326 path="";
327 }
328 if(atomised.item(i).getNodeName().equals("Subgenus")){
329 atomisedMap.put("Subgenus",atomised.item(i).getTextContent());
330 path=atomised.item(i).getNodeName();
331 getHierarchie(atomised.item(i));
332 knownABCDelements.add(path);
333 path="";
334 }
335 if(atomised.item(i).getNodeName().equals("SpeciesEpithet")){
336 atomisedMap.put("SpeciesEpithet",atomised.item(i).getTextContent());
337 path=atomised.item(i).getNodeName();
338 getHierarchie(atomised.item(i));
339 knownABCDelements.add(path);
340 path="";
341 }
342 if(atomised.item(i).getNodeName().equals("SubspeciesEpithet")){
343 atomisedMap.put("SubspeciesEpithet",atomised.item(i).getTextContent());
344 path=atomised.item(i).getNodeName();
345 getHierarchie(atomised.item(i));
346 knownABCDelements.add(path);
347 path="";
348 }
349 if(atomised.item(i).getNodeName().equals("AuthorTeamOriginalAndYear")){
350 atomisedMap.put("AuthorTeamOriginalAndYear",atomised.item(i).getTextContent());
351 path=atomised.item(i).getNodeName();
352 getHierarchie(atomised.item(i));
353 knownABCDelements.add(path);
354 path="";}
355 if(atomised.item(i).getNodeName().equals("AuthorTeamParenthesisAndYear")){
356 atomisedMap.put("AuthorTeamParenthesisAndYear",atomised.item(i).getTextContent());
357 path=atomised.item(i).getNodeName();
358 getHierarchie(atomised.item(i));
359 knownABCDelements.add(path);
360 path="";}
361 if(atomised.item(i).getNodeName().equals("CombinationAuthorTeamAndYear")){
362 atomisedMap.put("CombinationAuthorTeamAndYear",atomised.item(i).getTextContent());
363 path=atomised.item(i).getNodeName();
364 getHierarchie(atomised.item(i));
365 knownABCDelements.add(path);
366 path="";}
367 if(atomised.item(i).getNodeName().equals("Breed")){
368 atomisedMap.put("Breed",atomised.item(i).getTextContent());
369 path=atomised.item(i).getNodeName();
370 getHierarchie(atomised.item(i));
371 knownABCDelements.add(path);
372 path="";}
373 if(atomised.item(i).getNodeName().equals("NamedIndividual")){
374 atomisedMap.put("NamedIndividual",atomised.item(i).getTextContent());
375 path=atomised.item(i).getNodeName();
376 getHierarchie(atomised.item(i));
377 knownABCDelements.add(path);
378 path="";
379 }
380 }
381 return atomisedMap;
382
383 }
384
385 private HashMap<String,String> getAtomisedViral(NodeList atomised){
386 HashMap<String,String> atomisedMap = new HashMap<String,String>();
387 for (int i=0;i<atomised.getLength();i++){
388 if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
389 atomisedMap.put("Genus",atomised.item(i).getTextContent());
390 path=atomised.item(i).getNodeName();
391 getHierarchie(atomised.item(i));
392 knownABCDelements.add(path);
393 path="";
394 }
395 if(atomised.item(i).getNodeName().equals("ViralSpeciesDesignation")){
396 atomisedMap.put("ViralSpeciesDesignation", atomised.item(i).getTextContent());
397 path=atomised.item(i).getNodeName();
398 getHierarchie(atomised.item(i));
399 knownABCDelements.add(path);
400 path="";
401 }
402 if(atomised.item(i).getNodeName().equals("Acronym")){
403 atomisedMap.put("Acronym",atomised.item(i).getTextContent());
404 path=atomised.item(i).getNodeName();
405 getHierarchie(atomised.item(i));
406 knownABCDelements.add(path);
407 path="";
408 }
409 }
410 return atomisedMap;
411 }
412
413 private HashMap<String,String> getAtomisedBotanical(NodeList atomised){
414 HashMap<String,String> atomisedMap = new HashMap<String,String>();
415 for (int i=0;i<atomised.getLength();i++){
416 if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
417 atomisedMap.put("Genus",atomised.item(i).getTextContent());
418 path=atomised.item(i).getNodeName();
419 getHierarchie(atomised.item(i));
420 knownABCDelements.add(path);
421 path="";
422 }
423 if(atomised.item(i).getNodeName().equals("FirstEpithet")){
424 atomisedMap.put("FirstEpithet",atomised.item(i).getTextContent());
425 path=atomised.item(i).getNodeName();
426 getHierarchie(atomised.item(i));
427 knownABCDelements.add(path);
428 path="";
429 }
430 if(atomised.item(i).getNodeName().equals("InfraspecificEpithet")){
431 atomisedMap.put("InfraSpeEpithet", atomised.item(i).getTextContent());
432 path=atomised.item(i).getNodeName();
433 getHierarchie(atomised.item(i));
434 knownABCDelements.add(path);
435 path="";
436 }
437 if(atomised.item(i).getNodeName().equals("Rank")){
438 atomisedMap.put("Rank",atomised.item(i).getTextContent());
439 path=atomised.item(i).getNodeName();
440 getHierarchie(atomised.item(i));
441 knownABCDelements.add(path);
442 path="";
443 }
444 if(atomised.item(i).getNodeName().equals("HybridFlag")){
445 atomisedMap.put("HybridFlag",atomised.item(i).getTextContent());
446 path=atomised.item(i).getNodeName();
447 getHierarchie(atomised.item(i));
448 knownABCDelements.add(path);
449 path="";
450 }
451 if(atomised.item(i).getNodeName().equals("AuthorTeamParenthesis")){
452 atomisedMap.put("AuthorTeamParenthesis",atomised.item(i).getTextContent());
453 path=atomised.item(i).getNodeName();
454 getHierarchie(atomised.item(i));
455 knownABCDelements.add(path);
456 path="";
457 }
458 if(atomised.item(i).getNodeName().equals("AuthorTeam")){
459 atomisedMap.put("AuthorTeam",atomised.item(i).getTextContent());
460 path=atomised.item(i).getNodeName();
461 getHierarchie(atomised.item(i));
462 knownABCDelements.add(path);
463 path="";
464 }
465 if(atomised.item(i).getNodeName().equals("CultivarGroupName")){
466 atomisedMap.put("CultivarGroupName",atomised.item(i).getTextContent());
467 path=atomised.item(i).getNodeName();
468 getHierarchie(atomised.item(i));
469 knownABCDelements.add(path);
470 path="";
471 }
472 if(atomised.item(i).getNodeName().equals("CultivarName")){
473 atomisedMap.put("CultivarName",atomised.item(i).getTextContent());
474 path=atomised.item(i).getNodeName();
475 getHierarchie(atomised.item(i));
476 knownABCDelements.add(path);
477 path="";
478 }
479 if(atomised.item(i).getNodeName().equals("TradeDesignationNames")){
480 atomisedMap.put("Trade",atomised.item(i).getTextContent());
481 path=atomised.item(i).getNodeName();
482 getHierarchie(atomised.item(i));
483 knownABCDelements.add(path);
484 path="";
485 }
486 }
487 return atomisedMap;
488 }
489
490 private HashMap<String,String> getAtomisedBacterial(NodeList atomised){
491 HashMap<String,String> atomisedMap = new HashMap<String,String>();
492 for (int i=0;i<atomised.getLength();i++){
493 if(atomised.item(i).getNodeName().equals("GenusOrMonomial")){
494 atomisedMap.put("Genus",atomised.item(i).getTextContent());
495 path=atomised.item(i).getNodeName();
496 getHierarchie(atomised.item(i));
497 knownABCDelements.add(path);
498 path="";
499 }
500 if(atomised.item(i).getNodeName().equals("Subgenus")){
501 atomisedMap.put("SubGenus",atomised.item(i).getTextContent());
502 path=atomised.item(i).getNodeName();
503 getHierarchie(atomised.item(i));
504 knownABCDelements.add(path);
505 path="";
506 }
507 if(atomised.item(i).getNodeName().equals("SubgenusAuthorAndYear")){
508 atomisedMap.put("SubgenusAuthorAndYear",atomised.item(i).getTextContent());
509 path=atomised.item(i).getNodeName();
510 getHierarchie(atomised.item(i));
511 knownABCDelements.add(path);
512 path="";
513 }
514 if(atomised.item(i).getNodeName().equals("SpeciesEpithet")){
515 atomisedMap.put("SpeciesEpithet",atomised.item(i).getTextContent());
516 path=atomised.item(i).getNodeName();
517 getHierarchie(atomised.item(i));
518 knownABCDelements.add(path);
519 path="";
520 }
521 if(atomised.item(i).getNodeName().equals("SubspeciesEpithet")){
522 atomisedMap.put("SubspeciesEpithet",atomised.item(i).getTextContent());
523 path=atomised.item(i).getNodeName();
524 getHierarchie(atomised.item(i));
525 knownABCDelements.add(path);
526 path="";
527 }
528 if(atomised.item(i).getNodeName().equals("ParentheticalAuthorTeamAndYear")){
529 atomisedMap.put("ParentheticalAuthorTeamAndYear",atomised.item(i).getTextContent());
530 path=atomised.item(i).getNodeName();
531 getHierarchie(atomised.item(i));
532 knownABCDelements.add(path);
533 path="";
534 }
535 if(atomised.item(i).getNodeName().equals("AuthorTeamAndYear")){
536 atomisedMap.put("AuthorTeamAndYear",atomised.item(i).getTextContent());
537 path=atomised.item(i).getNodeName();
538 getHierarchie(atomised.item(i));
539 knownABCDelements.add(path);
540 path="";
541 }
542 if(atomised.item(i).getNodeName().equals("NameApprobation")){
543 atomisedMap.put("NameApprobation",atomised.item(i).getTextContent());
544 path=atomised.item(i).getNodeName();
545 getHierarchie(atomised.item(i));
546 knownABCDelements.add(path);
547 path="";
548 }
549 }
550 return atomisedMap;
551 }
552
553 private void getIDs(Element racine){
554 NodeList group;
555 try {
556 group = racine.getElementsByTagName("SourceInstitutionID");
557 path=group.item(0).getNodeName();
558 getHierarchie(group.item(0));
559 knownABCDelements.add(path);
560 path="";
561 this.institutionCode = group.item(0).getTextContent();
562 } catch (NullPointerException e) {
563 this.institutionCode= "";
564 }
565 try {
566 group = racine.getElementsByTagName("SourceID");
567 path=group.item(0).getNodeName();
568 getHierarchie(group.item(0));
569 knownABCDelements.add(path);
570 path="";
571 this.collectionCode = group.item(0).getTextContent();
572 } catch (NullPointerException e) {
573 this.collectionCode = "";
574 }
575 try {
576 group = racine.getElementsByTagName("UnitID");
577 path=group.item(0).getNodeName();
578 getHierarchie(group.item(0));
579 knownABCDelements.add(path);
580 path="";
581 this.unitID = group.item(0).getTextContent();
582 } catch (NullPointerException e) {
583 this.unitID = "";
584 }
585 }
586
587 private void getRecordBasis(Element racine){
588 NodeList group;
589 try {
590 group = racine.getElementsByTagName("RecordBasis");
591 path=group.item(0).getNodeName();
592 getHierarchie(group.item(0));
593 knownABCDelements.add(path);
594 path="";
595 this.recordBasis = group.item(0).getTextContent();
596 } catch (NullPointerException e) {
597 this.recordBasis = "";
598 }
599 }
600
601 private void getMultimedia(Element racine){
602 NodeList group, multimedias, multimedia;
603 try {
604 group = racine.getElementsByTagName("MultiMediaObjects");
605 for(int i=0;i<group.getLength();i++){
606 multimedias = group.item(i).getChildNodes();
607 for (int j=0;j<multimedias.getLength();j++){
608 if (multimedias.item(j).getNodeName().equals("MultiMediaObject")){
609 multimedia = multimedias.item(j).getChildNodes();
610 for (int k=0;k<multimedia.getLength();k++){
611 if(multimedia.item(k).getNodeName().equals("FileURI")){
612 this.multimediaObjects.add(multimedia.item(k).getTextContent());
613 path = multimedia.item(k).getNodeName();
614 getHierarchie(multimedia.item(k));
615 knownABCDelements.add(path);
616 path="";
617 }
618 }
619 }
620 }
621 }
622 } catch (NullPointerException e) {
623 logger.info(e);
624 }
625 }
626
627 private void getNumbers(Element racine){
628 NodeList group;
629 try {
630 group = racine.getElementsByTagName("AccessionNumber");
631 path=group.item(0).getNodeName();
632 getHierarchie(group.item(0));
633 knownABCDelements.add(path);
634 path="";
635 this.accessionNumber = group.item(0).getTextContent();
636 } catch (NullPointerException e) {
637 this.accessionNumber = "";
638 }
639 try {
640 group = racine.getElementsByTagName("CollectorsFieldNumber");
641 path=group.item(0).getNodeName();
642 getHierarchie(group.item(0));
643 knownABCDelements.add(path);
644 path="";
645 this.fieldNumber = group.item(0).getTextContent();
646 } catch (NullPointerException e) {
647 this.fieldNumber = "";
648 }
649
650 try {
651 group = racine.getElementsByTagName("CollectorsNumber");
652 path=group.item(0).getNodeName();
653 getHierarchie(group.item(0));
654 knownABCDelements.add(path);
655 path="";
656 this.collectorsNumber = group.item(0).getTextContent();
657 } catch (NullPointerException e) {
658 this.collectorsNumber = "";
659 }
660
661 try {
662 group = racine.getElementsByTagName("AccessionNumber");
663 path=group.item(0).getNodeName();
664 getHierarchie(group.item(0));
665 knownABCDelements.add(path);
666 path="";
667 this.accessionNumber = group.item(0).getTextContent();
668 } catch (NullPointerException e) {
669 this.accessionNumber = "";
670 }
671 }
672
673 private void getGeolocation(Element racine){
674 NodeList group, childs;
675 try {
676 group = racine.getElementsByTagName("LocalityText");
677 path=group.item(0).getNodeName();
678 getHierarchie(group.item(0));
679 knownABCDelements.add(path);
680 path="";
681 this.locality = group.item(0).getTextContent();
682 if (group.item(0).hasAttributes())
683 if (group.item(0).getAttributes().getNamedItem("lang") != null)
684 this.languageIso = group.item(0).getAttributes().getNamedItem("lang").getTextContent();
685 } catch (NullPointerException e) {
686 this.locality = "";
687 }
688 try {
689 group = racine.getElementsByTagName("LongitudeDecimal");
690 path=group.item(0).getNodeName();
691 getHierarchie(group.item(0));
692 knownABCDelements.add(path);
693 path="";
694 this.longitude = Double.valueOf(group.item(0).getTextContent());
695 } catch (NullPointerException e) {
696 this.longitude=0.0;
697 }
698 try {
699 group = racine.getElementsByTagName("LatitudeDecimal");
700 path=group.item(0).getNodeName();
701 getHierarchie(group.item(0));
702 knownABCDelements.add(path);
703 path="";
704 this.latitude = Double.valueOf(group.item(0).getTextContent());
705 } catch (NullPointerException e) {
706 this.latitude=0.0;
707 }
708 try {
709 group = racine.getElementsByTagName("Country");
710 childs = group.item(0).getChildNodes();
711 for (int i=0;i<childs.getLength(); i++){
712 if(childs.item(i).getNodeName() == "Name"){
713 path=childs.item(i).getNodeName();
714 getHierarchie(childs.item(i));
715 knownABCDelements.add(path);
716 path="";
717 this.country = childs.item(i).getTextContent();
718 }
719 }
720 } catch (NullPointerException e) {
721 this.country = "";
722 }
723 try {
724 group = racine.getElementsByTagName("Country");
725 childs = group.item(0).getChildNodes();
726 for (int i=0;i<childs.getLength(); i++){
727 if(childs.item(i).getNodeName() == "ISO3166Code"){
728 path=childs.item(i).getNodeName();
729 getHierarchie(childs.item(i));
730 knownABCDelements.add(path);
731 path="";
732 this.isocountry = childs.item(i).getTextContent();
733 }
734 }
735 } catch (NullPointerException e) {
736 this.isocountry = "";
737 }
738 try {
739 group = racine.getElementsByTagName("Altitude");
740 for (int i=0;i<group.getLength();i++){
741 childs = group.item(i).getChildNodes();
742 for (int j=0;j<childs.getLength();j++){
743 if (childs.item(j).getNodeName().equals("MeasurementOrFactText")){
744 path=childs.item(j).getNodeName();
745 getHierarchie(childs.item(j));
746 knownABCDelements.add(path);
747 path="";
748 this.altitude = Integer.valueOf(childs.item(j).getTextContent());
749 }
750 }
751 }
752 } catch (NullPointerException e) {
753 this.altitude = -9999;
754 }
755
756 try {
757 group = racine.getElementsByTagName("Depth");
758 path=group.item(0).getNodeName();
759 getHierarchie(group.item(0));
760 knownABCDelements.add(path);
761 path="";
762 this.depth = Integer.valueOf(group.item(0).getTextContent());
763 } catch (NullPointerException e) {
764 this.depth = -9999;
765 }
766
767 try{
768 group = racine.getElementsByTagName("NamedArea");
769 this.namedAreaList = new ArrayList<String>();
770 for (int i=0;i<group.getLength();i++){
771 childs = group.item(i).getChildNodes();
772 for (int j=0; j<childs.getLength();j++){
773 if (childs.item(j).getNodeName().equals("AreaName")){
774 path = childs.item(j).getNodeName();
775 getHierarchie(childs.item(j));
776 knownABCDelements.add(path);
777 path="";
778 this.namedAreaList.add(childs.item(j).getTextContent());
779 }
780 }
781 }
782 }catch(NullPointerException e){
783 this.namedAreaList = new ArrayList<String>();
784 }
785 }
786
787 private void getGatheringPeople(Element racine){
788 NodeList group, childs, person;
789 try {
790 group = racine.getElementsByTagName("GatheringAgent");
791 this.gatheringAgentList = new ArrayList<String>();
792 for (int i=0; i< group.getLength(); i++){
793 childs = group.item(i).getChildNodes();
794 for (int j=0; j<childs.getLength();j++){
795 if (childs.item(j).getNodeName().equals("Person")){
796 person = childs.item(j).getChildNodes();
797 for (int k=0; k<person.getLength(); k++)
798 if (person.item(k).getNodeName().equals("FullName")){
799 path=person.item(k).getNodeName();
800 getHierarchie(person.item(k));
801 knownABCDelements.add(path);
802 path="";
803 this.gatheringAgentList.add(person.item(k).getTextContent());
804 }
805 }
806
807 }
808 }
809 } catch (NullPointerException e) {
810 this.gatheringAgentList = new ArrayList<String>();
811 }
812 }
813
814 private Institution getInstitution(String institutionCode, SpecimenImportConfigurator config){
815 Institution institution;
816 List<Institution> institutions;
817 try{
818 logger.info(this.institutionCode);
819 institutions = getAgentService().searchInstitutionByCode(this.institutionCode);
820 }catch(Exception e){
821 institutions=new ArrayList<Institution>();
822 }
823 if (institutions.size() ==0 || !config.getReUseExistingMetadata()){
824 logger.info("Institution (agent) unknown or not allowed to reuse existing metadata");
825 //create institution
826 institution = Institution.NewInstance();
827 institution.setCode(this.institutionCode);
828 }
829 else{
830 logger.info("Institution (agent) already in the db");
831 institution = institutions.get(0);
832 }
833 return institution;
834 }
835
836 /*
837 * Look if the Collection does already exists
838 * @param collectionCode: a string
839 * @param institution: the current Institution
840 * @param app
841 * @return the Collection (existing or new)
842 */
843 private Collection getCollection(String collectionCode, Institution institution, SpecimenImportConfigurator config){
844 Collection collection = Collection.NewInstance();
845 List<Collection> collections;
846 try{
847 collections = getOccurrenceService().searchCollectionByCode(this.collectionCode);
848 }catch(Exception e){
849 collections=new ArrayList<Collection>();
850 }
851 if (collections.size() ==0 || !config.getReUseExistingMetadata()){
852 logger.info("Collection not found or do not reuse existing metadata "+this.collectionCode);
853 //create new collection
854 collection.setCode(this.collectionCode);
855 collection.setCodeStandard("GBIF");
856 collection.setInstitute(institution);
857 }
858 else{
859 boolean collectionFound=false;
860 for (int i=0; i<collections.size(); i++){
861 collection = collections.get(i);
862 try {
863 if (collection.getInstitute().getCode().equalsIgnoreCase(institution.getCode())){
864 //found a collection with the same code and the same institution
865 collectionFound=true;
866 }
867 } catch (NullPointerException e) {}
868 }
869 if (!collectionFound){
870 collection.setCode(this.collectionCode);
871 collection.setCodeStandard("GBIF");
872 collection.setInstitute(institution);
873 }
874
875 }
876 return collection;
877 }
878
879 /*
880 *
881 * @param app
882 * @param derivedThing
883 * @param sec
884 */
885 private void setTaxonNameBase(SpecimenImportConfigurator config, DerivedUnitBase derivedThing, ReferenceBase sec){
886 NonViralName<?> taxonName = null;
887 String fullScientificNameString;
888 Taxon taxon = null;
889 DeterminationEvent determinationEvent = null;
890 List<TaxonBase> names = null;
891
892 String scientificName="";
893 boolean preferredFlag=false;
894
895 for (int i = 0; i < this.identificationList.size(); i++) {
896 fullScientificNameString = this.identificationList.get(i);
897 fullScientificNameString = fullScientificNameString.replaceAll(" et ", " & ");
898 if (fullScientificNameString.indexOf("_preferred_") != -1){
899 scientificName = fullScientificNameString.split("_preferred_")[0];
900 String pTmp = fullScientificNameString.split("_preferred_")[1].split("_code_")[0];
901 if (pTmp.equals("1") || pTmp.toLowerCase().indexOf("true") != -1)
902 preferredFlag=true;
903 else
904 preferredFlag=false;
905 }
906 else scientificName = fullScientificNameString;
907
908 logger.info(fullScientificNameString);
909 if (fullScientificNameString.indexOf("_code_") != -1)
910 this.nomenclatureCode = fullScientificNameString.split("_code_")[1];
911
912 if (config.getDoAutomaticParsing() || this.atomisedIdentificationList == null || this.atomisedIdentificationList.size()==0)
913 taxonName = this.parseScientificName(scientificName);
914 else {
915 if (this.atomisedIdentificationList != null || this.atomisedIdentificationList.size()>0)
916 taxonName = this.setTaxonNameByType(this.atomisedIdentificationList.get(i), scientificName);
917 }
918 if(taxonName == null){
919 taxonName = NonViralName.NewInstance(null);
920 taxonName.setFullTitleCache(scientificName);
921 }
922 if (config.getDoReUseTaxon()){
923 try{
924 names = getTaxonService().searchTaxaByName(scientificName, sec);
925 taxon = (Taxon)names.get(0);
926 }
927 catch(Exception e){taxon=null;}
928 }
929 // taxonName = NonViralName.NewInstance(null);
930 // taxonName.setFullTitleCache(scientificName);
931
932 if (!config.getDoReUseTaxon() || taxon == null){
933 getNameService().saveTaxonName(taxonName);
934 taxon = Taxon.NewInstance(taxonName, sec); //sec set null
935 }
936 determinationEvent = DeterminationEvent.NewInstance();
937 determinationEvent.setTaxon(taxon);
938 determinationEvent.setPreferredFlag(preferredFlag);
939 for (int l=0;l<this.referenceList.size();l++){
940 ReferenceBase reference = new Generic();
941 reference.setTitleCache(this.referenceList.get(l));
942 determinationEvent.addReference(reference);
943 }
944 derivedThing.addDetermination(determinationEvent);
945 }
946
947 }
948
949 private NonViralName<?> parseScientificName(String scientificName){
950 NonViralNameParserImpl nvnpi = NonViralNameParserImpl.NewInstance();
951 NonViralName<?>taxonName = null;
952 boolean problem=false;
953
954 if (this.nomenclatureCode.toString().equals("Zoological")){
955 taxonName = (ZoologicalName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICZN,null);
956 if (taxonName.hasProblem())
957 problem=true;
958 }
959 if (this.nomenclatureCode.toString().equals("Botanical")){
960 taxonName = (BotanicalName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICBN,null);
961 if (taxonName.hasProblem())
962 problem=true;;}
963 if (this.nomenclatureCode.toString().equals("Bacterial")){
964 taxonName = (BacterialName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNB, null);
965 if (taxonName.hasProblem())
966 problem=true;
967 }
968 if (this.nomenclatureCode.toString().equals("Cultivar")){
969 taxonName = (CultivarPlantName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICNCP, null);
970 if (taxonName.hasProblem())
971 problem=true;;
972 }
973 // if (this.nomenclatureCode.toString().equals("Viral")){
974 // ViralName taxonName = (ViralName)nvnpi.parseFullName(scientificName,NomenclaturalCode.ICVCN(), null);
975 // if (taxonName.hasProblem())
976 // logger.info("pb ICVCN");
977 // }
978 //TODO: parsing of ViralNames?
979 if(problem){
980 taxonName = NonViralName.NewInstance(null);
981 taxonName.setTitleCache(scientificName);
982 }
983 return taxonName;
984
985 }
986
987 @SuppressWarnings("unchecked")
988 private NonViralName<?> setTaxonNameByType(HashMap<String, String> atomisedMap,String fullName){
989 if (this.nomenclatureCode.equals("Zoological")){
990 NonViralName<ZoologicalName> taxonName = ZoologicalName.NewInstance(null);
991 taxonName.setFullTitleCache(fullName, true);
992 taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
993 taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"SubGenus"));
994 taxonName.setSpecificEpithet(getFromMap(atomisedMap,"SpeciesEpithet"));
995 taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"SubspeciesEpithet"));
996 Team team = null;
997 if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
998 team = Team.NewInstance();
999 team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1000 }else{
1001 if (getFromMap(atomisedMap,"AuthorTeamAndYear") != null){
1002 team = Team.NewInstance();
1003 team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamAndYear"));
1004 }
1005 }
1006 if(team != null)
1007 taxonName.setBasionymAuthorTeam(team);
1008 else{
1009 if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null)
1010 taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1011 else if (getFromMap(atomisedMap,"AuthorTeamAndYear") != null)
1012 taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamAndYear"));
1013 }
1014 if(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear") != null){
1015 team = Team.NewInstance();
1016 team.setTitleCache(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear"));
1017 taxonName.setCombinationAuthorTeam(team);
1018 }
1019 if (taxonName.hasProblem())
1020 logger.info("pb ICZN");
1021 else return taxonName;
1022 }
1023 if (this.nomenclatureCode.equals("Botanical")){
1024 NonViralName<BotanicalName> taxonName = BotanicalName.NewInstance(null);
1025 taxonName.setFullTitleCache(fullName, true);
1026 taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
1027 taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"FirstEpithet"));
1028 taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"InfraSpeEpithet"));
1029 try{taxonName.setRank(Rank.getRankByName(getFromMap(atomisedMap,"Rank")));
1030 }catch(Exception e){}
1031 Team team = null;
1032 if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null){
1033 team = Team.NewInstance();
1034 team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1035 if(team != null)
1036 taxonName.setBasionymAuthorTeam(team);
1037 }
1038 if (getFromMap(atomisedMap,"AuthorTeam") != null){
1039 team = Team.NewInstance();
1040 team.setTitleCache(getFromMap(atomisedMap,"AuthorTeam"));
1041 if(team != null)
1042 taxonName.setCombinationAuthorTeam(team);
1043 }
1044 if (team == null) {
1045 if(getFromMap(atomisedMap,"AuthorTeamParenthesis") != null)
1046 taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeamParenthesis"));
1047 else if (getFromMap(atomisedMap,"AuthorTeam") != null)
1048 taxonName.setAuthorshipCache(getFromMap(atomisedMap,"AuthorTeam"));
1049 }
1050 if(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear") != null){
1051 team = Team.NewInstance();
1052 team.setTitleCache(getFromMap(atomisedMap,"CombinationAuthorTeamAndYear"));
1053 taxonName.setCombinationAuthorTeam(team);
1054 }
1055 if (taxonName.hasProblem())
1056 logger.info("pb ICBN");
1057 else return taxonName;
1058 }
1059 if (this.nomenclatureCode.equals("Bacterial")){
1060 NonViralName<BacterialName> taxonName = BacterialName.NewInstance(null);
1061 taxonName.setFullTitleCache(fullName, true);
1062 taxonName.setGenusOrUninomial(getFromMap(atomisedMap,"Genus"));
1063 taxonName.setInfraGenericEpithet(getFromMap(atomisedMap,"SubGenus"));
1064 taxonName.setSpecificEpithet(getFromMap(atomisedMap,"Species"));
1065 taxonName.setInfraSpecificEpithet(getFromMap(atomisedMap,"SubspeciesEpithet"));
1066 if(getFromMap(atomisedMap,"AuthorTeamAndYear") != null){
1067 Team team = Team.NewInstance();
1068 team.setTitleCache(getFromMap(atomisedMap,"AuthorTeamAndYear"));
1069 taxonName.setCombinationAuthorTeam(team);
1070 }
1071 if(getFromMap(atomisedMap,"ParentheticalAuthorTeamAndYear") != null){
1072 Team team = Team.NewInstance();
1073 team.setTitleCache(getFromMap(atomisedMap,"ParentheticalAuthorTeamAndYear"));
1074 taxonName.setBasionymAuthorTeam(team);
1075 }
1076 if (taxonName.hasProblem())
1077 logger.info("pb ICNB");
1078 else return taxonName;
1079 }
1080 if (this.nomenclatureCode.equals("Cultivar")){
1081 CultivarPlantName taxonName = CultivarPlantName.NewInstance(null);
1082
1083 if (taxonName.hasProblem())
1084 logger.info("pb ICNCP");
1085 else return taxonName;
1086 }
1087 // if (this.nomenclatureCode.equals("Viral")){
1088 // ViralName taxonName = ViralName.NewInstance(null);
1089 // taxonName.setFullTitleCache(fullName, true);
1090 // taxonName.setAcronym(getFromMap(atomisedMap,"Acronym"));
1091 // if (taxonName.hasProblem())
1092 // logger.info("pb ICVCN");
1093 // else return taxonName;
1094 // }
1095 //TODO ViralName
1096 NonViralName<?>taxonName = NonViralName.NewInstance(null);
1097 taxonName.setFullTitleCache(fullName, true);
1098 return taxonName;
1099 }
1100
1101 private String getFromMap(HashMap<String, String> atomisedMap, String key){
1102 String value = null;
1103 if (atomisedMap.containsKey(key))
1104 value = atomisedMap.get(key);
1105 try{
1106 if (value != null && key.matches(".*Year.*")){
1107 value=value.trim();
1108 if (value.matches("[a-z A-Z ]*[0-9]{4}$")){
1109 String tmp=value.split("[0-9]{4}$")[0];
1110 int year = Integer.parseInt(value.split(tmp)[1]);
1111 if (year >= 1752)
1112 value=tmp;
1113 else
1114 value=null;
1115 }
1116 else
1117 value=null;
1118 }
1119 }catch(Exception e){value=null;}
1120
1121 return value;
1122 }
1123 /*
1124 * Store the unit with its Gathering informations in the CDM
1125 */
1126 public boolean start(SpecimenImportConfigurator config){
1127 boolean result = true;
1128
1129 TransactionStatus tx = null;
1130
1131 // try {
1132 // app = CdmApplicationController.NewInstance(config.getDestination(), config.getDbSchemaValidation());
1133 // } catch (DataSourceNotFoundException e1) {
1134 // e1.printStackTrace();
1135 // logger.info("DataSourceNotFoundException "+e1);
1136 // } catch (TermNotFoundException e1) {
1137 // e1.printStackTrace();
1138 // logger.info("TermNotFoundException " +e1);
1139 // }
1140
1141 tx = startTransaction();
1142 try {
1143 // ReferenceBase sec = Database.NewInstance();
1144 // sec.setTitleCache("XML DATA");
1145 ReferenceBase sec = config.getTaxonReference();
1146
1147 /**
1148 * SPECIMEN OR OBSERVATION OR LIVING
1149 */
1150 DerivedUnitBase derivedThing = null;
1151 //create specimen
1152 boolean rbFound=false;
1153 if (this.recordBasis != null){
1154 if (this.recordBasis.toLowerCase().startsWith("s")) {//specimen
1155 derivedThing = Specimen.NewInstance();
1156 rbFound = true;
1157 }
1158 else if (this.recordBasis.toLowerCase().startsWith("o")) {//observation
1159 derivedThing = Observation.NewInstance();
1160 rbFound = true;
1161 }
1162 else if (this.recordBasis.toLowerCase().startsWith("l")) {//living -> fossil, herbarium sheet....???
1163 derivedThing = LivingBeing.NewInstance();
1164 rbFound = true;
1165 }
1166 if (! rbFound){
1167 logger.info("The basis of record does not seem to be known: "+this.recordBasis);
1168 derivedThing = DerivedUnit.NewInstance();
1169 }
1170 }
1171 else{
1172 logger.info("The basis of record is null");
1173 derivedThing = DerivedUnit.NewInstance();
1174 }
1175 // if(derivedThing == null)derivedThing=Observation.NewInstance();
1176
1177 this.setTaxonNameBase(config, derivedThing, sec);
1178
1179
1180 //set catalogue number (unitID)
1181 derivedThing.setCatalogNumber(this.unitID);
1182 derivedThing.setAccessionNumber(this.accessionNumber);
1183 derivedThing.setCollectorsNumber(this.collectorsNumber);
1184
1185
1186 /**
1187 * INSTITUTION & COLLECTION
1188 */
1189 //manage institution
1190 Institution institution = this.getInstitution(this.institutionCode,config);
1191 //manage collection
1192 Collection collection = this.getCollection(this.collectionCode, institution, config);
1193 //link specimen & collection
1194 derivedThing.setCollection(collection);
1195
1196 /**
1197 * GATHERING EVENT
1198 */
1199
1200 UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(this, this.locality, this.languageIso, this.longitude,
1201 this.latitude, this.gatheringAgentList);
1202 UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea(this.isocountry, this.country, this);
1203 NamedArea areaCountry = unitsGatheringArea.getArea();
1204 unitsGatheringEvent.addArea(areaCountry);
1205 unitsGatheringArea = new UnitsGatheringArea(this.namedAreaList);
1206 ArrayList<NamedArea> nas = unitsGatheringArea.getAreas();
1207 for (int i=0; i<nas.size();i++)
1208 unitsGatheringEvent.addArea(nas.get(i));
1209
1210
1211 //create field/observation
1212 FieldObservation fieldObservation = FieldObservation.NewInstance();
1213 //add fieldNumber
1214 fieldObservation.setFieldNumber(this.fieldNumber);
1215 //join gatheringEvent to fieldObservation
1216 fieldObservation.setGatheringEvent(unitsGatheringEvent.getGatheringEvent());
1217 // //add Multimedia URLs
1218 if(this.multimediaObjects.size()>0){
1219 MediaRepresentation representation;
1220 Media media;
1221 ImageMetaData imd ;
1222 URL url ;
1223 ImageFile imf;
1224 for (int i=0;i<this.multimediaObjects.size();i++){
1225 if(this.multimediaObjects.get(i) != null){
1226 imd = new ImageMetaData();
1227 url = new URL(this.multimediaObjects.get(i));
1228 imd = MediaMetaData.readImageMetaData(url, imd);
1229 if (imd != null){
1230 representation = MediaRepresentation.NewInstance();
1231 imf = ImageFile.NewInstance(this.multimediaObjects.get(i), null, imd);
1232 representation.addRepresentationPart(imf);
1233 media = Media.NewInstance();
1234 media.addRepresentation(representation);
1235 fieldObservation.addMedia(media);
1236 }
1237 }
1238 }
1239 }
1240 // //link fieldObservation and specimen
1241 DerivationEvent derivationEvent = DerivationEvent.NewInstance();
1242 derivationEvent.addOriginal(fieldObservation);
1243 derivedThing.addDerivationEvent(derivationEvent);
1244
1245 /**
1246 * SAVE AND STORE DATA
1247 */
1248 getTermService().saveTerm(areaCountry);//save it sooner
1249 for (int i=0; i<nas.size();i++)
1250 getTermService().saveTerm(nas.get(i));//save it sooner (foreach area)
1251 getTermService().saveLanguageData(unitsGatheringEvent.getLocality());//save it sooner
1252 getOccurrenceService().saveSpecimenOrObservationBase(derivedThing);
1253 logger.info("saved ABCD specimen ...");
1254
1255
1256 } catch (Exception e) {
1257 logger.warn("Error when reading record!!");
1258 e.printStackTrace();
1259 result = false;
1260 }
1261 commitTransaction(tx);
1262
1263 return result;
1264 }
1265
1266 private void compareABCDtoCDM(String urlFileName, ArrayList<String> knownElts){
1267
1268 try {
1269 DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
1270 DocumentBuilder constructeur = fabrique.newDocumentBuilder();
1271 URL url = new URL(urlFileName);
1272 Object o = url.getContent();
1273 InputStream is = (InputStream)o;
1274 Document document = constructeur.parse(is);
1275 Element racine = document.getDocumentElement();
1276 traverse(racine);
1277 } catch (ParserConfigurationException e) {
1278 // TODO Auto-generated catch block
1279 e.printStackTrace();
1280 } catch (SAXException e) {
1281 // TODO Auto-generated catch block
1282 e.printStackTrace();
1283 } catch (IOException e) {
1284 // TODO Auto-generated catch block
1285 e.printStackTrace();
1286 }
1287 Set<String> elts = allABCDelements.keySet();
1288 Iterator< String>it = elts.iterator();
1289 String elt;
1290 while (it.hasNext()){
1291 elt = it.next();
1292 if (knownElts.indexOf(elt) == -1)
1293 logger.info("Unsaved ABCD element: "+elt+" - "+allABCDelements.get(elt));
1294 }
1295 }
1296
1297
1298
1299 private void traverse(Node node){
1300 // Extract node info:
1301 String test = node.getTextContent();
1302
1303 // Print and continue traversing.
1304 if(test != null && test != "#text" && node.getNodeName() != "#text" && test.split("\n").length==1 && test.length()>0)
1305 {
1306 path=node.getNodeName();
1307 getHierarchie(node);
1308 allABCDelements.put(path,test);
1309 path="";
1310 }
1311 // Now traverse the rest of the tree in depth-first order.
1312 if (node.hasChildNodes()) {
1313 // Get the children in a list.
1314 NodeList nl = node.getChildNodes();
1315 // How many of them?
1316 int size = nl.getLength();
1317 for (int i=0; i<size; i++)
1318 // Recursively traverse each of the children.
1319 traverse (nl.item(i));
1320 }
1321 }
1322
1323
1324 @Override
1325 protected boolean doInvoke(IImportConfigurator config,
1326 Map<String, MapWrapper<? extends CdmBase>> stores) {
1327 logger.info("INVOKE Specimen Import from ABCD2.06 XML File");
1328 boolean result = true;
1329 SpecimenImportConfigurator specimenImportConfig = (SpecimenImportConfigurator)config;
1330 //AbcdIO test = new AbcdIO();
1331 String sourceName = specimenImportConfig.getSource();
1332 NodeList unitsList = getUnitsNodeList(sourceName);
1333 if (unitsList != null)
1334 {
1335 logger.info("nb units to insert: "+unitsList.getLength());
1336 for (int i=0;i<unitsList.getLength();i++){
1337 this.setUnitPropertiesXML((Element)unitsList.item(i));
1338 result &= this.start(specimenImportConfig);
1339 config.setDbSchemaValidation(DbSchemaValidation.UPDATE);
1340 //compare the ABCD elements added in to the CDM and the unhandled ABCD elements
1341 compareABCDtoCDM(sourceName,this.knownABCDelements);
1342 //reset the ABCD elements added in CDM
1343 //knownABCDelements = new ArrayList<String>();
1344 allABCDelements = new HashMap<String,String>();
1345 }
1346 }
1347
1348 return result;
1349
1350 }
1351
1352
1353 // public boolean invoke(IImportConfigurator config, Map stores) {
1354 // logger.info("invoke AbcdIO");
1355 // return invoke((SpecimenImportConfigurator)config);
1356 // }
1357
1358
1359 @Override
1360 protected boolean isIgnore(IImportConfigurator config) {
1361 //return ! config.isDoNameFacts();
1362 return false;
1363 }
1364
1365
1366
1367 }