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