new source SQLServer2008 added
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / sdd / SDDDescriptionIO.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.sdd;
11
12 import java.io.File;
13 import java.net.MalformedURLException;
14 import java.net.URI;
15 import java.net.URL;
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.Date;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.log4j.Logger;
27 import org.jdom.Element;
28 import org.jdom.Namespace;
29 import org.joda.time.DateTime;
30 import org.springframework.stereotype.Component;
31 import org.springframework.transaction.TransactionStatus;
32
33 import eu.etaxonomy.cdm.api.service.IAgentService;
34 import eu.etaxonomy.cdm.api.service.IVersionableService;//rajout
35 import eu.etaxonomy.cdm.api.service.IDescriptionService;
36 import eu.etaxonomy.cdm.api.service.IReferenceService;
37 import eu.etaxonomy.cdm.api.service.ITermService;
38 import eu.etaxonomy.cdm.common.mediaMetaData.ImageMetaData;
39 import eu.etaxonomy.cdm.io.common.CdmImportBase;
40 import eu.etaxonomy.cdm.io.common.ICdmImport;
41 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
42 import eu.etaxonomy.cdm.io.common.ImportHelper;
43 import eu.etaxonomy.cdm.model.agent.Contact;
44 import eu.etaxonomy.cdm.model.agent.Institution;
45 import eu.etaxonomy.cdm.model.agent.Person;
46 import eu.etaxonomy.cdm.model.agent.Address;//rajout
47 import eu.etaxonomy.cdm.model.agent.Team;
48 import eu.etaxonomy.cdm.model.common.Annotation;
49 import eu.etaxonomy.cdm.model.common.AnnotationType;
50 import eu.etaxonomy.cdm.model.common.CdmBase;
51 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
52 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
53 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
54 import eu.etaxonomy.cdm.model.common.Language;
55 import eu.etaxonomy.cdm.model.common.LanguageString;
56 import eu.etaxonomy.cdm.model.common.Marker;
57 import eu.etaxonomy.cdm.model.common.MarkerType;
58 import eu.etaxonomy.cdm.model.common.Representation;
59 import eu.etaxonomy.cdm.model.common.TermBase;
60 import eu.etaxonomy.cdm.model.common.TermVocabulary;
61 import eu.etaxonomy.cdm.model.common.User;
62 import eu.etaxonomy.cdm.model.common.VersionableEntity;
63 import eu.etaxonomy.cdm.model.description.CategoricalData;
64 import eu.etaxonomy.cdm.model.description.Feature;
65 import eu.etaxonomy.cdm.model.description.FeatureNode;
66 import eu.etaxonomy.cdm.model.description.FeatureTree;
67 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
68 import eu.etaxonomy.cdm.model.description.QuantitativeData;
69 import eu.etaxonomy.cdm.model.description.State;
70 import eu.etaxonomy.cdm.model.description.StateData;
71 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
72 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
73 import eu.etaxonomy.cdm.model.description.TaxonDescription;
74 import eu.etaxonomy.cdm.model.description.TextData;
75 import eu.etaxonomy.cdm.model.description.Modifier;
76 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
77 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
78 import eu.etaxonomy.cdm.model.media.ImageFile;
79 import eu.etaxonomy.cdm.model.media.Media;
80 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
81 import eu.etaxonomy.cdm.model.media.IdentifiableMediaEntity;
82 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
83 import eu.etaxonomy.cdm.model.media.Rights;
84 import eu.etaxonomy.cdm.model.name.NonViralName;
85 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
86 import eu.etaxonomy.cdm.model.reference.IArticle;
87 import eu.etaxonomy.cdm.model.reference.IDatabase;
88 import eu.etaxonomy.cdm.model.reference.IGeneric;
89 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
90 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
91 import eu.etaxonomy.cdm.model.taxon.Taxon;
92 import eu.etaxonomy.cdm.model.taxon.Synonym;
93
94 /**
95 * @author h.fradin
96 * @created 24.10.2008
97 * @version 1.0
98 */
99 @Component("sddDescriptionIO")
100 public class SDDDescriptionIO extends CdmImportBase<SDDImportConfigurator, SDDImportState> implements ICdmImport<SDDImportConfigurator, SDDImportState> {
101 private static final Logger logger = Logger.getLogger(SDDDescriptionIO.class);
102
103 private static int modCount = 1000;
104
105 private Map<String,Person> authors = new HashMap<String,Person>();
106 private Map<String,String> citations = new HashMap<String,String>();
107 private Map<String,String> defaultUnitPrefixes = new HashMap<String,String>();
108 private Map<String,Person> editors = new HashMap<String,Person>();
109 private Map<String,FeatureNode> featureNodes = new HashMap<String,FeatureNode>();
110 private Map<String,Feature> features = new HashMap<String,Feature>();
111 private Map<String,String> locations = new HashMap<String,String>();
112 private Map<String,List<CdmBase>> mediaObject_ListCdmBase = new HashMap<String,List<CdmBase>>();
113 private Map<String,String> mediaObject_Role = new HashMap<String,String>();
114 private Map<String,FeatureNode> nodes = new HashMap<String,FeatureNode>();
115 private Map<String,ReferenceBase> publications = new HashMap<String,ReferenceBase>();
116 private Map<String,StateData> stateDatas = new HashMap<String,StateData>();
117 private Map<String,TaxonDescription> taxonDescriptions = new HashMap<String,TaxonDescription>();
118 private Map<String,NonViralName> taxonNameBases = new HashMap<String,NonViralName>();
119 private Map<String,MeasurementUnit> units = new HashMap<String,MeasurementUnit>();
120 private Map<String,TaxonNode> taxonNodes = new HashMap<String,TaxonNode>();
121
122 private Set<AnnotationType> annotationTypes = new HashSet<AnnotationType>();
123 private Set<Feature> featureSet = new HashSet<Feature>();
124 ReferenceFactory refFactory = ReferenceFactory.newInstance();
125 private ReferenceBase sec = refFactory.newDatabase();
126 private ReferenceBase sourceReference = null;
127
128 private Language datasetLanguage = null;
129
130 private Namespace xmlNamespace = Namespace.getNamespace("xml","http://www.w3.org/XML/1998/namespace");
131
132 private String generatorName = "";
133 private String generatorVersion = "";
134
135 private Set<StatisticalMeasure> statisticalMeasures = new HashSet<StatisticalMeasure>();
136 private Set<VersionableEntity> featureData = new HashSet<VersionableEntity>();
137 private Set<FeatureTree> featureTrees = new HashSet<FeatureTree>();
138 private Set<TaxonomicTree> taxonomicTrees = new HashSet<TaxonomicTree>();
139
140 private Rights copyright = null;
141
142 public SDDDescriptionIO(){
143 super();
144 }
145
146 @Override
147 public boolean doCheck(SDDImportState state){
148 boolean result = true;
149 logger.warn("No check implemented for SDD");
150 return result;
151 }
152
153 // @Override
154 // public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
155 @Override
156 public boolean doInvoke(SDDImportState state){
157
158 TransactionStatus ts = startTransaction();
159 SDDImportConfigurator sddConfig = state.getConfig();
160
161 logger.info("start Datasets ...");
162 // <Datasets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://rs.tdwg.org/UBIF/2006/" xsi:schemaLocation="http://rs.tdwg.org/UBIF/2006/ ../SDD.xsd">
163 Element root = sddConfig.getSourceRoot();
164 boolean success = true;
165 Namespace sddNamespace = sddConfig.getSddNamespace();
166
167 logger.info("start TechnicalMetadata ...");
168 // <TechnicalMetadata created="2006-04-20T10:00:00">
169 importTechnicalMetadata(root, sddNamespace, sddConfig);
170 List<Element> elDatasets = root.getChildren("Dataset",sddNamespace);
171 int i = 0;
172
173 //for each Dataset
174 logger.info("start Dataset ...");
175 for (Element elDataset : elDatasets){
176 importDataset(elDataset, sddNamespace, success, sddConfig);
177 if ((++i % modCount) == 0){ logger.info("Datasets handled: " + i);}
178 logger.info(i + " Datasets handled");
179 }
180 commitTransaction(ts);
181 return success;
182 }
183
184 /* (non-Javadoc)
185 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
186 */
187 protected boolean isIgnore(SDDImportState state){
188 return false;
189 }
190
191
192 // associates the reference of a media object in SDD with a CdmBase Object
193 protected void associateImageWithCdmBase(String refMO, CdmBase cb){
194 if ((refMO != null) && (cb!=null)) {
195 if (!refMO.equals("")) {
196 if (!mediaObject_ListCdmBase.containsKey(refMO)) {
197 List<CdmBase> lcb = new ArrayList<CdmBase>();
198 lcb.add(cb);
199 mediaObject_ListCdmBase.put(refMO,lcb);
200 } else {
201 List<CdmBase> lcb = mediaObject_ListCdmBase.get(refMO);
202 lcb.add(cb);
203 mediaObject_ListCdmBase.put(refMO,lcb);
204 }
205 }
206 }
207 }
208
209 // imports information about the Dataset
210 protected void importDatasetRepresentation(Element parent, Namespace sddNamespace){
211 logger.info("start Representation ...");
212 /* <Representation>
213 <Label>The Genus Viola</Label>
214 <Detail>This is an example for a very simple SDD file, representing a single description with categorical, quantitative, and text character. Compare also the "Fragment*" examples, which contain more complex examples in the form of document fragments. Intended for version="SDD 1.1".</Detail>
215 </Representation>
216 */
217 Element elRepresentation = parent.getChild("Representation",sddNamespace);
218 String label = (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace);
219 String detail = (String)ImportHelper.getXmlInputValue(elRepresentation, "Detail",sddNamespace);
220
221 sec.setTitleCache(label);
222
223 if (detail != null) {
224 Annotation annotation = Annotation.NewInstance(detail, datasetLanguage);
225 annotation.setAnnotationType(AnnotationType.EDITORIAL());
226 sec.addAnnotation(annotation);
227 }
228
229 List<Element> listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace);
230
231 for (Element elMediaObject : listMediaObjects) {
232 String ref = null;
233 String role = null;
234 if (elMediaObject != null) {
235 ref = elMediaObject.getAttributeValue("ref");
236 role = elMediaObject.getAttributeValue("role");
237 }
238 if (ref != null) {
239 if (!ref.equals("")) {
240 this.associateImageWithCdmBase(ref,sourceReference);
241 this.associateImageWithCdmBase(ref,sec);
242 mediaObject_Role.put(ref,role);
243 }
244 }
245 }
246 }
247
248 // imports the representation (label, detail, lang) of a particular SDD element
249 protected void importRepresentation(Element parent, Namespace sddNamespace, VersionableEntity ve, String id, IImportConfigurator config){
250 Element elRepresentation = parent.getChild("Representation",sddNamespace);
251 // <Label xml:lang="la">Viola hederacea Labill.</Label>
252 List<Element> listLabels = elRepresentation.getChildren("Label",sddNamespace);
253 List<Element> listDetails = elRepresentation.getChildren("Detail",sddNamespace);
254 Map<Language,List<String>> langLabDet = new HashMap<Language,List<String>>();
255
256 for (Element elLabel : listLabels){
257 String lang = elLabel.getAttributeValue("lang",xmlNamespace);
258 Language language = null;
259 if (lang != null) {
260 if (!lang.equals("")) {
261 language = getTermService().getLanguageByIso(lang.substring(0, 2));
262 } else {
263 language = datasetLanguage;
264 }
265 } else {
266 language = datasetLanguage;
267 }
268 String label = elLabel.getText();
269 List<String> labDet = new ArrayList<String>(3);
270 labDet.add(label);
271 langLabDet.put(language, labDet);
272 }
273
274 for (Element elDetail : listDetails){
275 String lang = elDetail.getAttributeValue("lang",xmlNamespace);
276 String role = elDetail.getAttributeValue("role");
277 Language language = null;
278 if (lang != null) {
279 if (!lang.equals("")) {
280 language = getTermService().getLanguageByIso(lang.substring(0, 2));
281 } else {
282 language = datasetLanguage;
283 }
284 } else {
285 language = datasetLanguage;
286 }
287 String detail = elDetail.getText();
288 List<String> labDet = langLabDet.get(language);
289 labDet.add(detail);
290 labDet.add(role);
291 langLabDet.put(language, labDet);
292 }
293
294 if (ve instanceof TermBase) {
295 TermBase tb = (TermBase) ve;
296
297 for (Iterator<Language> l = langLabDet.keySet().iterator() ; l.hasNext() ;){
298 Language lang = l.next();
299 List<String> labDet = langLabDet.get(lang);
300 if (labDet.size()>0){
301 if (labDet.size()>1) {
302 tb.addRepresentation(Representation.NewInstance(labDet.get(1), labDet.get(0), labDet.get(0), lang));
303 } else {
304 tb.addRepresentation(Representation.NewInstance(labDet.get(0), labDet.get(0), labDet.get(0), lang));
305 }
306 }
307 ve = tb;
308 }
309
310 } else if (ve instanceof Media) {
311 Media m = (Media) ve;
312
313 for (Iterator<Language> l = langLabDet.keySet().iterator() ; l.hasNext() ;){
314 Language lang = l.next();
315 List<String> labDet = langLabDet.get(lang);
316 if (labDet.get(0) != null){
317 m.addTitle(LanguageString.NewInstance(labDet.get(0), lang));
318 }
319 if (labDet.size()>1) {
320 m.addDescription(labDet.get(1), lang);
321 }
322 ve = m;
323 }
324
325 }
326 //XIM else if
327 if (ve instanceof IdentifiableEntity) {
328 IdentifiableEntity ie = (IdentifiableEntity) ve;
329 List<String> labDet = null;
330
331 if (ve instanceof TaxonNameBase) {
332 if (langLabDet.keySet().contains(getTermService().getLanguageByIso("la"))) {
333 labDet = langLabDet.get(getTermService().getLanguageByIso("la"));
334 } else if (langLabDet.keySet().contains(datasetLanguage)) {
335 labDet = langLabDet.get(datasetLanguage);
336 logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
337 } else {
338 labDet = langLabDet.get(langLabDet.keySet().iterator().next());
339 logger.info("TaxonName " + (String)ImportHelper.getXmlInputValue(elRepresentation, "Label",sddNamespace) + " is not specified as a latin name.");
340 }
341 } else {
342 labDet = langLabDet.get(langLabDet.keySet().iterator().next());
343 }
344
345 ie.setTitleCache(labDet.get(0));
346
347 if (labDet.size()>1) {
348 Annotation annotation = null;
349 if (labDet.get(1) != null) {
350 if (labDet.get(2) != null) {
351 annotation = Annotation.NewInstance(labDet.get(2) + " - " + labDet.get(1), datasetLanguage);
352 } else {
353 annotation = Annotation.NewInstance(labDet.get(1), datasetLanguage);
354 }
355 }
356 ie.addAnnotation(annotation);
357 }
358
359 ve = ie;
360
361 }
362
363 if (ve instanceof IdentifiableMediaEntity){
364 IdentifiableMediaEntity ime = (IdentifiableMediaEntity) ve;
365 Element elLinks = parent.getChild("Links",sddNamespace);
366
367 if (elLinks != null) {
368
369 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
370 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
371 Media link = Media.NewInstance();
372 MediaRepresentation mr = MediaRepresentation.NewInstance();
373 int k = 0;
374 //for each Link
375 for (Element elLink : listLinks){
376
377 try {
378
379 String rel = elLink.getAttributeValue("rel");
380 String href = elLink.getAttributeValue("href");
381
382 mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
383 link.addRepresentation(mr);
384 ime.addMedia(link);
385
386 } catch (Exception e) {
387 //FIXME
388 logger.warn("Import of Link " + k + " failed.");
389 }
390
391 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
392
393 }
394 }
395 }
396
397 List <Element> listMediaObjects = elRepresentation.getChildren("MediaObject",sddNamespace);
398 for (Element elMediaObject : listMediaObjects) {
399 String ref = null;
400 String role = null;
401 if (elMediaObject != null) {
402 ref = elMediaObject.getAttributeValue("ref");
403 role = elMediaObject.getAttributeValue("role");
404 }
405 if (ref != null) {
406 if (!ref.equals("")) {
407 if (ref != null) {
408 if (ve instanceof TaxonDescription) {
409 TaxonDescription td = (TaxonDescription) ve;
410 //TODO: ensure that all images are imported
411 if (td.getDescriptionSources().toArray().length > 0) {
412 this.associateImageWithCdmBase(ref,(ReferenceBase) td.getDescriptionSources().toArray()[0]);
413 } else {
414 ReferenceBase descriptionSource = refFactory.newGeneric();
415 td.addDescriptionSource(descriptionSource);
416 this.associateImageWithCdmBase(ref,descriptionSource);
417 }
418 } else {
419 //System.out.println(parent.getName());
420 this.associateImageWithCdmBase(ref,ve);
421 }
422 }
423
424 }
425 }
426 }
427
428 }
429
430 // imports the representation (label, detail, lang) of a particular SDD element
431 protected void importTechnicalMetadata(Element root, Namespace sddNamespace, SDDImportConfigurator sddConfig){
432 Element elTechnicalMetadata = root.getChild("TechnicalMetadata", sddNamespace);
433 String nameCreated = elTechnicalMetadata.getAttributeValue("created");
434 sourceReference = sddConfig.getSourceReference();
435
436 if (nameCreated != null) {
437 if (!nameCreated.equals("")) {
438 int year = Integer.parseInt(nameCreated.substring(0,4));
439 int monthOfYear = Integer.parseInt(nameCreated.substring(5,7));
440 int dayOfMonth = Integer.parseInt(nameCreated.substring(8,10));
441 int hourOfDay = Integer.parseInt(nameCreated.substring(11,13));
442 int minuteOfHour = Integer.parseInt(nameCreated.substring(14,16));
443 int secondOfMinute = Integer.parseInt(nameCreated.substring(17,19));
444 DateTime created = new DateTime(year,monthOfYear,dayOfMonth,hourOfDay,minuteOfHour,secondOfMinute,0);
445 sourceReference.setCreated(created);
446 sec.setCreated(created);
447 }
448 }
449
450 // <Generator name="n/a, handcrafted instance document" version="n/a"/>
451 Element elGenerator = elTechnicalMetadata.getChild("Generator", sddNamespace);
452 generatorName = elGenerator.getAttributeValue("name");
453 generatorVersion = elGenerator.getAttributeValue("version");
454
455 sec.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
456 sourceReference.addAnnotation(Annotation.NewDefaultLanguageInstance(generatorName + " - " + generatorVersion));
457
458 }
459
460 // imports the complete dataset information
461 protected void importDataset(Element elDataset, Namespace sddNamespace, boolean success, SDDImportConfigurator sddConfig){ // <Dataset xml:lang="en-us">
462
463 importDatasetLanguage(elDataset,sddConfig);
464 importDatasetRepresentation(elDataset, sddNamespace);
465 importRevisionData(elDataset, sddNamespace);
466 importIPRStatements(elDataset, sddNamespace, sddConfig);
467 importTaxonNames(elDataset, sddNamespace, sddConfig);
468 importDescriptiveConcepts(elDataset, sddNamespace, sddConfig);
469 importCharacters(elDataset, sddNamespace, sddConfig, success);
470 importCharacterTrees(elDataset, sddNamespace, sddConfig, success);
471 importCodedDescriptions(elDataset, sddNamespace, sddConfig, success);
472 importAgents(elDataset, sddNamespace, sddConfig, success);
473 importPublications(elDataset, sddNamespace, sddConfig, success);
474 importMediaObjects(elDataset, sddNamespace, sddConfig, success);
475 importTaxonHierarchies(elDataset, sddNamespace, sddConfig, success);
476
477
478 MarkerType editou = MarkerType.NewInstance("Editor", "editor", "edt") ;
479 if (authors != null) {
480 Team team = Team.NewInstance();
481 for (Iterator<Person> author = authors.values().iterator() ; author.hasNext() ;){
482 team.addTeamMember(author.next());
483 }
484 if (editors != null) {
485 Marker marker = Marker.NewInstance();
486 marker.setMarkerType(editou);
487 for (Iterator<Person> editor = editors.values().iterator() ; editor.hasNext() ;){
488 Person edit = editor.next();
489 edit.addMarker(marker);
490 team.addTeamMember(edit);
491 }
492 }
493 sec.setAuthorTeam(team);
494 sourceReference.setAuthorTeam(team);
495 }
496
497 if (editors != null) {
498 Person ed = Person.NewInstance();
499 for (Iterator<Person> editor = editors.values().iterator() ; editor.hasNext() ;){
500 ed = editor.next();
501 }
502 // TODO updatedBy refactored to use a user account, so setting a person is no longer applicable
503 // sec.setUpdatedBy(ed);
504 // sourceReference.setUpdatedBy(ed);
505 }
506
507 if (copyright != null) {
508 sourceReference.addRights(copyright);
509 sec.addRights(copyright);
510 }
511
512 for (Iterator<String> refCD = taxonDescriptions.keySet().iterator() ; refCD.hasNext() ;){
513 String ref = refCD.next();
514 TaxonDescription td = taxonDescriptions.get(ref);
515 td.addDescriptionSource(sec);
516 if (citations.containsKey(ref)) {
517 IArticle publication = (IArticle) publications.get(citations.get(ref));
518 if (locations.containsKey(ref)) {
519 Annotation location = Annotation.NewInstance(locations.get(ref), datasetLanguage);
520 AnnotationType annotationType = AnnotationType.NewInstance("", "location", "");
521 annotationTypes.add(annotationType);
522 location.setAnnotationType(annotationType);
523 ((ReferenceBase)publication).addAnnotation(location);
524 }
525 td.addDescriptionSource((ReferenceBase)publication);
526 }
527 }
528 logger.info("end makeTaxonDescriptions ...");
529
530 //sddConfig.setSourceReference(sourceReference);
531
532 //saving of all imported data into the CDM db
533 ITermService termService = getTermService();
534 for (Iterator<StateData> k = stateDatas.values().iterator() ; k.hasNext() ;){
535 StateData sd = k.next();
536 termService.save(sd.getState());
537 }
538 for (Iterator<Feature> k = features.values().iterator() ; k.hasNext() ;){
539 Feature feature = k.next();
540 termService.save(feature);
541 }
542 termService.save(editou);
543 if (units != null) {
544 for (Iterator<MeasurementUnit> k = units.values().iterator() ; k.hasNext() ;){
545 MeasurementUnit unit = k.next();
546 if (unit != null) {
547 termService.save(unit);
548 }
549 }
550 }
551 for (Iterator<StatisticalMeasure> k = statisticalMeasures.iterator() ; k.hasNext() ;) {
552 StatisticalMeasure sm = k.next();
553 termService.save(sm);
554 }
555
556 for (Iterator<AnnotationType> at = annotationTypes.iterator() ; at.hasNext() ;) {
557 AnnotationType annotationType = at.next();
558 termService.save(annotationType);
559 }
560
561 IReferenceService referenceService = getReferenceService();
562 // referenceService.saveReference(sourceReference);
563 for (Iterator<ReferenceBase> k = publications.values().iterator() ; k.hasNext() ;){
564 ReferenceBase publication = (ReferenceBase) k.next();
565 referenceService.save(publication);
566 }
567
568 IAgentService agentService = getAgentService();/*
569 for (Iterator<Person> p = authors.values().iterator() ; p.hasNext() ;) {
570 Contact contact = new Contact();
571 Person person = p.next();
572 Address address = Address.NewInstance();
573 address.setRegion("Ondurera");
574 contact.addAddress(address);
575 person.setContact(contact);
576 agentService.save(person);
577 }
578
579 /* editors.put("id10", Person.NewTitledInstance("Maxou"));
580 for (Iterator<Person> p = editors.values().iterator() ; p.hasNext() ;) {
581 Person person = p.next();
582 agentService.save(person);
583 }
584 Person mixou = Person.NewInstance();
585 mixou.getMarkers() ;
586 agentService.save(Person.NewTitledInstance("Mixou"));
587 Institution instit = Institution.NewInstance();
588 instit.setName("Maxtitution");
589 agentService.save(instit);*/
590
591 // Returns a CdmApplicationController created by the values of this configuration.
592 IDescriptionService descriptionService = getDescriptionService();
593
594 for (Iterator<TaxonDescription> k = taxonDescriptions.values().iterator() ; k.hasNext() ;){
595 TaxonDescription taxonDescription = k.next();
596 // Persists a Description
597 descriptionService.save(taxonDescription);
598 }
599
600 //descriptionService.saveFeatureNodeAll(featureNodes.values());
601
602 for (Iterator<FeatureTree> k = featureTrees.iterator() ; k.hasNext() ;) {
603 FeatureTree tree = k.next();
604 getFeatureTreeService().save(tree);
605 }
606
607 for (Iterator<TaxonomicTree> k = taxonomicTrees.iterator() ; k.hasNext() ;) {
608 TaxonomicTree tree = k.next();
609 getTaxonTreeService().save(tree);
610 }
611
612 }
613
614 // imports the default language of the dataset
615 protected void importDatasetLanguage(Element elDataset, SDDImportConfigurator sddConfig){
616 String nameLang = elDataset.getAttributeValue("lang",xmlNamespace);
617
618 if (!nameLang.equals("")) {
619 String iso = nameLang.substring(0, 2);
620 datasetLanguage = getTermService().getLanguageByIso(iso);
621 } else {
622 datasetLanguage = Language.ENGLISH();
623 }
624 if (datasetLanguage == null) {
625 datasetLanguage = Language.ENGLISH();
626 }
627 }
628
629
630
631
632
633
634 // imports the revision data associated with the Dataset (authors, modifications)
635 protected void importRevisionData(Element elDataset, Namespace sddNamespace){
636 // <RevisionData>
637 logger.info("start RevisionData ...");
638 Element elRevisionData = elDataset.getChild("RevisionData",sddNamespace);
639 if (elRevisionData != null){
640 // <Creators>
641 Element elCreators = elRevisionData.getChild("Creators",sddNamespace);
642
643 // <Agent role="aut" ref="a1"/>
644 List<Element> listAgents = elCreators.getChildren("Agent", sddNamespace);
645
646 int j = 0;
647 //for each Agent
648 for (Element elAgent : listAgents){
649
650 String role = elAgent.getAttributeValue("role");
651 String ref = elAgent.getAttributeValue("ref");
652 if (role.equals("aut")) {
653 if(!ref.equals("")) {
654 authors.put(ref, null);
655 }
656 }
657 if (role.equals("edt")) {
658 if(!ref.equals("")) {
659 editors.put(ref, null);
660 }
661 }
662 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
663
664 }
665
666 // <DateModified>2006-04-08T00:00:00</DateModified>
667 String stringDateModified = (String)ImportHelper.getXmlInputValue(elRevisionData, "DateModified",sddNamespace);
668
669 if (stringDateModified != null) {
670 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
671 Date d = null;
672 try {
673 d = sdf.parse(stringDateModified);
674 } catch(Exception e) {
675 System.err.println("Exception :");
676 e.printStackTrace();
677 }
678
679 DateTime updated = null;
680 if (d != null) {
681 updated = new DateTime(d);
682 sourceReference.setUpdated(updated);
683 sec.setUpdated(updated);
684 }
685 }
686 }
687 }
688
689 // imports ipr statements associated with a dataset
690 protected void importIPRStatements(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
691 // <IPRStatements>
692 logger.info("start IPRStatements ...");
693 Element elIPRStatements = elDataset.getChild("IPRStatements",sddNamespace);
694 // <IPRStatement role="Copyright">
695 if (elIPRStatements != null) {
696 List<Element> listIPRStatements = elIPRStatements.getChildren("IPRStatement", sddNamespace);
697 int j = 0;
698 //for each IPRStatement
699
700 for (Element elIPRStatement : listIPRStatements){
701
702 String role = elIPRStatement.getAttributeValue("role");
703 // <Label xml:lang="en-au">(c) 2003-2006 Centre for Occasional Botany.</Label>
704 Element elLabel = elIPRStatement.getChild("Label",sddNamespace);
705 String lang = "";
706 if (elLabel != null) {
707 lang = elLabel.getAttributeValue("lang",xmlNamespace);
708 }
709 String label = (String)ImportHelper.getXmlInputValue(elIPRStatement, "Label",sddNamespace);
710
711 if (role.equals("Copyright")) {
712 Language iprLanguage = null;
713 if (lang != null) {
714 if (!lang.equals("")) {
715 iprLanguage = getTermService().getLanguageByIso(lang.substring(0, 2));
716 //iprLanguage = datasetLanguage;
717 } else {
718 iprLanguage = datasetLanguage;
719 }
720 }
721 if (iprLanguage == null) {
722 iprLanguage = datasetLanguage;
723 }
724 copyright = Rights.NewInstance(label, iprLanguage);
725 }
726
727 if (copyright != null) {
728 sourceReference.addRights(copyright);
729 sec.addRights(copyright);
730 }
731
732 if ((++j % modCount) == 0){ logger.info("IPRStatements handled: " + j);}
733
734 }
735 }
736 }
737
738 // imports the taxon names
739 protected void importTaxonNames(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
740 // <TaxonNames>
741 logger.info("start TaxonNames ...");
742 Element elTaxonNames = elDataset.getChild("TaxonNames",sddNamespace);
743 // <TaxonName id="t1" uri="urn:lsid:authority:namespace:my-own-id">
744 if (elTaxonNames != null) {
745 List<Element> listTaxonNames = elTaxonNames.getChildren("TaxonName", sddNamespace);
746 int j = 0;
747 //for each TaxonName
748 for (Element elTaxonName : listTaxonNames){
749
750 String id = elTaxonName.getAttributeValue("id");
751 String uri = elTaxonName.getAttributeValue("uri");
752
753 NonViralName tnb = null;
754 if (!id.equals("")) {
755 tnb = NonViralName.NewInstance(null);
756 IdentifiableSource source = null;
757 if (uri != null) {
758 if (!uri.equals("")) {
759 source = IdentifiableSource.NewInstance(id, "TaxonName", refFactory.newGeneric(), uri);
760 }
761 } else {
762 source = IdentifiableSource.NewInstance(id, "TaxonName");
763 }
764 tnb.addSource(source);
765 taxonNameBases.put(id,tnb);
766 }
767
768 // <Representation>
769 // <Label xml:lang="la">Viola hederacea Labill.</Label>
770 importRepresentation(elTaxonName, sddNamespace, tnb, id, sddConfig);
771
772 if ((++j % modCount) == 0){ logger.info("TaxonNames handled: " + j);}
773
774 }
775 }
776 }
777
778 // imports the representation (label, detail, lang) of a particular SDD element
779 protected void importCharacters(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
780 // <Characters>
781 logger.info("start Characters ...");
782 Element elCharacters = elDataset.getChild("Characters", sddNamespace);
783
784 // <CategoricalCharacter id="c1">
785 if (elCharacters != null) {
786 List<Element> elCategoricalCharacters = elCharacters.getChildren("CategoricalCharacter", sddNamespace);
787 int j = 0;
788 //for each CategoricalCharacter
789 for (Element elCategoricalCharacter : elCategoricalCharacters){
790 try {
791
792 String idCC = elCategoricalCharacter.getAttributeValue("id");
793
794 // <Representation>
795 // <Label> Leaf complexity</Label>
796 // </Representation>
797
798 Feature categoricalCharacter = Feature.NewInstance();
799 categoricalCharacter.setKindOf(Feature.DESCRIPTION());
800 importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig);
801
802 categoricalCharacter.setSupportsCategoricalData(true);
803
804 // <States>
805 Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
806
807 // <StateDefinition id="s1">
808 List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
809 TermVocabulary<State> termVocabularyState = new TermVocabulary<State>();
810 int k = 0;
811 //for each StateDefinition
812 for (Element elStateDefinition : elStateDefinitions){
813
814 if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));}
815
816 String idSD = elStateDefinition.getAttributeValue("id");
817 // <Representation>
818 // <Label>Simple</Label>
819 // <MediaObject ref="ib" role="Primary"/>
820 // </Representation>
821 State state = State.NewInstance();
822 importRepresentation(elStateDefinition, sddNamespace, state, idSD, sddConfig);
823
824 StateData stateData = StateData.NewInstance();
825 stateData.setState(state);
826 termVocabularyState.addTerm(state);
827 stateDatas.put(idSD,stateData);
828 }
829 categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
830 features.put(idCC, categoricalCharacter);
831
832 } catch (Exception e) {
833 //FIXME
834 logger.warn("Import of CategoricalCharacter " + j + " failed.");
835 success = false;
836 }
837
838 if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);}
839
840 }
841
842 // <QuantitativeCharacter id="c2">
843 List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
844 j = 0;
845 //for each QuantitativeCharacter
846 for (Element elQuantitativeCharacter : elQuantitativeCharacters){
847
848 try {
849
850 String idQC = elQuantitativeCharacter.getAttributeValue("id");
851
852 // <Representation>
853 // <Label>Leaf length</Label>
854 // </Representation>
855 Feature quantitativeCharacter = Feature.NewInstance();
856 quantitativeCharacter.setKindOf(Feature.DESCRIPTION());
857 importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig);
858
859 quantitativeCharacter.setSupportsQuantitativeData(true);
860
861 // <MeasurementUnit>
862 // <Label role="Abbrev">m</Label>
863 // </MeasurementUnit>
864 Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
865 String label = "";
866 String role = "";
867 if (elMeasurementUnit != null) {
868 Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
869 role = elLabel.getAttributeValue("role");
870 label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
871 }
872
873 MeasurementUnit unit = null;
874 if (!label.equals("")){
875 if (role != null) {
876 if (role.equals("Abbrev")){
877 unit = MeasurementUnit.NewInstance(label,label,label);
878 }
879 } else {
880 unit = MeasurementUnit.NewInstance(label,label,label);
881 }
882 }
883
884 if (unit != null) {
885 units.put(idQC, unit);
886 }
887
888 //<Default>
889 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
890 //</Default>
891 Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
892 if (elDefault != null) {
893 String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
894 if (!measurementUnitPrefix.equals("")){
895 defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
896 }
897 }
898
899 features.put(idQC, quantitativeCharacter);
900
901 } catch (Exception e) {
902 //FIXME
903 logger.warn("Import of QuantitativeCharacter " + j + " failed.");
904 success = false;
905 }
906
907 if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);}
908
909 }
910
911 // <TextCharacter id="c3">
912 List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
913 j = 0;
914 //for each TextCharacter
915 for (Element elTextCharacter : elTextCharacters){
916
917 try {
918
919 String idTC = elTextCharacter.getAttributeValue("id");
920
921 // <Representation>
922 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
923 // </Representation>
924 Feature textCharacter = Feature.NewInstance();
925 textCharacter.setKindOf(Feature.DESCRIPTION());
926 importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig);
927
928 textCharacter.setSupportsTextData(true);
929
930 features.put(idTC, textCharacter);
931
932 } catch (Exception e) {
933 //FIXME
934 logger.warn("Import of TextCharacter " + j + " failed.");
935 success = false;
936 }
937
938 if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);}
939
940 }
941
942 }
943
944 for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
945 featureSet.add(f.next());
946 }
947
948 }
949
950 // imports the descriptions of taxa (specimens TODO)
951 protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
952 // <CodedDescriptions>
953 logger.info("start CodedDescriptions ...");
954 Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
955
956 // <CodedDescription id="D101">
957
958 if (elCodedDescriptions != null) {
959 List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
960 int j = 0;
961 //for each CodedDescription
962
963 for (Element elCodedDescription : listCodedDescriptions){
964
965 try {
966
967 String idCD = elCodedDescription.getAttributeValue("id");
968
969 // <Representation>
970 // <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
971 // </Representation>
972 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
973 importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig);
974
975 // <Scope>
976 // <TaxonName ref="t1"/>
977 // <Citation ref="p1" location="p. 30"/>
978 // </Scope>
979 Element elScope = elCodedDescription.getChild("Scope",sddNamespace);
980 String ref = "";
981 Taxon taxon = null;
982 if (elScope != null) {
983 Element elTaxonName = elScope.getChild("TaxonName",sddNamespace);
984 ref = elTaxonName.getAttributeValue("ref");
985
986 NonViralName taxonNameBase = taxonNameBases.get(ref);
987 taxon = Taxon.NewInstance(taxonNameBase, sec);
988 }
989
990 String refCitation = "";
991 String location = "";
992
993 if (elScope != null) {
994 Element elCitation = elScope.getChild("Citation",sddNamespace);
995 if (elCitation != null) {
996 refCitation = elCitation.getAttributeValue("ref");
997 location = elCitation.getAttributeValue("location");
998 }
999 }
1000
1001 // <SummaryData>
1002 Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
1003
1004 if (elSummaryData != null) {
1005
1006 // <Categorical ref="c4">
1007 List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
1008 int k = 0;
1009 //for each Categorical
1010 for (Element elCategorical : elCategoricals){
1011 if ((++k % modCount) == 0){ logger.info("Categorical handled: " + (k-1));}
1012 ref = elCategorical.getAttributeValue("ref");
1013 Feature feature = features.get(ref);
1014 CategoricalData categoricalData = CategoricalData.NewInstance();
1015 categoricalData.setFeature(feature);
1016
1017 // <State ref="s3"/>
1018 List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
1019 int l = 0;
1020 //for each State
1021 for (Element elState : elStates){
1022 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1023 ref = elState.getAttributeValue("ref");
1024 StateData stateData = stateDatas.get(ref);
1025 categoricalData.addState(stateData);
1026 }
1027 taxonDescription.addElement(categoricalData);
1028 }
1029
1030 // <Quantitative ref="c2">
1031 List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
1032 k = 0;
1033 //for each Quantitative
1034 for (Element elQuantitative : elQuantitatives){
1035 if ((++k % modCount) == 0){ logger.info("Quantitative handled: " + (k-1));}
1036 ref = elQuantitative.getAttributeValue("ref");
1037 Feature feature = features.get(ref);
1038 QuantitativeData quantitativeData = QuantitativeData.NewInstance();
1039 quantitativeData.setFeature(feature);
1040
1041 MeasurementUnit unit = units.get(ref);
1042 String prefix = defaultUnitPrefixes.get(ref);
1043 if (unit != null) {
1044 String u = unit.getLabel();
1045 if (prefix != null) {
1046 u = prefix + u;
1047 }
1048 unit.setLabel(u);
1049 quantitativeData.setUnit(unit);
1050 }
1051
1052 // <Measure type="Min" value="2.3"/>
1053 List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
1054 int l = 0;
1055 //for each State
1056 for (Element elMeasure : elMeasures){
1057 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
1058 String type = elMeasure.getAttributeValue("type");
1059 String value = elMeasure.getAttributeValue("value");
1060 float v = Float.parseFloat(value);
1061 StatisticalMeasure t = null;
1062 if (type.equals("Min")) {
1063 t = StatisticalMeasure.MIN();
1064 } else if (type.equals("Mean")) {
1065 t = StatisticalMeasure.AVERAGE();
1066 } else if (type.equals("Max")) {
1067 t = StatisticalMeasure.MAX();
1068 } else if (type.equals("SD")) {
1069 // Create a new StatisticalMeasure for standard deviation
1070 t = StatisticalMeasure.STANDARD_DEVIATION();
1071 } else if (type.equals("N")) {
1072 t = StatisticalMeasure.SAMPLE_SIZE();
1073 } else {
1074 t = StatisticalMeasure.NewInstance(type,type,type);
1075 statisticalMeasures.add(t);
1076 }
1077
1078 StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
1079 statisticalValue.setValue(v);
1080 statisticalValue.setType(t);
1081 quantitativeData.addStatisticalValue(statisticalValue);
1082 featureData.add(statisticalValue);
1083 }
1084 taxonDescription.addElement(quantitativeData);
1085 }
1086
1087 // <TextChar ref="c3">
1088 List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
1089 k = 0;
1090 //for each TextChar
1091 for (Element elTextChar : elTextChars){
1092 if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1093 ref = elTextChar.getAttributeValue("ref");
1094 Feature feature = features.get(ref);
1095 TextData textData = TextData.NewInstance();
1096 textData.setFeature(feature);
1097
1098 // <Content>Free form text</Content>
1099 String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1100 textData.putText(content, datasetLanguage);
1101 taxonDescription.addElement(textData);
1102 }
1103
1104 }
1105
1106 if (taxon != null) {
1107 taxon.addDescription(taxonDescription);
1108 }
1109
1110 if (!refCitation.equals("")){
1111 citations.put(idCD,refCitation);
1112 }
1113
1114 if (!location.equals("")){
1115 locations.put(idCD, location);
1116 }
1117
1118 taxonDescription.setDescriptiveSystem(featureSet);
1119
1120 taxonDescriptions.put(idCD, taxonDescription);
1121
1122 } catch (Exception e) {
1123 //FIXME
1124 logger.warn("Import of CodedDescription " + j + " failed.");
1125 success = false;
1126 }
1127
1128 if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1129
1130 }
1131
1132 }
1133 }
1134
1135 // imports the persons associated with the dataset creation, modification, related publications
1136 protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1137 // <Agents>
1138 logger.info("start Agents ...");
1139 Element elAgents = elDataset.getChild("Agents",sddNamespace);
1140 if (elAgents != null) {
1141 // <Agent id="a1">
1142 List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1143 int j = 0;
1144 //for each Agent
1145 for (Element elAgent : listAgents){
1146
1147 try {
1148
1149 String idA = elAgent.getAttributeValue("id");
1150
1151 // <Representation>
1152 // <Label>Kevin Thiele</Label>
1153 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1154 // </Representation>
1155 Person person = Person.NewInstance();
1156 importRepresentation(elAgent, sddNamespace, person, idA, sddConfig);
1157 person.addSource(IdentifiableSource.NewInstance(idA, "Agent"));
1158
1159 /*XIM <Links>
1160 Element elLinks = elAgent.getChild("Links",sddNamespace);
1161
1162 if (elLinks != null) {
1163
1164 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1165 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1166 int k = 0;
1167 //for each Link
1168 for (Element elLink : listLinks){
1169
1170 try {
1171
1172 String rel = elLink.getAttributeValue("rel");
1173 String href = elLink.getAttributeValue("href");
1174
1175 Media link = Media.NewInstance();
1176 MediaRepresentation mr = MediaRepresentation.NewInstance();
1177 mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1178 link.addRepresentation(mr);
1179 person.addMedia(link);
1180
1181 } catch (Exception e) {
1182 //FIXME
1183 logger.warn("Import of Link " + k + " failed.");
1184 success = false;
1185 }
1186
1187 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
1188
1189 }
1190 }
1191 */
1192 if (authors.containsKey(idA)) {
1193 authors.put(idA,person);
1194 }
1195
1196 if (editors.containsKey(idA)) {
1197 editors.put(idA, person);
1198 }
1199
1200 } catch (Exception e) {
1201 //FIXME
1202 logger.warn("Import of Agent " + j + " failed.");
1203 success = false;
1204 }
1205
1206 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
1207
1208 }
1209 }
1210 }
1211
1212 // imports publications related with the data set
1213 protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1214 // <Publications>
1215 logger.info("start Publications ...");
1216 Element elPublications = elDataset.getChild("Publications",sddNamespace);
1217
1218 if (elPublications != null) {
1219 // <Publication id="p1">
1220 List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1221 int j = 0;
1222 //for each Publication
1223 for (Element elPublication : listPublications){
1224
1225 try {
1226
1227 String idP = elPublication.getAttributeValue("id");
1228
1229 // <Representation>
1230 // <Label>Sample Citation</Label>
1231 // </Representation>
1232 ReferenceBase publication = refFactory.newArticle();
1233 importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig);
1234
1235 publications.put(idP,publication);
1236
1237 } catch (Exception e) {
1238 //FIXME
1239 logger.warn("Import of Publication " + j + " failed.");
1240 success = false;
1241 }
1242
1243 if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
1244
1245 }
1246 }
1247 }
1248
1249 // imports media objects such as images
1250 protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1251 // <MediaObjects>
1252 logger.info("start MediaObjects ...");
1253 Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1254
1255 if (elMediaObjects != null) {
1256 // <MediaObject id="m1">
1257 List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1258 int j = 0;
1259 //for each Publication
1260 for (Element elMO : listMediaObjects){
1261
1262 String id = "";
1263
1264 try {
1265 String idMO = elMO.getAttributeValue("id");
1266 id = idMO;
1267
1268 // <Representation>
1269 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1270 // </Representation>
1271 Media media = Media.NewInstance();
1272 importRepresentation(elMO, sddNamespace, media, idMO, sddConfig);
1273
1274 // <Type>Image</Type>
1275 // <Source href="http://test.edu/test.jpg"/>
1276 String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1277
1278 if ((type != null) && (type.equals("Image"))) {
1279 Element elSource = elMO.getChild("Source",sddNamespace);
1280 String href = elSource.getAttributeValue("href");
1281
1282 ImageMetaData imageMetaData = ImageMetaData.newInstance();
1283 ImageFile image = null;
1284 if (href.substring(0,7).equals("http://")) {
1285 try{
1286 URL url = new URL(href);
1287
1288 imageMetaData.readMetaData(url.toURI(), 0);
1289 image = ImageFile.NewInstance(url.toString(), null, imageMetaData);
1290 } catch (MalformedURLException e) {
1291 logger.error("Malformed URL", e);
1292 }
1293 } else {
1294 String sns = sddConfig.getSourceNameString();
1295 File f = new File(sns);
1296 File parent = f.getParentFile();
1297 String fi = href;
1298 //XIMString fi = parent.toString() + File.separator + href; enlever le file:/ ou truc du genre
1299 File file = new File(fi);
1300 imageMetaData.readMetaData(file.toURI(), 0);
1301 image = ImageFile.NewInstance(file.toString(), null, imageMetaData);
1302 }
1303 MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1304 representation.addRepresentationPart(image);
1305
1306 media.addRepresentation(representation);
1307
1308 ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1309 if (lcb != null) {
1310 for (int k = 0; k < lcb.size(); k++) {
1311 if (lcb.get(k) instanceof DefinedTermBase) {
1312 DefinedTermBase dtb = (DefinedTermBase) lcb.get(k);
1313 // if (lcb.get(0) instanceof DefinedTermBase) {
1314 // DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1315 // if (dtb!=null) {
1316 // if (k == 0) {
1317 dtb.addMedia(media);
1318 //System.out.println(dtb.getLabel());
1319 // } else {
1320 // Media me = (Media) media.clone();
1321 // dtb.addMedia(me);
1322 // }
1323 // }
1324 } else if (lcb.get(k) instanceof ReferenceBase) {
1325 ReferenceBase rb = (ReferenceBase) lcb.get(k);
1326 //} else if (lcb.get(0) instanceof ReferenceBase) {
1327 //ReferenceBase rb = (ReferenceBase) lcb.get(0);
1328 // rb.setTitleCache(label);
1329 // if (rb!=null) {
1330 // if (k == 0) {
1331 rb.addMedia(media);
1332 //System.out.println(rb.getTitle());
1333 // } else {
1334 // Media me = (Media) media.clone();
1335 // rb.addMedia(me);
1336 // }
1337 // }
1338 }/* else if (lcb.get(k) instanceof TaxonNameBase){
1339 TaxonNameBase tb = (TaxonNameBase) lcb.get(k);
1340 tb.addMedia(media);
1341 }*/
1342 }
1343 }
1344 }
1345
1346 } catch (Exception e) {
1347 //FIXME
1348 logger.warn("Could not attach MediaObject " + j + "(SDD: " + id + ") to several objects.");
1349 success = false;
1350 }
1351
1352 if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j);
1353
1354 }
1355 }
1356 }
1357 }
1358
1359 // imports the <DescriptiveConcepts> block
1360 protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
1361 // <DescriptiveConcepts>
1362 logger.info("start DescriptiveConcepts ...");
1363 Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1364 // <DescriptiveConcept id="b">
1365 if (elDescriptiveConcepts != null) {
1366 List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1367 int j = 0;
1368 //for each DescriptiveConcept
1369 int g = 1;
1370
1371 for (Element elDescriptiveConcept : listDescriptiveConcepts){
1372
1373 String id = elDescriptiveConcept.getAttributeValue("id");
1374 String uri = elDescriptiveConcept.getAttributeValue("uri");
1375
1376 FeatureNode fn = null;
1377
1378 if (!id.equals("")) {
1379 fn = FeatureNode.NewInstance();
1380 Feature feature = Feature.NewInstance();
1381 feature.setKindOf(Feature.DESCRIPTION());
1382 // <Representation>
1383 // <Label>Body</Label>
1384 importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, sddConfig);
1385 features.put("g" + g, feature);
1386 g++;
1387 Element elModifiers = elDescriptiveConcept.getChild("Modifiers", sddNamespace);
1388 if (elModifiers !=null){
1389 List<Element> listModifiers = elModifiers.getChildren("Modifier", sddNamespace);
1390 for (Element elModifier : listModifiers) {
1391 TermVocabulary<Modifier> termVocabularyState = new TermVocabulary<Modifier>("test","test","test","test");
1392 Modifier modif = Modifier.NewInstance();
1393 String idmod = elModifier.getAttributeValue("id");
1394 importRepresentation(elModifier, sddNamespace, modif, idmod, sddConfig);
1395 termVocabularyState.addTerm(modif);
1396 //XIMfeature.addRecommendedModifierEnumeration(termVocabularyState);
1397 }
1398 }
1399
1400 fn.setFeature(feature);
1401
1402 // TODO if an OriginalSource can be attached to a FeatureNode or a Feature
1403 // OriginalSource source = null;
1404 // if (uri != null) {
1405 // if (!uri.equals("")) {
1406 // source = OriginalSource.NewInstance(id, "DescriptiveConcept", Generic.NewInstance(), uri);
1407 // }
1408 // } else {
1409 // source = OriginalSource.NewInstance(id, "DescriptiveConcept");
1410 // }
1411 // f.addSource(source);
1412
1413 featureNodes.put(id,fn);
1414 }
1415
1416 if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1417
1418 }
1419 }
1420 }
1421
1422 protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1423 // <CharacterTrees>
1424 logger.info("start CharacterTrees ...");
1425 Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1426
1427 // <CharacterTree>
1428
1429 if (elCharacterTrees != null) {
1430 List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1431 int j = 0;
1432 //for each CharacterTree
1433 for (Element elCharacterTree : listCharacterTrees){
1434 try {
1435 Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1436 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1437 Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);
1438 /*XIMList<Element> listRoles = elDesignedFor.getChildren("Role",sddNamespace);
1439 boolean isgroups = false;
1440
1441 for (Element elRole : listRoles){
1442 if (elRole.getText().equals("Filtering")) {
1443 isgroups = true;
1444 }
1445 }*/
1446
1447 // only treats the case of flat groups containing characters
1448 // should also be added: dependencies between characters,
1449 //XIM
1450
1451 FeatureTree feattree = FeatureTree.NewInstance();
1452 importRepresentation(elCharacterTree, sddNamespace, feattree, "", sddConfig);
1453 FeatureNode root = feattree.getRoot();
1454
1455 Element elNodes = elCharacterTree.getChild("Nodes", sddNamespace);
1456 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1457 for (Element elNode : listNodes){
1458 String idN = elNode.getAttributeValue("id");
1459 FeatureNode fn = null;
1460 if (!idN.equals("")) {
1461 Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1462 String refDC = elDescriptiveConcept.getAttributeValue("ref");
1463 fn = featureNodes.get(refDC);
1464 Element elParent = elNode.getChild("Parent", sddNamespace);
1465 if (elParent!=null){
1466 String refP = elParent.getAttributeValue("ref");
1467 if (!refP.equals("")) {
1468 FeatureNode parent = nodes.get(refP);
1469 parent.addChild(fn);
1470 }
1471 }
1472 else {
1473 root.addChild(fn);
1474 }
1475 }
1476 nodes.put(idN, fn);
1477 }
1478
1479 List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1480 for (Element elCharNode : listCharNodes){
1481 Element elParent = elCharNode.getChild("Parent", sddNamespace);
1482 Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1483 FeatureNode fn = FeatureNode.NewInstance();
1484 if (elParent!=null){
1485 String refP = elParent.getAttributeValue("ref");
1486 if (!refP.equals("")) {
1487 FeatureNode parent = nodes.get(refP);
1488 parent.addChild(fn);
1489 }
1490 }
1491 String refC = elCharacter.getAttributeValue("ref");
1492 if (!refC.equals("")){
1493 Feature character = features.get(refC);
1494 fn.setFeature(character);
1495 // if method setParent() in FeatureNode becomes visible
1496 // fn.setParent(parent);
1497 }
1498 nodes.put(refC, fn);
1499 }
1500 featureTrees.add(feattree);
1501 }
1502
1503 catch (Exception e) {
1504 //FIXME
1505 logger.warn("Import of Character tree " + j + " failed.");
1506 success = false;
1507 }
1508
1509 if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1510
1511 }
1512
1513 }
1514 }
1515
1516
1517
1518
1519
1520 protected void importTaxonHierarchies(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1521
1522 logger.info("start TaxonHierarchies ...");
1523 Element elTaxonHierarchies = elDataset.getChild("TaxonHierarchies",sddNamespace);
1524
1525 if (elTaxonHierarchies != null) {
1526 List<Element> listTaxonHierarchies = elTaxonHierarchies.getChildren("TaxonHierarchy", sddNamespace);
1527 int j = 0;
1528 for (Element elTaxonHierarchy : listTaxonHierarchies){
1529 try {
1530 Element elRepresentation = elTaxonHierarchy.getChild("Representation",sddNamespace);
1531 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1532
1533 TaxonomicTree taxonomicTree = TaxonomicTree.NewInstance(label);
1534 importRepresentation(elTaxonHierarchy, sddNamespace, taxonomicTree, "", sddConfig);
1535 Set<TaxonNode> root = taxonomicTree.getRootNodes();
1536 Element elNodes = elTaxonHierarchy.getChild("Nodes", sddNamespace);
1537 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1538 for (Element elNode : listNodes){
1539 String idN = elNode.getAttributeValue("id");
1540 TaxonNameBase tnb = null;
1541 if (!idN.equals("")) {
1542 Element elTaxonName = elNode.getChild("TaxonName", sddNamespace);
1543 String refTN = elTaxonName.getAttributeValue("ref");
1544 tnb = taxonNameBases.get(refTN);
1545 Taxon taxonou = (Taxon) tnb.getTaxa().iterator().next() ;
1546 Element elParent = elNode.getChild("Parent", sddNamespace);
1547 if (elParent!=null){
1548 String refP = elParent.getAttributeValue("ref");
1549 if (!refP.equals("")) {
1550 TaxonNode parent = taxonNodes.get(refP);
1551 //TaxonNode child = parent.addChildTaxon(Taxon.NewInstance(tnb, sec), sec, "", Synonym.NewInstance(tnb, sec));
1552 TaxonNode child = parent.addChildTaxon(taxonou, sec, "", Synonym.NewInstance(tnb, sec));
1553 taxonNodes.put(idN,child);
1554 }
1555 }
1556 else {
1557 //XIM addChildtaxon (taxon, referencebase ???
1558 //TaxonNode tn = taxonomicTree.addChildTaxon(Taxon.NewInstance(tnb, sec), sec, "", Synonym.NewInstance(tnb, sec));
1559 TaxonNode tn = taxonomicTree.addChildTaxon(taxonou, sec, "", Synonym.NewInstance(tnb, sec));
1560 taxonNodes.put(idN,tn);
1561 }
1562 }
1563 }
1564
1565 taxonomicTrees.add(taxonomicTree);
1566 }
1567
1568 catch (Exception e) {
1569 //FIXME
1570 logger.warn("Import of Taxon Hierarchy " + j + " failed.");
1571 success = false;
1572 }
1573
1574 if ((++j % modCount) == 0){ logger.info("TaxonHierarchies handled: " + j);}
1575
1576 }
1577
1578 }
1579 }
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589 // imports the descriptions of taxa (specimens TODO)
1590 protected void importCharacterTrees2(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1591 // <CharacterTrees>
1592 logger.info("start CharacterTrees ...");
1593 Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1594
1595 // <CharacterTree>
1596
1597 if (elCharacterTrees != null) {
1598 List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1599 int j = 0;
1600 //for each CharacterTree
1601 for (Element elCharacterTree : listCharacterTrees){
1602
1603 try {
1604 Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1605 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1606 Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);
1607 List<Element> listRoles = elDesignedFor.getChildren("Role",sddNamespace);
1608 boolean isgroups = false;
1609
1610 for (Element elRole : listRoles){
1611 if (elRole.getText().equals("Filtering")) {
1612 isgroups = true;
1613 }
1614 }
1615
1616 // only treats the case of flat groups containing characters
1617 // should also be added: dependencies between characters,
1618 //XIM
1619 if ((label.contains("group")) || (isgroups) || (1==1)) {
1620
1621 FeatureTree groups = FeatureTree.NewInstance();
1622 importRepresentation(elCharacterTree, sddNamespace, groups, "", sddConfig);
1623 FeatureNode root = groups.getRoot();
1624
1625 Element elNodes = elCharacterTree.getChild("Nodes", sddNamespace);
1626 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1627 for (Element elNode : listNodes){
1628 String idN = elNode.getAttributeValue("id");
1629 FeatureNode fn = null;
1630 if (!idN.equals("")) {
1631 Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1632 String refDC = elDescriptiveConcept.getAttributeValue("ref");
1633 fn = featureNodes.get(refDC);
1634 root.addChild(fn);
1635 }
1636 nodes.put(idN, fn);
1637 }
1638
1639 List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1640 for (Element elCharNode : listCharNodes){
1641 Element elParent = elCharNode.getChild("Parent", sddNamespace);
1642 Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1643 FeatureNode fn = FeatureNode.NewInstance();
1644 if (elParent!=null){
1645 String refP = elParent.getAttributeValue("ref");
1646 if (!refP.equals("")) {
1647 FeatureNode parent = nodes.get(refP);
1648 parent.addChild(fn);
1649 }
1650 }
1651 String refC = elCharacter.getAttributeValue("ref");
1652 if (!refC.equals("")){
1653 Feature character = features.get(refC);
1654 fn.setFeature(character);
1655 // if method setParent() in FeatureNode becomes visible
1656 // fn.setParent(parent);
1657 }
1658 nodes.put(refC, fn);
1659 }
1660 featureTrees.add(groups);
1661 }
1662
1663 } catch (Exception e) {
1664 //FIXME
1665 logger.warn("Import of Character tree " + j + " failed.");
1666 success = false;
1667 }
1668
1669 if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1670
1671 }
1672
1673 }
1674 }
1675 }