2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.specimen
.abcd206
.in
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
13 import java
.net
.MalformedURLException
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import java
.util
.UUID
;
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
;
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
;
82 public class Abcd206Import
extends SpecimenImportBase
<Abcd206ImportConfigurator
, Abcd206ImportState
> {
84 private static final long serialVersionUID
= 3918095362150986307L;
86 private static final UUID SPECIMEN_SCAN_TERM
= UUID
.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
88 private static final Logger logger
= Logger
.getLogger(Abcd206Import
.class);
90 public Abcd206Import() {
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();
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);
112 if (config
.getOccurenceQuery() != null) {
113 BioCaseQueryServiceWrapper queryService
= new BioCaseQueryServiceWrapper();
116 response
= queryService
.query(config
.getOccurenceQuery(), sourceUri
);
117 state
.setActualAccessPoint(sourceUri
);
119 } catch (Exception e
) {
120 logger
.error("An error during ABCD import");
123 SpecimenUserInteraction sui
= state
.getConfig().getSpecimenUserInteraction();
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
);
135 if (state
.getRef() == null) {
136 if (state
.getConfig().getSourceReference() != null) {
137 state
.setRef(state
.getConfig().getSourceReference());
138 state
.addImportReference(state
.getRef());
141 state
.setRef(ReferenceFactory
.newGeneric());
142 state
.getRef().setUri(sourceUri
);
144 if (sourceUri
!= null) {
145 state
.getRef().setTitle(StringUtils
.substringAfter(sourceUri
.toString(), "dsa="));
148 if (!StringUtils
.isBlank(state
.getConfig().getSourceReferenceTitle())) {
149 state
.getRef().setTitle(state
.getConfig().getSourceReferenceTitle());
151 state
.addImportReference(state
.getRef());
158 save(state
.getRef(), state
);
159 state
.getConfig().setSourceReference(state
.getRef());
161 if (state
.getConfig().getClassificationUuid() != null) {
162 // load classification from config if it exists
163 state
.setClassification(getClassificationService().load(state
.getConfig().getClassificationUuid()));
165 if (state
.getClassification() == null) {// no existing
166 // classification was set in
168 List
<Classification
> classificationList
= getClassificationService().list(Classification
.class, 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
);
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
));
184 state
.setClassification(
185 Classification
.NewInstance(cla
, state
.getRef(), Language
.DEFAULT()));
188 save(state
.getClassification(), state
);
190 // use default classification as the classification to import
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
);
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
203 state
.setDefaultClassification(state
.getClassification());
204 save(state
.getDefaultClassification(false), state
);
206 save(state
.getClassification(), state
);
210 if (response
== null) {
211 response
= state
.getConfig().getSource();
213 UnitAssociationWrapper unitAssociationWrapper
= AbcdParseUtility
.parseUnitsNodeList(response
,
215 NodeList unitsList
= unitAssociationWrapper
.getAssociatedUnits();
216 state
.setPrefix(unitAssociationWrapper
.getPrefix());
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
);
224 state
.setDataHolder(new Abcd206DataHolder());
225 state
.getDataHolder().reset();
227 Abcd206XMLFieldGetter abcdFieldGetter
= new Abcd206XMLFieldGetter(state
.getDataHolder(),
229 if (config
.getNomenclaturalCode() != null) {
230 state
.getDataHolder().setNomenclatureCode(config
.getNomenclaturalCode().getKey());
232 prepareCollectors(state
, unitsList
, abcdFieldGetter
);
235 getAgentService().saveOrUpdate((java
.util
.Collection
) state
.getPersonStore().objects());
237 commitTransaction(state
.getTx());
238 state
.setTx(startTransaction());
239 if (state
.getDefaultClassification(false) != null) {
240 state
.setDefaultClassification(
241 getClassificationService().load(state
.getDefaultClassification(false).getUuid()));
243 if (state
.getClassification() != null) {
244 state
.setClassification(getClassificationService().load(state
.getClassification().getUuid()));
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());
253 if (state
.getConfig().getProgressMonitor().isCanceled()) {
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() + ")");
264 // import unit + field unit data
265 state
.setAssociatedUnitIds(state
.getDataHolder().getAssociatedUnitIds());
267 this.handleSingleUnit(state
, item
, true);
268 state
.setLastFieldUnit(null);
271 commitTransaction(state
.getTx());
272 state
.setTx(startTransaction());
273 if (state
.getConfig().isDeduplicateReferences()) {
274 getReferenceService().deduplicate(Reference
.class, null, null);
276 if (state
.getConfig().isDeduplicateClassifications()) {
277 getClassificationService().deduplicate(Classification
.class, null, null);
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
);
286 state
.getReport().printReport(state
.getConfig().getReportUri());
288 if (state
.getConfig().isDownloadSequenceData()) {
289 for (URI uri
: state
.getSequenceDataStableIdentifier()) {
290 // Files.createDirectories(file.getParent()); // optional, make
291 // sure parent dir exists
293 StreamUtils
.downloadFile(uri
.toURL(), "temp");
294 } catch (IOException e
) {
295 // TODO Auto-generated catch block
308 private void createKindOfUnitsMap(Abcd206ImportState state
) {
310 ICdmRepository cdmRepository
= state
.getConfig().getCdmAppController();
311 if (cdmRepository
== null) {
312 cdmRepository
= this;
315 List
<DefinedTerm
> terms
= cdmRepository
.getTermService().listByTermType(TermType
.KindOfUnit
, null, 0, 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
);
331 private void getSiblings(Abcd206ImportState state
, Object item
, DerivedUnitFacade facade
) {
332 String unitId
= facade
.getCatalogNumber();
333 if (unitId
== null) {
334 unitId
= facade
.getAccessionNumber();
337 unitId
= facade
.getBarcode();
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) {
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
) {
359 String associationType
= AbcdParseUtility
360 .parseFirstTextContent(((Element
) associatedUnits
.item(m
))
361 .getElementsByTagName(state
.getPrefix() + "AssociationType"));
363 Abcd206ImportParser
.setUnitPropertiesXML((Element
) associatedUnits
.item(m
),
364 new Abcd206XMLFieldGetter(state
.getDataHolder(), unitAssociationWrapper
.getPrefix()),
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());
373 handleSingleUnit(state
, associatedUnits
.item(m
), false);
375 DerivedUnit associatedUnit
= state
.getDerivedUnitBase();
376 FieldUnit associatedFieldUnit
= null;
377 commitTransaction(state
.getTx());
378 state
.setTx(startTransaction());
379 java
.util
.Collection
<FieldUnit
> associatedFieldUnits
= null;
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());
386 // ignore field unit if associated unit has more than
388 if (associatedFieldUnits
!= null && associatedFieldUnits
.size() > 1) {
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();
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());
400 updatedDerivationEvent
.setDescription(associationType
);
401 if (associatedFieldUnit
!= null && associatedFieldUnit
!= currentFieldUnit
&& currentFieldUnit
!= null) {
402 // associatedFieldUnit.removeDerivationEvent(updatedDerivationEvent);
403 // save(associatedFieldUnit, state);
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");
414 state
.setFieldUnit(currentFieldUnit
);
419 state
.getReport().addDerivate(associatedUnit
, currentUnit
, state
.getConfig());
421 save(associatedUnit
, state
);
428 state
.setDerivedUnitBase(currentUnit
);
433 * Handle a single unit
439 public void handleSingleUnit(Abcd206ImportState state
, Object itemObject
) {
440 handleSingleUnit(state
, itemObject
, true);
443 @SuppressWarnings("rawtypes")
444 public void handleSingleUnit(Abcd206ImportState state
, Object itemObject
, boolean handleAssociatedUnits
) {
445 Element item
= (Element
) itemObject
;
447 Abcd206ImportConfigurator config
= state
.getConfig();
448 if (logger
.isDebugEnabled()) {
449 logger
.info("handleSingleUnit " + state
.getRef());
452 ICdmRepository cdmAppController
= state
.getConfig().getCdmAppController();
453 if (cdmAppController
== null) {
454 cdmAppController
= this;
456 // check if unit already exists
457 DerivedUnitFacade derivedUnitFacade
= null;
458 if (state
.getConfig().isIgnoreImportOfExistingSpecimen() && state
.getDataHolder().getUnitID() != null) {
460 SpecimenOrObservationBase
<?
> existingSpecimen
= findExistingSpecimen(state
.getDataHolder().getUnitID(),
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
);
469 if (state
.getConfig().getDnaSoure() != null) {
470 importAssociatedDna(state
, item
, derivedUnitFacade
);
473 state
.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility
.getUnitID(derivedUnit
, config
),
479 System
.err
.println("dataholder does not contain unit id");
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
494 state
.setDerivedUnitBase(dnaSample
);
495 derivedUnitFacade
= DerivedUnitFacade
.NewInstance(state
.getDerivedUnitBase());
498 derivedUnitFacade
= getFacade(state
);
499 state
.setDerivedUnitBase(derivedUnitFacade
.innerDerivedUnit());
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
);
515 fieldUnit
= state
.getLastFieldUnit();
518 if (fieldUnit
== null){
519 fieldUnit
= FieldUnit
.NewInstance();
520 fieldUnit
.setFieldNumber(state
.getDataHolder().getFieldNumber());
521 state
.setLastFieldUnit(fieldUnit
);
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()),
535 unitsGatheringEvent
.setGatheringDepth(state
.getDataHolder().getGatheringDepthText(),
536 state
.getDataHolder().getGatheringDepthMin(), state
.getDataHolder().getGatheringDepthMax(),
537 state
.getDataHolder().getGatheringDepthUnit());
538 // unitsGatheringEvent.setHeight(heightText, heightMin, heightMax,
540 if (state
.getDataHolder().gatheringAgentsList
.isEmpty()) {
541 TeamOrPersonBase team
= state
.getPersonStore().get(state
.getDataHolder().gatheringAgentsText
);
543 team
= parseAuthorString(state
.getDataHolder().gatheringAgentsText
);
545 state
.getPersonStore().put(team
.getTitleCache(), team
);
549 unitsGatheringEvent
.setCollector(team
,
555 TeamOrPersonBase team
= state
.getPersonStore().get(state
.getDataHolder().gatheringAgentsList
.toString());
557 team
= parseAuthorString(state
.getDataHolder().gatheringAgentsList
.toString());
559 state
.getPersonStore().put(team
.getTitleCache(), team
);
564 unitsGatheringEvent
.setCollector(team
,
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());
576 DefinedTermBase
<?
> areaCountry
= unitsGatheringArea
.getCountry();
579 unitsGatheringArea
= new UnitsGatheringArea();
580 // unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
582 unitsGatheringArea
.setAreas(state
.getDataHolder().getNamedAreaList(), (state
.getConfig()),
583 cdmAppController
.getTermService(), cdmAppController
.getVocabularyService());
585 ArrayList
<DefinedTermBase
> nas
= unitsGatheringArea
.getAreas();
586 for (DefinedTermBase namedArea
: nas
) {
587 unitsGatheringEvent
.addArea(namedArea
);
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());
597 if (derivedUnitFacade
.getLocality() == null && gatheringEvent
.getLocality() != null){
598 derivedUnitFacade
.setLocality(gatheringEvent
.getLocality());
600 if (derivedUnitFacade
.getExactLocation() == null && gatheringEvent
.getExactLocation() != null){
601 derivedUnitFacade
.setExactLocation(gatheringEvent
.getExactLocation());
604 if (derivedUnitFacade
.getCollector() == null && gatheringEvent
.getCollector() != null){
605 derivedUnitFacade
.setCollector(gatheringEvent
.getCollector());
607 if (derivedUnitFacade
.getCountry() == null && areaCountry
!= null){
608 derivedUnitFacade
.setCountry((NamedArea
) areaCountry
);
610 if (StringUtils
.isBlank(derivedUnitFacade
.getAbsoluteElevationText()) && StringUtils
.isNotBlank(gatheringEvent
.getAbsoluteElevationText())){
611 derivedUnitFacade
.setAbsoluteElevationText(gatheringEvent
.getAbsoluteElevationText());
613 if (derivedUnitFacade
.getAbsoluteElevation() == null && gatheringEvent
.getAbsoluteElevation() != null){
614 derivedUnitFacade
.setAbsoluteElevation(gatheringEvent
.getAbsoluteElevation());
616 if (derivedUnitFacade
.getAbsoluteElevationMaximum() == null && gatheringEvent
.getAbsoluteElevationMax() != null){
617 derivedUnitFacade
.setAbsoluteElevationMax(gatheringEvent
.getAbsoluteElevationMax());
619 if (StringUtils
.isBlank(derivedUnitFacade
.getDistanceToGroundText()) && StringUtils
.isNotBlank(gatheringEvent
.getDistanceToGroundText())){
620 derivedUnitFacade
.setDistanceToGroundText(gatheringEvent
.getDistanceToGroundText());
622 if (derivedUnitFacade
.getDistanceToGroundMax() == null && gatheringEvent
.getDistanceToGroundMax() != null){
623 derivedUnitFacade
.setDistanceToGroundMax(gatheringEvent
.getDistanceToGroundMax());
625 if (derivedUnitFacade
.getDistanceToGround() == null && gatheringEvent
.getDistanceToGround() != null){
626 derivedUnitFacade
.setDistanceToGround(gatheringEvent
.getDistanceToGround());
628 if (StringUtils
.isBlank(derivedUnitFacade
.getDistanceToWaterSurfaceText()) && StringUtils
.isNotBlank(gatheringEvent
.getDistanceToWaterSurfaceText())){
629 derivedUnitFacade
.setDistanceToWaterSurfaceText(gatheringEvent
.getDistanceToWaterSurfaceText());
631 if (derivedUnitFacade
.getDistanceToWaterSurfaceMax() == null && gatheringEvent
.getDistanceToWaterSurfaceMax() != null){
632 derivedUnitFacade
.setDistanceToWaterSurfaceMax(gatheringEvent
.getDistanceToWaterSurfaceMax());
634 if (derivedUnitFacade
.getDistanceToWaterSurface() == null && gatheringEvent
.getDistanceToWaterSurface() != null){
635 derivedUnitFacade
.setDistanceToWaterSurface(gatheringEvent
.getDistanceToWaterSurface());
637 if (derivedUnitFacade
.getGatheringPeriod() == null && gatheringEvent
.getTimeperiod() != null){
638 derivedUnitFacade
.setGatheringPeriod(gatheringEvent
.getTimeperiod());
640 if (derivedUnitFacade
.getCollectingMethod() == null && gatheringEvent
.getCollectingMethod() != null){
641 derivedUnitFacade
.setCollectingMethod(gatheringEvent
.getCollectingMethod());
643 for (DefinedTermBase
<?
> area
: unitsGatheringArea
.getAreas()) {
644 derivedUnitFacade
.addCollectingArea((NamedArea
) area
);
647 if (state
.getDataHolder().getUnitNotes() != null) {
649 .addAnnotation(Annotation
.NewDefaultLanguageInstance(NB(state
.getDataHolder().getUnitNotes())));
651 if (gatheringEvent
.getAnnotations() != null) {
652 for (Annotation annotation
: gatheringEvent
.getAnnotations()) {
653 derivedUnitFacade
.getGatheringEvent(true).addAnnotation(annotation
);
664 // derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
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()));
676 save(unitsGatheringEvent
.getLocality(), state
);
681 // //add Multimedia URLs
682 if (state
.getDataHolder().getMultimediaObjects().size() != -1) {
683 for (String multimediaObject
: state
.getDataHolder().getMultimediaObjects().keySet()) {
686 media
= extractMedia(state
, multimediaObject
);
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
);
697 DefinedTermBase specimenScanTerm
= getTermService().load(SPECIMEN_SCAN_TERM
);
698 if (specimenScanTerm
instanceof DefinedTerm
) {
699 mediaSpecimen
.setKindOfUnit((DefinedTerm
) specimenScanTerm
);
701 DerivationEvent derivationEvent
= DerivationEvent
702 .NewInstance(DerivationEventType
.PREPARATION());
703 derivationEvent
.addDerivative(mediaSpecimen
);
704 derivedUnitFacade
.innerDerivedUnit().addDerivationEvent(derivationEvent
);
707 } catch (MalformedURLException e
) {
708 // TODO Auto-generated catch block
714 // multimedia for fieldUnit
715 if (state
.getDataHolder().getGatheringMultimediaObjects().size() != -1) {
716 for (String multimediaObject
: state
.getDataHolder().getGatheringMultimediaObjects().keySet()) {
719 media
= extractMedia(state
, multimediaObject
);
723 derivedUnitFacade
.addFieldObjectMedia(media
);
725 } catch (MalformedURLException e
) {
726 // TODO Auto-generated catch block
734 if (derivedUnitFacade
.getFieldUnit(false) != null) {
735 state
.setFieldUnit(derivedUnitFacade
.getFieldUnit(false));
738 // handle collection data
739 setCollectionData(state
, derivedUnitFacade
);
742 SpecimenUserInteraction sui
= config
.getSpecimenUserInteraction();
743 Map
<String
, OriginalSourceBase
<?
>> sourceMap
= new HashMap
<>();
745 state
.getDataHolder().setDocSources(new ArrayList
<>());
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
);
755 if (sour
.getCitation() != null) {
756 if (StringUtils
.isNotBlank(sour
.getCitationMicroReference())) {
757 state
.getDataHolder().getDocSources()
758 .add(sour
.getCitation().getTitleCache() + "---" + sour
.getCitationMicroReference());
760 state
.getDataHolder().getDocSources().add(sour
.getCitation().getTitleCache());
763 } catch (Exception e
) {
767 derivedUnitFacade
.addSource(sour
);
770 save(state
.getDerivedUnitBase(), state
);
772 if (logger
.isDebugEnabled()) {
773 logger
.info("saved ABCD specimen ...");
776 // handle identifications
777 handleIdentifications(state
, derivedUnitFacade
);
780 if (handleAssociatedUnits
) {
781 importAssociatedUnits(state
, item
, derivedUnitFacade
);
783 if (state
.getConfig().getDnaSoure() != null ) {
784 importAssociatedDna(state
, item
, derivedUnitFacade
);
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
);
796 } catch (Exception e
) {
797 String message
= "Error when reading record! " + itemObject
.toString();
798 logger
.warn(message
);
799 state
.getReport().addException(message
, e
);
801 state
.setUnsuccessfull();
809 * @param multimediaObject
811 * @throws MalformedURLException
813 private Media
extractMedia(Abcd206ImportState state
, String multimediaObject
) throws MalformedURLException
{
815 media
= getImageMedia(multimediaObject
, READ_MEDIA_DATA
);
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");
826 if (attributes
.containsKey("Context")) {
827 LanguageString description
= LanguageString
.NewInstance(attributes
.get("Context"), Language
.ENGLISH());
828 media
.addDescription(description
);
830 if (attributes
.containsKey("Comment")) {
831 LanguageString description
= LanguageString
.NewInstance(attributes
.get("Comment"), Language
.ENGLISH());
832 media
.addDescription(description
);
834 if (attributes
.containsKey("Creators")) {
835 String creators
= attributes
.get("Creators");
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
);
847 media
.setArtist(artistTeam
);
850 artist
= Person
.NewTitledInstance(creators
);
851 media
.setArtist(artist
);
856 if (attributes
.containsKey("CreateDate")) {
857 String createDate
= attributes
.get("CreateDate");
859 if (createDate
!= null) {
861 media
.setMediaCreated(TimePeriodParser
.parseString(createDate
));
866 if (attributes
.containsKey("License")) {
867 String licence
= attributes
.get("License");
869 if (licence
!= null) {
870 Rights right
= Rights
.NewInstance(licence
, Language
.ENGLISH(), RightsType
.LICENSE());
871 media
.addRights(right
);
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();
892 if (itemObject
instanceof Element
) {
893 item
= (Element
) itemObject
;
895 NodeList unitAssociationList
= 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
) {
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() +
919 // + state.getDataHolder().accessionNumber +
921 // + state.getDataHolder().getRecordBasis()
923 // + state.getDataHolder().getUnitID());
924 handleSingleUnit(state
, associatedUnits
.item(m
), true);
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
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();
939 // attach current unit and associated unit
940 // depending on association type
942 // parent-child relation:
943 // copy derivation event and connect parent
944 // and sub derivative
945 // if(associationWrapper.getAssociationType().contains("individual")
947 // associationWrapper.getAssociationType().contains("culture")
949 // associationWrapper.getAssociationType().contains("sample")
951 // associationWrapper.getAssociationType().contains("same
953 if (currentDerivedFrom
== null) {
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());
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());
969 state
.getReport().addDerivate(associatedUnit
, currentUnit
, config
);
972 // connect current unit to field unit of
975 // if(associationWrapper.getAssociationType().contains("population")||
976 // associationWrapper.getAssociationType().contains("sample")){
977 // //no associated field unit -> using
979 // if(associatedFieldUnit==null){
980 // if(currentFieldUnit!=null){
981 // DerivationEvent.NewSimpleInstance(currentFieldUnit,
983 // DerivationEventType.ACCESSIONING());
987 // if(currentDerivedFrom==null){
988 // state.getReport().addInfoMessage("No
989 // derivation event found for unit
990 // "+SpecimenImportUtility.getUnitID(currentUnit,
991 // config)+". Defaulting to ACCESIONING
993 // DerivationEvent.NewSimpleInstance(associatedFieldUnit,
995 // DerivationEventType.ACCESSIONING());
997 // if(currentDerivedFrom!=null &&
998 // associatedFieldUnit==currentFieldUnit){
999 // DerivationEvent updatedDerivationEvent =
1000 // DerivationEvent.NewSimpleInstance(associatedFieldUnit,
1002 // currentDerivedFrom.getType());
1003 // updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
1004 // updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
1005 // updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
1006 // updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
1011 // delete current field unit if replaced
1012 if (currentFieldUnit
!= null && currentDerivedFrom
!= null
1013 && currentFieldUnit
.getDerivationEvents().size() == 1
1014 && currentFieldUnit
.getDerivationEvents().contains(currentDerivedFrom
) // making
1020 && currentDerivedFrom
.getDerivatives().size() == 1
1021 && currentDerivedFrom
.getDerivatives().contains(currentUnit
) // is
1028 && currentDerivedFrom
!= currentUnit
.getDerivedFrom() // <-
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());
1042 if (associatedFieldUnit
!= null && associatedFieldUnit
.getFieldNumber() == null && currentFieldUnit
.getFieldNumber() != null) {
1043 associatedFieldUnit
.setFieldNumber(currentFieldUnit
.getFieldNumber());
1046 if (currentFieldUnit
.getDerivationEvents().isEmpty()) {
1047 DeleteResult result
= state
.getCdmRepository().getOccurrenceService()
1048 .delete(currentFieldUnit
, deleteConfig
);
1053 logger
.debug("there are still derivation events in fieldUnit "
1054 + currentFieldUnit
.getId());
1058 state
.setLastFieldUnit(associatedFieldUnit
);
1059 save(associatedUnit
, state
);
1069 state
.setDerivedUnitBase(currentUnit
);
1070 state
.setActualAccessPoint(currentAccessPoint
);
1071 state
.setPrefix(currentPrefix
);
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();
1080 if (unitId
== null) {
1081 unitId
= derivedUnitFacade
.getBarcode();
1084 UnitAssociationParser unitParser
= new UnitAssociationParser(state
.getPrefix(), state
.getReport(),
1085 state
.getCdmRepository());
1086 UnitAssociationWrapper unitAssociationWrapper
= null;
1088 unitAssociationWrapper
= unitParser
.parseSiblings(unitId
, dnaSource
);
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
) {
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
),
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
1125 if (associatedFieldUnits
.size() > 1) {
1127 .addInfoMessage(String
.format("%s has more than one field unit.", associatedUnit
));
1128 } else if (associatedFieldUnits
.size() == 1) {
1129 associatedFieldUnit
= associatedFieldUnits
.iterator().next();
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());
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());
1144 if (associatedFieldUnit
.getFieldNumber() != null && currentFieldUnit
.getFieldNumber() == null){
1145 currentFieldUnit
.setFieldNumber(associatedFieldUnit
.getFieldNumber());
1150 SpecimenDeleteConfigurator deleteConfig
= new SpecimenDeleteConfigurator();
1151 deleteConfig
.setDeleteChildren(false);
1152 DeleteResult result
= state
.getCdmRepository().getOccurrenceService()
1153 .delete(associatedFieldUnit
, deleteConfig
);
1154 state
.setFieldUnit(currentFieldUnit
);
1157 if (updatedDerivationEvent
.getOriginals().isEmpty()) {
1158 // state.getCdmRepository().getOccurrenceService().deleteDerivationEvent(updatedDerivationEvent);
1161 state
.setLastFieldUnit(associatedFieldUnit
);
1163 state
.getReport().addDerivate(associatedUnit
, currentUnit
, state
.getConfig());
1165 save(associatedUnit
, state
);
1172 state
.setDerivedUnitBase(currentUnit
);
1177 * @param derivedUnitFacade
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()))) {
1199 * setCollectionData : store the collection object into the
1204 protected void setCollectionData(Abcd206ImportState state
, DerivedUnitFacade derivedUnitFacade
) {
1205 Abcd206ImportConfigurator config
= state
.getConfig();
1206 SpecimenImportUtility
.setUnitID(derivedUnitFacade
.innerDerivedUnit(), state
.getDataHolder().getUnitID(),
1208 if (!config
.isMapUnitIdToAccessionNumber()) {
1209 derivedUnitFacade
.setAccessionNumber(NB(state
.getDataHolder().accessionNumber
));
1211 // derivedUnitFacade.setCollectorsNumber(NB(state.getDataHolder().collectorsNumber));
1214 * INSTITUTION & COLLECTION
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
);
1225 * getFacade : get the DerivedUnitFacade based on the recordBasis
1229 * @return DerivedUnitFacade
1232 protected DerivedUnitFacade
getFacade(Abcd206ImportState state
) {
1233 if (logger
.isDebugEnabled()) {
1234 logger
.info("getFacade()");
1236 SpecimenOrObservationType type
= null;
1237 DefinedTerm kindOfUnit
= null;
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
;
1255 type
= SpecimenOrObservationType
.Observation
;
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
;
1266 logger
.info("The basis of record does not seem to be known: " + state
.getDataHolder().getRecordBasis());
1267 type
= SpecimenOrObservationType
.DerivedUnit
;
1270 logger
.info("The basis of record is null");
1271 type
= SpecimenOrObservationType
.DerivedUnit
;
1274 if (NB((state
.getDataHolder()).getKindOfUnit()) != null) {
1275 kindOfUnit
= getKindOfUnit(state
, null, state
.getDataHolder().getKindOfUnit().toLowerCase(), null, 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);
1286 if (kindOfUnit
== null) {
1287 logger
.info("The kind of unit does not seem to be known: " + state
.getDataHolder().getKindOfUnit());
1292 logger
.info("The kind of unit is null");
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
;
1303 private void getCollectorsFromXML(Element root
, Abcd206XMLFieldGetter abcdFieldGetter
, Abcd206ImportState state
) {
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();
1313 // state.getDataHolder().gatheringAgents = "";
1315 abcdFieldGetter
.getType(root
);
1316 abcdFieldGetter
.getGatheringPeople(root
);
1321 * Load the list of names from the ABCD file and save them
1324 * : the current ABCD import state
1326 * : the unit list from the ABCD file
1327 * @param abcdFieldGetter
1330 private void prepareCollectors(Abcd206ImportState state
, NodeList unitsList
,
1331 Abcd206XMLFieldGetter abcdFieldGetter
) {
1333 TeamOrPersonBase
<?
> teamOrPerson
= 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));
1344 for (String collector
: state
.getDataHolder().gatheringAgentsList
) {
1345 teamOrPerson
= parseAuthorString(collector
);
1346 if (teamOrPerson
instanceof Person
) {
1347 team
.addTeamMember((Person
) teamOrPerson
);
1349 for (Person person
: ((Team
) teamOrPerson
).getTeamMembers()) {
1350 team
.addTeamMember(person
);
1354 if (team
.getTeamMembers() != null && team
.getTeamMembers().isEmpty()){
1355 teamOrPerson
= team
;
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());
1363 logger
.warn("Not imported author with duplicated aut_id "
1364 + state
.getDataHolder().gatheringAgentsList
.toString());
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);
1373 // team = new Team();
1374 // for (Person person : ((Team) teamOrPerson).getTeamMembers())
1376 // team.addTeamMember(person);
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
);
1384 logger
.warn("Not imported author with duplicated aut_id "
1385 + state
.getDataHolder().gatheringAgentsList
.toString());
1391 // List<String> collectorsU = new ArrayList<String>(new
1392 // HashSet<String>(collectors));
1393 // List<String> teamsU = new ArrayList<String>(new
1394 // HashSet<String>(teams));
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();
1402 // Set<UUID> uuids = new HashSet<UUID>();
1403 // for (UuidAndTitleCache<Team> hibernateT:hiberTeam){
1404 // uuids.add(hibernateT.getUuid());
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));
1413 // Map<String,UUID> teamMap = new HashMap<String, UUID>();
1414 // for (UuidAndTitleCache<Team> uuidt:hiberTeam){
1415 // teamMap.put(uuidt.getTitleCache(), uuidt.getUuid());
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());
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));
1434 // Map<String,UUID> personMap = new HashMap<String, UUID>();
1435 // for (UuidAndTitleCache<Person> person:hiberPersons){
1436 // personMap.put(person.getTitleCache(), person.getUuid());
1439 // java.util.Collection<Person> personToadd = new ArrayList<Person>();
1440 // java.util.Collection<Team> teamToAdd = new ArrayList<Team>();
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);
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);
1457 // if(!personToadd.isEmpty()){
1458 // for (Person agent: personToadd){
1459 // save(agent, state);
1460 // titleCachePerson.put(agent.getTitleCache(),CdmBase.deproxy(agent,
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);
1479 // teamToAdd.add(team);
1481 // teamdone.put(StringUtils.join(collteam.toArray(),"-"),0);
1485 // if(!teamToAdd.isEmpty()){
1486 // for (Team agent: teamToAdd){
1487 // save(agent, state);
1488 // titleCacheTeam.put(agent.getTitleCache(), CdmBase.deproxy(
1489 // agent,Team.class) );
1493 // ((Abcd206ImportConfigurator)
1494 // state.getConfig()).setTeams(titleCacheTeam);
1495 // ((Abcd206ImportConfigurator)
1496 // state.getConfig()).setPersons(titleCachePerson);
1500 protected boolean doCheck(Abcd206ImportState state
) {
1501 logger
.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
1506 protected boolean isIgnore(Abcd206ImportState state
) {
1511 protected DefinedTerm
getKindOfUnit(Abcd206ImportState state
, UUID uuid
, String label
, String description
,
1512 String labelAbbrev
, TermVocabulary
<DefinedTerm
> voc
) {
1514 DefinedTerm unit
= null;
1517 unit
= this.kindOfUnitsMap
.get(label
.toLowerCase());
1519 unit
= state
.getKindOfUnit(uuid
);
1523 unit
= (DefinedTerm
) getTermService().find(uuid
);
1526 uuid
= UUID
.randomUUID();
1528 unit
= DefinedTerm
.NewKindOfUnitInstance(description
, label
, labelAbbrev
);
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
);
1537 getTermService().save(unit
);
1539 state
.putKindOfUnit(unit
);
1540 kindOfUnitsMap
.put(unit
.getLabel().toLowerCase(), unit
);