Project

General

Profile

Download (65 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.common.TermType;
53
import eu.etaxonomy.cdm.model.common.TermVocabulary;
54
import eu.etaxonomy.cdm.model.location.NamedArea;
55
import eu.etaxonomy.cdm.model.media.Media;
56
import eu.etaxonomy.cdm.model.molecular.DnaSample;
57
import eu.etaxonomy.cdm.model.occurrence.Collection;
58
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
59
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
60
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
61
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
62
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
63
import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
64
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
65
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
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

    
70
/**
71
 * @author p.kelbert
72
 * @author p.plitzner
73
 * @author k.luther
74
 * @since 20.10.2008
75
 */
76
@Component
77
public class Abcd206Import extends SpecimenImportBase<Abcd206ImportConfigurator, Abcd206ImportState> {
78

    
79
    private static final long serialVersionUID = 3918095362150986307L;
80

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

    
83
    private static final Logger logger = Logger.getLogger(Abcd206Import.class);
84

    
85

    
86
    public Abcd206Import() {
87
        super();
88
    }
89

    
90

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

    
113
                   response = queryService.query(config.getOccurenceQuery(), sourceUri);
114
                   state.setActualAccessPoint(sourceUri);
115

    
116
                }catch(Exception e){
117
                    logger.error("An error during ABCD import");
118
                }
119
            }
120
            SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
121

    
122
            //init import reference
123
        //  List<Reference> references = getReferenceService().list(Reference.class, null, null, null, null);
124
         //   List<Reference> references = new ArrayList<Reference>();
125

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

    
165
                            if (!StringUtils.isBlank(state.getConfig().getSourceReferenceTitle())){
166
                                state.getRef().setTitle(state.getConfig().getSourceReferenceTitle());
167
                            }
168
                        }
169

    
170
                    }
171
                }
172
            //}
173

    
174
            save(state.getRef(), state);
175
            state.getConfig().setSourceReference(state.getRef());
176

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

    
220
            if (response == null){
221
                response =state.getConfig().getSource();
222
            }
223
            UnitAssociationWrapper unitAssociationWrapper = AbcdParseUtility.parseUnitsNodeList(response, state.getReport());
224
            NodeList unitsList = unitAssociationWrapper.getAssociatedUnits();
225
            state.setPrefix(unitAssociationWrapper.getPrefix());
226

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

    
233
                state.setDataHolder(new Abcd206DataHolder());
234
                state.getDataHolder().reset();
235

    
236
                Abcd206XMLFieldGetter abcdFieldGetter = new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix());
237
                if (config.getNomenclaturalCode() != null){
238
                    state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().getKey());
239
                }
240
                prepareCollectors(state, unitsList, abcdFieldGetter);
241

    
242

    
243
                // save authors
244
                getAgentService().saveOrUpdate((java.util.Collection)state.getPersonStore().objects());
245

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

    
262
                    state.reset();
263

    
264
                    Element item = (Element) unitsList.item(i);
265
                    Abcd206ImportParser.setUnitPropertiesXML( item, abcdFieldGetter, state);
266
                    updateProgress(state, "Importing data for unit "+state.getDataHolder().getUnitID()+" ("+i+"/"+unitsList.getLength()+")");
267

    
268
                    //import unit + field unit data
269
                    state.setAssociatedUnitIds(state.getDataHolder().getAssociatedUnitIds());
270
                    this.handleSingleUnit(state, item, true);
271

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

    
291
        if (state.getConfig().isDownloadSequenceData()){
292
            //download fasta files for imported sequences
293
            //TODO: where to store the files and how to create the new Blast DB
294
        }
295
        return;
296
    }
297

    
298

    
299
    /**
300
     *
301
     */
302
    private void createKindOfUnitsMap(Abcd206ImportState state) {
303

    
304
        ICdmRepository cdmRepository = state.getConfig().getCdmAppController();
305
        if (cdmRepository == null){
306
            cdmRepository = this;
307
        }
308

    
309
        List<DefinedTerm> terms = cdmRepository.getTermService().listByTermType(TermType.KindOfUnit, null, 0, null, null);
310
        kindOfUnitsMap = new HashMap<>();
311
        for (DefinedTerm kindOfUnit:terms) {
312
            kindOfUnitsMap.put(kindOfUnit.getLabel().toLowerCase(), kindOfUnit);
313
        }
314
    }
315

    
316

    
317
    /**
318
     * @param state
319
     * @param item
320
     */
321
    private void getSiblings(Abcd206ImportState state, Object item, DerivedUnitFacade facade) {
322
        String unitId = facade.getCatalogNumber();
323
        if (unitId == null){
324
            unitId = facade.getAccessionNumber();
325
        }
326

    
327
        UnitAssociationParser unitParser = new UnitAssociationParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
328
        UnitAssociationWrapper unitAssociationWrapper = null;
329
        for (URI accessPoint: state.getActualAccesPoint()){
330
            unitAssociationWrapper = unitParser.parseSiblings(unitId, accessPoint);
331
            if (unitAssociationWrapper != null &&  unitAssociationWrapper.getAssociatedUnits() != null){
332
                break;
333
            }
334
        }
335

    
336
       DerivedUnit currentUnit = state.getDerivedUnitBase();
337
     //  DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
338
       FieldUnit currentFieldUnit = facade.getFieldUnit(false);
339
       if(unitAssociationWrapper!=null){
340
            NodeList associatedUnits = unitAssociationWrapper.getAssociatedUnits();
341
            if(associatedUnits!=null){
342
                for(int m=0;m<associatedUnits.getLength();m++){
343
                    if(associatedUnits.item(m) instanceof Element){
344
                        state.reset();
345
                        String associationType = AbcdParseUtility.parseFirstTextContent(((Element) associatedUnits.item(m)).getElementsByTagName(state.getPrefix()+"AssociationType"));
346

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

    
350
                        handleSingleUnit(state, associatedUnits.item(m), false);
351

    
352
                        DerivedUnit associatedUnit = state.getDerivedUnitBase();
353
                        FieldUnit associatedFieldUnit = null;
354
                        java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository().getOccurrenceService().getFieldUnits(associatedUnit.getUuid(), null);
355
                        //ignore field unit if associated unit has more than one
356
                        if(associatedFieldUnits.size()>1){
357
                            state.getReport().addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
358
                        }
359
                        else if(associatedFieldUnits.size()==1){
360
                            associatedFieldUnit = associatedFieldUnits.iterator().next();
361
                        }
362
                        //parent-child relation:
363
                        if(associationType.contains("individual") || associationType.contains("culture") || associationType.contains("sample")){
364
                            DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(currentUnit, associatedUnit, DerivationEventType.ACCESSIONING());
365
                            if(associatedFieldUnit!=null && associatedFieldUnit != currentFieldUnit){
366
                                associatedFieldUnit.removeDerivationEvent(updatedDerivationEvent);
367
                                state.getCdmRepository().getOccurrenceService().delete(associatedFieldUnit);
368
                            }
369
                            state.getReport().addDerivate(associatedUnit, currentUnit, state.getConfig());
370
                        }
371
                        save(associatedUnit, state);
372

    
373
                    }
374
                }
375
            }
376
        }
377
        state.reset();
378
        state.setDerivedUnitBase(currentUnit);
379

    
380
    }
381

    
382
    /**
383
     * Handle a single unit
384
     * @param state
385
     * @param item
386
     */
387
    @Override
388
    public void handleSingleUnit(Abcd206ImportState state, Object itemObject){
389
        handleSingleUnit(state, itemObject, true);
390
    }
391

    
392

    
393
    @SuppressWarnings("rawtypes")
394
    public void handleSingleUnit(Abcd206ImportState state, Object itemObject, boolean handleAssociatedUnits) {
395
        Element item = (Element) itemObject;
396

    
397
        Abcd206ImportConfigurator config = state.getConfig();
398
        if (logger.isDebugEnabled()) {
399
            logger.info("handleSingleUnit "+state.getRef());
400
        }
401
        try {
402
            ICdmRepository cdmAppController = state.getConfig().getCdmAppController();
403
            if(cdmAppController==null){
404
                cdmAppController = this;
405
            }
406
            //check if unit already exists
407
            DerivedUnitFacade derivedUnitFacade = null;
408
            if(state.getConfig().isIgnoreImportOfExistingSpecimen()){
409
                SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(state.getDataHolder().getUnitID(), state);
410
                if(existingSpecimen!=null && existingSpecimen.isInstanceOf(DerivedUnit.class)){
411
                    DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
412
                    state.setDerivedUnitBase(derivedUnit);
413
                    derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
414
                    if (handleAssociatedUnits){
415
                        importAssociatedUnits(state, item, derivedUnitFacade);
416
                    }
417

    
418
                    state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, config), derivedUnit);
419

    
420
                    return;
421
                }
422
            }
423
            // TODO: implement overwrite/merge specimen
424
//            else if(state.getConfig().isOverwriteExistingSpecimens()){
425
//                Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
426
//                if(!existingSpecimens.getRecords().isEmpty()){
427
//                    derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit);
428
//                    derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
429
//                    fieldUnit = derivedUnitFacade.getFieldUnit(true);
430
//                }
431
//            }
432
            //import new specimen
433

    
434
            // import DNA unit
435
            if(state.getDataHolder().getKindOfUnit() !=null && state.getDataHolder().getKindOfUnit().equalsIgnoreCase("dna")){
436
                AbcdDnaParser dnaParser = new AbcdDnaParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
437
                DnaSample dnaSample = dnaParser.parse(item, state);
438
                save(dnaSample, state);
439
                //set dna as derived unit to avoid creating an extra specimen for this dna sample (instead just the field unit will be created)
440
                state.setDerivedUnitBase(dnaSample);
441
                derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
442
            }
443
            else{
444
                // create facade
445
                derivedUnitFacade = getFacade(state);
446
                state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
447

    
448
            }
449

    
450
            /**
451
             * GATHERING EVENT
452
             */
453

    
454
            //look for existing fieldUnit
455

    
456
            FieldUnit fieldUnit = state.getFieldUnit(state.getDataHolder().getFieldNumber());
457

    
458

    
459
                // gathering event
460
                UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(cdmAppController.getTermService(),
461
                        state.getDataHolder().locality, state.getDataHolder().languageIso, state.getDataHolder().longitude,
462
                        state.getDataHolder().latitude, state.getDataHolder().getGatheringElevationText(),
463
                        state.getDataHolder().getGatheringElevationMin(), state.getDataHolder().getGatheringElevationMax(),
464
                        state.getDataHolder().getGatheringElevationUnit(), state.getDataHolder().getGatheringDateText(),
465
                        state.getDataHolder().getGatheringNotes(), state.getDataHolder().getGatheringMethod(), state.getTransformer().getReferenceSystemByKey(
466
                                state.getDataHolder().getGatheringSpatialDatum()),
467
                         state.getConfig());
468

    
469
                unitsGatheringEvent.setGatheringDepth(state.getDataHolder().getGatheringDepthText(),state.getDataHolder().getGatheringDepthMin(), state.getDataHolder().getGatheringDepthMax(), state.getDataHolder().getGatheringDepthUnit());
470
                //unitsGatheringEvent.setHeight(heightText, heightMin, heightMax, heightUnit);
471
                if(state.getDataHolder().gatheringAgentsList.isEmpty()) {
472
                    unitsGatheringEvent.setCollector(state.getPersonStore().get(state.getDataHolder().gatheringAgentsText), config);
473
                }else{
474
                    unitsGatheringEvent.setCollector(state.getPersonStore().get(state.getDataHolder().gatheringAgentsList.toString()), config);
475
                }
476
                // count
477
                UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
478
                //  unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
479
                unitsGatheringArea.setParams(state.getDataHolder().isocountry, state.getDataHolder().country, (state.getConfig()), cdmAppController.getTermService(), cdmAppController.getOccurrenceService(), cdmAppController.getVocabularyService());
480

    
481
                DefinedTermBase<?> areaCountry =  unitsGatheringArea.getCountry();
482

    
483
                // other areas
484
                unitsGatheringArea = new UnitsGatheringArea();
485
                //            unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
486

    
487
                unitsGatheringArea.setAreas(state.getDataHolder().getNamedAreaList(),(state.getConfig()), cdmAppController.getTermService(), cdmAppController.getVocabularyService());
488

    
489
                ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
490
                for (DefinedTermBase namedArea : nas) {
491
                    unitsGatheringEvent.addArea(namedArea);
492
                }
493

    
494
                // copy gathering event to facade
495
                GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
496
                if (fieldUnit != null){
497
                    derivedUnitFacade.setFieldUnit(fieldUnit);
498
                }
499

    
500
                derivedUnitFacade.setLocality(gatheringEvent.getLocality());
501
                derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
502
                derivedUnitFacade.setCollector(gatheringEvent.getCollector());
503
                derivedUnitFacade.setCountry((NamedArea)areaCountry);
504
                derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
505
                derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
506
                derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
507
                derivedUnitFacade.setDistanceToGroundText(gatheringEvent.getDistanceToGroundText());
508
                derivedUnitFacade.setDistanceToGroundMax(gatheringEvent.getDistanceToGroundMax());
509
                derivedUnitFacade.setDistanceToGround(gatheringEvent.getDistanceToGround());
510
                derivedUnitFacade.setDistanceToWaterSurfaceText(gatheringEvent.getDistanceToWaterSurfaceText());
511
                derivedUnitFacade.setDistanceToWaterSurfaceMax(gatheringEvent.getDistanceToWaterSurfaceMax());
512
                derivedUnitFacade.setDistanceToWaterSurface(gatheringEvent.getDistanceToWaterSurface());
513
                derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
514
                derivedUnitFacade.setCollectingMethod(gatheringEvent.getCollectingMethod());
515

    
516
                for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
517
                    derivedUnitFacade.addCollectingArea((NamedArea) area);
518
                }
519
                //            derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
520
                // TODO exsiccatum
521

    
522
                // add fieldNumber
523
                derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().getFieldNumber()));
524
                save(unitsGatheringEvent.getLocality(), state);
525

    
526
            // add unitNotes
527
            if (state.getDataHolder().getUnitNotes() != null){
528
                derivedUnitFacade.addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().getUnitNotes())));
529
            }
530

    
531

    
532

    
533
            // //add Multimedia URLs
534
            if (state.getDataHolder().getMultimediaObjects().size() != -1) {
535
                for (String multimediaObject : state.getDataHolder().getMultimediaObjects().keySet()) {
536
                    Media media;
537
                    try {
538
                        media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
539
                        Map<String, String> attributes = state.getDataHolder().getMultimediaObjects().get(multimediaObject);
540
                        if (attributes.containsKey("Context")){
541
                            LanguageString description = LanguageString.NewInstance(attributes.get("Context"), Language.ENGLISH());
542
                            media.addDescription(description);
543
                        }
544
                        if (attributes.containsKey("Comment")){
545
                            LanguageString description = LanguageString.NewInstance(attributes.get("Comment"), Language.ENGLISH());
546
                            media.addDescription(description);
547
                        }
548
                        if (attributes.containsKey("Creators")){
549
                            String creators = attributes.get("Creators");
550
                            Person artist;
551
                            Team artistTeam;
552
                            String[] artists;
553
                            if (creators != null){
554
                                if (creators.contains("&")){
555
                                    artists = creators.split("&");
556
                                    artistTeam = new Team();
557
                                    for (String creator:artists){
558
                                        artist = Person.NewTitledInstance(creator);
559
                                        artistTeam.addTeamMember(artist);
560
                                    }
561
                                    media.setArtist(artistTeam);
562
                                } else{
563

    
564
                                    artist = Person.NewTitledInstance(creators);
565
                                    media.setArtist(artist);
566
                                }
567
                            }
568

    
569

    
570

    
571
                        }
572

    
573
                        derivedUnitFacade.addDerivedUnitMedia(media);
574
                        if(state.getConfig().isAddMediaAsMediaSpecimen()){
575
                            //add media also as specimen scan
576
                            MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.StillImage);
577
                            mediaSpecimen.setMediaSpecimen(media);
578
                            //do it only once!!
579
                            DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
580
                            if(specimenScanTerm instanceof DefinedTerm){
581
                                mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
582
                            }
583
                            DerivationEvent derivationEvent = DerivationEvent.NewInstance(DerivationEventType.PREPARATION());
584
                            derivationEvent.addDerivative(mediaSpecimen);
585
                            derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
586
                        }
587

    
588
                    } catch (MalformedURLException e) {
589
                        // TODO Auto-generated catch block
590
                        e.printStackTrace();
591
                    }
592

    
593
                }
594
            }
595
            //multimedia for fieldUnit
596
            if (state.getDataHolder().getGatheringMultimediaObjects().size() != -1) {
597
                for (String multimediaObject : state.getDataHolder().getGatheringMultimediaObjects().keySet()) {
598
                    Media media;
599
                    try {
600
                        media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
601
                        Map<String, String> attributes = state.getDataHolder().getGatheringMultimediaObjects().get(multimediaObject);
602
                        if (attributes.containsKey("Context")){
603
                            LanguageString description = LanguageString.NewInstance(attributes.get("Context"), Language.ENGLISH());
604
                            media.addDescription(description);
605
                        }
606
                        if (attributes.containsKey("Comment")){
607
                            LanguageString description = LanguageString.NewInstance(attributes.get("Comment"), Language.ENGLISH());
608
                            media.addDescription(description);
609
                        }
610
                        if (attributes.containsKey("Creators")){
611
                            String creators = attributes.get("Creators");
612
                            Person artist;
613
                            Team artistTeam;
614
                            String[] artists;
615
                            if (creators != null){
616
                                if (creators.contains("&")){
617
                                    artists = creators.split("&");
618
                                    artistTeam = new Team();
619
                                    for (String creator:artists){
620
                                        artist = Person.NewTitledInstance(creator);
621
                                        artistTeam.addTeamMember(artist);
622
                                    }
623
                                    media.setArtist(artistTeam);
624
                                } else{
625

    
626
                                    artist = Person.NewTitledInstance(creators);
627
                                    media.setArtist(artist);
628
                                }
629
                            }
630

    
631

    
632

    
633
                        }
634

    
635
                        derivedUnitFacade.addFieldObjectMedia(media);
636

    
637

    
638
                    } catch (MalformedURLException e) {
639
                        // TODO Auto-generated catch block
640
                        e.printStackTrace();
641
                    }
642

    
643
                }
644
            }
645

    
646
            //			/*
647
            //			 * merge AND STORE DATA
648
            //			 */
649
            //			getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
650
            //
651
            //			for (NamedArea area : otherAreas) {
652
            //				getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
653
            //			}
654
//           save(derivedUnitFacade.getFieldUnit(false), state);
655
           if (derivedUnitFacade.getFieldUnit(false) != null){
656
               state.setFieldUnit(derivedUnitFacade.getFieldUnit(false));
657
           }
658

    
659
            // handle collection data
660
            setCollectionData(state, derivedUnitFacade);
661

    
662
            //Reference stuff
663
            SpecimenUserInteraction sui = config.getSpecimenUserInteraction();
664
            Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<>();
665

    
666
            state.getDataHolder().setDocSources(new ArrayList<>());
667
            for (String[] fullReference : state.getDataHolder().getReferenceList()) {
668
                String strReference=fullReference[0];
669
                String citationDetail = fullReference[1];
670
                String citationURL = fullReference[2];
671

    
672
                if (!citationURL.isEmpty()) {
673
                    citationDetail+=", "+citationURL;
674
                }
675

    
676
                Reference reference;
677
                if(strReference.equals(state.getRef().getTitleCache())){
678
                    reference = state.getRef();
679
                }
680
                else{
681
                    reference = ReferenceFactory.newGeneric();
682
                    reference.setTitle(strReference);
683
                }
684

    
685

    
686
                save(reference, state);
687
                IdentifiableSource sour = getIdentifiableSource(reference, citationDetail);
688
                sour.getCitation().setUri(state.getActualAccessPoint());
689
                sour.setType(OriginalSourceType.PrimaryTaxonomicSource);
690
                try{
691
                    if (sour.getCitation() != null){
692
                        if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
693
                            state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
694
                        } else {
695
                            state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache());
696
                        }
697
                    }
698
                }catch(Exception e){
699
                    logger.warn("oups");
700
                }
701
                derivedUnitFacade.addSource(sour);
702

    
703
            }
704
//            List<IdentifiableSource> issTmp = new ArrayList<IdentifiableSource>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
705
//            List<DescriptionElementSource> issTmp2 = new ArrayList<DescriptionElementSource>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
706
//
707
//            Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
708
//            if(issTmp2!=null) {
709
//                osbSet.addAll(issTmp2);
710
//            }
711
//            if(issTmp!=null) {
712
//                osbSet.addAll(issTmp);
713
//            }
714

    
715
            IdentifiableSource sour = getIdentifiableSource(state.getRef(),null);
716
            String idInSource = derivedUnitFacade.getAccessionNumber() != null? derivedUnitFacade.getAccessionNumber():derivedUnitFacade.getCatalogNumber();
717
            sour.getCitation().setUri(state.getActualAccessPoint());
718
            sour.setIdInSource(idInSource);
719
            try{
720
                if (sour.getCitation() != null){
721
                    if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
722
                        state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
723
                    } else {
724
                        state.getDataHolder().getDocSources().add(sour.getCitation().getTitleCache());
725
                    }
726
                }
727
            }catch(Exception e){
728
                logger.warn("oups");
729
            }
730

    
731
           derivedUnitFacade.addSource(sour);
732
          // sourceMap.put(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference(),sour);
733

    
734
//            if( state.getConfig().isInteractWithUser()){
735
//                List<OriginalSourceBase<?>>sources=null;
736
//                if(!state.isDerivedUnitSourcesSet()){
737
//                    sources= sui.askForSource(sourceMap, "the unit itself","",getReferenceService(), state.getDataHolder().getDocSources());
738
//                    state.setDerivedUnitSources(sources);
739
//                    state.setDerivedUnitSourcesSet(true);
740
//                }
741
//                else{
742
//                    sources=state.getDerivedUnitSources();
743
//                }
744
////                for (OriginalSourceBase<?> source:sources){
745
////                    if(source.isInstanceOf(IdentifiableSource.class)){
746
////                        if(sourceNotLinkedToElement(derivedUnitFacade,source)) {
747
////                            derivedUnitFacade.addSource((IdentifiableSource)source.clone());
748
////                        }
749
////                    }else{
750
////                        if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
751
////                            derivedUnitFacade.addSource(OriginalSourceType.Import,source.getCitation(),source.getCitationMicroReference(), ioName);
752
////                        }
753
////                    }
754
////                }
755
//            }else{
756
//                for (OriginalSourceBase<?> sr : sourceMap.values()){
757
//                    if(sr.isInstanceOf(IdentifiableSource.class)){
758
//                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
759
//                            derivedUnitFacade.addSource((IdentifiableSource)sr.clone());
760
//                        }
761
//                    }else{
762
//                        if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
763
//                            derivedUnitFacade.addSource(OriginalSourceType.Import,sr.getCitation(),sr.getCitationMicroReference(), ioName);
764
//                        }
765
//                    }
766
//                }
767
//            }
768

    
769
            save(state.getDerivedUnitBase(), state);
770

    
771
            if(logger.isDebugEnabled()) {
772
                logger.info("saved ABCD specimen ...");
773
            }
774

    
775
            // handle identifications
776
            handleIdentifications(state, derivedUnitFacade);
777

    
778
            //associatedUnits
779
            if (handleAssociatedUnits){
780
                importAssociatedUnits(state, item, derivedUnitFacade);
781
            }
782
            //siblings/ other children
783
            if (derivedUnitFacade.getType() != null && (derivedUnitFacade.getType().equals(SpecimenOrObservationType.LivingSpecimen) ||  derivedUnitFacade.getType().equals(SpecimenOrObservationType.TissueSample)  ||  derivedUnitFacade.getType().equals(SpecimenOrObservationType.OtherSpecimen)) &&state.getConfig().isGetSiblings()){
784
                getSiblings(state, item, derivedUnitFacade);
785
            }
786

    
787

    
788
        } catch (Exception e) {
789
            String message = "Error when reading record!";
790
            logger.warn(message);
791
            state.getReport().addException(message, e);
792
            e.printStackTrace();
793
            state.setUnsuccessfull();
794
        }
795

    
796
        return;
797
    }
798

    
799
    @Override
800
    protected void importAssociatedUnits(Abcd206ImportState state, Object itemObject, DerivedUnitFacade derivedUnitFacade) {
801

    
802
        Abcd206ImportConfigurator config = state.getConfig();
803
        //import associated units
804
        FieldUnit currentFieldUnit = derivedUnitFacade.innerFieldUnit();
805
        //TODO: push state (think of implementing stack architecture for state
806
        DerivedUnit currentUnit = state.getDerivedUnitBase();
807
        DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
808
        URI currentAccessPoint = state.getActualAccessPoint();
809
        String currentPrefix = state.getPrefix();
810
        Element item = null;
811
        if (itemObject instanceof Element){
812
            item = (Element)itemObject;
813
        }
814
        NodeList unitAssociationList = null;
815
        if (item != null){
816
            unitAssociationList = item.getElementsByTagName(currentPrefix+"UnitAssociation");
817
            for(int k=0;k<unitAssociationList.getLength();k++){
818
                if(unitAssociationList.item(k) instanceof Element){
819
                    Element unitAssociation = (Element)unitAssociationList.item(k);
820
                    UnitAssociationParser unitAssociationParser = new UnitAssociationParser(currentPrefix, state.getReport(), state.getCdmRepository());
821
                    UnitAssociationWrapper associationWrapper = unitAssociationParser.parse(unitAssociation);
822

    
823
                    state.setActualAccessPoint(associationWrapper.getAccesPoint());
824
                    if(associationWrapper!=null){
825
                        NodeList associatedUnits = associationWrapper.getAssociatedUnits();
826
                        if(associatedUnits!=null){
827
                            for(int m=0;m<associatedUnits.getLength();m++){
828
                                if(associatedUnits.item(m) instanceof Element){
829
                                    state.reset();
830
                                    state.setPrefix(associationWrapper.getPrefix());
831
                                    Abcd206ImportParser.setUnitPropertiesXML((Element) associatedUnits.item(m), new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix()), state);
832
                                    logger.debug("derived unit: " + state.getDerivedUnitBase().toString() + " associated unit: " +state.getDataHolder().getKindOfUnit() + ", " + state.getDataHolder().accessionNumber + ", " + state.getDataHolder().getRecordBasis() + ", " + state.getDataHolder().getUnitID());
833
                                    handleSingleUnit(state, associatedUnits.item(m), true);
834

    
835
                                    DerivedUnit associatedUnit = state.getDerivedUnitBase();
836
                                    FieldUnit associatedFieldUnit = null;
837
                                    java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository().getOccurrenceService().getFieldUnits(associatedUnit.getUuid(), null);
838
                                    //ignore field unit if associated unit has more than one
839
                                    if(associatedFieldUnits.size()>1){
840
                                        state.getReport().addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
841
                                    }
842
                                    else if(associatedFieldUnits.size()==1){
843
                                        associatedFieldUnit = associatedFieldUnits.iterator().next();
844
                                    }
845

    
846
                                    //attach current unit and associated unit depending on association type
847

    
848
                                    //parent-child relation:
849
                                    //copy derivation event and connect parent and sub derivative
850
//                                    if(associationWrapper.getAssociationType().contains("individual") || associationWrapper.getAssociationType().contains("culture") || associationWrapper.getAssociationType().contains("sample") || associationWrapper.getAssociationType().contains("same in situ")){
851
                                        if(currentDerivedFrom==null){
852
                                            state.getReport().addInfoMessage(String.format("No derivation event found for unit %s. Defaulting to ACCESSIONING event.",SpecimenImportUtility.getUnitID(currentUnit, config)));
853
                                            DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, DerivationEventType.ACCESSIONING());
854
                                        }
855
                                        else{
856
                                            DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, currentDerivedFrom.getType());
857
                                            updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
858
                                            updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
859
                                            updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
860
                                            updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
861

    
862
                                        }
863
                                        state.getReport().addDerivate(associatedUnit, currentUnit, config);
864
//                                    }
865
                                    //siblings relation
866
                                    //connect current unit to field unit of associated unit
867
//                                    else if(associationWrapper.getAssociationType().contains("population")|| associationWrapper.getAssociationType().contains("sample")){
868
//                                        //no associated field unit -> using current one
869
//                                        if(associatedFieldUnit==null){
870
//                                            if(currentFieldUnit!=null){
871
//                                                DerivationEvent.NewSimpleInstance(currentFieldUnit, associatedUnit, DerivationEventType.ACCESSIONING());
872
//                                            }
873
//                                        }
874
//                                        else{
875
//                                            if(currentDerivedFrom==null){
876
//                                                state.getReport().addInfoMessage("No derivation event found for unit "+SpecimenImportUtility.getUnitID(currentUnit, config)+". Defaulting to ACCESIONING event.");
877
//                                                DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, DerivationEventType.ACCESSIONING());
878
//                                            }
879
//                                            if(currentDerivedFrom!=null && associatedFieldUnit==currentFieldUnit){
880
//                                                DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, currentDerivedFrom.getType());
881
//                                                updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
882
//                                                updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
883
//                                                updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
884
//                                                updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
885
//                                            }
886
//                                        }
887
//                                    }
888

    
889
                                    //delete current field unit if replaced
890
                                    if(currentFieldUnit!=null && currentDerivedFrom!=null
891
                                            && currentFieldUnit.getDerivationEvents().size()==1  && currentFieldUnit.getDerivationEvents().contains(currentDerivedFrom) //making sure that the field unit
892
                                            && currentDerivedFrom.getDerivatives().size()==1 && currentDerivedFrom.getDerivatives().contains(currentUnit) //is not attached to other derived units
893
                                            && currentDerivedFrom!=currentUnit.getDerivedFrom() // <- derivation has been replaced and can be deleted
894
                                            ){
895
                                        currentFieldUnit.removeDerivationEvent(currentDerivedFrom);
896
                                        state.getCdmRepository().getOccurrenceService().delete(currentFieldUnit);
897
                                         }
898

    
899
                                    save(associatedUnit, state);
900
                                }
901
                            }
902
                        }
903
                    }
904
                }
905
            }
906
        }
907
        //TODO: pop state
908
        state.reset();
909
        state.setDerivedUnitBase(currentUnit);
910
        state.setActualAccessPoint(currentAccessPoint);
911
        state.setPrefix(currentPrefix);
912
    }
913

    
914

    
915

    
916

    
917
    /**
918
     * @param derivedUnitFacade
919
     * @param sour
920
     * @return
921
     */
922
    private boolean sourceNotLinkedToElement(DerivedUnitFacade derivedUnitFacade, OriginalSourceBase<?> source) {
923
        Set<IdentifiableSource> linkedSources = derivedUnitFacade.getSources();
924
        for (IdentifiableSource is:linkedSources){
925
            if (is.getCitation()!=null && source.getCitation()!=null &&
926
                    is.getCitation().getTitleCache().equalsIgnoreCase(source.getCitation().getTitleCache())){
927
                String isDetail =  is.getCitationMicroReference();
928
                if ((StringUtils.isBlank(isDetail) && StringUtils.isBlank(source.getCitationMicroReference()))
929
                        || (isDetail != null && isDetail.equalsIgnoreCase(source.getCitationMicroReference())) ) {
930
                    return false;
931
                }
932
            }
933
        }
934
        return true;
935
    }
936

    
937

    
938

    
939
    //    /**
940
    //     * @param reference
941
    //     * @param citationDetail
942
    //     * @return
943
    //     */
944
    //    private DescriptionElementSource getDescriptionSource(Reference reference, String citationDetail) {
945
    //
946
    //        List<OriginalSourceBase> issTmp2 = getCommonService().list(DescriptionElementSource.class, null, null, null, null);
947
    //
948
    //        try {
949
    //            for (OriginalSourceBase<?> osb:issTmp2){
950
    //                if (osb.getCitation().equals(reference) && osb.getCitationMicroReference().equalsIgnoreCase(citationDetail)) {
951
    //                    return (DescriptionElementSource) osb.clone();
952
    //                }
953
    //            }
954
    //        } catch (CloneNotSupportedException e) {
955
    //            // TODO Auto-generated catch block
956
    //            e.printStackTrace();
957
    //        }
958
    //
959
    //        DescriptionElementSource sour = DescriptionElementSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
960
    //        return sour;
961
    //    }
962

    
963

    
964

    
965

    
966
    /**
967
     * setCollectionData : store the collection object into the
968
     * derivedUnitFacade
969
     *
970
     * @param state
971
     */
972
    protected void setCollectionData(Abcd206ImportState state, DerivedUnitFacade derivedUnitFacade) {
973
        Abcd206ImportConfigurator config = state.getConfig();
974
        SpecimenImportUtility.setUnitID(derivedUnitFacade.innerDerivedUnit(), state.getDataHolder().getUnitID(), config);
975
        if(!config.isMapUnitIdToAccessionNumber()){
976
            derivedUnitFacade.setAccessionNumber(NB(state.getDataHolder().accessionNumber));
977
        }
978
        // derivedUnitFacade.setCollectorsNumber(NB(state.getDataHolder().collectorsNumber));
979

    
980
        /*
981
         * INSTITUTION & COLLECTION
982
         */
983
        // manage institution
984
        Institution institution = this.getInstitution(NB(state.getDataHolder().institutionCode), state);
985
        // manage collection
986
        Collection collection = this.getCollection(institution, NB(state.getDataHolder().collectionCode), state);
987
        // link specimen & collection
988
        derivedUnitFacade.setCollection(collection);
989
    }
990

    
991
    /**
992
     * getFacade : get the DerivedUnitFacade based on the recordBasis
993
     * @param state
994
     *
995
     * @return DerivedUnitFacade
996
     */
997
    @Override
998
    protected DerivedUnitFacade getFacade(Abcd206ImportState state) {
999
        if(logger.isDebugEnabled()) {
1000
            logger.info("getFacade()");
1001
        }
1002
        SpecimenOrObservationType type = null;
1003
        DefinedTerm kindOfUnit = null;
1004

    
1005
        // create specimen
1006
        if (NB((state.getDataHolder()).getRecordBasis()) != null) {
1007
            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
1008
                type = SpecimenOrObservationType.LivingSpecimen;
1009
            }
1010
            else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
1011
                type = SpecimenOrObservationType.PreservedSpecimen;
1012
            }
1013
            else if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
1014
                type = SpecimenOrObservationType.Observation;
1015
            }
1016
            else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
1017
                type = SpecimenOrObservationType.Fossil;
1018
            }else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("materialsample")>-1){
1019
                type = SpecimenOrObservationType.OtherSpecimen;
1020
            }
1021
            else if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("sample")>-1){
1022
                type = SpecimenOrObservationType.TissueSample;
1023
            }
1024

    
1025
            if (type == null) {
1026
                logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
1027
                type = SpecimenOrObservationType.DerivedUnit;
1028
            }
1029
        } else {
1030
            logger.info("The basis of record is null");
1031
            type = SpecimenOrObservationType.DerivedUnit;
1032
        }
1033

    
1034
        if (NB((state.getDataHolder()).getKindOfUnit()) != null) {
1035
            kindOfUnit =  getKindOfUnit(state, null, state.getDataHolder().getKindOfUnit().toLowerCase(), null, null, null);
1036
            if (kindOfUnit == null){
1037
                if (state.getDataHolder().getKindOfUnit().toLowerCase().indexOf("clone")>-1) {
1038
                    kindOfUnit = getKindOfUnit(state, null, "clone culture", "clone culture", "cc", null);
1039
                }
1040
                else if (state.getDataHolder().getKindOfUnit().toLowerCase().startsWith("live"))  {
1041
                    kindOfUnit = getKindOfUnit(state, null, "live sample", "live sample", "ls", null);
1042
                }
1043
                else if (state.getDataHolder().getKindOfUnit().toLowerCase().startsWith("microscopic slide"))  {
1044
                    kindOfUnit = getKindOfUnit(state, null, "microscopic slide", "microscopic slide", "ms", null);
1045
                }
1046

    
1047

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

    
1051
                }
1052
            }
1053
        } else {
1054
            logger.info("The kind of unit is null");
1055

    
1056
        }
1057
        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
1058
        derivedUnitFacade.setFieldUnit(state.getFieldUnit(state.getDataHolder().getFieldNumber()));
1059
        derivedUnitFacade.setKindOfUnit(kindOfUnit);
1060
        return derivedUnitFacade;
1061
    }
1062

    
1063
    private void getCollectorsFromXML(Element root, Abcd206XMLFieldGetter abcdFieldGetter, Abcd206ImportState state) {
1064
        NodeList group;
1065

    
1066
        group = root.getChildNodes();
1067
        for (int i = 0; i < group.getLength(); i++) {
1068
            if (group.item(i).getNodeName().equals(state.getPrefix() + "Identifications")) {
1069
                group = group.item(i).getChildNodes();
1070
                break;
1071
            }
1072
        }
1073
        //state.getDataHolder().gatheringAgents = "";
1074

    
1075
        abcdFieldGetter.getType(root);
1076
        abcdFieldGetter.getGatheringPeople(root);
1077
    }
1078

    
1079

    
1080

    
1081
    //    private void compareABCDtoCDM(URI urlFileName, List<String> knownElts, Abcd206XMLFieldGetter abcdFieldGetter) {
1082
    //        try {
1083
    //            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1084
    //            DocumentBuilder constructeur = factory.newDocumentBuilder();
1085
    //            URL url = urlFileName.toURL();
1086
    //            Object o = url.getContent();
1087
    //            InputStream is = (InputStream) o;
1088
    //            Document document = constructeur.parse(is);
1089
    //            Element root = document.getDocumentElement();
1090
    //            abcdFieldGetter.traverse(root);
1091
    //        }
1092
    //        catch (ParserConfigurationException e){
1093
    //            e.printStackTrace();
1094
    //        }
1095
    //        catch (SAXException e) {
1096
    //            e.printStackTrace();
1097
    //        }
1098
    //        catch (IOException e) {
1099
    //            e.printStackTrace();
1100
    //        }
1101
    //        Set<String> elts = state.getDataHolder().allABCDelements.keySet();
1102
    //        Iterator<String> it = elts.iterator();
1103
    //        String elt;
1104
    //        while (it.hasNext()) {
1105
    //            elt = it.next();
1106
    //            if (knownElts.indexOf(elt) == -1) {
1107
    //                if(DEBUG) {
1108
    //                    logger.info("Unmerged ABCD element: " + elt + " - "+ state.getDataHolder().allABCDelements.get(elt));
1109
    //                }
1110
    //            }
1111
    //        }
1112
    //    }
1113

    
1114
    /**
1115
     * Load the list of names from the ABCD file and save them
1116
     * @param state : the current ABCD import state
1117
     * @param unitsList : the unit list from the ABCD file
1118
     * @param abcdFieldGetter : the ABCD parser
1119
     */
1120
    private void prepareCollectors(Abcd206ImportState state, NodeList unitsList, Abcd206XMLFieldGetter abcdFieldGetter) {
1121

    
1122
        TeamOrPersonBase<?> teamOrPerson = null;
1123
        Team team = null;
1124
        //ImportHelper.setOriginalSource(teamOrPerson, state.getConfig().getSourceReference(), collector, "Collector");
1125
        for (int i = 0; i < unitsList.getLength(); i++) {
1126
            this.getCollectorsFromXML((Element) unitsList.item(i), abcdFieldGetter, state);
1127
            if (!(state.getDataHolder().gatheringAgentsList.isEmpty())){
1128
                if (state.getDataHolder().gatheringAgentsList.size() == 1){
1129
                    teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsList.get(0));
1130
                }else{
1131
                    team = new Team();
1132
                    for(String collector: state.getDataHolder().gatheringAgentsList){
1133
                        teamOrPerson = parseAuthorString(collector);
1134
                        if (teamOrPerson instanceof Person){
1135
                            team.addTeamMember((Person)teamOrPerson);
1136
                        }else{
1137
                            for (Person person: ((Team)teamOrPerson).getTeamMembers()){
1138
                                team.addTeamMember(person);
1139
                            }
1140
                        }
1141
                    }
1142
                }
1143
                if (!state.getPersonStore().containsId(state.getDataHolder().gatheringAgentsList.toString())) {
1144
                    state.getPersonStore().put(state.getDataHolder().gatheringAgentsList.toString(), teamOrPerson);
1145
                    if (logger.isDebugEnabled()) { logger.debug("Stored author " + state.getDataHolder().gatheringAgentsList.toString());}
1146
                    logger.warn("Not imported author with duplicated aut_id " + state.getDataHolder().gatheringAgentsList.toString() );
1147
                }
1148
            }
1149
            if (!StringUtils.isBlank(state.getDataHolder().gatheringAgentsText) && state.getDataHolder().gatheringAgentsList.isEmpty()){
1150
                teamOrPerson = parseAuthorString(state.getDataHolder().gatheringAgentsText);
1151
            }
1152

    
1153
        }
1154

    
1155

    
1156

    
1157

    
1158
//        List<String> collectorsU = new ArrayList<String>(new HashSet<String>(collectors));
1159
//        List<String> teamsU = new ArrayList<String>(new HashSet<String>(teams));
1160
//
1161
//
1162
//        //existing teams in DB
1163
//        Map<String,Team> titleCacheTeam = new HashMap<String, Team>();
1164
       // List<UuidAndTitleCache<Team>> hiberTeam = new ArrayList<UuidAndTitleCache<Team>>();//getAgentService().getTeamUuidAndTitleCache();
1165

    
1166
//        Set<UUID> uuids = new HashSet<UUID>();
1167
//        for (UuidAndTitleCache<Team> hibernateT:hiberTeam){
1168
//            uuids.add(hibernateT.getUuid());
1169
//        }
1170
//        if (!uuids.isEmpty()){
1171
//            List<AgentBase> existingTeams = getAgentService().find(uuids);
1172
//            for (AgentBase<?> existingP:existingTeams){
1173
//                titleCacheTeam.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Team.class));
1174
//            }
1175
//        }
1176

    
1177

    
1178
//        Map<String,UUID> teamMap = new HashMap<String, UUID>();
1179
//        for (UuidAndTitleCache<Team> uuidt:hiberTeam){
1180
//            teamMap.put(uuidt.getTitleCache(), uuidt.getUuid());
1181
//        }
1182

    
1183
        //existing persons in DB
1184
//        List<UuidAndTitleCache<Person>> hiberPersons = new ArrayList<UuidAndTitleCache<Person>>();//getAgentService().getPersonUuidAndTitleCache();
1185
//        Map<String,Person> titleCachePerson = new HashMap<String, Person>();
1186
//        uuids = new HashSet<UUID>();
1187
//        for (UuidAndTitleCache<Person> hibernateP:hiberPersons){
1188
//            uuids.add(hibernateP.getUuid());
1189
//        }
1190
//
1191
//        if (!uuids.isEmpty()){
1192
//            List<AgentBase> existingPersons = getAgentService().find(uuids);
1193
//            for (AgentBase<?> existingP:existingPersons){
1194
//                titleCachePerson.put(existingP.getTitleCache(),CdmBase.deproxy(existingP,Person.class));
1195
//            }
1196
//        }
1197
//
1198
//        Map<String,UUID> personMap = new HashMap<String, UUID>();
1199
//        for (UuidAndTitleCache<Person> person:hiberPersons){
1200
//            personMap.put(person.getTitleCache(), person.getUuid());
1201
//        }
1202
//
1203
//        java.util.Collection<Person> personToadd = new ArrayList<Person>();
1204
//        java.util.Collection<Team> teamToAdd = new ArrayList<Team>();
1205
//
1206
//        for (String collector:collectorsU){
1207
//            Person p = Person.NewInstance();
1208
//            p.setTitleCache(collector,true);
1209
//            if (!personMap.containsKey(p.getTitleCache())){
1210
//                personToadd.add(p);
1211
//            }
1212
//        }
1213
//        for (String team:teamsU){
1214
//            Team p = Team.NewInstance();
1215
//            p.setTitleCache(team,true);
1216
//            if (!teamMap.containsKey(p.getTitleCache())){
1217
//                teamToAdd.add(p);
1218
//            }
1219
//        }
1220
//
1221
//        if(!personToadd.isEmpty()){
1222
//            for (Person agent: personToadd){
1223
//                save(agent, state);
1224
//                titleCachePerson.put(agent.getTitleCache(),CdmBase.deproxy(agent, Person.class) );
1225
//            }
1226
//        }
1227
//
1228
//        Person ptmp ;
1229
//        Map <String,Integer>teamdone = new HashMap<String, Integer>();
1230
//        for (List<String> collteam: collectorinteams){
1231
//            if (!teamdone.containsKey(StringUtils.join(collteam.toArray(),"-"))){
1232
//                Team team = new Team();
1233
//                boolean em =true;
1234
//                for (String collector:collteam){
1235
//                    ptmp = Person.NewInstance();
1236
//                    ptmp.setTitleCache(collector,true);
1237
//                    Person p2 = titleCachePerson.get(ptmp.getTitleCache());
1238
//                    team.addTeamMember(p2);
1239
//                    em=false;
1240
//                }
1241
//                if (!em) {
1242
//                    teamToAdd.add(team);
1243
//                }
1244
//                teamdone.put(StringUtils.join(collteam.toArray(),"-"),0);
1245
//            }
1246
//        }
1247
//
1248
//        if(!teamToAdd.isEmpty()){
1249
//            for (Team agent: teamToAdd){
1250
//                save(agent, state);
1251
//                titleCacheTeam.put(agent.getTitleCache(), CdmBase.deproxy( agent,Team.class) );
1252
//            }
1253
//        }
1254

    
1255
//        ((Abcd206ImportConfigurator) state.getConfig()).setTeams(titleCacheTeam);
1256
//        ((Abcd206ImportConfigurator) state.getConfig()).setPersons(titleCachePerson);
1257
    }
1258

    
1259

    
1260
    @Override
1261
    protected boolean doCheck(Abcd206ImportState state) {
1262
        logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
1263
        return true;
1264
    }
1265

    
1266
    @Override
1267
    protected boolean isIgnore(Abcd206ImportState state) {
1268
        return false;
1269
    }
1270

    
1271

    
1272
    @Override
1273
    protected DefinedTerm getKindOfUnit(Abcd206ImportState state, UUID uuid, String label, String description, String labelAbbrev, TermVocabulary<DefinedTerm> voc){
1274

    
1275
        DefinedTerm unit = null;
1276

    
1277
        if (uuid == null){
1278
            unit = this.kindOfUnitsMap.get(label.toLowerCase());
1279
        }else{
1280
            unit = state.getKindOfUnit(uuid);
1281

    
1282
        }
1283
        if (unit == null){
1284
            unit = (DefinedTerm)getTermService().find(uuid);
1285
            if (unit == null){
1286
                if (uuid == null){
1287
                    uuid = UUID.randomUUID();
1288
                }
1289
                unit = DefinedTerm.NewKindOfUnitInstance(description, label, labelAbbrev);
1290
                unit.setUuid(uuid);
1291
                if (voc == null){
1292
                    boolean isOrdered = false;
1293
                    voc = getVocabulary(TermType.KindOfUnit, uuidUserDefinedKindOfUnitVocabulary, "User defined vocabulary for kind-of-units", "User Defined Measurement kind-of-units", null, null, isOrdered, unit);
1294
                }
1295
                voc.addTerm(unit);
1296
                getTermService().save(unit);
1297
            }
1298
            state.putKindOfUnit(unit);
1299
            kindOfUnitsMap.put(unit.getLabel().toLowerCase(), unit);
1300
        }
1301
        return unit;
1302
    }
1303

    
1304

    
1305
}
(2-2/15)