Project

General

Profile

Download (62.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9

    
10
package eu.etaxonomy.cdm.io.specimen.abcd206.in;
11

    
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.ext.occurrence.bioCase.BioCaseQueryServiceWrapper;
31
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
32
import eu.etaxonomy.cdm.io.common.ICdmIO;
33
import eu.etaxonomy.cdm.io.common.MapWrapper;
34
import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
35
import eu.etaxonomy.cdm.io.specimen.SpecimenUserInteraction;
36
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringArea;
37
import eu.etaxonomy.cdm.io.specimen.UnitsGatheringEvent;
38
import eu.etaxonomy.cdm.io.specimen.abcd206.in.molecular.AbcdDnaParser;
39
import eu.etaxonomy.cdm.model.agent.Institution;
40
import eu.etaxonomy.cdm.model.agent.Person;
41
import eu.etaxonomy.cdm.model.agent.Team;
42
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
43
import eu.etaxonomy.cdm.model.common.Annotation;
44
import eu.etaxonomy.cdm.model.common.CdmBase;
45
import eu.etaxonomy.cdm.model.common.DefinedTerm;
46
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
47
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
48
import eu.etaxonomy.cdm.model.common.Language;
49
import eu.etaxonomy.cdm.model.common.LanguageString;
50
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
51
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
52
import eu.etaxonomy.cdm.model.location.NamedArea;
53
import eu.etaxonomy.cdm.model.media.Media;
54
import eu.etaxonomy.cdm.model.molecular.DnaSample;
55
import eu.etaxonomy.cdm.model.occurrence.Collection;
56
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
57
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
58
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
59
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
60
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
61
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
62
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
63
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
64
import eu.etaxonomy.cdm.model.reference.Reference;
65
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
66
import eu.etaxonomy.cdm.model.taxon.Classification;
67

    
68
/**
69
 * @author p.kelbert
70
 * @author p.plitzner
71
 * @created 20.10.2008
72
 */
73
@Component
74
public class Abcd206Import extends SpecimenImportBase<Abcd206ImportConfigurator, Abcd206ImportState> {
75

    
76
    private static final long serialVersionUID = 3918095362150986307L;
77

    
78
    private static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
79

    
80
    private static final Logger logger = Logger.getLogger(Abcd206Import.class);
81

    
82

    
83
    public Abcd206Import() {
84
        super();
85
    }
86

    
87

    
88
    @Override
89
//    @SuppressWarnings("rawtypes")
90
    public void doInvoke(Abcd206ImportState state) {
91
        Abcd206ImportConfigurator config = state.getConfig();
92
        Map<String, MapWrapper<? extends CdmBase>> stores = state.getStores();
93
        MapWrapper<TeamOrPersonBase<?>> authorStore = (MapWrapper<TeamOrPersonBase<?>>)stores.get(ICdmIO.TEAM_STORE);
94
        state.setPersonStore(authorStore);
95
        MapWrapper<Reference> referenceStore = (MapWrapper<Reference>)stores.get(ICdmIO.REFERENCE_STORE);
96
        URI sourceUri = config.getSourceUri();
97
        try{
98
            state.setTx(startTransaction());
99
            logger.info("INVOKE Specimen Import from ABCD2.06 XML ");
100
            InputStream response = null;
101
            //init cd repository
102
            if(state.getCdmRepository()==null){
103
                state.setCdmRepository(this);
104
            }
105
            if (config.getOccurenceQuery() != null){
106
                BioCaseQueryServiceWrapper queryService = new BioCaseQueryServiceWrapper();
107
                try {
108

    
109
                   response = queryService.query(config.getOccurenceQuery(), sourceUri);
110
                   state.setActualAccessPoint(sourceUri);
111

    
112
                }catch(Exception e){
113
                    logger.error("An error during ABCD import");
114
                }
115
            }
116
            SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
117

    
118
            //init import reference
119
        //  List<Reference> references = getReferenceService().list(Reference.class, null, null, null, null);
120
         //   List<Reference> references = new ArrayList<Reference>();
121

    
122
//            if (state.getConfig().isInteractWithUser()){
123
//                Map<String,Reference> refMap = new HashMap<String, Reference>();
124
//                for (Reference reference : references) {
125
//                    if (! StringUtils.isBlank(reference.getTitleCache())) {
126
//                        refMap.put(reference.getTitleCache(),reference);
127
//                    }
128
//                }
129
//                state.setRef(sui.askForReference(refMap));
130
//
131
//                if (state.getRef() == null){
132
//                    String cla = sui.createNewReference();
133
//                    if (refMap.get(cla)!= null) {
134
//                        state.setRef(refMap.get(cla));
135
//                    } else {
136
//                        state.setRef(ReferenceFactory.newGeneric());
137
//                        state.getRef().setTitle(cla);
138
//                    }
139
//                }
140
//                else{
141
//                    state.setRef(getReferenceService().find(state.getRef().getUuid()));
142
//                }
143
//            }else{
144
                if (state.getRef()==null){
145
                    String name = NB(state.getConfig().getSourceReferenceTitle());
146
                    for (Reference reference : referenceStore.getAllValues()) {
147
                        if (! StringUtils.isBlank(reference.getTitleCache())) {
148
                            if (reference.getTitleCache().equalsIgnoreCase(name)) {
149
                                state.setRef(reference);
150
                            }
151
                        }
152
                    }
153
                    if (state.getRef() == null){
154
                        if (state.getConfig().getSourceReference() != null){
155
                            state.setRef(state.getConfig().getSourceReference());
156
                        }else{
157
                            state.setRef(ReferenceFactory.newGeneric());
158

    
159
                            if (state.getConfig().getSourceReferenceTitle() != null){
160
                                state.getRef().setTitle(state.getConfig().getSourceReferenceTitle());
161
                            } else{
162
                                state.getRef().setTitle("ABCD Import Source Reference");
163
                            }
164
                        }
165

    
166
                    }
167
                }
168
            //}
169

    
170
            save(state.getRef(), state);
171
            state.getConfig().setSourceReference(state.getRef());
172

    
173
            if(state.getConfig().getClassificationUuid()!=null){
174
                //load classification from config if it exists
175
                state.setClassification(getClassificationService().load(state.getConfig().getClassificationUuid()));
176
            }
177
            if(state.getClassification()==null){//no existing classification was set in config
178
                List<Classification> classificationList = getClassificationService().list(Classification.class, null, null, null, null);
179
                //get classification via user interaction
180
                if (state.getConfig().isUseClassification() && state.getConfig().isInteractWithUser()){
181
                    Map<String,Classification> classMap = new HashMap<>();
182
                    for (Classification tree : classificationList) {
183
                        if (! StringUtils.isBlank(tree.getTitleCache())) {
184
                            classMap.put(tree.getTitleCache(),tree);
185
                        }
186
                    }
187
                    state.setClassification(sui.askForClassification(classMap));
188
                    if (state.getClassification() == null){
189
                        String cla = sui.createNewClassification();
190
                        if (classMap.get(cla)!= null) {
191
                            state.setClassification(classMap.get(cla));
192
                        } else {
193
                            state.setClassification(Classification.NewInstance(cla, state.getRef(), Language.DEFAULT()));
194
                        }
195
                    }
196
                    save(state.getClassification(), state);
197
                }
198
                // use default classification as the classification to import into
199
                if (state.getClassification() == null) {
200
                    String name = NB(state.getConfig().getClassificationName());
201
                    for (Classification classif : classificationList){
202
                        if (classif.getTitleCache() != null && classif.getTitleCache().equalsIgnoreCase(name)) {
203
                            state.setClassification(classif);
204
                        }
205
                    }
206
                    if (state.getClassification() == null){
207
                        state.setClassification(Classification.NewInstance(name, state.getRef(), Language.DEFAULT()));
208
                        //we do not need a default classification when creating an empty new one
209
                        state.setDefaultClassification(state.getClassification());
210
                        save(state.getDefaultClassification(false), state);
211
                    }
212
                    save(state.getClassification(), state);
213
                }
214
            }
215

    
216
            if (response == null){
217
                response =state.getConfig().getSource();
218
            }
219
            UnitAssociationWrapper unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(response, state.getReport());
220
            NodeList unitsList = unitAssociationWrapper.getAssociatedUnits();
221
            state.setPrefix(unitAssociationWrapper.getPrefix());
222

    
223
            if (unitsList != null) {
224
                String message = "nb units to insert: " + unitsList.getLength();
225
                logger.info(message);
226
                state.getConfig().getProgressMonitor().beginTask("Importing ABCD file", unitsList.getLength() + 3);
227
                updateProgress(state, message);
228

    
229
                state.setDataHolder(new Abcd206DataHolder());
230
                state.getDataHolder().reset();
231

    
232
                Abcd206XMLFieldGetter abcdFieldGetter = new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix());
233
                if (config.getNomenclaturalCode() != null){
234
                    state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().getKey());
235
                }
236
                prepareCollectors(state, unitsList, abcdFieldGetter);
237

    
238

    
239
                // save authors
240
                getAgentService().saveOrUpdate((java.util.Collection)state.getPersonStore().objects());
241

    
242
                commitTransaction(state.getTx());
243
                state.setTx(startTransaction());
244
                if (state.getDefaultClassification(false) != null){
245
                    state.setDefaultClassification(getClassificationService().load(state.getDefaultClassification(false).getUuid()));
246
                }
247
                if (state.getClassification() != null){
248
                    state.setClassification(getClassificationService().load(state.getClassification().getUuid()));
249
                }
250
                state.setAssociationRefs(new ArrayList<>());
251
                state.setDescriptionRefs(new ArrayList<>());
252
                state.setDerivedUnitSources(new ArrayList<>());
253
                for (int i = 0; i < unitsList.getLength(); i++) {
254
                    if(state.getConfig().getProgressMonitor().isCanceled()){
255
                        break;
256
                    }
257

    
258
                    state.reset();
259

    
260
                    Element item = (Element) unitsList.item(i);
261
                    Abcd206ImportParser.setUnitPropertiesXML( item, abcdFieldGetter, state);
262
                    updateProgress(state, "Importing data for unit "+state.getDataHolder().getUnitID()+" ("+i+"/"+unitsList.getLength()+")");
263

    
264
                    //import unit + field unit data
265
                    state.setAssociatedUnitIds(state.getDataHolder().getAssociatedUnitIds());
266
                    this.handleSingleUnit(state, item, true);
267

    
268
                }
269
                if(state.getConfig().isDeduplicateReferences()){
270
                    getReferenceService().deduplicate(Reference.class, null, null);
271
                }
272
                if(state.getConfig().isDeduplicateClassifications()){
273
                    getClassificationService().deduplicate(Classification.class, null, null);
274
                }
275
            }
276
            commitTransaction(state.getTx());
277
        }
278
        catch(Exception e){
279
            String errorDuringImport = "Exception during import!";
280
            logger.error(errorDuringImport, e);
281
            state.getReport().addException(errorDuringImport, e);
282
        }
283
        finally{
284
            state.getReport().printReport(state.getConfig().getReportUri());
285
        }
286
        return;
287
    }
288

    
289

    
290
    /**
291
     * @param state
292
     * @param item
293
     */
294
    private void getSiblings(Abcd206ImportState state, Object item, DerivedUnitFacade facade) {
295
        String unitId = facade.getCatalogNumber();
296

    
297
        UnitAssociationParser unitParser = new UnitAssociationParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
298
        UnitAssociationWrapper unitAssociationWrapper = null;
299
        for (URI accessPoint: state.getActualAccesPoint()){
300
            unitAssociationWrapper = unitParser.parseSiblings(unitId, accessPoint);
301
            if (unitAssociationWrapper.getAssociatedUnits() != null){
302
                break;
303
            }
304
        }
305

    
306
       DerivedUnit currentUnit = state.getDerivedUnitBase();
307
     //  DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
308
       FieldUnit currentFieldUnit = facade.getFieldUnit(false);
309
       if(unitAssociationWrapper!=null){
310
            NodeList associatedUnits = unitAssociationWrapper.getAssociatedUnits();
311
            if(associatedUnits!=null){
312
                for(int m=0;m<associatedUnits.getLength();m++){
313
                    if(associatedUnits.item(m) instanceof Element){
314
                        state.reset();
315
                        String associationType = AbcdParseUtility.parseFirstTextContent(((Element) associatedUnits.item(m)).getElementsByTagName(state.getPrefix()+"AssociationType"));
316

    
317
                        Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m), new Abcd206XMLFieldGetter(state.getDataHolder(), unitAssociationWrapper.getPrefix()), state);
318
                       // logger.debug("derived unit: " + state.getDerivedUnitBase().toString() + " associated unit: " +state.getDataHolder().getKindOfUnit() + ", " + state.getDataHolder().accessionNumber + ", " + state.getDataHolder().getRecordBasis() + ", " + state.getDataHolder().getUnitID());
319

    
320
                        handleSingleUnit(state, associatedUnits.item(m), false);
321

    
322
                        DerivedUnit associatedUnit = state.getDerivedUnitBase();
323
                        FieldUnit associatedFieldUnit = null;
324
                        java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository().getOccurrenceService().getFieldUnits(associatedUnit.getUuid());
325
                        //ignore field unit if associated unit has more than one
326
                        if(associatedFieldUnits.size()>1){
327
                            state.getReport().addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
328
                        }
329
                        else if(associatedFieldUnits.size()==1){
330
                            associatedFieldUnit = associatedFieldUnits.iterator().next();
331
                        }
332
                        //parent-child relation:
333
                        if(associationType.contains("individual") || associationType.contains("culture") || associationType.contains("sample")){
334
                            DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(currentUnit, associatedUnit, DerivationEventType.ACCESSIONING());
335
                            if(associatedFieldUnit!=null && associatedFieldUnit != currentFieldUnit){
336
                                associatedFieldUnit.removeDerivationEvent(updatedDerivationEvent);
337
                                state.getCdmRepository().getOccurrenceService().delete(associatedFieldUnit);
338
                            }
339
                            state.getReport().addDerivate(associatedUnit, currentUnit, state.getConfig());
340
                        }
341
                        save(associatedUnit, state);
342

    
343
                    }
344
                }
345
            }
346
        }
347
        state.reset();
348
        state.setDerivedUnitBase(currentUnit);
349

    
350
    }
351

    
352
    /**
353
     * Handle a single unit
354
     * @param state
355
     * @param item
356
     */
357
    @Override
358
    public void handleSingleUnit(Abcd206ImportState state, Object itemObject){
359
        handleSingleUnit(state, itemObject, true);
360
    }
361

    
362

    
363
    @SuppressWarnings("rawtypes")
364
    public void handleSingleUnit(Abcd206ImportState state, Object itemObject, boolean handleAssociatedUnits) {
365
        Element item = (Element) itemObject;
366

    
367
        Abcd206ImportConfigurator config = state.getConfig();
368
        if (logger.isDebugEnabled()) {
369
            logger.info("handleSingleUnit "+state.getRef());
370
        }
371
        try {
372
            ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
373
            if(cdmAppController==null){
374
                cdmAppController = this;
375
            }
376
            //check if unit already exists
377
            DerivedUnitFacade derivedUnitFacade = null;
378
            if(state.getConfig().isIgnoreImportOfExistingSpecimen()){
379
                SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(state.getDataHolder().getUnitID(), state);
380
                if(existingSpecimen!=null && existingSpecimen.isInstanceOf(DerivedUnit.class)){
381
                    DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
382
                    state.setDerivedUnitBase(derivedUnit);
383
                    derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
384
                    if (handleAssociatedUnits){
385
                        importAssociatedUnits(state, item, derivedUnitFacade);
386
                    }
387

    
388
                    state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, config), derivedUnit);
389

    
390
                    return;
391
                }
392
            }
393
            // TODO: implement overwrite/merge specimen
394
//            else if(state.getConfig().isOverwriteExistingSpecimens()){
395
//                Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
396
//                if(!existingSpecimens.getRecords().isEmpty()){
397
//                    derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit);
398
//                    derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
399
//                    fieldUnit = derivedUnitFacade.getFieldUnit(true);
400
//                }
401
//            }
402
            //import new specimen
403

    
404
            // import DNA unit
405
            if(state.getDataHolder().getKindOfUnit() !=null && state.getDataHolder().getKindOfUnit().equalsIgnoreCase("dna")){
406
                AbcdDnaParser dnaParser = new AbcdDnaParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
407
                DnaSample dnaSample = dnaParser.parse(item, state);
408
                save(dnaSample, state);
409
                //set dna as derived unit to avoid creating an extra specimen for this dna sample (instead just the field unit will be created)
410
                state.setDerivedUnitBase(dnaSample);
411
                derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
412
            }
413
            else{
414
                // create facade
415
                derivedUnitFacade = getFacade(state);
416
                state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
417

    
418
            }
419

    
420
            /**
421
             * GATHERING EVENT
422
             */
423

    
424
            //look for existing fieldUnit
425

    
426
            FieldUnit fieldUnit = state.getFieldUnit(state.getDataHolder().getFieldNumber());
427

    
428

    
429
                // gathering event
430
                UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(cdmAppController.getTermService(),
431
                        state.getDataHolder().locality, state.getDataHolder().languageIso, state.getDataHolder().longitude,
432
                        state.getDataHolder().latitude, state.getDataHolder().getGatheringElevationText(),
433
                        state.getDataHolder().getGatheringElevationMin(), state.getDataHolder().getGatheringElevationMax(),
434
                        state.getDataHolder().getGatheringElevationUnit(), state.getDataHolder().getGatheringDateText(),
435
                        state.getDataHolder().getGatheringNotes(), state.getDataHolder().getGatheringMethod(), state.getTransformer().getReferenceSystemByKey(
436
                                state.getDataHolder().getGatheringSpatialDatum()),
437
                         state.getConfig());
438

    
439
                unitsGatheringEvent.setGatheringDepth(state.getDataHolder().getGatheringDepthText(),state.getDataHolder().getGatheringDepthMin(), state.getDataHolder().getGatheringDepthMax(), state.getDataHolder().getGatheringDepthUnit());
440
                //unitsGatheringEvent.setHeight(heightText, heightMin, heightMax, heightUnit);
441
                if(state.getDataHolder().gatheringAgentsList.isEmpty()) {
442
                    unitsGatheringEvent.setCollector(state.getPersonStore().get(state.getDataHolder().gatheringAgentsText), config);
443
                }else{
444
                    unitsGatheringEvent.setCollector(state.getPersonStore().get(state.getDataHolder().gatheringAgentsList.toString()), config);
445
                }
446
                // count
447
                UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
448
                //  unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
449
                unitsGatheringArea.setParams(state.getDataHolder().isocountry, state.getDataHolder().country, (state.getConfig()), cdmAppController.getTermService(), cdmAppController.getOccurrenceService(), cdmAppController.getVocabularyService());
450

    
451
                DefinedTermBase<?> areaCountry =  unitsGatheringArea.getCountry();
452

    
453
                // other areas
454
                unitsGatheringArea = new UnitsGatheringArea();
455
                //            unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
456

    
457
                unitsGatheringArea.setAreas(state.getDataHolder().getNamedAreaList(),(state.getConfig()), cdmAppController.getTermService(), cdmAppController.getVocabularyService());
458

    
459
                ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
460
                for (DefinedTermBase namedArea : nas) {
461
                    unitsGatheringEvent.addArea(namedArea);
462
                }
463

    
464
                // copy gathering event to facade
465
                GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
466
                if (fieldUnit != null){
467
                    derivedUnitFacade.setFieldUnit(fieldUnit);
468
                }
469

    
470
                derivedUnitFacade.setLocality(gatheringEvent.getLocality());
471
                derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
472
                derivedUnitFacade.setCollector(gatheringEvent.getCollector());
473
                derivedUnitFacade.setCountry((NamedArea)areaCountry);
474
                derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
475
                derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
476
                derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
477
                derivedUnitFacade.setDistanceToGroundText(gatheringEvent.getDistanceToGroundText());
478
                derivedUnitFacade.setDistanceToGroundMax(gatheringEvent.getDistanceToGroundMax());
479
                derivedUnitFacade.setDistanceToGround(gatheringEvent.getDistanceToGround());
480
                derivedUnitFacade.setDistanceToWaterSurfaceText(gatheringEvent.getDistanceToWaterSurfaceText());
481
                derivedUnitFacade.setDistanceToWaterSurfaceMax(gatheringEvent.getDistanceToWaterSurfaceMax());
482
                derivedUnitFacade.setDistanceToWaterSurface(gatheringEvent.getDistanceToWaterSurface());
483
                derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
484
                derivedUnitFacade.setCollectingMethod(gatheringEvent.getCollectingMethod());
485

    
486
                for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
487
                    derivedUnitFacade.addCollectingArea((NamedArea) area);
488
                }
489
                //            derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
490
                // TODO exsiccatum
491

    
492
                // add fieldNumber
493
                derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().getFieldNumber()));
494
                save(unitsGatheringEvent.getLocality(), state);
495

    
496
            // add unitNotes
497
            if (state.getDataHolder().getUnitNotes() != null){
498
                derivedUnitFacade.addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().getUnitNotes())));
499
            }
500

    
501

    
502

    
503
            // //add Multimedia URLs
504
            if (state.getDataHolder().getMultimediaObjects().size() != -1) {
505
                for (String multimediaObject : state.getDataHolder().getMultimediaObjects().keySet()) {
506
                    Media media;
507
                    try {
508
                        media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
509
                        Map<String, String> attributes = state.getDataHolder().getMultimediaObjects().get(multimediaObject);
510
                        if (attributes.containsKey("Context")){
511
                            LanguageString description = LanguageString.NewInstance(attributes.get("Context"), Language.ENGLISH());
512
                            media.addDescription(description);
513
                        }
514
                        if (attributes.containsKey("Comment")){
515
                            LanguageString description = LanguageString.NewInstance(attributes.get("Comment"), Language.ENGLISH());
516
                            media.addDescription(description);
517
                        }
518
                        if (attributes.containsKey("Creators")){
519
                            String creators = attributes.get("Creators");
520
                            Person artist;
521
                            Team artistTeam;
522
                            String[] artists;
523
                            if (creators != null){
524
                                if (creators.contains("&")){
525
                                    artists = creators.split("&");
526
                                    artistTeam = new Team();
527
                                    for (String creator:artists){
528
                                        artist = Person.NewTitledInstance(creator);
529
                                        artistTeam.addTeamMember(artist);
530
                                    }
531
                                    media.setArtist(artistTeam);
532
                                } else{
533

    
534
                                    artist = Person.NewTitledInstance(creators);
535
                                    media.setArtist(artist);
536
                                }
537
                            }
538

    
539

    
540

    
541
                        }
542

    
543
                        derivedUnitFacade.addDerivedUnitMedia(media);
544
                        if(state.getConfig().isAddMediaAsMediaSpecimen()){
545
                            //add media also as specimen scan
546
                            MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
547
                            mediaSpecimen.setMediaSpecimen(media);
548
                            //do it only once!!
549
                            DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
550
                            if(specimenScanTerm instanceof DefinedTerm){
551
                                mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
552
                            }
553
                            DerivationEvent derivationEvent = DerivationEvent.NewInstance(DerivationEventType.PREPARATION());
554
                            derivationEvent.addDerivative(mediaSpecimen);
555
                            derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
556
                        }
557

    
558
                    } catch (MalformedURLException e) {
559
                        // TODO Auto-generated catch block
560
                        e.printStackTrace();
561
                    }
562

    
563
                }
564
            }
565
            //multimedia for fieldUnit
566
            if (state.getDataHolder().getGatheringMultimediaObjects().size() != -1) {
567
                for (String multimediaObject : state.getDataHolder().getGatheringMultimediaObjects().keySet()) {
568
                    Media media;
569
                    try {
570
                        media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
571
                        Map<String, String> attributes = state.getDataHolder().getGatheringMultimediaObjects().get(multimediaObject);
572
                        if (attributes.containsKey("Context")){
573
                            LanguageString description = LanguageString.NewInstance(attributes.get("Context"), Language.ENGLISH());
574
                            media.addDescription(description);
575
                        }
576
                        if (attributes.containsKey("Comment")){
577
                            LanguageString description = LanguageString.NewInstance(attributes.get("Comment"), Language.ENGLISH());
578
                            media.addDescription(description);
579
                        }
580
                        if (attributes.containsKey("Creators")){
581
                            String creators = attributes.get("Creators");
582
                            Person artist;
583
                            Team artistTeam;
584
                            String[] artists;
585
                            if (creators != null){
586
                                if (creators.contains("&")){
587
                                    artists = creators.split("&");
588
                                    artistTeam = new Team();
589
                                    for (String creator:artists){
590
                                        artist = Person.NewTitledInstance(creator);
591
                                        artistTeam.addTeamMember(artist);
592
                                    }
593
                                    media.setArtist(artistTeam);
594
                                } else{
595

    
596
                                    artist = Person.NewTitledInstance(creators);
597
                                    media.setArtist(artist);
598
                                }
599
                            }
600

    
601

    
602

    
603
                        }
604

    
605
                        derivedUnitFacade.addFieldObjectMedia(media);
606

    
607

    
608
                    } catch (MalformedURLException e) {
609
                        // TODO Auto-generated catch block
610
                        e.printStackTrace();
611
                    }
612

    
613
                }
614
            }
615

    
616
            //			/*
617
            //			 * merge AND STORE DATA
618
            //			 */
619
            //			getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
620
            //
621
            //			for (NamedArea area : otherAreas) {
622
            //				getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
623
            //			}
624
//           save(derivedUnitFacade.getFieldUnit(false), state);
625
           if (derivedUnitFacade.getFieldUnit(false) != null){
626
               state.setFieldUnit(derivedUnitFacade.getFieldUnit(false));
627
           }
628

    
629
            // handle collection data
630
            setCollectionData(state, derivedUnitFacade);
631

    
632
            //Reference stuff
633
            SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
634
            Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<>();
635

    
636
            state.getDataHolder().setDocSources(new ArrayList<>());
637
            for (String[] fullReference : state.getDataHolder().getReferenceList()) {
638
                String strReference=fullReference[0];
639
                String citationDetail = fullReference[1];
640
                String citationURL = fullReference[2];
641

    
642
                if (!citationURL.isEmpty()) {
643
                    citationDetail+=", "+citationURL;
644
                }
645

    
646
                Reference reference;
647
                if(strReference.equals(state.getRef().getTitleCache())){
648
                    reference = state.getRef();
649
                }
650
                else{
651
                    reference = ReferenceFactory.newGeneric();
652
                    reference.setTitle(strReference);
653
                }
654

    
655

    
656
                save(reference, state);
657
                IdentifiableSource sour = getIdentifiableSource(reference, citationDetail);
658
                sour.getCitation().setUri(state.getActualAccessPoint());
659
                sour.setType(OriginalSourceType.PrimaryTaxonomicSource);
660
                try{
661
                    if (sour.getCitation() != null){
662
                        if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
663
                            state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
664
                        } else {
665
                            state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache());
666
                        }
667
                    }
668
                }catch(Exception e){
669
                    logger.warn("oups");
670
                }
671
                derivedUnitFacade.addSource(sour);
672

    
673
            }
674
//            List<IdentifiableSource> issTmp = new ArrayList<IdentifiableSource>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
675
//            List<DescriptionElementSource> issTmp2 = new ArrayList<DescriptionElementSource>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
676
//
677
//            Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
678
//            if(issTmp2!=null) {
679
//                osbSet.addAll(issTmp2);
680
//            }
681
//            if(issTmp!=null) {
682
//                osbSet.addAll(issTmp);
683
//            }
684

    
685
            IdentifiableSource sour = getIdentifiableSource(state.getRef(),null);
686
            String idInSource = derivedUnitFacade.getAccessionNumber() != null? derivedUnitFacade.getAccessionNumber():derivedUnitFacade.getCatalogNumber();
687
            sour.getCitation().setUri(state.getActualAccessPoint());
688
            sour.setIdInSource(idInSource);
689
            try{
690
                if (sour.getCitation() != null){
691
                    if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
692
                        state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
693
                    } else {
694
                        state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache());
695
                    }
696
                }
697
            }catch(Exception e){
698
                logger.warn("oups");
699
            }
700

    
701
           derivedUnitFacade.addSource(sour);
702
          // sourceMap.put(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference(),sour);
703

    
704
//            if( state.getConfig().isInteractWithUser()){
705
//                List<OriginalSourceBase<?>>sources=null;
706
//                if(!state.isDerivedUnitSourcesSet()){
707
//                    sources= sui.askForSource(sourceMap, "the unit itself","",getReferenceService(), state.getDataHolder().getDocSources());
708
//                    state.setDerivedUnitSources(sources);
709
//                    state.setDerivedUnitSourcesSet(true);
710
//                }
711
//                else{
712
//                    sources=state.getDerivedUnitSources();
713
//                }
714
////                for (OriginalSourceBase<?> source:sources){
715
////                    if(source.isInstanceOf(IdentifiableSource.class)){
716
////                        if(sourceNotLinkedToElement(derivedUnitFacade,source)) {
717
////                            derivedUnitFacade.addSource((IdentifiableSource)source.clone());
718
////                        }
719
////                    }else{
720
////                        if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
721
////                            derivedUnitFacade.addSource(OriginalSourceType.Import,source.getCitation(),source.getCitationMicroReference(), ioName);
722
////                        }
723
////                    }
724
////                }
725
//            }else{
726
//                for (OriginalSourceBase<?> sr : sourceMap.values()){
727
//                    if(sr.isInstanceOf(IdentifiableSource.class)){
728
//                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
729
//                            derivedUnitFacade.addSource((IdentifiableSource)sr.clone());
730
//                        }
731
//                    }else{
732
//                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
733
//                            derivedUnitFacade.addSource(OriginalSourceType.Import,sr.getCitation(),sr.getCitationMicroReference(), ioName);
734
//                        }
735
//                    }
736
//                }
737
//            }
738

    
739
            save(state.getDerivedUnitBase(), state);
740

    
741
            if(logger.isDebugEnabled()) {
742
                logger.info("saved ABCD specimen ...");
743
            }
744

    
745
            // handle identifications
746
            handleIdentifications(state, derivedUnitFacade);
747

    
748
            //associatedUnits
749
            if (handleAssociatedUnits){
750
                importAssociatedUnits(state, item, derivedUnitFacade);
751
            }
752
            //siblings/ other children
753
            if (derivedUnitFacade.getType() != null && (derivedUnitFacade.getType().equals(SpecimenOrObservationType.LivingSpecimen) ||  derivedUnitFacade.getType().equals(SpecimenOrObservationType.TissueSample)  ||  derivedUnitFacade.getType().equals(SpecimenOrObservationType.OtherSpecimen)) &&state.getConfig().isGetSiblings()){
754
                getSiblings(state, item, derivedUnitFacade);
755
            }
756

    
757

    
758
        } catch (Exception e) {
759
            String message = "Error when reading record!";
760
            logger.warn(message);
761
            state.getReport().addException(message, e);
762
            e.printStackTrace();
763
            state.setUnsuccessfull();
764
        }
765

    
766
        return;
767
    }
768

    
769
    @Override
770
    protected void importAssociatedUnits(Abcd206ImportState state, Object itemObject, DerivedUnitFacade derivedUnitFacade) {
771

    
772
        Abcd206ImportConfigurator config = state.getConfig();
773
        //import associated units
774
        FieldUnit currentFieldUnit = derivedUnitFacade.innerFieldUnit();
775
        //TODO: push state (think of implementing stack architecture for state
776
        DerivedUnit currentUnit = state.getDerivedUnitBase();
777
        DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
778
        URI currentAccessPoint = state.getActualAccessPoint();
779
        String currentPrefix = state.getPrefix();
780
        Element item = null;
781
        if (itemObject instanceof Element){
782
            item = (Element)itemObject;
783
        }
784
        NodeList unitAssociationList = null;
785
        if (item != null){
786
            unitAssociationList = item.getElementsByTagName(currentPrefix+"UnitAssociation");
787
            for(int k=0;k<unitAssociationList.getLength();k++){
788
                if(unitAssociationList.item(k) instanceof Element){
789
                    Element unitAssociation = (Element)unitAssociationList.item(k);
790
                    UnitAssociationParser unitAssociationParser = new UnitAssociationParser(currentPrefix, state.getReport(), state.getCdmRepository());
791
                    UnitAssociationWrapper associationWrapper = unitAssociationParser.parse(unitAssociation);
792

    
793
                    state.setActualAccessPoint(associationWrapper.getAccesPoint());
794
                    if(associationWrapper!=null){
795
                        NodeList associatedUnits = associationWrapper.getAssociatedUnits();
796
                        if(associatedUnits!=null){
797
                            for(int m=0;m<associatedUnits.getLength();m++){
798
                                if(associatedUnits.item(m) instanceof Element){
799
                                    state.reset();
800
                                    state.setPrefix(associationWrapper.getPrefix());
801
                                    Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m), new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix()), state);
802
                                    logger.debug("derived unit: " + state.getDerivedUnitBase().toString() + " associated unit: " +state.getDataHolder().getKindOfUnit() + ", " + state.getDataHolder().accessionNumber + ", " + state.getDataHolder().getRecordBasis() + ", " + state.getDataHolder().getUnitID());
803
                                    handleSingleUnit(state, associatedUnits.item(m), true);
804

    
805
                                    DerivedUnit associatedUnit = state.getDerivedUnitBase();
806
                                    FieldUnit associatedFieldUnit = null;
807
                                    java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository().getOccurrenceService().getFieldUnits(associatedUnit.getUuid());
808
                                    //ignore field unit if associated unit has more than one
809
                                    if(associatedFieldUnits.size()>1){
810
                                        state.getReport().addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
811
                                    }
812
                                    else if(associatedFieldUnits.size()==1){
813
                                        associatedFieldUnit = associatedFieldUnits.iterator().next();
814
                                    }
815

    
816
                                    //attach current unit and associated unit depending on association type
817

    
818
                                    //parent-child relation:
819
                                    //copy derivation event and connect parent and sub derivative
820
//                                    if(associationWrapper.getAssociationType().contains("individual") || associationWrapper.getAssociationType().contains("culture") || associationWrapper.getAssociationType().contains("sample") || associationWrapper.getAssociationType().contains("same in situ")){
821
                                        if(currentDerivedFrom==null){
822
                                            state.getReport().addInfoMessage(String.format("No derivation event found for unit %s. Defaulting to ACCESSIONING event.",SpecimenImportUtility.getUnitID(currentUnit, config)));
823
                                            DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, DerivationEventType.ACCESSIONING());
824
                                        }
825
                                        else{
826
                                            DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, currentDerivedFrom.getType());
827
                                            updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
828
                                            updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
829
                                            updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
830
                                            updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
831

    
832
                                        }
833
                                        state.getReport().addDerivate(associatedUnit, currentUnit, config);
834
//                                    }
835
                                    //siblings relation
836
                                    //connect current unit to field unit of associated unit
837
//                                    else if(associationWrapper.getAssociationType().contains("population")|| associationWrapper.getAssociationType().contains("sample")){
838
//                                        //no associated field unit -> using current one
839
//                                        if(associatedFieldUnit==null){
840
//                                            if(currentFieldUnit!=null){
841
//                                                DerivationEvent.NewSimpleInstance(currentFieldUnit, associatedUnit, DerivationEventType.ACCESSIONING());
842
//                                            }
843
//                                        }
844
//                                        else{
845
//                                            if(currentDerivedFrom==null){
846
//                                                state.getReport().addInfoMessage("No derivation event found for unit "+SpecimenImportUtility.getUnitID(currentUnit, config)+". Defaulting to ACCESIONING event.");
847
//                                                DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, DerivationEventType.ACCESSIONING());
848
//                                            }
849
//                                            if(currentDerivedFrom!=null && associatedFieldUnit==currentFieldUnit){
850
//                                                DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, currentDerivedFrom.getType());
851
//                                                updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
852
//                                                updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
853
//                                                updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
854
//                                                updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
855
//                                            }
856
//                                        }
857
//                                    }
858

    
859
                                    //delete current field unit if replaced
860
                                    if(currentFieldUnit!=null && currentDerivedFrom!=null
861
                                            && currentFieldUnit.getDerivationEvents().size()==1  && currentFieldUnit.getDerivationEvents().contains(currentDerivedFrom) //making sure that the field unit
862
                                            && currentDerivedFrom.getDerivatives().size()==1 && currentDerivedFrom.getDerivatives().contains(currentUnit) //is not attached to other derived units
863
                                            && currentDerivedFrom!=currentUnit.getDerivedFrom() // <- derivation has been replaced and can be deleted
864
                                            ){
865
                                        currentFieldUnit.removeDerivationEvent(currentDerivedFrom);
866
                                        state.getCdmRepository().getOccurrenceService().delete(currentFieldUnit);
867
                                         }
868

    
869
                                    save(associatedUnit, state);
870
                                }
871
                            }
872
                        }
873
                    }
874
                }
875
            }
876
        }
877
        //TODO: pop state
878
        state.reset();
879
        state.setDerivedUnitBase(currentUnit);
880
        state.setActualAccessPoint(currentAccessPoint);
881
        state.setPrefix(currentPrefix);
882
    }
883

    
884

    
885

    
886

    
887
    /**
888
     * @param derivedUnitFacade
889
     * @param sour
890
     * @return
891
     */
892
    private boolean sourceNotLinkedToElement(DerivedUnitFacade derivedUnitFacade, OriginalSourceBase<?> source) {
893
        Set<IdentifiableSource> linkedSources = derivedUnitFacade.getSources();
894
        for (IdentifiableSource is:linkedSources){
895
            if (is.getCitation()!=null && source.getCitation()!=null &&
896
                    is.getCitation().getTitleCache().equalsIgnoreCase(source.getCitation().getTitleCache())){
897
                String isDetail =  is.getCitationMicroReference();
898
                if ((StringUtils.isBlank(isDetail) && StringUtils.isBlank(source.getCitationMicroReference()))
899
                        || (isDetail != null && isDetail.equalsIgnoreCase(source.getCitationMicroReference())) ) {
900
                    return false;
901
                }
902
            }
903
        }
904
        return true;
905
    }
906

    
907

    
908

    
909
    //    /**
910
    //     * @param reference
911
    //     * @param citationDetail
912
    //     * @return
913
    //     */
914
    //    private DescriptionElementSource getDescriptionSource(Reference reference, String citationDetail) {
915
    //
916
    //        List<OriginalSourceBase> issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
917
    //
918
    //        try {
919
    //            for (OriginalSourceBase<?> osb:issTmp2){
920
    //                if (osb.getCitation().equals(reference) && osb.getCitationMicroReference().equalsIgnoreCase(citationDetail)) {
921
    //                    return (DescriptionElementSource) osb.clone();
922
    //                }
923
    //            }
924
    //        } catch (CloneNotSupportedException e) {
925
    //            // TODO Auto-generated catch block
926
    //            e.printStackTrace();
927
    //        }
928
    //
929
    //        DescriptionElementSource sour = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
930
    //        return sour;
931
    //    }
932

    
933

    
934

    
935

    
936
    /**
937
     * setCollectionData : store the collection object into the
938
     * derivedUnitFacade
939
     *
940
     * @param state
941
     */
942
    protected void setCollectionData(Abcd206ImportState state, DerivedUnitFacade derivedUnitFacade) {
943
        Abcd206ImportConfigurator config = state.getConfig();
944
        SpecimenImportUtility.setUnitID(derivedUnitFacade.innerDerivedUnit(), state.getDataHolder().getUnitID(), config);
945
        if(!config.isMapUnitIdToAccessionNumber()){
946
            derivedUnitFacade.setAccessionNumber(NB(state.getDataHolder().accessionNumber));
947
        }
948
        // derivedUnitFacade.setCollectorsNumber(NB(state.getDataHolder().collectorsNumber));
949

    
950
        /*
951
         * INSTITUTION & COLLECTION
952
         */
953
        // manage institution
954
        Institution institution = this.getInstitution(NB(state.getDataHolder().institutionCode), state);
955
        // manage collection
956
        Collection collection = this.getCollection(institution, NB(state.getDataHolder().collectionCode), state);
957
        // link specimen & collection
958
        derivedUnitFacade.setCollection(collection);
959
    }
960

    
961
    /**
962
     * getFacade : get the DerivedUnitFacade based on the recordBasis
963
     * @param state
964
     *
965
     * @return DerivedUnitFacade
966
     */
967
    @Override
968
    protected DerivedUnitFacade getFacade(Abcd206ImportState state) {
969
        if(logger.isDebugEnabled()) {
970
            logger.info("getFacade()");
971
        }
972
        SpecimenOrObservationType type = null;
973
        DefinedTerm kindOfUnit = null;
974

    
975
        // create specimen
976
        if (NB((state.getDataHolder()).getRecordBasis()) != null) {
977
            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
978
                type = SpecimenOrObservationType.LivingSpecimen;
979
            }
980
            else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
981
                type = SpecimenOrObservationType.PreservedSpecimen;
982
            }
983
            else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
984
                type = SpecimenOrObservationType.Observation;
985
            }
986
            else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
987
                type = SpecimenOrObservationType.Fossil;
988
            }else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("materialsample")>-1){
989
                type = SpecimenOrObservationType.OtherSpecimen;
990
            }
991
            else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("sample")>-1){
992
                type = SpecimenOrObservationType.TissueSample;
993
            }
994

    
995
            if (type == null) {
996
                logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
997
                type = SpecimenOrObservationType.DerivedUnit;
998
            }
999
        } else {
1000
            logger.info("The basis of record is null");
1001
            type = SpecimenOrObservationType.DerivedUnit;
1002
        }
1003

    
1004
        if (NB((state.getDataHolder()).getKindOfUnit()) != null) {
1005
            if (state.getDataHolder().getKindOfUnit().toLowerCase().indexOf("clone")>-1) {
1006
                kindOfUnit = getKindOfUnit(state, null, "clone culture", "clone culture", "cc", null);
1007
            }
1008
            else if (state.getDataHolder().getKindOfUnit().toLowerCase().startsWith("live"))  {
1009
                kindOfUnit = getKindOfUnit(state, null, "live sample", "live sample", "ls", null);
1010
            }
1011

    
1012

    
1013
            if (kindOfUnit == null) {
1014
                logger.info("The kind of unit does not seem to be known: " + state.getDataHolder().getKindOfUnit());
1015

    
1016
            }
1017
        } else {
1018
            logger.info("The kind of unit is null");
1019

    
1020
        }
1021
        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
1022
        derivedUnitFacade.setFieldUnit(state.getFieldUnit(state.getDataHolder().getFieldNumber()));
1023
        derivedUnitFacade.setKindOfUnit(kindOfUnit);
1024
        return derivedUnitFacade;
1025
    }
1026

    
1027
    private void getCollectorsFromXML(Element root, Abcd206XMLFieldGetter abcdFieldGetter, Abcd206ImportState state) {
1028
        NodeList group;
1029

    
1030
        group = root.getChildNodes();
1031
        for (int i = 0; i < group.getLength(); i++) {
1032
            if (group.item(i).getNodeName().equals(state.getPrefix() + "Identifications")) {
1033
                group = group.item(i).getChildNodes();
1034
                break;
1035
            }
1036
        }
1037
        //state.getDataHolder().gatheringAgents = "";
1038

    
1039
        abcdFieldGetter.getType(root);
1040
        abcdFieldGetter.getGatheringPeople(root);
1041
    }
1042

    
1043

    
1044

    
1045
    //    private void compareABCDtoCDM(URI urlFileName, List<String> knownElts, Abcd206XMLFieldGetter abcdFieldGetter) {
1046
    //        try {
1047
    //            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1048
    //            DocumentBuilder constructeur = factory.newDocumentBuilder();
1049
    //            URL url = urlFileName.toURL();
1050
    //            Object o = url.getContent();
1051
    //            InputStream is = (InputStream) o;
1052
    //            Document document = constructeur.parse(is);
1053
    //            Element root = document.getDocumentElement();
1054
    //            abcdFieldGetter.traverse(root);
1055
    //        }
1056
    //        catch (ParserConfigurationException e){
1057
    //            e.printStackTrace();
1058
    //        }
1059
    //        catch (SAXException e) {
1060
    //            e.printStackTrace();
1061
    //        }
1062
    //        catch (IOException e) {
1063
    //            e.printStackTrace();
1064
    //        }
1065
    //        Set<String> elts = state.getDataHolder().allABCDelements.keySet();
1066
    //        Iterator<String> it = elts.iterator();
1067
    //        String elt;
1068
    //        while (it.hasNext()) {
1069
    //            elt = it.next();
1070
    //            if (knownElts.indexOf(elt) == -1) {
1071
    //                if(DEBUG) {
1072
    //                    logger.info("Unmerged ABCD element: " + elt + " - "+ state.getDataHolder().allABCDelements.get(elt));
1073
    //                }
1074
    //            }
1075
    //        }
1076
    //    }
1077

    
1078
    /**
1079
     * Load the list of names from the ABCD file and save them
1080
     * @param state : the current ABCD import state
1081
     * @param unitsList : the unit list from the ABCD file
1082
     * @param abcdFieldGetter : the ABCD parser
1083
     */
1084
    private void prepareCollectors(Abcd206ImportState state, NodeList unitsList, Abcd206XMLFieldGetter abcdFieldGetter) {
1085

    
1086
        TeamOrPersonBase<?> teamOrPerson = null;
1087
        Team team = null;
1088
        //ImportHelper.setOriginalSource(teamOrPerson, state.getConfig().getSourceReference(), collector, "Collector");
1089
        for (int i = 0; i < unitsList.getLength(); i++) {
1090
            this.getCollectorsFromXML((Element) unitsList.item(i), abcdFieldGetter, state);
1091
            if (!(state.getDataHolder().gatheringAgentsList.isEmpty())){
1092
                if (state.getDataHolder().gatheringAgentsList.size() == 1){
1093
                    teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsList.get(0));
1094
                }else{
1095
                    team = new Team();
1096
                    for(String collector: state.getDataHolder().gatheringAgentsList){
1097
                        teamOrPerson = parseAuthorString(collector);
1098
                        if (teamOrPerson instanceof Person){
1099
                            team.addTeamMember((Person)teamOrPerson);
1100
                        }else{
1101
                            for (Person person: ((Team)teamOrPerson).getTeamMembers()){
1102
                                team.addTeamMember(person);
1103
                            }
1104
                        }
1105
                    }
1106
                }
1107
                if (!state.getPersonStore().containsId(state.getDataHolder().gatheringAgentsList.toString())) {
1108
                    state.getPersonStore().put(state.getDataHolder().gatheringAgentsList.toString(), teamOrPerson);
1109
                    if (logger.isDebugEnabled()) { logger.debug("Stored author " + state.getDataHolder().gatheringAgentsList.toString());}
1110
                    logger.warn("Not imported author with duplicated aut_id " + state.getDataHolder().gatheringAgentsList.toString() );
1111
                }
1112
            }
1113
            if (!StringUtils.isBlank(state.getDataHolder().gatheringAgentsText) && state.getDataHolder().gatheringAgentsList.isEmpty()){
1114
                teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsText);
1115
            }
1116

    
1117
        }
1118

    
1119

    
1120

    
1121

    
1122
//        List<String> collectorsU = new ArrayList<String>(new HashSet<String>(collectors));
1123
//        List<String> teamsU = new ArrayList<String>(new HashSet<String>(teams));
1124
//
1125
//
1126
//        //existing teams in DB
1127
//        Map<String,Team> titleCacheTeam = new HashMap<String, Team>();
1128
       // List<UuidAndTitleCache<Team>> hiberTeam = new ArrayList<UuidAndTitleCache<Team>>();//getAgentService().getTeamUuidAndTitleCache();
1129

    
1130
//        Set<UUID> uuids = new HashSet<UUID>();
1131
//        for (UuidAndTitleCache<Team> hibernateT:hiberTeam){
1132
//            uuids.add(hibernateT.getUuid());
1133
//        }
1134
//        if (!uuids.isEmpty()){
1135
//            List<AgentBase> existingTeams = getAgentService().find(uuids);
1136
//            for (AgentBase<?> existingP:existingTeams){
1137
//                titleCacheTeam.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Team.class));
1138
//            }
1139
//        }
1140

    
1141

    
1142
//        Map<String,UUID> teamMap = new HashMap<String, UUID>();
1143
//        for (UuidAndTitleCache<Team> uuidt:hiberTeam){
1144
//            teamMap.put(uuidt.getTitleCache(), uuidt.getUuid());
1145
//        }
1146

    
1147
        //existing persons in DB
1148
//        List<UuidAndTitleCache<Person>> hiberPersons = new ArrayList<UuidAndTitleCache<Person>>();//getAgentService().getPersonUuidAndTitleCache();
1149
//        Map<String,Person> titleCachePerson = new HashMap<String, Person>();
1150
//        uuids = new HashSet<UUID>();
1151
//        for (UuidAndTitleCache<Person> hibernateP:hiberPersons){
1152
//            uuids.add(hibernateP.getUuid());
1153
//        }
1154
//
1155
//        if (!uuids.isEmpty()){
1156
//            List<AgentBase> existingPersons = getAgentService().find(uuids);
1157
//            for (AgentBase<?> existingP:existingPersons){
1158
//                titleCachePerson.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Person.class));
1159
//            }
1160
//        }
1161
//
1162
//        Map<String,UUID> personMap = new HashMap<String, UUID>();
1163
//        for (UuidAndTitleCache<Person> person:hiberPersons){
1164
//            personMap.put(person.getTitleCache(), person.getUuid());
1165
//        }
1166
//
1167
//        java.util.Collection<Person> personToadd = new ArrayList<Person>();
1168
//        java.util.Collection<Team> teamToAdd = new ArrayList<Team>();
1169
//
1170
//        for (String collector:collectorsU){
1171
//            Person p = Person.NewInstance();
1172
//            p.setTitleCache(collector,true);
1173
//            if (!personMap.containsKey(p.getTitleCache())){
1174
//                personToadd.add(p);
1175
//            }
1176
//        }
1177
//        for (String team:teamsU){
1178
//            Team p = Team.NewInstance();
1179
//            p.setTitleCache(team,true);
1180
//            if (!teamMap.containsKey(p.getTitleCache())){
1181
//                teamToAdd.add(p);
1182
//            }
1183
//        }
1184
//
1185
//        if(!personToadd.isEmpty()){
1186
//            for (Person agent: personToadd){
1187
//                save(agent, state);
1188
//                titleCachePerson.put(agent.getTitleCache(),CdmBase.deproxy(agent, Person.class) );
1189
//            }
1190
//        }
1191
//
1192
//        Person ptmp ;
1193
//        Map <String,Integer>teamdone = new HashMap<String, Integer>();
1194
//        for (List<String> collteam: collectorinteams){
1195
//            if (!teamdone.containsKey(StringUtils.join(collteam.toArray(),"-"))){
1196
//                Team team = new Team();
1197
//                boolean em =true;
1198
//                for (String collector:collteam){
1199
//                    ptmp = Person.NewInstance();
1200
//                    ptmp.setTitleCache(collector,true);
1201
//                    Person p2 = titleCachePerson.get(ptmp.getTitleCache());
1202
//                    team.addTeamMember(p2);
1203
//                    em=false;
1204
//                }
1205
//                if (!em) {
1206
//                    teamToAdd.add(team);
1207
//                }
1208
//                teamdone.put(StringUtils.join(collteam.toArray(),"-"),0);
1209
//            }
1210
//        }
1211
//
1212
//        if(!teamToAdd.isEmpty()){
1213
//            for (Team agent: teamToAdd){
1214
//                save(agent, state);
1215
//                titleCacheTeam.put(agent.getTitleCache(), CdmBase.deproxy( agent,Team.class) );
1216
//            }
1217
//        }
1218

    
1219
//        ((Abcd206ImportConfigurator) state.getConfig()).setTeams(titleCacheTeam);
1220
//        ((Abcd206ImportConfigurator) state.getConfig()).setPersons(titleCachePerson);
1221
    }
1222

    
1223

    
1224
    @Override
1225
    protected boolean doCheck(Abcd206ImportState state) {
1226
        logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
1227
        return true;
1228
    }
1229

    
1230
    @Override
1231
    protected boolean isIgnore(Abcd206ImportState state) {
1232
        return false;
1233
    }
1234

    
1235
}
(2-2/15)