cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / specimen / abcd206 / in / Abcd206Import.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 package eu.etaxonomy.cdm.io.specimen.abcd206.in;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.net.MalformedURLException;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.UUID;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.log4j.Logger;
24 import org.springframework.stereotype.Component;
25 import org.w3c.dom.Element;
26 import org.w3c.dom.NodeList;
27
28 import eu.etaxonomy.cdm.api.application.ICdmRepository;
29 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
30 import eu.etaxonomy.cdm.api.service.DeleteResult;
31 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
32 import eu.etaxonomy.cdm.common.StreamUtils;
33 import eu.etaxonomy.cdm.ext.occurrence.bioCase.BioCaseQueryServiceWrapper;
34 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
35 import eu.etaxonomy.cdm.io.common.ICdmIO;
36 import eu.etaxonomy.cdm.io.common.MapWrapper;
37 import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
38 import eu.etaxonomy.cdm.io.specimen.SpecimenUserInteraction;
39 import eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea;
40 import eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent;
41 import eu.etaxonomy.cdm.io.specimen.abcd206.in.molecular.AbcdDnaParser;
42 import eu.etaxonomy.cdm.model.agent.Institution;
43 import eu.etaxonomy.cdm.model.agent.Person;
44 import eu.etaxonomy.cdm.model.agent.Team;
45 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
46 import eu.etaxonomy.cdm.model.common.Annotation;
47 import eu.etaxonomy.cdm.model.common.CdmBase;
48 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
49 import eu.etaxonomy.cdm.model.common.Language;
50 import eu.etaxonomy.cdm.model.common.LanguageString;
51 import eu.etaxonomy.cdm.model.location.NamedArea;
52 import eu.etaxonomy.cdm.model.media.Media;
53 import eu.etaxonomy.cdm.model.media.Rights;
54 import eu.etaxonomy.cdm.model.media.RightsType;
55 import eu.etaxonomy.cdm.model.molecular.DnaSample;
56 import eu.etaxonomy.cdm.model.occurrence.Collection;
57 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
58 import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
59 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
60 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
61 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
62 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
63 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
64 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
65 import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
66 import eu.etaxonomy.cdm.model.reference.Reference;
67 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
68 import eu.etaxonomy.cdm.model.taxon.Classification;
69 import eu.etaxonomy.cdm.model.term.DefinedTerm;
70 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
71 import eu.etaxonomy.cdm.model.term.TermType;
72 import eu.etaxonomy.cdm.model.term.TermVocabulary;
73 import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
74
75 /**
76 * @author p.kelbert
77 * @author p.plitzner
78 * @author k.luther
79 * @since 20.10.2008
80 */
81 @Component
82 public class Abcd206Import extends SpecimenImportBase<Abcd206ImportConfigurator, Abcd206ImportState> {
83
84 private static final long serialVersionUID = 3918095362150986307L;
85
86 private static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
87
88 private static final Logger logger = Logger.getLogger(Abcd206Import.class);
89
90 public Abcd206Import() {
91 super();
92 }
93
94 @Override
95 // @SuppressWarnings("rawtypes")
96 public void doInvoke(Abcd206ImportState state) {
97 Abcd206ImportConfigurator config = state.getConfig();
98 Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
99 MapWrapper<TeamOrPersonBase<?>> authorStore = (MapWrapper<TeamOrPersonBase<?>>) stores.get(ICdmIO.TEAM_STORE);
100 state.setPersonStore(authorStore);
101 MapWrapper<Reference> referenceStore = (MapWrapper<Reference>) stores.get(ICdmIO.REFERENCE_STORE);
102 createKindOfUnitsMap(state);
103 URI sourceUri = config.getSourceUri();
104 try {
105 state.setTx(startTransaction());
106 logger.info("INVOKE Specimen Import from ABCD2.06 XML ");
107 InputStream response = null;
108 // init cd repository
109 if (state.getCdmRepository() == null) {
110 state.setCdmRepository(this);
111 }
112 if (config.getOccurenceQuery() != null) {
113 BioCaseQueryServiceWrapper queryService = new BioCaseQueryServiceWrapper();
114 try {
115
116 response = queryService.query(config.getOccurenceQuery(), sourceUri);
117 state.setActualAccessPoint(sourceUri);
118
119 } catch (Exception e) {
120 logger.error("An error during ABCD import");
121 }
122 }
123 SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
124
125 if (state.getRef() == null) {
126 String name = NB(state.getConfig().getSourceReferenceTitle());
127 for (Reference reference : referenceStore.getAllValues()) {
128 if (!StringUtils.isBlank(reference.getTitleCache())) {
129 if (reference.getTitleCache().equalsIgnoreCase(name)) {
130 state.setRef(reference);
131
132 }
133 }
134 }
135 if (state.getRef() == null) {
136 if (state.getConfig().getSourceReference() != null) {
137 state.setRef(state.getConfig().getSourceReference());
138 state.addImportReference(state.getRef());
139
140 } else {
141 state.setRef(ReferenceFactory.newGeneric());
142 state.getRef().setUri(sourceUri);
143
144 if (sourceUri != null) {
145 state.getRef().setTitle(StringUtils.substringAfter(sourceUri.toString(), "dsa="));
146 }
147
148 if (!StringUtils.isBlank(state.getConfig().getSourceReferenceTitle())) {
149 state.getRef().setTitle(state.getConfig().getSourceReferenceTitle());
150 }
151 state.addImportReference(state.getRef());
152 }
153
154 }
155 }
156 // }
157
158 save(state.getRef(), state);
159 state.getConfig().setSourceReference(state.getRef());
160
161 if (state.getConfig().getClassificationUuid() != null) {
162 // load classification from config if it exists
163 state.setClassification(getClassificationService().load(state.getConfig().getClassificationUuid()));
164 }
165 if (state.getClassification() == null) {// no existing
166 // classification was set in
167 // config
168 List<Classification> classificationList = getClassificationService().list(Classification.class, null,
169 null, null, null);
170 // get classification via user interaction
171 if (state.getConfig().isUseClassification() && state.getConfig().isInteractWithUser()) {
172 Map<String, Classification> classMap = new HashMap<>();
173 for (Classification tree : classificationList) {
174 if (!StringUtils.isBlank(tree.getTitleCache())) {
175 classMap.put(tree.getTitleCache(), tree);
176 }
177 }
178 state.setClassification(sui.askForClassification(classMap));
179 if (state.getClassification() == null) {
180 String cla = sui.createNewClassification();
181 if (classMap.get(cla) != null) {
182 state.setClassification(classMap.get(cla));
183 } else {
184 state.setClassification(
185 Classification.NewInstance(cla, state.getRef(), Language.DEFAULT()));
186 }
187 }
188 save(state.getClassification(), state);
189 }
190 // use default classification as the classification to import
191 // into
192 if (state.getClassification() == null) {
193 String name = NB(state.getConfig().getClassificationName());
194 for (Classification classif : classificationList) {
195 if (classif.getTitleCache() != null && classif.getTitleCache().equalsIgnoreCase(name)) {
196 state.setClassification(classif);
197 }
198 }
199 if (state.getClassification() == null) {
200 state.setClassification(Classification.NewInstance(name, state.getRef(), Language.DEFAULT()));
201 // we do not need a default classification when creating
202 // an empty new one
203 state.setDefaultClassification(state.getClassification());
204 save(state.getDefaultClassification(false), state);
205 }
206 save(state.getClassification(), state);
207 }
208 }
209
210 if (response == null) {
211 response = state.getConfig().getSource();
212 }
213 UnitAssociationWrapper unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(response,
214 state.getReport());
215 NodeList unitsList = unitAssociationWrapper.getAssociatedUnits();
216 state.setPrefix(unitAssociationWrapper.getPrefix());
217
218 if (unitsList != null) {
219 String message = "nb units to insert: " + unitsList.getLength();
220 logger.info(message);
221 state.getConfig().getProgressMonitor().beginTask("Importing ABCD file", unitsList.getLength() + 3);
222 updateProgress(state, message);
223
224 state.setDataHolder(new Abcd206DataHolder());
225 state.getDataHolder().reset();
226
227 Abcd206XMLFieldGetter abcdFieldGetter = new Abcd206XMLFieldGetter(state.getDataHolder(),
228 state.getPrefix());
229 if (config.getNomenclaturalCode() != null) {
230 state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().getKey());
231 }
232 prepareCollectors(state, unitsList, abcdFieldGetter);
233
234 // save authors
235 getAgentService().saveOrUpdate((java.util.Collection) state.getPersonStore().objects());
236
237 commitTransaction(state.getTx());
238 state.setTx(startTransaction());
239 if (state.getDefaultClassification(false) != null) {
240 state.setDefaultClassification(
241 getClassificationService().load(state.getDefaultClassification(false).getUuid()));
242 }
243 if (state.getClassification() != null) {
244 state.setClassification(getClassificationService().load(state.getClassification().getUuid()));
245 }
246 state.setAssociationRefs(new ArrayList<>());
247 state.setDescriptionRefs(new ArrayList<>());
248 state.setDerivedUnitSources(new ArrayList<>());
249 for (int i = 0; i < unitsList.getLength(); i++) {
250 commitTransaction(state.getTx());
251 state.setTx(startTransaction());
252
253 if (state.getConfig().getProgressMonitor().isCanceled()) {
254 break;
255 }
256
257 state.reset();
258 state.getDataHolder().setNomenclatureCode(state.getConfig().getNomenclaturalCode()!= null? state.getConfig().getNomenclaturalCode().getKey() : null);
259 Element item = (Element) unitsList.item(i);
260 Abcd206ImportParser.setUnitPropertiesXML(item, abcdFieldGetter, state);
261 updateProgress(state, "Importing data for unit " + state.getDataHolder().getUnitID() + " (" + i
262 + "/" + unitsList.getLength() + ")");
263
264 // import unit + field unit data
265 state.setAssociatedUnitIds(state.getDataHolder().getAssociatedUnitIds());
266
267 this.handleSingleUnit(state, item, true);
268 state.setLastFieldUnit(null);
269
270 }
271 commitTransaction(state.getTx());
272 state.setTx(startTransaction());
273 if (state.getConfig().isDeduplicateReferences()) {
274 getReferenceService().deduplicate(Reference.class, null, null);
275 }
276 if (state.getConfig().isDeduplicateClassifications()) {
277 getClassificationService().deduplicate(Classification.class, null, null);
278 }
279 }
280 commitTransaction(state.getTx());
281 } catch (Exception e) {
282 String errorDuringImport = "Exception during import!";
283 logger.error(errorDuringImport, e);
284 state.getReport().addException(errorDuringImport, e);
285 } finally {
286 state.getReport().printReport(state.getConfig().getReportUri());
287 }
288 if (state.getConfig().isDownloadSequenceData()) {
289 for (URI uri : state.getSequenceDataStableIdentifier()) {
290 // Files.createDirectories(file.getParent()); // optional, make
291 // sure parent dir exists
292 try {
293 StreamUtils.downloadFile(uri.toURL(), "temp");
294 } catch (IOException e) {
295 // TODO Auto-generated catch block
296 e.printStackTrace();
297 }
298
299 }
300 }
301
302 return;
303 }
304
305 /**
306 *
307 */
308 private void createKindOfUnitsMap(Abcd206ImportState state) {
309
310 ICdmRepository cdmRepository = state.getConfig().getCdmAppController();
311 if (cdmRepository == null) {
312 cdmRepository = this;
313 }
314
315 List<DefinedTerm> terms = cdmRepository.getTermService().listByTermType(TermType.KindOfUnit, null, 0, null,
316 null);
317 kindOfUnitsMap = new HashMap<>();
318 for (DefinedTerm kindOfUnit : terms) {
319 if (kindOfUnit != null && kindOfUnit.getLabel() != null) {
320 String kindOfUnitLabel = kindOfUnit.getLabel().toLowerCase();
321 kindOfUnitsMap.put(kindOfUnitLabel, kindOfUnit);
322 }
323
324 }
325 }
326
327 /**
328 * @param state
329 * @param item
330 */
331 private void getSiblings(Abcd206ImportState state, Object item, DerivedUnitFacade facade) {
332 String unitId = facade.getCatalogNumber();
333 if (unitId == null) {
334 unitId = facade.getAccessionNumber();
335 }
336 if (unitId == null){
337 unitId = facade.getBarcode();
338 }
339
340 UnitAssociationParser unitParser = new UnitAssociationParser(state.getPrefix(), state.getReport(),
341 state.getCdmRepository());
342 UnitAssociationWrapper unitAssociationWrapper = null;
343 for (URI accessPoint : state.getAllAccesPoint()) {
344 unitAssociationWrapper = unitParser.parseSiblings(unitId, accessPoint);
345 if (unitAssociationWrapper != null && unitAssociationWrapper.getAssociatedUnits() != null) {
346 break;
347 }
348 }
349
350 DerivedUnit currentUnit = state.getDerivedUnitBase();
351 // DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
352 FieldUnit currentFieldUnit = facade.getFieldUnit(false);
353 if (unitAssociationWrapper != null) {
354 NodeList associatedUnits = unitAssociationWrapper.getAssociatedUnits();
355 if (associatedUnits != null) {
356 for (int m = 0; m < associatedUnits.getLength(); m++) {
357 if (associatedUnits.item(m) instanceof Element) {
358 state.reset();
359 String associationType = AbcdParseUtility
360 .parseFirstTextContent(((Element) associatedUnits.item(m))
361 .getElementsByTagName(state.getPrefix() + "AssociationType"));
362
363 Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m),
364 new Abcd206XMLFieldGetter(state.getDataHolder(), unitAssociationWrapper.getPrefix()),
365 state);
366 // logger.debug("derived unit: " +
367 // state.getDerivedUnitBase().toString() + " associated
368 // unit: " +state.getDataHolder().getKindOfUnit() + ", "
369 // + state.getDataHolder().accessionNumber + ", " +
370 // state.getDataHolder().getRecordBasis() + ", " +
371 // state.getDataHolder().getUnitID());
372
373 handleSingleUnit(state, associatedUnits.item(m), false);
374
375 DerivedUnit associatedUnit = state.getDerivedUnitBase();
376 FieldUnit associatedFieldUnit = null;
377 commitTransaction(state.getTx());
378 state.setTx(startTransaction());
379 java.util.Collection<FieldUnit> associatedFieldUnits = null;
380 try {
381 associatedFieldUnits = state.getCdmRepository().getOccurrenceService()
382 .findFieldUnits(associatedUnit.getUuid(), null);
383 } catch (NullPointerException e) {
384 logger.error("Search for associated field unit creates a NPE" + e.getMessage());
385 }
386 // ignore field unit if associated unit has more than
387 // one
388 if (associatedFieldUnits != null && associatedFieldUnits.size() > 1) {
389 state.getReport()
390 .addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
391 } else if (associatedFieldUnits != null && associatedFieldUnits.size() == 1) {
392 associatedFieldUnit = associatedFieldUnits.iterator().next();
393 }
394 // parent-child relation:
395 if (associationType != null && (associationType.contains("individual") || associationType.contains("culture")
396 || associationType.contains("sample") || associationType.contains("isolated"))) {
397 DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(currentUnit,
398 associatedUnit, DerivationEventType.ACCESSIONING());
399
400 updatedDerivationEvent.setDescription(associationType);
401 if (associatedFieldUnit != null && associatedFieldUnit != currentFieldUnit && currentFieldUnit != null) {
402 // associatedFieldUnit.removeDerivationEvent(updatedDerivationEvent);
403 // save(associatedFieldUnit, state);
404 // if
405 // (associatedFieldUnit.getDerivationEvents().isEmpty()){
406 SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
407 config.setDeleteChildren(false);
408 DeleteResult result = state.getCdmRepository().getOccurrenceService()
409 .delete(associatedFieldUnit, config);
410 if (!result.isOk()) {
411 logger.debug("Deletion of field unit " + associatedFieldUnit.getFieldNumber()
412 + " not successfull");
413 }
414 state.setFieldUnit(currentFieldUnit);
415
416 // }
417
418 }
419 state.getReport().addDerivate(associatedUnit, currentUnit, state.getConfig());
420 }
421 save(associatedUnit, state);
422
423 }
424 }
425 }
426 }
427 state.reset();
428 state.setDerivedUnitBase(currentUnit);
429
430 }
431
432 /**
433 * Handle a single unit
434 *
435 * @param state
436 * @param item
437 */
438 @Override
439 public void handleSingleUnit(Abcd206ImportState state, Object itemObject) {
440 handleSingleUnit(state, itemObject, true);
441 }
442
443 @SuppressWarnings("rawtypes")
444 public void handleSingleUnit(Abcd206ImportState state, Object itemObject, boolean handleAssociatedUnits) {
445 Element item = (Element) itemObject;
446
447 Abcd206ImportConfigurator config = state.getConfig();
448 if (logger.isDebugEnabled()) {
449 logger.info("handleSingleUnit " + state.getRef());
450 }
451 try {
452 ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
453 if (cdmAppController == null) {
454 cdmAppController = this;
455 }
456 // check if unit already exists
457 DerivedUnitFacade derivedUnitFacade = null;
458 if (state.getConfig().isIgnoreImportOfExistingSpecimen() && state.getDataHolder().getUnitID() != null) {
459
460 SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(state.getDataHolder().getUnitID(),
461 state);
462 if (existingSpecimen != null && existingSpecimen.isInstanceOf(DerivedUnit.class)) {
463 DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
464 state.setDerivedUnitBase(derivedUnit);
465 derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
466 if (handleAssociatedUnits) {
467 importAssociatedUnits(state, item, derivedUnitFacade);
468 }
469 if (state.getConfig().getDnaSoure() != null) {
470 importAssociatedDna(state, item, derivedUnitFacade);
471 }
472
473 state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, config),
474 derivedUnit);
475
476 return;
477 }
478 } else {
479 System.err.println("dataholder does not contain unit id");
480 }
481
482
483 // import DNA unit
484 if (state.getDataHolder().getKindOfUnit() != null
485 && state.getDataHolder().getKindOfUnit().equalsIgnoreCase("dna")) {
486 AbcdDnaParser dnaParser = new AbcdDnaParser(state.getPrefix(), state.getReport(),
487 state.getCdmRepository());
488 DnaSample dnaSample = dnaParser.parse(item, state);
489 //dnaSample.addSource(OriginalSourceType.Import, dnaSample.getAccessionNumber(), "", state.getImportReference(state.getActualAccessPoint()), "");
490 save(dnaSample, state);
491 // set dna as derived unit to avoid creating an extra specimen
492 // for this dna sample (instead just the field unit will be
493 // created)
494 state.setDerivedUnitBase(dnaSample);
495 derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
496 } else {
497 // create facade
498 derivedUnitFacade = getFacade(state);
499 state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
500
501 }
502
503 /**
504 * GATHERING EVENT
505 */
506
507 // look for existing fieldUnit
508 FieldUnit fieldUnit = null;
509 if (StringUtils.isNotBlank(state.getDataHolder().getFieldNumber())){
510 fieldUnit = state.getFieldUnit(state.getDataHolder().getFieldNumber());
511 if (fieldUnit != null){
512 state.setLastFieldUnit(fieldUnit);
513 }
514 }else{
515 fieldUnit = state.getLastFieldUnit();
516
517 }
518 if (fieldUnit == null){
519 fieldUnit = FieldUnit.NewInstance();
520 fieldUnit.setFieldNumber(state.getDataHolder().getFieldNumber());
521 state.setLastFieldUnit(fieldUnit);
522 }
523
524 // gathering event
525 UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(cdmAppController.getTermService(),
526 state.getDataHolder().locality, state.getDataHolder().languageIso, state.getDataHolder().longitude,
527 state.getDataHolder().latitude, state.getDataHolder().getGatheringCoordinateErrorMethod(),
528 state.getDataHolder().getGatheringElevationText(), state.getDataHolder().getGatheringElevationMin(),
529 state.getDataHolder().getGatheringElevationMax(), state.getDataHolder().getGatheringElevationUnit(),
530 state.getDataHolder().getGatheringDateText(), state.getDataHolder().getGatheringNotes(),
531 state.getDataHolder().getGatheringMethod(),
532 state.getTransformer().getReferenceSystemByKey(state.getDataHolder().getGatheringSpatialDatum()),
533 state.getConfig());
534
535 unitsGatheringEvent.setGatheringDepth(state.getDataHolder().getGatheringDepthText(),
536 state.getDataHolder().getGatheringDepthMin(), state.getDataHolder().getGatheringDepthMax(),
537 state.getDataHolder().getGatheringDepthUnit());
538 // unitsGatheringEvent.setHeight(heightText, heightMin, heightMax,
539 // heightUnit);
540 if (state.getDataHolder().gatheringAgentsList.isEmpty()) {
541 TeamOrPersonBase team = state.getPersonStore().get(state.getDataHolder().gatheringAgentsText);
542 if (team == null){
543 team = parseAuthorString(state.getDataHolder().gatheringAgentsText);
544 if (team != null){
545 state.getPersonStore().put(team.getTitleCache(), team);
546 }
547 }
548 if (team != null){
549 unitsGatheringEvent.setCollector(team,
550 config);
551 }
552
553
554 } else {
555 TeamOrPersonBase team = state.getPersonStore().get(state.getDataHolder().gatheringAgentsList.toString());
556 if (team == null){
557 team = parseAuthorString(state.getDataHolder().gatheringAgentsList.toString());
558 if (team != null){
559 state.getPersonStore().put(team.getTitleCache(), team);
560 }
561
562 }
563 if (team != null){
564 unitsGatheringEvent.setCollector(team,
565 config);
566 }
567 }
568 // count
569 UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
570 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),
571 // getTermService());
572 unitsGatheringArea.setParams(state.getDataHolder().isocountry, state.getDataHolder().country,
573 (state.getConfig()), cdmAppController.getTermService(),
574 cdmAppController.getVocabularyService());
575
576 DefinedTermBase<?> areaCountry = unitsGatheringArea.getCountry();
577
578 // other areas
579 unitsGatheringArea = new UnitsGatheringArea();
580 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
581
582 unitsGatheringArea.setAreas(state.getDataHolder().getNamedAreaList(), (state.getConfig()),
583 cdmAppController.getTermService(), cdmAppController.getVocabularyService());
584
585 ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
586 for (DefinedTermBase namedArea : nas) {
587 unitsGatheringEvent.addArea(namedArea);
588 }
589
590 // copy gathering event to facade
591 GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
592 if (fieldUnit != null) {
593 derivedUnitFacade.setFieldUnit(fieldUnit);
594 if (derivedUnitFacade.getGatheringPeriod() == null && gatheringEvent.getTimeperiod() != null){
595 derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
596 }
597 if (derivedUnitFacade.getLocality() == null && gatheringEvent.getLocality() != null){
598 derivedUnitFacade.setLocality(gatheringEvent.getLocality());
599 }
600 if (derivedUnitFacade.getExactLocation() == null && gatheringEvent.getExactLocation() != null){
601 derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
602 }
603
604 if (derivedUnitFacade.getCollector() == null && gatheringEvent.getCollector() != null){
605 derivedUnitFacade.setCollector(gatheringEvent.getCollector());
606 }
607 if (derivedUnitFacade.getCountry() == null && areaCountry != null){
608 derivedUnitFacade.setCountry((NamedArea) areaCountry);
609 }
610 if (StringUtils.isBlank(derivedUnitFacade.getAbsoluteElevationText()) && StringUtils.isNotBlank(gatheringEvent.getAbsoluteElevationText())){
611 derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
612 }
613 if (derivedUnitFacade.getAbsoluteElevation() == null && gatheringEvent.getAbsoluteElevation() != null){
614 derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
615 }
616 if (derivedUnitFacade.getAbsoluteElevationMaximum() == null && gatheringEvent.getAbsoluteElevationMax() != null){
617 derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
618 }
619 if (StringUtils.isBlank(derivedUnitFacade.getDistanceToGroundText()) && StringUtils.isNotBlank(gatheringEvent.getDistanceToGroundText())){
620 derivedUnitFacade.setDistanceToGroundText(gatheringEvent.getDistanceToGroundText());
621 }
622 if (derivedUnitFacade.getDistanceToGroundMax() == null && gatheringEvent.getDistanceToGroundMax() != null){
623 derivedUnitFacade.setDistanceToGroundMax(gatheringEvent.getDistanceToGroundMax());
624 }
625 if (derivedUnitFacade.getDistanceToGround() == null && gatheringEvent.getDistanceToGround() != null){
626 derivedUnitFacade.setDistanceToGround(gatheringEvent.getDistanceToGround());
627 }
628 if (StringUtils.isBlank(derivedUnitFacade.getDistanceToWaterSurfaceText()) && StringUtils.isNotBlank(gatheringEvent.getDistanceToWaterSurfaceText())){
629 derivedUnitFacade.setDistanceToWaterSurfaceText(gatheringEvent.getDistanceToWaterSurfaceText());
630 }
631 if (derivedUnitFacade.getDistanceToWaterSurfaceMax() == null && gatheringEvent.getDistanceToWaterSurfaceMax() != null){
632 derivedUnitFacade.setDistanceToWaterSurfaceMax(gatheringEvent.getDistanceToWaterSurfaceMax());
633 }
634 if (derivedUnitFacade.getDistanceToWaterSurface() == null && gatheringEvent.getDistanceToWaterSurface() != null){
635 derivedUnitFacade.setDistanceToWaterSurface(gatheringEvent.getDistanceToWaterSurface());
636 }
637 if (derivedUnitFacade.getGatheringPeriod() == null && gatheringEvent.getTimeperiod() != null){
638 derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
639 }
640 if (derivedUnitFacade.getCollectingMethod() == null && gatheringEvent.getCollectingMethod() != null){
641 derivedUnitFacade.setCollectingMethod(gatheringEvent.getCollectingMethod());
642 }
643 for (DefinedTermBase<?> area : unitsGatheringArea.getAreas()) {
644 derivedUnitFacade.addCollectingArea((NamedArea) area);
645 }
646 // add unitNotes
647 if (state.getDataHolder().getUnitNotes() != null) {
648 derivedUnitFacade
649 .addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().getUnitNotes())));
650 }
651 if (gatheringEvent.getAnnotations() != null) {
652 for (Annotation annotation : gatheringEvent.getAnnotations()) {
653 derivedUnitFacade.getGatheringEvent(true).addAnnotation(annotation);
654 }
655 }
656
657 }else{
658 //TODO??
659 }
660
661
662
663
664 // derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
665 // TODO exsiccatum
666
667 // add fieldNumber
668 String fieldNumber = null;
669 if (derivedUnitFacade.getFieldUnit(false) != null) {
670 fieldNumber = derivedUnitFacade.getFieldUnit(false).getFieldNumber();
671 if (fieldNumber == null){
672 derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().getFieldNumber()));
673 }
674 }
675
676 save(unitsGatheringEvent.getLocality(), state);
677
678
679
680
681 // //add Multimedia URLs
682 if (state.getDataHolder().getMultimediaObjects().size() != -1) {
683 for (String multimediaObject : state.getDataHolder().getMultimediaObjects().keySet()) {
684 Media media;
685 try {
686 media = extractMedia(state, multimediaObject);
687 if (media == null) {
688 continue;
689 }
690 derivedUnitFacade.addDerivedUnitMedia(media);
691 if (state.getConfig().isAddMediaAsMediaSpecimen()) {
692 // add media also as specimen scan
693 MediaSpecimen mediaSpecimen = MediaSpecimen
694 .NewInstance(SpecimenOrObservationType.StillImage);
695 mediaSpecimen.setMediaSpecimen(media);
696 // do it only once!!
697 DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
698 if (specimenScanTerm instanceof DefinedTerm) {
699 mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
700 }
701 DerivationEvent derivationEvent = DerivationEvent
702 .NewInstance(DerivationEventType.PREPARATION());
703 derivationEvent.addDerivative(mediaSpecimen);
704 derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
705 }
706
707 } catch (MalformedURLException e) {
708 // TODO Auto-generated catch block
709 e.printStackTrace();
710 }
711
712 }
713 }
714 // multimedia for fieldUnit
715 if (state.getDataHolder().getGatheringMultimediaObjects().size() != -1) {
716 for (String multimediaObject : state.getDataHolder().getGatheringMultimediaObjects().keySet()) {
717 Media media;
718 try {
719 media = extractMedia(state, multimediaObject);
720 if (media == null) {
721 continue;
722 }
723 derivedUnitFacade.addFieldObjectMedia(media);
724
725 } catch (MalformedURLException e) {
726 // TODO Auto-generated catch block
727 e.printStackTrace();
728 }
729
730 }
731 }
732
733
734 if (derivedUnitFacade.getFieldUnit(false) != null) {
735 state.setFieldUnit(derivedUnitFacade.getFieldUnit(false));
736 }
737
738 // handle collection data
739 setCollectionData(state, derivedUnitFacade);
740
741 // Reference stuff
742 SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
743 Map<String, OriginalSourceBase<?>> sourceMap = new HashMap<>();
744
745 state.getDataHolder().setDocSources(new ArrayList<>());
746
747
748
749 IdentifiableSource sour = getIdentifiableSource(state.getImportReference(state.getActualAccessPoint()), null);
750 String idInSource = derivedUnitFacade.getAccessionNumber() != null ? derivedUnitFacade.getAccessionNumber()
751 : derivedUnitFacade.getCatalogNumber() != null ? derivedUnitFacade.getCatalogNumber() : derivedUnitFacade.getBarcode();
752 //sour.getCitation().setUri(state.getActualAccessPoint());
753 sour.setIdInSource(idInSource);
754 try {
755 if (sour.getCitation() != null) {
756 if (StringUtils.isNotBlank(sour.getCitationMicroReference())) {
757 state.getDataHolder().getDocSources()
758 .add(sour.getCitation().getTitleCache() + "---" + sour.getCitationMicroReference());
759 } else {
760 state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache());
761 }
762 }
763 } catch (Exception e) {
764 logger.warn("oups");
765 }
766
767 derivedUnitFacade.addSource(sour);
768
769
770 save(state.getDerivedUnitBase(), state);
771
772 if (logger.isDebugEnabled()) {
773 logger.info("saved ABCD specimen ...");
774 }
775
776 // handle identifications
777 handleIdentifications(state, derivedUnitFacade);
778
779 // associatedUnits
780 if (handleAssociatedUnits) {
781 importAssociatedUnits(state, item, derivedUnitFacade);
782 }
783 if (state.getConfig().getDnaSoure() != null ) {
784 importAssociatedDna(state, item, derivedUnitFacade);
785 }
786 // siblings/ other children
787 if (derivedUnitFacade.getType() != null
788 && (derivedUnitFacade.getType().equals(SpecimenOrObservationType.LivingSpecimen)
789 || derivedUnitFacade.getType().equals(SpecimenOrObservationType.TissueSample)
790 || derivedUnitFacade.getType().equals(SpecimenOrObservationType.OtherSpecimen)
791 || derivedUnitFacade.getType().equals(SpecimenOrObservationType.MaterialSample))
792 && state.getConfig().isGetSiblings()) {
793 getSiblings(state, item, derivedUnitFacade);
794 }
795
796 } catch (Exception e) {
797 String message = "Error when reading record! " + itemObject.toString();
798 logger.warn(message);
799 state.getReport().addException(message, e);
800 e.printStackTrace();
801 state.setUnsuccessfull();
802 }
803
804 return;
805 }
806
807 /**
808 * @param state
809 * @param multimediaObject
810 * @return
811 * @throws MalformedURLException
812 */
813 private Media extractMedia(Abcd206ImportState state, String multimediaObject) throws MalformedURLException {
814 Media media;
815 media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
816
817 Map<String, String> attributes = state.getDataHolder().getMultimediaObjects().get(multimediaObject);
818 if (attributes == null) {
819 attributes = state.getDataHolder().getGatheringMultimediaObjects().get(multimediaObject);
820 if (attributes == null) {
821 logger.error(multimediaObject + " does not exist in dataHolder");
822 state.getResult().addError(multimediaObject + " does not exist in dataHolder");
823 return null;
824 }
825 }
826 if (attributes.containsKey("Context")) {
827 LanguageString description = LanguageString.NewInstance(attributes.get("Context"), Language.ENGLISH());
828 media.addDescription(description);
829 }
830 if (attributes.containsKey("Comment")) {
831 LanguageString description = LanguageString.NewInstance(attributes.get("Comment"), Language.ENGLISH());
832 media.addDescription(description);
833 }
834 if (attributes.containsKey("Creators")) {
835 String creators = attributes.get("Creators");
836 Person artist;
837 Team artistTeam;
838 String[] artists;
839 if (creators != null) {
840 if (creators.contains("&")) {
841 artists = creators.split("&");
842 artistTeam = new Team();
843 for (String creator : artists) {
844 artist = Person.NewTitledInstance(creator);
845 artistTeam.addTeamMember(artist);
846 }
847 media.setArtist(artistTeam);
848 } else {
849
850 artist = Person.NewTitledInstance(creators);
851 media.setArtist(artist);
852 }
853 }
854
855 }
856 if (attributes.containsKey("CreateDate")) {
857 String createDate = attributes.get("CreateDate");
858
859 if (createDate != null) {
860
861 media.setMediaCreated(TimePeriodParser.parseString(createDate));
862 }
863
864 }
865
866 if (attributes.containsKey("License")) {
867 String licence = attributes.get("License");
868
869 if (licence != null) {
870 Rights right = Rights.NewInstance(licence, Language.ENGLISH(), RightsType.LICENSE());
871 media.addRights(right);
872 }
873
874 }
875 return media;
876 }
877
878 @Override
879 protected void importAssociatedUnits(Abcd206ImportState state, Object itemObject,
880 DerivedUnitFacade derivedUnitFacade) {
881 SpecimenDeleteConfigurator deleteConfig = new SpecimenDeleteConfigurator();
882 deleteConfig.setDeleteChildren(false);
883 Abcd206ImportConfigurator config = state.getConfig();
884 // import associated units
885 FieldUnit currentFieldUnit = derivedUnitFacade.innerFieldUnit();
886 // TODO: push state (think of implementing stack architecture for state
887 DerivedUnit currentUnit = state.getDerivedUnitBase();
888 DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
889 URI currentAccessPoint = state.getActualAccessPoint();
890 String currentPrefix = state.getPrefix();
891 Element item = null;
892 if (itemObject instanceof Element) {
893 item = (Element) itemObject;
894 }
895 NodeList unitAssociationList = null;
896 if (item != null) {
897 unitAssociationList = item.getElementsByTagName(currentPrefix + "UnitAssociation");
898 for (int k = 0; k < unitAssociationList.getLength(); k++) {
899 if (unitAssociationList.item(k) instanceof Element) {
900 Element unitAssociation = (Element) unitAssociationList.item(k);
901 UnitAssociationParser unitAssociationParser = new UnitAssociationParser(currentPrefix,
902 state.getReport(), state.getCdmRepository());
903 UnitAssociationWrapper associationWrapper = unitAssociationParser.parse(unitAssociation);
904 if (associationWrapper != null) {
905 state.setActualAccessPoint(associationWrapper.getAccesPoint());
906 NodeList associatedUnits = associationWrapper.getAssociatedUnits();
907 if (associatedUnits != null) {
908 for (int m = 0; m < associatedUnits.getLength(); m++) {
909 if (associatedUnits.item(m) instanceof Element) {
910 state.reset();
911 state.setPrefix(associationWrapper.getPrefix());
912 Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m),
913 new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix()), state);
914 // logger.debug("derived unit: " +
915 // state.getDerivedUnitBase().toString()
916 // + " associated unit: " +
917 // state.getDataHolder().getKindOfUnit() +
918 // ", "
919 // + state.getDataHolder().accessionNumber +
920 // ", "
921 // + state.getDataHolder().getRecordBasis()
922 // + ", "
923 // + state.getDataHolder().getUnitID());
924 handleSingleUnit(state, associatedUnits.item(m), true);
925
926 DerivedUnit associatedUnit = state.getDerivedUnitBase();
927 FieldUnit associatedFieldUnit = null;
928 java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository()
929 .getOccurrenceService().findFieldUnits(associatedUnit.getUuid(), null);
930 // ignore field unit if associated unit has
931 // more than one
932 if (associatedFieldUnits != null && associatedFieldUnits.size() > 1) {
933 state.getReport().addInfoMessage(
934 String.format("%s has more than one field unit.", associatedUnit));
935 } else if (associatedFieldUnits != null && associatedFieldUnits.size() == 1) {
936 associatedFieldUnit = associatedFieldUnits.iterator().next();
937 }
938
939 // attach current unit and associated unit
940 // depending on association type
941
942 // parent-child relation:
943 // copy derivation event and connect parent
944 // and sub derivative
945 // if(associationWrapper.getAssociationType().contains("individual")
946 // ||
947 // associationWrapper.getAssociationType().contains("culture")
948 // ||
949 // associationWrapper.getAssociationType().contains("sample")
950 // ||
951 // associationWrapper.getAssociationType().contains("same
952 // in situ")){
953 if (currentDerivedFrom == null) {
954 state.getReport()
955 .addInfoMessage(String.format(
956 "No derivation event found for unit %s. Defaulting to ACCESSIONING event.",
957 SpecimenImportUtility.getUnitID(currentUnit, config)));
958 DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit,
959 DerivationEventType.ACCESSIONING());
960 } else {
961 DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(
962 associatedUnit, currentUnit, currentDerivedFrom.getType());
963 updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
964 updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
965 updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
966 updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
967
968 }
969 state.getReport().addDerivate(associatedUnit, currentUnit, config);
970 // }
971 // siblings relation
972 // connect current unit to field unit of
973 // associated unit
974 // else
975 // if(associationWrapper.getAssociationType().contains("population")||
976 // associationWrapper.getAssociationType().contains("sample")){
977 // //no associated field unit -> using
978 // current one
979 // if(associatedFieldUnit==null){
980 // if(currentFieldUnit!=null){
981 // DerivationEvent.NewSimpleInstance(currentFieldUnit,
982 // associatedUnit,
983 // DerivationEventType.ACCESSIONING());
984 // }
985 // }
986 // else{
987 // if(currentDerivedFrom==null){
988 // state.getReport().addInfoMessage("No
989 // derivation event found for unit
990 // "+SpecimenImportUtility.getUnitID(currentUnit,
991 // config)+". Defaulting to ACCESIONING
992 // event.");
993 // DerivationEvent.NewSimpleInstance(associatedFieldUnit,
994 // currentUnit,
995 // DerivationEventType.ACCESSIONING());
996 // }
997 // if(currentDerivedFrom!=null &&
998 // associatedFieldUnit==currentFieldUnit){
999 // DerivationEvent updatedDerivationEvent =
1000 // DerivationEvent.NewSimpleInstance(associatedFieldUnit,
1001 // currentUnit,
1002 // currentDerivedFrom.getType());
1003 // updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
1004 // updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
1005 // updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
1006 // updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
1007 // }
1008 // }
1009 // }
1010
1011 // delete current field unit if replaced
1012 if (currentFieldUnit != null && currentDerivedFrom != null
1013 && currentFieldUnit.getDerivationEvents().size() == 1
1014 && currentFieldUnit.getDerivationEvents().contains(currentDerivedFrom) // making
1015 // sure
1016 // that
1017 // the
1018 // field
1019 // unit
1020 && currentDerivedFrom.getDerivatives().size() == 1
1021 && currentDerivedFrom.getDerivatives().contains(currentUnit) // is
1022 // not
1023 // attached
1024 // to
1025 // other
1026 // derived
1027 // units
1028 && currentDerivedFrom != currentUnit.getDerivedFrom() // <-
1029 // derivation
1030 // has
1031 // been
1032 // replaced
1033 // and
1034 // can
1035 // be
1036 // deleted
1037 ) {
1038 currentFieldUnit.removeDerivationEvent(currentDerivedFrom);
1039 if (associatedFieldUnit != null && associatedFieldUnit.getGatheringEvent() != null && associatedFieldUnit.getGatheringEvent().getActor() == null && currentFieldUnit.getGatheringEvent().getActor() != null){
1040 associatedFieldUnit.getGatheringEvent().setActor(currentFieldUnit.getGatheringEvent().getActor());
1041 }
1042 if (associatedFieldUnit != null && associatedFieldUnit.getFieldNumber() == null && currentFieldUnit.getFieldNumber() != null) {
1043 associatedFieldUnit.setFieldNumber(currentFieldUnit.getFieldNumber());
1044 }
1045
1046 if (currentFieldUnit.getDerivationEvents().isEmpty()) {
1047 DeleteResult result = state.getCdmRepository().getOccurrenceService()
1048 .delete(currentFieldUnit, deleteConfig);
1049
1050
1051 } else {
1052
1053 logger.debug("there are still derivation events in fieldUnit "
1054 + currentFieldUnit.getId());
1055 }
1056
1057 }
1058 state.setLastFieldUnit(associatedFieldUnit);
1059 save(associatedUnit, state);
1060 }
1061 }
1062 }
1063 }
1064 }
1065 }
1066 }
1067 // TODO: pop state
1068 state.reset();
1069 state.setDerivedUnitBase(currentUnit);
1070 state.setActualAccessPoint(currentAccessPoint);
1071 state.setPrefix(currentPrefix);
1072 }
1073
1074 private void importAssociatedDna(Abcd206ImportState state, Object itemObject, DerivedUnitFacade derivedUnitFacade) {
1075 URI dnaSource = state.getConfig().getDnaSoure();
1076 String unitId = derivedUnitFacade.getCatalogNumber();
1077 if (unitId == null) {
1078 unitId = derivedUnitFacade.getAccessionNumber();
1079 }
1080 if (unitId == null) {
1081 unitId = derivedUnitFacade.getBarcode();
1082 }
1083
1084 UnitAssociationParser unitParser = new UnitAssociationParser(state.getPrefix(), state.getReport(),
1085 state.getCdmRepository());
1086 UnitAssociationWrapper unitAssociationWrapper = null;
1087
1088 unitAssociationWrapper = unitParser.parseSiblings(unitId, dnaSource);
1089
1090 DerivedUnit currentUnit = state.getDerivedUnitBase();
1091 // DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
1092 FieldUnit currentFieldUnit = derivedUnitFacade.getFieldUnit(false);
1093 if (unitAssociationWrapper != null) {
1094 NodeList associatedUnits = unitAssociationWrapper.getAssociatedUnits();
1095 if (associatedUnits != null) {
1096 for (int m = 0; m < associatedUnits.getLength(); m++) {
1097 if (associatedUnits.item(m) instanceof Element) {
1098 state.reset();
1099 String associationType = AbcdParseUtility
1100 .parseFirstTextContent(((Element) associatedUnits.item(m))
1101 .getElementsByTagName(unitAssociationWrapper.getPrefix() + "AssociationType"));
1102 Abcd206XMLFieldGetter fieldGetter = new Abcd206XMLFieldGetter(state.getDataHolder(), unitAssociationWrapper.getPrefix());
1103 Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m),
1104 fieldGetter,
1105 state);
1106 // logger.debug("derived unit: " +
1107 // state.getDerivedUnitBase().toString() + " associated
1108 // unit: " +state.getDataHolder().getKindOfUnit() + ", "
1109 // + state.getDataHolder().accessionNumber + ", " +
1110 // state.getDataHolder().getRecordBasis() + ", " +
1111 // state.getDataHolder().getUnitID());
1112 URI lastAccessPoint = state.getActualAccessPoint();
1113 state.setActualAccessPoint(dnaSource);
1114 String oldPrefix = state.getPrefix();
1115 state.setPrefix(unitAssociationWrapper.getPrefix());
1116 handleSingleUnit(state, associatedUnits.item(m), false);
1117 state.setActualAccessPoint(lastAccessPoint);
1118 state.setPrefix(oldPrefix);
1119 DerivedUnit associatedUnit = state.getDerivedUnitBase();
1120 FieldUnit associatedFieldUnit = null;
1121 java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository()
1122 .getOccurrenceService().findFieldUnits(associatedUnit.getUuid(), null);
1123 // ignore field unit if associated unit has more than
1124 // one
1125 if (associatedFieldUnits.size() > 1) {
1126 state.getReport()
1127 .addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
1128 } else if (associatedFieldUnits.size() == 1) {
1129 associatedFieldUnit = associatedFieldUnits.iterator().next();
1130 }
1131 // parent-child relation:
1132 if (associationType != null && (associationType.contains("individual")
1133 || associationType.contains("culture") || associationType.contains("sample")
1134 || associationType.contains("isolated"))) {
1135 DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(currentUnit,
1136 associatedUnit, DerivationEventType.ACCESSIONING());
1137
1138 updatedDerivationEvent.setDescription(associationType);
1139 if (associatedFieldUnit != null && !associatedFieldUnit.equals(currentFieldUnit) && currentFieldUnit != null) {
1140 associatedFieldUnit.removeDerivationEvent(updatedDerivationEvent);
1141 if ((associatedFieldUnit.getGatheringEvent() != null && associatedFieldUnit.getGatheringEvent().getActor() != null) && (currentFieldUnit.getGatheringEvent() != null && currentFieldUnit.getGatheringEvent().getActor() == null)){
1142 currentFieldUnit.getGatheringEvent().setActor(associatedFieldUnit.getGatheringEvent().getActor());
1143 }
1144 if (associatedFieldUnit.getFieldNumber() != null && currentFieldUnit.getFieldNumber() == null){
1145 currentFieldUnit.setFieldNumber(associatedFieldUnit.getFieldNumber());
1146 }
1147
1148
1149
1150 SpecimenDeleteConfigurator deleteConfig = new SpecimenDeleteConfigurator();
1151 deleteConfig.setDeleteChildren(false);
1152 DeleteResult result = state.getCdmRepository().getOccurrenceService()
1153 .delete(associatedFieldUnit, deleteConfig);
1154 state.setFieldUnit(currentFieldUnit);
1155
1156 // }
1157 if (updatedDerivationEvent.getOriginals().isEmpty()) {
1158 // state.getCdmRepository().getOccurrenceService().deleteDerivationEvent(updatedDerivationEvent);
1159 }
1160 }else{
1161 state.setLastFieldUnit(associatedFieldUnit);
1162 }
1163 state.getReport().addDerivate(associatedUnit, currentUnit, state.getConfig());
1164 }
1165 save(associatedUnit, state);
1166
1167 }
1168 }
1169 }
1170 }
1171 state.reset();
1172 state.setDerivedUnitBase(currentUnit);
1173
1174 }
1175
1176 /**
1177 * @param derivedUnitFacade
1178 * @param sour
1179 * @return
1180 */
1181 private boolean sourceNotLinkedToElement(DerivedUnitFacade derivedUnitFacade, OriginalSourceBase<?> source) {
1182 Set<IdentifiableSource> linkedSources = derivedUnitFacade.getSources();
1183 for (IdentifiableSource is : linkedSources) {
1184 if (is.getCitation() != null && source.getCitation() != null
1185 && is.getCitation().getTitleCache().equalsIgnoreCase(source.getCitation().getTitleCache())) {
1186 String isDetail = is.getCitationMicroReference();
1187 if ((StringUtils.isBlank(isDetail) && StringUtils.isBlank(source.getCitationMicroReference()))
1188 || (isDetail != null && isDetail.equalsIgnoreCase(source.getCitationMicroReference()))) {
1189 return false;
1190 }
1191 }
1192 }
1193 return true;
1194 }
1195
1196
1197
1198 /**
1199 * setCollectionData : store the collection object into the
1200 * derivedUnitFacade
1201 *
1202 * @param state
1203 */
1204 protected void setCollectionData(Abcd206ImportState state, DerivedUnitFacade derivedUnitFacade) {
1205 Abcd206ImportConfigurator config = state.getConfig();
1206 SpecimenImportUtility.setUnitID(derivedUnitFacade.innerDerivedUnit(), state.getDataHolder().getUnitID(),
1207 config);
1208 if (!config.isMapUnitIdToAccessionNumber()) {
1209 derivedUnitFacade.setAccessionNumber(NB(state.getDataHolder().accessionNumber));
1210 }
1211 // derivedUnitFacade.setCollectorsNumber(NB(state.getDataHolder().collectorsNumber));
1212
1213 /*
1214 * INSTITUTION & COLLECTION
1215 */
1216 // manage institution
1217 Institution institution = this.getInstitution(NB(state.getDataHolder().institutionCode), state);
1218 // manage collection
1219 Collection collection = this.getCollection(institution, NB(state.getDataHolder().collectionCode), state);
1220 // link specimen & collection
1221 derivedUnitFacade.setCollection(collection);
1222 }
1223
1224 /**
1225 * getFacade : get the DerivedUnitFacade based on the recordBasis
1226 *
1227 * @param state
1228 *
1229 * @return DerivedUnitFacade
1230 */
1231 @Override
1232 protected DerivedUnitFacade getFacade(Abcd206ImportState state) {
1233 if (logger.isDebugEnabled()) {
1234 logger.info("getFacade()");
1235 }
1236 SpecimenOrObservationType type = null;
1237 DefinedTerm kindOfUnit = null;
1238
1239 // create specimen
1240 if (NB((state.getDataHolder()).getRecordBasis()) != null) {
1241 if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living") > -1) {
1242 type = SpecimenOrObservationType.LivingSpecimen;
1243 } else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s")
1244 || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen") > -1) {// specimen
1245 type = SpecimenOrObservationType.PreservedSpecimen;
1246 } else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o")
1247 || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation") > -1) {
1248 if (state.getDataHolder().getRecordBasis().toLowerCase().contains("machine")
1249 && state.getDataHolder().getRecordBasis().toLowerCase().contains("observation")) {
1250 type = SpecimenOrObservationType.MachineObservation;
1251 } else if (state.getDataHolder().getRecordBasis().toLowerCase().contains("human")
1252 && state.getDataHolder().getRecordBasis().toLowerCase().contains("observation")) {
1253 type = SpecimenOrObservationType.HumanObservation;
1254 } else {
1255 type = SpecimenOrObservationType.Observation;
1256 }
1257 } else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil") > -1) {
1258 type = SpecimenOrObservationType.Fossil;
1259 } else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("materialsample") > -1) {
1260 type = SpecimenOrObservationType.MaterialSample;
1261 } else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("sample") > -1) {
1262 type = SpecimenOrObservationType.TissueSample;
1263 }
1264
1265 if (type == null) {
1266 logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
1267 type = SpecimenOrObservationType.DerivedUnit;
1268 }
1269 } else {
1270 logger.info("The basis of record is null");
1271 type = SpecimenOrObservationType.DerivedUnit;
1272 }
1273
1274 if (NB((state.getDataHolder()).getKindOfUnit()) != null) {
1275 kindOfUnit = getKindOfUnit(state, null, state.getDataHolder().getKindOfUnit().toLowerCase(), null, null,
1276 null);
1277 if (kindOfUnit == null) {
1278 if (state.getDataHolder().getKindOfUnit().toLowerCase().indexOf("clone") > -1) {
1279 kindOfUnit = getKindOfUnit(state, null, "clone culture", "clone culture", "cc", null);
1280 } else if (state.getDataHolder().getKindOfUnit().toLowerCase().startsWith("live")) {
1281 kindOfUnit = getKindOfUnit(state, null, "live sample", "live sample", "ls", null);
1282 } else if (state.getDataHolder().getKindOfUnit().toLowerCase().startsWith("microscopic slide")) {
1283 kindOfUnit = getKindOfUnit(state, null, "microscopic slide", "microscopic slide", "ms", null);
1284 }
1285
1286 if (kindOfUnit == null) {
1287 logger.info("The kind of unit does not seem to be known: " + state.getDataHolder().getKindOfUnit());
1288
1289 }
1290 }
1291 } else {
1292 logger.info("The kind of unit is null");
1293
1294 }
1295 DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
1296 derivedUnitFacade.setFieldUnit(state.getFieldUnit(state.getDataHolder().getFieldNumber()));
1297 derivedUnitFacade.setDerivedUnitKindOfUnit(kindOfUnit);
1298 derivedUnitFacade.setPreferredStableUri(state.getDataHolder().getPreferredStableUri());
1299 // derivedUnitFacade.setDerivedUnitKindOfUnit(kindOfUnit);
1300 return derivedUnitFacade;
1301 }
1302
1303 private void getCollectorsFromXML(Element root, Abcd206XMLFieldGetter abcdFieldGetter, Abcd206ImportState state) {
1304 NodeList group;
1305
1306 group = root.getChildNodes();
1307 for (int i = 0; i < group.getLength(); i++) {
1308 if (group.item(i).getNodeName().equals(state.getPrefix() + "Identifications")) {
1309 group = group.item(i).getChildNodes();
1310 break;
1311 }
1312 }
1313 // state.getDataHolder().gatheringAgents = "";
1314
1315 abcdFieldGetter.getType(root);
1316 abcdFieldGetter.getGatheringPeople(root);
1317 }
1318
1319
1320 /**
1321 * Load the list of names from the ABCD file and save them
1322 *
1323 * @param state
1324 * : the current ABCD import state
1325 * @param unitsList
1326 * : the unit list from the ABCD file
1327 * @param abcdFieldGetter
1328 * : the ABCD parser
1329 */
1330 private void prepareCollectors(Abcd206ImportState state, NodeList unitsList,
1331 Abcd206XMLFieldGetter abcdFieldGetter) {
1332
1333 TeamOrPersonBase<?> teamOrPerson = null;
1334 Team team = null;
1335 // ImportHelper.setOriginalSource(teamOrPerson,
1336 // state.getConfig().getSourceReference(), collector, "Collector");
1337 for (int i = 0; i < unitsList.getLength(); i++) {
1338 this.getCollectorsFromXML((Element) unitsList.item(i), abcdFieldGetter, state);
1339 if (!(state.getDataHolder().gatheringAgentsList.isEmpty())) {
1340 if (state.getDataHolder().gatheringAgentsList.size() == 1) {
1341 teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsList.get(0));
1342 } else {
1343 team = new Team();
1344 for (String collector : state.getDataHolder().gatheringAgentsList) {
1345 teamOrPerson = parseAuthorString(collector);
1346 if (teamOrPerson instanceof Person) {
1347 team.addTeamMember((Person) teamOrPerson);
1348 } else {
1349 for (Person person : ((Team) teamOrPerson).getTeamMembers()) {
1350 team.addTeamMember(person);
1351 }
1352 }
1353 }
1354 if (team.getTeamMembers() != null && team.getTeamMembers().isEmpty()){
1355 teamOrPerson = team;
1356 }
1357 }
1358 if (!state.getPersonStore().containsId(teamOrPerson.getTitleCache())) {
1359 state.getPersonStore().put(teamOrPerson.getTitleCache(), teamOrPerson);
1360 if (logger.isDebugEnabled()) {
1361 logger.debug("Stored author " + state.getDataHolder().gatheringAgentsList.toString());
1362 }
1363 logger.warn("Not imported author with duplicated aut_id "
1364 + state.getDataHolder().gatheringAgentsList.toString());
1365 }
1366 }
1367 if (!StringUtils.isBlank(state.getDataHolder().gatheringAgentsText)
1368 && state.getDataHolder().gatheringAgentsList.isEmpty()) {
1369 teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsText);
1370 // if (teamOrPerson instanceof Person) {
1371 // //team.addTeamMember((Person) teamOrPerson);
1372 // } else {
1373 // team = new Team();
1374 // for (Person person : ((Team) teamOrPerson).getTeamMembers())
1375 // {
1376 // team.addTeamMember(person);
1377 // }
1378 // }
1379 if (!state.getPersonStore().containsId(teamOrPerson.getTitleCache())) {
1380 state.getPersonStore().put(teamOrPerson.getTitleCache(), teamOrPerson);
1381 if (logger.isDebugEnabled()) {
1382 logger.debug("Stored author " + state.getDataHolder().gatheringAgentsText);
1383 }
1384 logger.warn("Not imported author with duplicated aut_id "
1385 + state.getDataHolder().gatheringAgentsList.toString());
1386 }
1387 }
1388
1389 }
1390
1391 // List<String> collectorsU = new ArrayList<String>(new
1392 // HashSet<String>(collectors));
1393 // List<String> teamsU = new ArrayList<String>(new
1394 // HashSet<String>(teams));
1395 //
1396 //
1397 // //existing teams in DB
1398 // Map<String,Team> titleCacheTeam = new HashMap<String, Team>();
1399 // List<UuidAndTitleCache<Team>> hiberTeam = new
1400 // ArrayList<UuidAndTitleCache<Team>>();//getAgentService().getTeamUuidAndTitleCache();
1401
1402 // Set<UUID> uuids = new HashSet<UUID>();
1403 // for (UuidAndTitleCache<Team> hibernateT:hiberTeam){
1404 // uuids.add(hibernateT.getUuid());
1405 // }
1406 // if (!uuids.isEmpty()){
1407 // List<AgentBase> existingTeams = getAgentService().find(uuids);
1408 // for (AgentBase<?> existingP:existingTeams){
1409 // titleCacheTeam.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Team.class));
1410 // }
1411 // }
1412
1413 // Map<String,UUID> teamMap = new HashMap<String, UUID>();
1414 // for (UuidAndTitleCache<Team> uuidt:hiberTeam){
1415 // teamMap.put(uuidt.getTitleCache(), uuidt.getUuid());
1416 // }
1417
1418 // existing persons in DB
1419 // List<UuidAndTitleCache<Person>> hiberPersons = new
1420 // ArrayList<UuidAndTitleCache<Person>>();//getAgentService().getPersonUuidAndTitleCache();
1421 // Map<String,Person> titleCachePerson = new HashMap<String, Person>();
1422 // uuids = new HashSet<UUID>();
1423 // for (UuidAndTitleCache<Person> hibernateP:hiberPersons){
1424 // uuids.add(hibernateP.getUuid());
1425 // }
1426 //
1427 // if (!uuids.isEmpty()){
1428 // List<AgentBase> existingPersons = getAgentService().find(uuids);
1429 // for (AgentBase<?> existingP:existingPersons){
1430 // titleCachePerson.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Person.class));
1431 // }
1432 // }
1433 //
1434 // Map<String,UUID> personMap = new HashMap<String, UUID>();
1435 // for (UuidAndTitleCache<Person> person:hiberPersons){
1436 // personMap.put(person.getTitleCache(), person.getUuid());
1437 // }
1438 //
1439 // java.util.Collection<Person> personToadd = new ArrayList<Person>();
1440 // java.util.Collection<Team> teamToAdd = new ArrayList<Team>();
1441 //
1442 // for (String collector:collectorsU){
1443 // Person p = Person.NewInstance();
1444 // p.setTitleCache(collector,true);
1445 // if (!personMap.containsKey(p.getTitleCache())){
1446 // personToadd.add(p);
1447 // }
1448 // }
1449 // for (String team:teamsU){
1450 // Team p = Team.NewInstance();
1451 // p.setTitleCache(team,true);
1452 // if (!teamMap.containsKey(p.getTitleCache())){
1453 // teamToAdd.add(p);
1454 // }
1455 // }
1456 //
1457 // if(!personToadd.isEmpty()){
1458 // for (Person agent: personToadd){
1459 // save(agent, state);
1460 // titleCachePerson.put(agent.getTitleCache(),CdmBase.deproxy(agent,
1461 // Person.class) );
1462 // }
1463 // }
1464 //
1465 // Person ptmp ;
1466 // Map <String,Integer>teamdone = new HashMap<String, Integer>();
1467 // for (List<String> collteam: collectorinteams){
1468 // if (!teamdone.containsKey(StringUtils.join(collteam.toArray(),"-"))){
1469 // Team team = new Team();
1470 // boolean em =true;
1471 // for (String collector:collteam){
1472 // ptmp = Person.NewInstance();
1473 // ptmp.setTitleCache(collector,true);
1474 // Person p2 = titleCachePerson.get(ptmp.getTitleCache());
1475 // team.addTeamMember(p2);
1476 // em=false;
1477 // }
1478 // if (!em) {
1479 // teamToAdd.add(team);
1480 // }
1481 // teamdone.put(StringUtils.join(collteam.toArray(),"-"),0);
1482 // }
1483 // }
1484 //
1485 // if(!teamToAdd.isEmpty()){
1486 // for (Team agent: teamToAdd){
1487 // save(agent, state);
1488 // titleCacheTeam.put(agent.getTitleCache(), CdmBase.deproxy(
1489 // agent,Team.class) );
1490 // }
1491 // }
1492
1493 // ((Abcd206ImportConfigurator)
1494 // state.getConfig()).setTeams(titleCacheTeam);
1495 // ((Abcd206ImportConfigurator)
1496 // state.getConfig()).setPersons(titleCachePerson);
1497 }
1498
1499 @Override
1500 protected boolean doCheck(Abcd206ImportState state) {
1501 logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
1502 return true;
1503 }
1504
1505 @Override
1506 protected boolean isIgnore(Abcd206ImportState state) {
1507 return false;
1508 }
1509
1510 @Override
1511 protected DefinedTerm getKindOfUnit(Abcd206ImportState state, UUID uuid, String label, String description,
1512 String labelAbbrev, TermVocabulary<DefinedTerm> voc) {
1513
1514 DefinedTerm unit = null;
1515
1516 if (uuid == null) {
1517 unit = this.kindOfUnitsMap.get(label.toLowerCase());
1518 } else {
1519 unit = state.getKindOfUnit(uuid);
1520
1521 }
1522 if (unit == null) {
1523 unit = (DefinedTerm) getTermService().find(uuid);
1524 if (unit == null) {
1525 if (uuid == null) {
1526 uuid = UUID.randomUUID();
1527 }
1528 unit = DefinedTerm.NewKindOfUnitInstance(description, label, labelAbbrev);
1529 unit.setUuid(uuid);
1530 if (voc == null) {
1531 boolean isOrdered = false;
1532 voc = getVocabulary(state, TermType.KindOfUnit, uuidUserDefinedKindOfUnitVocabulary,
1533 "User defined vocabulary for kind-of-units", "User Defined Measurement kind-of-units", null,
1534 null, isOrdered, unit);
1535 }
1536 voc.addTerm(unit);
1537 getTermService().save(unit);
1538 }
1539 state.putKindOfUnit(unit);
1540 kindOfUnitsMap.put(unit.getLabel().toLowerCase(), unit);
1541 }
1542 return unit;
1543 }
1544
1545
1546
1547 }