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