minor
[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 importRepresentation(elCategoricalCharacter, sddNamespace, categoricalCharacter, idCC, sddConfig);
716
717 categoricalCharacter.setSupportsCategoricalData(true);
718
719 // <States>
720 Element elStates = elCategoricalCharacter.getChild("States",sddNamespace);
721
722 // <StateDefinition id="s1">
723 List<Element> elStateDefinitions = elStates.getChildren("StateDefinition",sddNamespace);
724 TermVocabulary<State> termVocabularyState = new TermVocabulary<State>();
725
726 int k = 0;
727 //for each StateDefinition
728 for (Element elStateDefinition : elStateDefinitions){
729
730 if ((++k % modCount) == 0){ logger.info("StateDefinitions handled: " + (k-1));}
731
732 String idSD = elStateDefinition.getAttributeValue("id");
733 // <Representation>
734 // <Label>Simple</Label>
735 // <MediaObject ref="ib" role="Primary"/>
736 // </Representation>
737 State state = State.NewInstance();
738 importRepresentation(elStateDefinition, sddNamespace, state, idSD, sddConfig);
739
740 StateData stateData = StateData.NewInstance();
741 stateData.setState(state);
742 termVocabularyState.addTerm(state);
743 stateDatas.put(idSD,stateData);
744 }
745
746 categoricalCharacter.addSupportedCategoricalEnumeration(termVocabularyState);
747 features.put(idCC, categoricalCharacter);
748
749 } catch (Exception e) {
750 //FIXME
751 logger.warn("Import of CategoricalCharacter " + j + " failed.");
752 success = false;
753 }
754
755 if ((++j % modCount) == 0){ logger.info("CategoricalCharacters handled: " + j);}
756
757 }
758
759 // <QuantitativeCharacter id="c2">
760 List<Element> elQuantitativeCharacters = elCharacters.getChildren("QuantitativeCharacter", sddNamespace);
761 j = 0;
762 //for each QuantitativeCharacter
763 for (Element elQuantitativeCharacter : elQuantitativeCharacters){
764
765 try {
766
767 String idQC = elQuantitativeCharacter.getAttributeValue("id");
768
769 // <Representation>
770 // <Label>Leaf length</Label>
771 // </Representation>
772 Feature quantitativeCharacter = Feature.NewInstance();
773 importRepresentation(elQuantitativeCharacter, sddNamespace, quantitativeCharacter, idQC, sddConfig);
774
775 quantitativeCharacter.setSupportsQuantitativeData(true);
776
777 // <MeasurementUnit>
778 // <Label role="Abbrev">m</Label>
779 // </MeasurementUnit>
780 Element elMeasurementUnit = elQuantitativeCharacter.getChild("MeasurementUnit",sddNamespace);
781 String label = "";
782 String role = "";
783 if (elMeasurementUnit != null) {
784 Element elLabel = elMeasurementUnit.getChild("Label",sddNamespace);
785 role = elLabel.getAttributeValue("role");
786 label = (String)ImportHelper.getXmlInputValue(elMeasurementUnit, "Label",sddNamespace);
787 }
788
789 MeasurementUnit unit = null;
790 if (!label.equals("")){
791 if (role != null) {
792 if (role.equals("Abbrev")){
793 unit = MeasurementUnit.NewInstance(label,label,label);
794 }
795 } else {
796 unit = MeasurementUnit.NewInstance(label,label,label);
797 }
798 }
799
800 if (unit != null) {
801 units.put(idQC, unit);
802 }
803
804 //<Default>
805 // <MeasurementUnitPrefix>milli</MeasurementUnitPrefix>
806 //</Default>
807 Element elDefault = elQuantitativeCharacter.getChild("Default",sddNamespace);
808 if (elDefault != null) {
809 String measurementUnitPrefix = (String)ImportHelper.getXmlInputValue(elDefault, "MeasurementUnitPrefix",sddNamespace);
810 if (!measurementUnitPrefix.equals("")){
811 defaultUnitPrefixes.put(idQC, measurementUnitPrefix);
812 }
813 }
814
815 features.put(idQC, quantitativeCharacter);
816
817 } catch (Exception e) {
818 //FIXME
819 logger.warn("Import of QuantitativeCharacter " + j + " failed.");
820 success = false;
821 }
822
823 if ((++j % modCount) == 0){ logger.info("QuantitativeCharacters handled: " + j);}
824
825 }
826
827 // <TextCharacter id="c3">
828 List<Element> elTextCharacters = elCharacters.getChildren("TextCharacter", sddNamespace);
829 j = 0;
830 //for each TextCharacter
831 for (Element elTextCharacter : elTextCharacters){
832
833 try {
834
835 String idTC = elTextCharacter.getAttributeValue("id");
836
837 // <Representation>
838 // <Label xml:lang="en">Leaf features not covered by other characters</Label>
839 // </Representation>
840 Feature textCharacter = Feature.NewInstance();
841 importRepresentation(elTextCharacter, sddNamespace, textCharacter, idTC, sddConfig);
842
843 textCharacter.setSupportsTextData(true);
844
845 features.put(idTC, textCharacter);
846
847 } catch (Exception e) {
848 //FIXME
849 logger.warn("Import of TextCharacter " + j + " failed.");
850 success = false;
851 }
852
853 if ((++j % modCount) == 0){ logger.info("TextCharacters handled: " + j);}
854
855 }
856
857 }
858
859 for (Iterator<Feature> f = features.values().iterator() ; f.hasNext() ;){
860 featureSet.add(f.next());
861 }
862
863 }
864
865 // imports the descriptions of taxa (specimens TODO)
866 protected void importCodedDescriptions(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
867 // <CodedDescriptions>
868 logger.info("start CodedDescriptions ...");
869 Element elCodedDescriptions = elDataset.getChild("CodedDescriptions",sddNamespace);
870
871 // <CodedDescription id="D101">
872
873 if (elCodedDescriptions != null) {
874 List<Element> listCodedDescriptions = elCodedDescriptions.getChildren("CodedDescription", sddNamespace);
875 int j = 0;
876 //for each CodedDescription
877
878 for (Element elCodedDescription : listCodedDescriptions){
879
880 try {
881
882 String idCD = elCodedDescription.getAttributeValue("id");
883
884 // <Representation>
885 // <Label>&lt;i&gt;Viola hederacea&lt;/i&gt; Labill. as revised by R. Morris April 8, 2006</Label>
886 // </Representation>
887 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
888 importRepresentation(elCodedDescription, sddNamespace, taxonDescription, idCD, sddConfig);
889
890 // <Scope>
891 // <TaxonName ref="t1"/>
892 // <Citation ref="p1" location="p. 30"/>
893 // </Scope>
894 Element elScope = elCodedDescription.getChild("Scope",sddNamespace);
895 String ref = "";
896 Taxon taxon = null;
897 if (elScope != null) {
898 Element elTaxonName = elScope.getChild("TaxonName",sddNamespace);
899 ref = elTaxonName.getAttributeValue("ref");
900
901 NonViralName taxonNameBase = taxonNameBases.get(ref);
902 taxon = Taxon.NewInstance(taxonNameBase, sec);
903 }
904
905 String refCitation = "";
906 String location = "";
907
908 if (elScope != null) {
909 Element elCitation = elScope.getChild("Citation",sddNamespace);
910 if (elCitation != null) {
911 refCitation = elCitation.getAttributeValue("ref");
912 location = elCitation.getAttributeValue("location");
913 }
914 }
915
916 // <SummaryData>
917 Element elSummaryData = elCodedDescription.getChild("SummaryData",sddNamespace);
918
919 if (elSummaryData != null) {
920
921 // <Categorical ref="c4">
922 List<Element> elCategoricals = elSummaryData.getChildren("Categorical", sddNamespace);
923 int k = 0;
924 //for each Categorical
925 for (Element elCategorical : elCategoricals){
926 if ((++k % modCount) == 0){ logger.info("Categorical handled: " + (k-1));}
927 ref = elCategorical.getAttributeValue("ref");
928 Feature feature = features.get(ref);
929 CategoricalData categoricalData = CategoricalData.NewInstance();
930 categoricalData.setFeature(feature);
931
932 // <State ref="s3"/>
933 List<Element> elStates = elCategorical.getChildren("State", sddNamespace);
934 int l = 0;
935 //for each State
936 for (Element elState : elStates){
937 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
938 ref = elState.getAttributeValue("ref");
939 StateData stateData = stateDatas.get(ref);
940 categoricalData.addState(stateData);
941 }
942 taxonDescription.addElement(categoricalData);
943 }
944
945 // <Quantitative ref="c2">
946 List<Element> elQuantitatives = elSummaryData.getChildren("Quantitative", sddNamespace);
947 k = 0;
948 //for each Quantitative
949 for (Element elQuantitative : elQuantitatives){
950 if ((++k % modCount) == 0){ logger.info("Quantitative handled: " + (k-1));}
951 ref = elQuantitative.getAttributeValue("ref");
952 Feature feature = features.get(ref);
953 QuantitativeData quantitativeData = QuantitativeData.NewInstance();
954 quantitativeData.setFeature(feature);
955
956 MeasurementUnit unit = units.get(ref);
957 String prefix = defaultUnitPrefixes.get(ref);
958 if (unit != null) {
959 String u = unit.getLabel();
960 if (prefix != null) {
961 u = prefix + u;
962 }
963 unit.setLabel(u);
964 quantitativeData.setUnit(unit);
965 }
966
967 // <Measure type="Min" value="2.3"/>
968 List<Element> elMeasures = elQuantitative.getChildren("Measure", sddNamespace);
969 int l = 0;
970 //for each State
971 for (Element elMeasure : elMeasures){
972 if ((++l % modCount) == 0){ logger.info("States handled: " + (l-1));}
973 String type = elMeasure.getAttributeValue("type");
974 String value = elMeasure.getAttributeValue("value");
975 float v = Float.parseFloat(value);
976 StatisticalMeasure t = null;
977 if (type.equals("Min")) {
978 t = StatisticalMeasure.MIN();
979 } else if (type.equals("Mean")) {
980 t = StatisticalMeasure.AVERAGE();
981 } else if (type.equals("Max")) {
982 t = StatisticalMeasure.MAX();
983 } else if (type.equals("SD")) {
984 // Create a new StatisticalMeasure for standard deviation
985 t = StatisticalMeasure.STANDARD_DEVIATION();
986 } else if (type.equals("N")) {
987 t = StatisticalMeasure.SAMPLE_SIZE();
988 } else {
989 t = StatisticalMeasure.NewInstance(type,type,type);
990 statisticalMeasures.add(t);
991 }
992
993 StatisticalMeasurementValue statisticalValue = StatisticalMeasurementValue.NewInstance();
994 statisticalValue.setValue(v);
995 statisticalValue.setType(t);
996 quantitativeData.addStatisticalValue(statisticalValue);
997 featureData.add(statisticalValue);
998 }
999 taxonDescription.addElement(quantitativeData);
1000 }
1001
1002 // <TextChar ref="c3">
1003 List<Element> elTextChars = elSummaryData.getChildren("TextChar", sddNamespace);
1004 k = 0;
1005 //for each TextChar
1006 for (Element elTextChar : elTextChars){
1007 if ((++k % modCount) == 0){ logger.info("TextChar handled: " + (k-1));}
1008 ref = elTextChar.getAttributeValue("ref");
1009 Feature feature = features.get(ref);
1010 TextData textData = TextData.NewInstance();
1011 textData.setFeature(feature);
1012
1013 // <Content>Free form text</Content>
1014 String content = (String)ImportHelper.getXmlInputValue(elTextChar, "Content",sddNamespace);
1015 textData.putText(content, datasetLanguage);
1016 taxonDescription.addElement(textData);
1017 }
1018
1019 }
1020
1021 if (taxon != null) {
1022 taxon.addDescription(taxonDescription);
1023 }
1024
1025 if (!refCitation.equals("")){
1026 citations.put(idCD,refCitation);
1027 }
1028
1029 if (!location.equals("")){
1030 locations.put(idCD, location);
1031 }
1032
1033 taxonDescription.setDescriptiveSystem(featureSet);
1034
1035 taxonDescriptions.put(idCD, taxonDescription);
1036
1037 } catch (Exception e) {
1038 //FIXME
1039 logger.warn("Import of CodedDescription " + j + " failed.");
1040 success = false;
1041 }
1042
1043 if ((++j % modCount) == 0){ logger.info("CodedDescriptions handled: " + j);}
1044
1045 }
1046
1047 }
1048 }
1049
1050 // imports the persons associated with the dataset creation, modification, related publications
1051 protected void importAgents(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1052 // <Agents>
1053 logger.info("start Agents ...");
1054 Element elAgents = elDataset.getChild("Agents",sddNamespace);
1055
1056 // <Agent id="a1">
1057 List <Element> listAgents = elAgents.getChildren("Agent", sddNamespace);
1058 int j = 0;
1059 //for each Agent
1060 for (Element elAgent : listAgents){
1061
1062 try {
1063
1064 String idA = elAgent.getAttributeValue("id");
1065
1066 // <Representation>
1067 // <Label>Kevin Thiele</Label>
1068 // <Detail role="Description">Ali Baba is also known as r.a.m.</Detail>
1069 // </Representation>
1070 Person person = Person.NewInstance();
1071 importRepresentation(elAgent, sddNamespace, person, idA, sddConfig);
1072 person.addSource(OriginalSource.NewInstance(idA, "Agent"));
1073
1074 // <Links>
1075 Element elLinks = elAgent.getChild("Links",sddNamespace);
1076
1077 if (elLinks != null) {
1078
1079 // <Link rel="Alternate" href="http://www.diversitycampus.net/people/hagedorn"/>
1080 List<Element> listLinks = elLinks.getChildren("Link", sddNamespace);
1081 int k = 0;
1082 //for each Link
1083 for (Element elLink : listLinks){
1084
1085 try {
1086
1087 String rel = elLink.getAttributeValue("rel");
1088 String href = elLink.getAttributeValue("href");
1089
1090 Media link = Media.NewInstance();
1091 MediaRepresentation mr = MediaRepresentation.NewInstance();
1092 mr.addRepresentationPart(MediaRepresentationPart.NewInstance(href, null));
1093 link.addRepresentation(mr);
1094 person.addMedia(link);
1095
1096 } catch (Exception e) {
1097 //FIXME
1098 logger.warn("Import of Link " + k + " failed.");
1099 success = false;
1100 }
1101
1102 if ((++k % modCount) == 0){ logger.info("Links handled: " + k);}
1103
1104 }
1105 }
1106 if (authors.containsKey(idA)) {
1107 authors.put(idA,person);
1108 }
1109
1110 if (editors.containsKey(idA)) {
1111 editors.put(idA, person);
1112 }
1113
1114 } catch (Exception e) {
1115 //FIXME
1116 logger.warn("Import of Agent " + j + " failed.");
1117 success = false;
1118 }
1119
1120 if ((++j % modCount) == 0){ logger.info("Agents handled: " + j);}
1121
1122 }
1123 }
1124
1125 // imports publications related with the data set
1126 protected void importPublications(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1127 // <Publications>
1128 logger.info("start Publications ...");
1129 Element elPublications = elDataset.getChild("Publications",sddNamespace);
1130
1131 if (elPublications != null) {
1132 // <Publication id="p1">
1133 List<Element> listPublications = elPublications.getChildren("Publication", sddNamespace);
1134 int j = 0;
1135 //for each Publication
1136 for (Element elPublication : listPublications){
1137
1138 try {
1139
1140 String idP = elPublication.getAttributeValue("id");
1141
1142 // <Representation>
1143 // <Label>Sample Citation</Label>
1144 // </Representation>
1145 Article publication = Article.NewInstance();
1146 importRepresentation(elPublication, sddNamespace, publication, idP, sddConfig);
1147
1148 publications.put(idP,publication);
1149
1150 } catch (Exception e) {
1151 //FIXME
1152 logger.warn("Import of Publication " + j + " failed.");
1153 success = false;
1154 }
1155
1156 if ((++j % modCount) == 0){ logger.info("Publications handled: " + j);}
1157
1158 }
1159 }
1160 }
1161
1162 // imports media objects such as images
1163 protected void importMediaObjects(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1164 // <MediaObjects>
1165 logger.info("start MediaObjects ...");
1166 Element elMediaObjects = elDataset.getChild("MediaObjects",sddNamespace);
1167
1168 if (elMediaObjects != null) {
1169 // <MediaObject id="m1">
1170 List<Element> listMediaObjects = elMediaObjects.getChildren("MediaObject", sddNamespace);
1171 int j = 0;
1172 //for each Publication
1173 for (Element elMO : listMediaObjects){
1174
1175 String id = "";
1176
1177 try {
1178
1179 String idMO = elMO.getAttributeValue("id");
1180 id = idMO;
1181
1182 // <Representation>
1183 // <Label>Image description, e.g. to be used for alt-attribute in html.</Label>
1184 // </Representation>
1185 Media media = Media.NewInstance();
1186 importRepresentation(elMO, sddNamespace, media, idMO, sddConfig);
1187
1188 // <Type>Image</Type>
1189 // <Source href="http://test.edu/test.jpg"/>
1190 String type = (String)ImportHelper.getXmlInputValue(elMO,"Type",sddNamespace);
1191
1192 if ((type != null) && (type.equals("Image"))) {
1193 Element elSource = elMO.getChild("Source",sddNamespace);
1194 String href = elSource.getAttributeValue("href");
1195
1196 ImageMetaData imageMetaData = new ImageMetaData();
1197 ImageFile image = null;
1198
1199 if (href.substring(0,7).equals("http://")) {
1200 try{
1201 URL url = new URL(href);
1202 imageMetaData.readFrom(url);
1203 image = ImageFile.NewInstance(url.toString(), null, imageMetaData);
1204 } catch (MalformedURLException e) {
1205 logger.error("Malformed URL", e);
1206 }
1207 } else {
1208 String sns = sddConfig.getSourceNameString();
1209 File f = new File(sns);
1210 File parent = f.getParentFile();
1211 String fi = parent.toString() + File.separator + href;
1212 File file = new File(fi);
1213 imageMetaData.readFrom(file);
1214 image = ImageFile.NewInstance(file.toString(), null, imageMetaData);
1215 }
1216
1217 MediaRepresentation representation = MediaRepresentation.NewInstance(imageMetaData.getMimeType(), null);
1218 representation.addRepresentationPart(image);
1219
1220 media.addRepresentation(representation);
1221
1222 ArrayList<CdmBase> lcb = (ArrayList<CdmBase>) mediaObject_ListCdmBase.get(idMO);
1223 if (lcb != null) {
1224 for (int k = 0; k < lcb.size(); k++) {
1225 if (lcb.get(k) instanceof DefinedTermBase) {
1226 DefinedTermBase dtb = (DefinedTermBase) lcb.get(k);
1227 // if (lcb.get(0) instanceof DefinedTermBase) {
1228 // DefinedTermBase dtb = (DefinedTermBase) lcb.get(0);
1229 // if (dtb!=null) {
1230 // if (k == 0) {
1231 dtb.addMedia(media);
1232 // } else {
1233 // Media me = (Media) media.clone();
1234 // dtb.addMedia(me);
1235 // }
1236 // }
1237 } else if (lcb.get(k) instanceof ReferenceBase) {
1238 ReferenceBase rb = (ReferenceBase) lcb.get(k);
1239 //} else if (lcb.get(0) instanceof ReferenceBase) {
1240 //ReferenceBase rb = (ReferenceBase) lcb.get(0);
1241 // rb.setTitleCache(label);
1242 // if (rb!=null) {
1243 // if (k == 0) {
1244 rb.addMedia(media);
1245 // } else {
1246 // Media me = (Media) media.clone();
1247 // rb.addMedia(me);
1248 // }
1249 // }
1250 }
1251 }
1252 }
1253 }
1254
1255 } catch (Exception e) {
1256 //FIXME
1257 logger.warn("Could not attached MediaObject " + j + "(SDD: " + id + ") to several objects.");
1258 success = false;
1259 }
1260
1261 if ((++j % modCount) == 0){ logger.info("MediaObjects handled: " + j);
1262
1263 }
1264 }
1265 }
1266 }
1267
1268 // imports the <DescriptiveConcepts> block
1269 protected void importDescriptiveConcepts(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig){
1270 // <DescriptiveConcepts>
1271 logger.info("start DescriptiveConcepts ...");
1272 Element elDescriptiveConcepts = elDataset.getChild("DescriptiveConcepts",sddNamespace);
1273 // <DescriptiveConcept id="b">
1274 if (elDescriptiveConcepts != null) {
1275 List<Element> listDescriptiveConcepts = elDescriptiveConcepts.getChildren("DescriptiveConcept", sddNamespace);
1276 int j = 0;
1277 //for each DescriptiveConcept
1278 int g = 1;
1279 for (Element elDescriptiveConcept : listDescriptiveConcepts){
1280
1281 String id = elDescriptiveConcept.getAttributeValue("id");
1282 String uri = elDescriptiveConcept.getAttributeValue("uri");
1283
1284 FeatureNode fn = null;
1285
1286 if (!id.equals("")) {
1287 fn = FeatureNode.NewInstance();
1288 Feature feature = Feature.NewInstance();
1289 // <Representation>
1290 // <Label>Body</Label>
1291 importRepresentation(elDescriptiveConcept, sddNamespace, feature, id, sddConfig);
1292 features.put("g" + g, feature);
1293 g++;
1294 fn.setFeature(feature);
1295
1296 // TODO if an OriginalSource can be attached to a FeatureNode or a Feature
1297 // OriginalSource source = null;
1298 // if (uri != null) {
1299 // if (!uri.equals("")) {
1300 // source = OriginalSource.NewInstance(id, "DescriptiveConcept", Generic.NewInstance(), uri);
1301 // }
1302 // } else {
1303 // source = OriginalSource.NewInstance(id, "DescriptiveConcept");
1304 // }
1305 // fn.addSource(source);
1306
1307 featureNodes.put(id,fn);
1308 }
1309
1310 if ((++j % modCount) == 0){ logger.info("DescriptiveConcepts handled: " + j);}
1311
1312 }
1313 }
1314 }
1315
1316 // imports the descriptions of taxa (specimens TODO)
1317 protected void importCharacterTrees(Element elDataset, Namespace sddNamespace, SDDImportConfigurator sddConfig, boolean success){
1318 // <CharacterTrees>
1319 logger.info("start CharacterTrees ...");
1320 Element elCharacterTrees = elDataset.getChild("CharacterTrees",sddNamespace);
1321
1322 // <CharacterTree>
1323
1324 if (elCharacterTrees != null) {
1325 List<Element> listCharacterTrees = elCharacterTrees.getChildren("CharacterTree", sddNamespace);
1326 int j = 0;
1327 //for each CharacterTree
1328
1329 for (Element elCharacterTree : listCharacterTrees){
1330
1331 try {
1332 Element elRepresentation = elCharacterTree.getChild("Representation",sddNamespace);
1333 String label = (String)ImportHelper.getXmlInputValue(elRepresentation,"Label",sddNamespace);
1334 Element elDesignedFor = elCharacterTree.getChild("DesignedFor",sddNamespace);
1335 List<Element> listRoles = elDesignedFor.getChildren("Role",sddNamespace);
1336 boolean isgroups = false;
1337
1338 for (Element elRole : listRoles){
1339 if (elRole.getText().equals("Filtering")) {
1340 isgroups = true;
1341 }
1342 }
1343
1344 if ((label.contains("group")) || (isgroups)) {
1345
1346 FeatureTree groups = FeatureTree.NewInstance();
1347 importRepresentation(elCharacterTree, sddNamespace, groups, "", sddConfig);
1348 FeatureNode root = groups.getRoot();
1349
1350 Element elNodes = elCharacterTree.getChild("Nodes", sddNamespace);
1351 List<Element> listNodes = elNodes.getChildren("Node", sddNamespace);
1352 for (Element elNode : listNodes){
1353 String idN = elNode.getAttributeValue("id");
1354 FeatureNode fn = null;
1355 if (!idN.equals("")) {
1356 Element elDescriptiveConcept = elNode.getChild("DescriptiveConcept", sddNamespace);
1357 String refDC = elDescriptiveConcept.getAttributeValue("ref");
1358 fn = featureNodes.get(refDC);
1359 root.addChild(fn);
1360 }
1361 nodes.put(idN, fn);
1362 }
1363
1364 List<Element> listCharNodes = elNodes.getChildren("CharNode", sddNamespace);
1365 for (Element elCharNode : listCharNodes){
1366 Element elParent = elCharNode.getChild("Parent", sddNamespace);
1367 String refP = elParent.getAttributeValue("ref");
1368 Element elCharacter = elCharNode.getChild("Character", sddNamespace);
1369 String refC = elCharacter.getAttributeValue("ref");
1370 FeatureNode fn = FeatureNode.NewInstance();
1371 if (!refP.equals("")) {
1372 FeatureNode parent = nodes.get(refP);
1373 parent.addChild(fn);
1374 Feature character = features.get(refC);
1375 fn.setFeature(character);
1376 // if method setParent() in FeatureNode becomes visible
1377 // fn.setParent(parent);
1378 }
1379 nodes.put(refC, fn);
1380 }
1381 featureTrees.add(groups);
1382 }
1383
1384 } catch (Exception e) {
1385 //FIXME
1386 logger.warn("Import of Character tree " + j + " failed.");
1387 success = false;
1388 }
1389
1390 if ((++j % modCount) == 0){ logger.info("CharacterTrees handled: " + j);}
1391
1392 }
1393
1394 }
1395 }
1396 }