Project

General

Profile

Revision b0d023b8

IDb0d023b84642f93e0a3ae9edd4a73ba03a4cc4a9
Parent be647596
Child 9eb8d3fa

Added by Katja Luther over 5 years ago

hamonization of specimen import

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifDataHolder.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.specimen.gbif.in;
11

  
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15

  
16
import org.apache.log4j.Logger;
17

  
18
import eu.etaxonomy.cdm.io.specimen.SpecimenDataHolder;
19
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206DataHolder;
20
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
21

  
22
/**
23
 * @author k.luther
24
 * @date 15.07.2016
25
 *
26
 */
27
public class GbifDataHolder extends SpecimenDataHolder{
28
    @SuppressWarnings("unused")
29
    private static final Logger logger = Logger.getLogger(Abcd206DataHolder.class);
30

  
31
    //per import
32
    protected List<SpecimenTypeDesignationStatus> statusList;
33
    protected List<String> knownGbifElements = new ArrayList<String>();
34
    protected HashMap<String,String> allGbifElements = new HashMap<String,String>();
35
    public List<String> gatheringAgentList;
36
    protected List<String> gatheringTeamList;
37

  
38
    /*per unit
39
    protected List<Identification> identificationList;
40
    private List<HashMap<String, String>> atomisedIdentificationList;
41
    protected Map<String, String> namedAreaList;
42
    protected List<String[]> referenceList;
43
    protected List<String> multimediaObjects;
44
    protected List<String> docSources;
45
    protected List<String> associatedUnitIds;
46

  
47
    private String nomenclatureCode;
48
    protected String institutionCode;
49
    protected String collectionCode;
50
    protected String unitID;
51
    protected String recordBasis;
52
    protected String kindOfUnit;
53
    protected String accessionNumber;
54
    protected String fieldNumber;
55
    protected Double longitude;
56
    protected Double latitude;
57
    protected String locality;
58
    protected String languageIso;
59
    protected String country;
60
    protected String isocountry;
61
    protected Integer depth;
62
    protected Integer altitude;
63
    protected String unitNotes;
64
    protected String gatheringNotes;
65
    protected String gatheringDateText;
66
    protected String gatheringElevationText;
67
    protected String gatheringElevation;
68
    protected String gatheringElevationMax;
69
    protected String gatheringElevationMin;
70
    protected String gatheringElevationUnit;
71
    protected String gatheringSpatialDatum;
72
    protected String gatheringCoordinateErrorMethod;
73
    */
74

  
75

  
76
    protected String key;
77
    protected String datasetKey;
78
    protected String publishingOrgKey;
79
    protected String publishingCountry;
80
    protected String protocol;
81
    protected String lastCrawled;
82
    protected String lastParsed;
83
    protected String extensions;
84
    protected String basisOfRecord;
85
    protected String individualCount;
86
    protected String taxonKey;
87
    protected String kingdomKey;
88
    protected String phylumKey;
89
    protected String classKey;
90
    protected String orderKey;
91
    protected String familyKey;
92
    protected String genusKey;
93
    protected String speciesKey;
94
    protected String scientificName;
95
    protected String kingdom;
96
    protected String phylum;
97
    protected String order;
98
    protected String family;
99
    protected String genus;
100
    protected String species;
101
    protected String genericName;
102
    protected String specificEpithet;
103
    protected String taxonRank;
104
    protected String dateIdentified;
105
    protected String decimalLongitude;
106
    protected String decimalLatitude;
107
    protected String elevation;
108
    protected String continent;
109
    protected String stateProvince;
110
    protected String year;
111
    protected String month;
112
    protected String day;
113
    protected String eventDate;
114
    protected String[] issues;
115
   protected String lastInterpreted;
116
   protected String identifiers;
117
   protected String[] facts;
118
   protected String[] relations;
119
   protected String geodeticDatum;
120
   protected String className;
121
   protected String countryCode;
122
   protected String country;
123
   protected String nomenclaturalStatus;
124
   protected String rightsHolder;
125
   protected String identifier;
126
   protected String recordNumber;
127
   protected String nomenclaturalCode;
128
   protected String county;
129
   protected String locality;
130
   protected String datasetName;
131
   protected String gbifID;
132
   protected String collectionCode;
133
   protected String occurrenceID;
134
   protected String type;
135
   protected String taxonID;
136
   protected String license;
137
   protected String catalogNumber;
138
   protected String recordedBy;
139
    protected String institutionCode;
140
    protected String ownerInstitutionCode;
141
    protected String bibliographicCitation;
142
    protected String identifiedBy;
143
    protected String collectionID;
144

  
145

  
146
    public void reset() {
147

  
148
        key = null;
149
        datasetKey = null;
150
        publishingOrgKey = null;
151
        publishingCountry = null;
152
        protocol = null;
153
        lastCrawled = null;
154
        lastParsed = null;
155
        extensions = null;
156
        basisOfRecord = null;
157
        individualCount = null;
158
        taxonKey = null;
159
        kingdomKey = null;
160
        phylumKey = null;
161
        classKey = null;
162
        orderKey = null;
163
        familyKey = null;
164
        genusKey = null;
165
        speciesKey = null;
166
        scientificName = null;
167
        kingdom = null;
168
        phylum = null;
169
        order = null;
170
        family = null;
171
        genus = null;
172
        species = null;
173
        genericName = null;
174
        specificEpithet = null;
175
        taxonRank = null;
176
        dateIdentified = null;
177
        decimalLongitude = null;
178
        decimalLatitude = null;
179
        elevation = null;
180
        continent = null;
181
        stateProvince = null;
182
        year = null;
183
        month = null;
184
        day = null;
185
        eventDate = null;
186
        issues = null;
187
       lastInterpreted = null;
188
       identifiers = null;
189
       facts =  null;
190
       relations = null;
191
       geodeticDatum = null;
192
       className = null;
193
       countryCode = null;
194
       country = null;
195
       nomenclaturalStatus = null;
196
       rightsHolder = null;
197
       identifier = null;
198
       recordNumber = null;
199
       nomenclaturalCode = null;
200
       county = null;
201
       locality = null;
202
       datasetName = null;
203
       gbifID = null;
204
       collectionCode = null;
205
       occurrenceID = null;
206
       type = null;
207
       taxonID = null;
208
       license = null;
209
       catalogNumber = null;
210
       recordedBy = null;
211
       institutionCode = null;
212
       ownerInstitutionCode = null;
213
       bibliographicCitation = null;
214
       identifiedBy = null;
215
       collectionID = null;
216
    }
217

  
218

  
219

  
220
    /**
221
     * @return the nomenclatureCode
222
     */
223
    public String getNomenclatureCode() {
224
        return nomenclaturalCode;
225
    }
226

  
227

  
228

  
229
    /**
230
     * @param nomenclatureCode the nomenclatureCode to set
231
     */
232
    public void setNomenclatureCode(String nomenclatureCode) {
233
        this.nomenclaturalCode = nomenclatureCode;
234
    }
235

  
236

  
237

  
238

  
239
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifImport.java
1
package eu.etaxonomy.cdm.io.specimen.gbif.in;
2
import java.io.IOException;
3
import java.net.URISyntaxException;
4
import java.util.Collection;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8

  
9
import org.apache.commons.lang.StringUtils;
10
import org.apache.http.client.ClientProtocolException;
11
import org.apache.log4j.Logger;
12

  
13
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
14
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
15
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper;
16
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
17
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18
import eu.etaxonomy.cdm.io.dwca.in.DwcaImportConfigurator;
19
import eu.etaxonomy.cdm.io.specimen.SpecimenImportBase;
20
import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportUtility;
21
import eu.etaxonomy.cdm.model.common.Language;
22
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
23
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
24
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
25
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
26
import eu.etaxonomy.cdm.model.taxon.Classification;
27

  
28
// $Id$
29
/**
30
 * Copyright (C) 2016 EDIT
31
 * European Distributed Institute of Taxonomy
32
 * http://www.e-taxonomy.eu
33
 *
34
 * The contents of this file are subject to the Mozilla Public License Version 1.1
35
 * See LICENSE.TXT at the top of this package for the full license terms.
36
 */
37

  
38
/**
39
 * @author k.luther
40
 * @date 15.07.2016
41
 *
42
 */
43
public class GbifImport extends SpecimenImportBase<DwcaImportConfigurator, GbifImportState> {
44
    /**
45
     *
46
     */
47
    private static final long serialVersionUID = 1L;
48
    private static final Logger logger = Logger.getLogger(GbifImport.class);
49

  
50
    @Override
51
    protected boolean doCheck(GbifImportState state) {
52
        logger.warn("Checking not yet implemented for " + this.getClass().getSimpleName());
53
        return true;
54
    }
55

  
56

  
57

  
58
    /* (non-Javadoc)
59
     * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
60
     */
61
    @Override
62
    protected boolean isIgnore(GbifImportState state) {
63
        // TODO Auto-generated method stub
64
        return false;
65
    }
66

  
67

  
68

  
69
    /* (non-Javadoc)
70
     * @see eu.etaxonomy.cdm.io.specimen.SpecimenImportBase#doInvoke(eu.etaxonomy.cdm.io.common.ImportStateBase)
71
     */
72
    @Override
73
    protected void doInvoke(GbifImportState state) {
74
        GbifImportState gbifImportState = state;
75
        GbifImportConfigurator config = state.getConfig();
76

  
77
            state.setTx(startTransaction());
78
            logger.info("INVOKE Specimen Import from ABCD2.06 XML ");
79
            Collection<GbifResponse> results = null;
80
            //init cd repository
81
            if(state.getCdmRepository()==null){
82
                state.setCdmRepository(this);
83
            }
84
            if (config.getOccurenceQuery() != null){
85
                 try {
86
                    results = new GbifQueryServiceWrapper().query(config.getOccurenceQuery());
87
                } catch (ClientProtocolException e) {
88
                    // TODO Auto-generated catch block
89
                    e.printStackTrace();
90
                } catch (IOException e) {
91
                    // TODO Auto-generated catch block
92
                    e.printStackTrace();
93
                } catch (URISyntaxException e) {
94
                    // TODO Auto-generated catch block
95
                    e.printStackTrace();
96
                }
97

  
98

  
99
            }
100

  
101
           if(state.getConfig().getClassificationUuid()!=null){
102
                //load classification from config if it exists
103
                state.setClassification(getClassificationService().load(state.getConfig().getClassificationUuid()));
104
            }
105
            if(state.getClassification()==null){//no existing classification was set in config
106
                List<Classification> classificationList = getClassificationService().list(Classification.class, null, null, null, null);
107
                //get classification via user interaction
108
                if (state.getConfig().isUseClassification() && state.getConfig().isInteractWithUser()){
109
                    Map<String,Classification> classMap = new HashMap<String, Classification>();
110
                    for (Classification tree : classificationList) {
111
                        if (! StringUtils.isBlank(tree.getTitleCache())) {
112
                            classMap.put(tree.getTitleCache(),tree);
113
                        }
114
                    }
115

  
116
                }
117
                // use default classification as the classification to import into
118
                if (state.getClassification() == null) {
119
                    String name = NB(state.getConfig().getClassificationName());
120
                    for (Classification classif : classificationList){
121
                        if (classif.getTitleCache() != null && classif.getTitleCache().equalsIgnoreCase(name)) {
122
                            state.setClassification(classif);
123
                        }
124
                    }
125
                    if (state.getClassification() == null){
126
                        state.setClassification(Classification.NewInstance(name, state.getRef(), Language.DEFAULT()));
127
                        //we do not need a default classification when creating an empty new one
128
                        state.setDefaultClassification(state.getClassification());
129

  
130
                    }
131

  
132
                }
133
            }
134
            String message = "nb units to insert: " + results.size();
135
            logger.info(message);
136
            state.getConfig().getProgressMonitor().beginTask("Importing ABCD file", results.size());
137
            updateProgress(state, message);
138

  
139
            state.setDataHolder(new GbifDataHolder());
140
            state.getDataHolder().reset();
141

  
142
            for (GbifResponse response:results) {
143
                if(state.getConfig().getProgressMonitor().isCanceled()){
144
                    break;
145
                }
146

  
147

  
148

  
149

  
150
                //this.setUnitPropertiesXML( item, abcdFieldGetter, state);
151
            //   updateProgress(state, "Importing data for unit "+state.getDataHolder().unitID+" ("+i+"/"+unitsList.getLength()+")");
152

  
153
                //import unit + field unit data
154
                this.handleSingleUnit(state, response);
155

  
156
            }
157

  
158

  
159
            commitTransaction(state.getTx());
160

  
161

  
162

  
163
    }
164

  
165
    @Override
166
    protected void importAssociatedUnits(GbifImportState state, Object item, DerivedUnitFacade derivedUnitFacade) {
167

  
168
        //import associated units
169
        FieldUnit currentFieldUnit = derivedUnitFacade.innerFieldUnit();
170
        //TODO: push state (think of implementing stack architecture for state
171
        DerivedUnit currentUnit = state.getDerivedUnitBase();
172
        DerivationEvent currentDerivedFrom = currentUnit.getDerivedFrom();
173
        String currentPrefix = state.getPrefix();
174
      /*  NodeList unitAssociationList = item.getElementsByTagName(currentPrefix+"UnitAssociation");
175
        for(int k=0;k<unitAssociationList.getLength();k++){
176
            if(unitAssociationList.item(k) instanceof Element){
177
                Element unitAssociation = (Element)unitAssociationList.item(k);
178
                UnitAssociationParser unitAssociationParser = new UnitAssociationParser(currentPrefix, state.getReport(), state.getCdmRepository());
179
                UnitAssociationWrapper associationWrapper = unitAssociationParser.parse(unitAssociation);
180
                if(associationWrapper!=null){
181
                    NodeList associatedUnits = associationWrapper.getAssociatedUnits();
182
                    if(associatedUnits!=null){
183
                        for(int m=0;m<associatedUnits.getLength();m++){
184
                            if(associatedUnits.item(m) instanceof Element){
185
                                state.reset();
186
                                state.setPrefix(associationWrapper.getPrefix());
187
                                this.setUnitPropertiesXML((Element) associatedUnits.item(m), new Abcd206XMLFieldGetter(state.getDataHolder(), state.getPrefix()), state);
188
                                handleSingleUnit(state, associatedUnits.item(m));
189

  
190
                                DerivedUnit associatedUnit = state.getDerivedUnitBase();
191
                                FieldUnit associatedFieldUnit = null;
192
                                java.util.Collection<FieldUnit> associatedFieldUnits = state.getCdmRepository().getOccurrenceService().getFieldUnits(associatedUnit.getUuid());
193
                                //ignore field unit if associated unit has more than one
194
                                if(associatedFieldUnits.size()>1){
195
                                    state.getReport().addInfoMessage(String.format("%s has more than one field unit.", associatedUnit));
196
                                }
197
                                else if(associatedFieldUnits.size()==1){
198
                                    associatedFieldUnit = associatedFieldUnits.iterator().next();
199
                                }
200

  
201
                                //attach current unit and associated unit depending on association type
202

  
203
                                //parent-child relation:
204
                                //copy derivation event and connect parent and sub derivative
205
                                if(associationWrapper.getAssociationType().contains("individual")){
206
                                    if(currentDerivedFrom==null){
207
                                        state.getReport().addInfoMessage(String.format("No derivation event found for unit %s. Defaulting to ACCESSIONING event.",AbcdImportUtility.getUnitID(currentUnit, config)));
208
                                        DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, DerivationEventType.ACCESSIONING());
209
                                    }
210
                                    else{
211
                                        DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedUnit, currentUnit, currentDerivedFrom.getType());
212
                                        updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
213
                                        updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
214
                                        updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
215
                                        updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
216
                                    }
217
                                    state.getReport().addDerivate(associatedUnit, currentUnit, config);
218
                                }
219
                                //siblings relation
220
                                //connect current unit to field unit of associated unit
221
                                else if(associationWrapper.getAssociationType().contains("population")){
222
                                    //no associated field unit -> using current one
223
                                    if(associatedFieldUnit==null){
224
                                        if(currentFieldUnit!=null){
225
                                            DerivationEvent.NewSimpleInstance(currentFieldUnit, associatedUnit, DerivationEventType.ACCESSIONING());
226
                                        }
227
                                    }
228
                                    else{
229
                                        if(currentDerivedFrom==null){
230
                                            state.getReport().addInfoMessage("No derivation event found for unit "+AbcdImportUtility.getUnitID(currentUnit, config)+". Defaulting to ACCESIONING event.");
231
                                            DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, DerivationEventType.ACCESSIONING());
232
                                        }
233
                                        if(currentDerivedFrom!=null && associatedFieldUnit!=currentFieldUnit){
234
                                            DerivationEvent updatedDerivationEvent = DerivationEvent.NewSimpleInstance(associatedFieldUnit, currentUnit, currentDerivedFrom.getType());
235
                                            updatedDerivationEvent.setActor(currentDerivedFrom.getActor());
236
                                            updatedDerivationEvent.setDescription(currentDerivedFrom.getDescription());
237
                                            updatedDerivationEvent.setInstitution(currentDerivedFrom.getInstitution());
238
                                            updatedDerivationEvent.setTimeperiod(currentDerivedFrom.getTimeperiod());
239
                                        }
240
                                    }
241
                                }
242

  
243
                                //delete current field unit if replaced
244
                                if(currentFieldUnit!=null && currentDerivedFrom!=null
245
                                        && currentFieldUnit.getDerivationEvents().size()==1  && currentFieldUnit.getDerivationEvents().contains(currentDerivedFrom) //making sure that the field unit
246
                                        && currentDerivedFrom.getDerivatives().size()==1 && currentDerivedFrom.getDerivatives().contains(currentUnit) //is not attached to other derived units
247
                                        && currentDerivedFrom!=currentUnit.getDerivedFrom() // <- derivation has been replaced and can be deleted
248
                                        ){
249
                                    currentFieldUnit.removeDerivationEvent(currentDerivedFrom);
250
                                    state.getCdmRepository().getOccurrenceService().delete(currentFieldUnit);
251
                                }
252

  
253
                                save(associatedUnit, state);
254
                            }
255
                        }
256
                    }
257
                }
258
            }
259
        }*/
260
        //TODO: pop state
261
        state.reset();
262
        state.setDerivedUnitBase(currentUnit);
263
        state.setPrefix(currentPrefix);
264
    }
265
@Override
266
protected void handleSingleUnit(GbifImportState state, Object itemObject){
267
    GbifResponse item;
268
    if (itemObject instanceof GbifResponse){
269
        item = (GbifResponse) itemObject;
270
    } else{
271
        logger.error("For Gbif Import the item has to be of type GbifResponse.");
272
        return;
273
    }
274
    if (DEBUG) {
275
        logger.info("handleSingleUnit "+state.getRef());
276
    }
277
    try {
278
        ICdmApplicationConfiguration cdmAppController = state.getConfig().getCdmAppController();
279
        if(cdmAppController==null){
280
            cdmAppController = this;
281
        }
282
        //check if unit already exists
283
        DerivedUnitFacade derivedUnitFacade = null;
284
        if(state.getConfig().isIgnoreImportOfExistingSpecimens()){
285
            String[] tripleId = item.getTripleID();
286
            SpecimenOrObservationBase<?> existingSpecimen = findExistingSpecimen(tripleId[0], state);
287
            if(existingSpecimen!=null && existingSpecimen.isInstanceOf(DerivedUnit.class)){
288
                DerivedUnit derivedUnit = HibernateProxyHelper.deproxy(existingSpecimen, DerivedUnit.class);
289
                state.setDerivedUnitBase(derivedUnit);
290
                derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
291
                importAssociatedUnits(state, item, derivedUnitFacade);
292
                state.getReport().addAlreadyExistingSpecimen(SpecimenImportUtility.getUnitID(derivedUnit, state.getConfig()), derivedUnit);
293
                return;
294
            }
295
        }
296
        // TODO: implement overwrite/merge specimen
297
//      else if(state.getConfig().isOverwriteExistingSpecimens()){
298
//          Pager<SpecimenOrObservationBase> existingSpecimens = cdmAppController.getOccurrenceService().findByTitle(config);
299
//          if(!existingSpecimens.getRecords().isEmpty()){
300
//              derivedUnitFacade = DerivedUnitFacade.NewInstance(derivedUnit);
301
//              derivedUnitBase = derivedUnitFacade.innerDerivedUnit();
302
//              fieldUnit = derivedUnitFacade.getFieldUnit(true);
303
//          }
304
//      }
305
        //import new specimen
306

  
307
        // import DNA unit
308
        //TODO!!!!
309
//        if(state.getDataHolder().catalogNumber!=null && state.getDataHolder()..equalsIgnoreCase("dna")){
310
//            AbcdDnaParser dnaParser = new AbcdDnaParser(state.getPrefix(), state.getReport(), state.getCdmRepository());
311
//            DnaSample dnaSample = dnaParser.parse(item, state);
312
//            save(dnaSample, state);
313
//            //set dna as derived unit to avoid creating an extra specimen for this dna sample (instead just the field unit will be created)
314
//            state.setDerivedUnitBase(dnaSample);
315
//            derivedUnitFacade = DerivedUnitFacade.NewInstance(state.getDerivedUnitBase());
316
//        }
317
//        else{
318
//            // create facade
319
//            derivedUnitFacade = getFacade(state);
320
//            state.setDerivedUnitBase(derivedUnitFacade.innerDerivedUnit());
321
//        }
322

  
323
        /**
324
         * GATHERING EVENT
325
         */
326
        // gathering event
327
      /*  UnitsGatheringEvent unitsGatheringEvent = new UnitsGatheringEvent(cdmAppController.getTermService(),
328
                state.getDataHolder().locality, state.getDataHolder().languageIso, state.getDataHolder().longitude,
329
                state.getDataHolder().latitude, state.getDataHolder().gatheringElevationText,
330
                state.getDataHolder().gatheringElevationMin, state.getDataHolder().gatheringElevationMax,
331
                state.getDataHolder().gatheringElevationUnit, state.getDataHolder().gatheringDateText,
332
                state.getDataHolder().gatheringNotes, state.getTransformer().getReferenceSystemByKey(
333
                        state.getDataHolder().gatheringSpatialDatum), state.getDataHolder().gatheringAgentList,
334
                state.getDataHolder().gatheringTeamList, state.getConfig());
335

  
336
        // country
337
        UnitsGatheringArea unitsGatheringArea = new UnitsGatheringArea();
338
        //  unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(), getTermService());
339
        unitsGatheringArea.setParams(state.getDataHolder().isocountry, state.getDataHolder().country, state.getConfig(), cdmAppController.getTermService(), cdmAppController.getOccurrenceService());
340

  
341
        DefinedTermBase<?> areaCountry =  unitsGatheringArea.getCountry();
342

  
343
        // other areas
344
        unitsGatheringArea = new UnitsGatheringArea();
345
        //            unitsGatheringArea.setConfig(state.getConfig(),getOccurrenceService(),getTermService());
346
        unitsGatheringArea.setAreas(state.getDataHolder().namedAreaList,state.getConfig(), cdmAppController.getTermService(), cdmAppController.getVocabularyService());
347
        ArrayList<DefinedTermBase> nas = unitsGatheringArea.getAreas();
348
        for (DefinedTermBase namedArea : nas) {
349
            unitsGatheringEvent.addArea(namedArea);
350
        }
351

  
352
        // copy gathering event to facade
353
        GatheringEvent gatheringEvent = unitsGatheringEvent.getGatheringEvent();
354
        derivedUnitFacade.setLocality(gatheringEvent.getLocality());
355
        derivedUnitFacade.setExactLocation(gatheringEvent.getExactLocation());
356
        derivedUnitFacade.setCollector(gatheringEvent.getCollector());
357
        derivedUnitFacade.setCountry((NamedArea)areaCountry);
358
        derivedUnitFacade.setAbsoluteElevationText(gatheringEvent.getAbsoluteElevationText());
359
        derivedUnitFacade.setAbsoluteElevation(gatheringEvent.getAbsoluteElevation());
360
        derivedUnitFacade.setAbsoluteElevationMax(gatheringEvent.getAbsoluteElevationMax());
361
        derivedUnitFacade.setGatheringPeriod(gatheringEvent.getTimeperiod());
362

  
363
        for(DefinedTermBase<?> area:unitsGatheringArea.getAreas()){
364
            derivedUnitFacade.addCollectingArea((NamedArea) area);
365
        }
366
        //            derivedUnitFacade.addCollectingAreas(unitsGatheringArea.getAreas());
367
        // TODO exsiccatum
368

  
369
        // add fieldNumber
370
        derivedUnitFacade.setFieldNumber(NB(state.getDataHolder().fieldNumber));
371

  
372
        // add unitNotes
373
        derivedUnitFacade.addAnnotation(Annotation.NewDefaultLanguageInstance(NB(state.getDataHolder().unitNotes)));
374

  
375
        // //add Multimedia URLs
376
        if (state.getDataHolder().multimediaObjects.size() != -1) {
377
            for (String multimediaObject : state.getDataHolder().multimediaObjects) {
378
                Media media;
379
                try {
380
                    media = getImageMedia(multimediaObject, READ_MEDIA_DATA);
381
                    derivedUnitFacade.addDerivedUnitMedia(media);
382
                    if(state.getConfig().isAddMediaAsMediaSpecimen()){
383
                        //add media also as specimen scan
384
                        MediaSpecimen mediaSpecimen = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
385
                        mediaSpecimen.setMediaSpecimen(media);
386
                        DefinedTermBase specimenScanTerm = getTermService().load(SPECIMEN_SCAN_TERM);
387
                        if(specimenScanTerm instanceof DefinedTerm){
388
                            mediaSpecimen.setKindOfUnit((DefinedTerm) specimenScanTerm);
389
                        }
390
                        DerivationEvent derivationEvent = DerivationEvent.NewInstance(DerivationEventType.PREPARATION());
391
                        derivationEvent.addDerivative(mediaSpecimen);
392
                        derivedUnitFacade.innerDerivedUnit().addDerivationEvent(derivationEvent);
393
                    }
394

  
395
                } catch (MalformedURLException e) {
396
                    // TODO Auto-generated catch block
397
                    e.printStackTrace();
398
                }
399

  
400
            }
401
        }
402

  
403
        //          /*
404
        //           * merge AND STORE DATA
405
        //           */
406
        //          getTermService().saveOrUpdate(areaCountry);// TODO save area sooner
407
        //
408
        //          for (NamedArea area : otherAreas) {
409
        //              getTermService().saveOrUpdate(area);// merge it sooner (foreach area)
410
        //          }
411
/*
412
        save(unitsGatheringEvent.getLocality(), state);
413

  
414
        // handle collection data
415
        setCollectionData(state, derivedUnitFacade);
416

  
417
        //Reference stuff
418
        SpecimenUserInteraction sui = state.getConfig().getSpecimenUserInteraction();
419
        Map<String,OriginalSourceBase<?>> sourceMap = new HashMap<String, OriginalSourceBase<?>>();
420

  
421
        state.getDataHolder().docSources = new ArrayList<String>();
422
        for (String[] fullReference : state.getDataHolder().referenceList) {
423
            String strReference=fullReference[0];
424
            String citationDetail = fullReference[1];
425
            String citationURL = fullReference[2];
426

  
427
            if (!citationURL.isEmpty()) {
428
                citationDetail+=", "+citationURL;
429
            }
430

  
431
            Reference reference;
432
            if(strReference.equals(state.getRef().getTitleCache())){
433
                reference = state.getRef();
434
            }
435
            else{
436
                reference = ReferenceFactory.newGeneric();
437
                reference.setTitle(strReference);
438
            }
439

  
440
            IdentifiableSource sour = getIdentifiableSource(reference,citationDetail);
441

  
442
            try{
443
                if (sour.getCitation() != null){
444
                    if(StringUtils.isNotBlank(sour.getCitationMicroReference())) {
445
                        state.getDataHolder().docSources.add(sour.getCitation().getTitleCache()+ "---"+sour.getCitationMicroReference());
446
                    } else {
447
                        state.getDataHolder().docSources.add(sour.getCitation().getTitleCache());
448
                    }
449
                }
450
            }catch(Exception e){
451
                logger.warn("oups");
452
            }
453
            reference.addSource(sour);
454
            save(reference, state);
455
        }
456
        List<IdentifiableSource> issTmp = new ArrayList<IdentifiableSource>();//getCommonService().list(IdentifiableSource.class, null, null, null, null);
457
        List<DescriptionElementSource> issTmp2 = new ArrayList<DescriptionElementSource>();//getCommonService().list(DescriptionElementSource.class, null, null, null, null);
458

  
459
        Set<OriginalSourceBase> osbSet = new HashSet<OriginalSourceBase>();
460
        if(issTmp2!=null) {
461
            osbSet.addAll(issTmp2);
462
        }
463
        if(issTmp!=null) {
464
            osbSet.addAll(issTmp);
465
        }
466

  
467
        addToSourceMap(sourceMap, osbSet);
468

  
469
        if( state.getConfig().isInteractWithUser()){
470
            List<OriginalSourceBase<?>>sources=null;
471
            if(!state.isDerivedUnitSourcesSet()){
472
                sources= sui.askForSource(sourceMap, "the unit itself","",getReferenceService(), state.getDataHolder().docSources);
473
                state.setDerivedUnitSources(sources);
474
                state.setDerivedUnitSourcesSet(true);
475
            }
476
            else{
477
                sources=state.getDerivedUnitSources();
478
            }
479
//          System.out.println("nb sources: "+sources.size());
480
//          System.out.println("derivedunitfacade : "+derivedUnitFacade.getTitleCache());
481
            for (OriginalSourceBase<?> sour:sources){
482
                if(sour.isInstanceOf(IdentifiableSource.class)){
483
                    if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
484
//                      System.out.println("add source to derivedunitfacade1 "+derivedUnitFacade.getTitleCache());
485
                        derivedUnitFacade.addSource((IdentifiableSource)sour.clone());
486
                    }
487
                }else{
488
                    if(sourceNotLinkedToElement(derivedUnitFacade,sour)) {
489
//                      System.out.println("add source to derivedunitfacade2 "+derivedUnitFacade.getTitleCache());
490
                        derivedUnitFacade.addSource(OriginalSourceType.Import,sour.getCitation(),sour.getCitationMicroReference(), ioName);
491
                    }
492
                }
493
            }
494
        }else{
495
            for (OriginalSourceBase<?> sr : sourceMap.values()){
496
                if(sr.isInstanceOf(IdentifiableSource.class)){
497
                    if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
498
//                      System.out.println("add source to derivedunitfacade3 "+derivedUnitFacade.getTitleCache());
499
                        derivedUnitFacade.addSource((IdentifiableSource)sr.clone());
500
                    }
501
                }else{
502
                    if(sourceNotLinkedToElement(derivedUnitFacade,sr)) {
503
//                      System.out.println("add source to derivedunitfacade4 "+derivedUnitFacade.getTitleCache());
504
                        derivedUnitFacade.addSource(OriginalSourceType.Import,sr.getCitation(),sr.getCitationMicroReference(), ioName);
505
                    }
506
                }
507
            }
508
        }
509

  
510
        save(state.getDerivedUnitBase(), state);
511

  
512
        if(DEBUG) {
513
            logger.info("saved ABCD specimen ...");
514
        }
515

  
516
        // handle identifications
517
        handleIdentifications(state, derivedUnitFacade);
518

  
519
        //associatedUnits
520
        importAssociatedUnits(state, item, derivedUnitFacade);
521

  
522
*/
523

  
524
    } catch (Exception e) {
525
        String message = "Error when reading record!";
526
        logger.warn(message);
527
        state.getReport().addException(message, e);
528
        e.printStackTrace();
529
        state.setUnsuccessfull();
530
    }
531

  
532
    return;
533
}
534

  
535

  
536

  
537

  
538

  
539

  
540

  
541

  
542

  
543

  
544
    /*
545
     * "key": 1257570425,
546
"datasetKey": "7bd65a7a-f762-11e1-a439-00145eb45e9a",
547
"publishingOrgKey": "90fd6680-349f-11d8-aa2d-b8a03c50a862",
548
"publishingCountry": "US",
549
"protocol": "DWC_ARCHIVE",
550
"lastCrawled": "2016-06-06T11:11:35.800+0000",
551
"lastParsed": "2016-03-21T14:11:42.224+0000",
552
"extensions": { },
553
"basisOfRecord": "PRESERVED_SPECIMEN",
554
"individualCount": 1,
555
"taxonKey": 5338762,
556
"kingdomKey": 6,
557
"phylumKey": 7707728,
558
"classKey": 220,
559
"orderKey": 412,
560
"familyKey": 8798,
561
"genusKey": 2907867,
562
"speciesKey": 5338762,
563
"scientificName": "Mitchella repens L.",
564
"kingdom": "Plantae",
565
"phylum": "Tracheophyta",
566
"order": "Gentianales",
567
"family": "Rubiaceae",
568
"genus": "Mitchella",
569
"species": "Mitchella repens",
570
"genericName": "Mitchella",
571
"specificEpithet": "repens",
572
"taxonRank": "SPECIES",
573
"dateIdentified": "2005-12-31T23:00:00.000+0000",
574
"decimalLongitude": -98.70693,
575
"decimalLatitude": 20.77805,
576
"elevation": 1524.0,
577
"continent": "NORTH_AMERICA",
578
"stateProvince": "Hidalgo",
579
"year": 2006,
580
"month": 6,
581
"day": 11,
582
"eventDate": "2006-06-10T22:00:00.000+0000",
583
"issues": [
584

  
585
    "COORDINATE_ROUNDED",
586
    "GEODETIC_DATUM_ASSUMED_WGS84"
587

  
588
],
589
"lastInterpreted": "2016-04-17T13:34:52.325+0000",
590
"identifiers": [ ],
591
"facts": [ ],
592
"relations": [ ],
593
"geodeticDatum": "WGS84",
594
"class": "Magnoliopsida",
595
"countryCode": "MX",
596
"country": "Mexico",
597
"nomenclaturalStatus": "No opinion",
598
"rightsHolder": "Missouri Botanical Garden",
599
"identifier": "urn:catalog:MO:Tropicos:100217973",
600
"recordNumber": "Oberle 274",
601
"nomenclaturalCode": "ICNafp",
602
"county": "Metztitlán",
603
"locality": "Along trail downslope of road between Molango and Zacualtipan.",
604
"datasetName": "Tropicos",
605
"gbifID": "1257570425",
606
"collectionCode": "MO",
607
"occurrenceID": "urn:catalog:MO:Tropicos:100217973",
608
"type": "PhysicalObject",
609
"taxonID": "27902971",
610
"license": "http://creativecommons.org/licenses/by/4.0/legalcode",
611
"catalogNumber": "100217973",
612
"recordedBy": "Brad Oberle",
613
"institutionCode": "MO",
614
"ownerInstitutionCode": "MOBOT",
615
"bibliographicCitation": "http://www.tropicos.org/Specimen/100217973",
616
"identifiedBy": "B. Oberle",
617
"collectionID": "http://biocol.org/urn:lsid:biocol.org:col:15859
618
     *
619
     */
620

  
621

  
622

  
623
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifImportConfigurator.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.specimen.gbif.in;
11

  
12
import eu.etaxonomy.cdm.io.common.ImportStateBase;
13
import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
14
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
15
import eu.etaxonomy.cdm.model.reference.Reference;
16

  
17
/**
18
 * @author k.luther
19
 * @date 15.07.2016
20
 *
21
 */
22
public class GbifImportConfigurator<GbifImportState> extends SpecimenImportConfiguratorBase{
23

  
24
    /**
25
     * @param transformer
26
     */
27
    public GbifImportConfigurator(IInputTransformer transformer) {
28
        super(transformer);
29
        // TODO Auto-generated constructor stub
30
    }
31

  
32
    /**
33
     *
34
     */
35
    private static final long serialVersionUID = 1L;
36

  
37

  
38
    /* (non-Javadoc)
39
     * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#makeIoClassList()
40
     */
41
    @Override
42
    protected void makeIoClassList() {
43
        System.out.println("makeIOClassList");
44
        ioClassList = new Class[]{
45
               GbifImport.class,
46
        };
47

  
48
    }
49

  
50
    /* (non-Javadoc)
51
     * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSourceReference()
52
     */
53
    @Override
54
    public Reference getSourceReference() {
55
        // TODO Auto-generated method stub
56
        return null;
57
    }
58

  
59
    /* (non-Javadoc)
60
     * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getNewState()
61
     */
62
    @Override
63
    public <STATE extends ImportStateBase> STATE getNewState() {
64
        // TODO Auto-generated method stub
65
        return null;
66
    }
67

  
68
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifImportReport.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.specimen.gbif.in;
11

  
12
import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
13

  
14
/**
15
 * @author k.luther
16
 * @date 15.07.2016
17
 *
18
 */
19
public class GbifImportReport extends SpecimenImportReport{
20

  
21
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/gbif/in/GbifImportState.java
1
// $Id$
2
/**
3
* Copyright (C) 2016 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.io.specimen.gbif.in;
11

  
12
import java.util.ArrayList;
13
import java.util.List;
14

  
15
import org.springframework.transaction.TransactionStatus;
16

  
17
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
18
import eu.etaxonomy.cdm.io.specimen.SpecimenImportStateBase;
19
import eu.etaxonomy.cdm.io.specimen.abcd206.in.AbcdTransformer;
20
import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
21
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
22
import eu.etaxonomy.cdm.model.description.TaxonDescription;
23
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
24
import eu.etaxonomy.cdm.model.reference.Reference;
25
import eu.etaxonomy.cdm.model.taxon.Classification;
26

  
27
/**
28
 * @author k.luther
29
 * @date 15.07.2016
30
 *
31
 */
32
public class GbifImportState extends SpecimenImportStateBase<GbifImportConfigurator, GbifImportState> {
33

  
34
    private TransactionStatus tx;
35

  
36

  
37

  
38

  
39
    private ICdmApplicationConfiguration cdmRepository;
40

  
41
    private String prefix;
42

  
43
    private GbifImportReport report;
44

  
45
    private Classification classification = null;
46
    private Classification defaultClassification = null;
47
    private Reference ref = null;
48

  
49
    private GbifDataHolder dataHolder;
50
    private DerivedUnit derivedUnitBase;
51

  
52
    private List<OriginalSourceBase<?>> associationRefs = new ArrayList<OriginalSourceBase<?>>();
53
    private boolean associationSourcesSet=false;
54
    private List<OriginalSourceBase<?>> descriptionRefs = new ArrayList<OriginalSourceBase<?>>();
55
    private boolean descriptionSourcesSet=false;
56
    private List<OriginalSourceBase<?>> derivedUnitSources = new ArrayList<OriginalSourceBase<?>>();
57
    private boolean derivedUnitSourcesSet=false;
58
    private boolean descriptionGroupSet = false;
59
    private TaxonDescription descriptionGroup = null;
60

  
61

  
62
    /**
63
     * @param config
64
     */
65
    protected GbifImportState(GbifImportConfigurator config) {
66
        super(config);
67
        report = new GbifImportReport();
68
        setTransformer(new AbcdTransformer());
69
    }
70

  
71
    /* ------Getter/Setter -----*/
72

  
73
    @Override
74
    public TransactionStatus getTx() {
75
        return tx;
76
    }
77

  
78

  
79
    @Override
80
    public void setTx(TransactionStatus tx) {
81
        this.tx = tx;
82
    }
83

  
84

  
85
    @Override
86
    public ICdmApplicationConfiguration getCdmRepository() {
87
        return cdmRepository;
88
    }
89

  
90

  
91
    @Override
92
    public void setCdmRepository(ICdmApplicationConfiguration cdmRepository) {
93
        this.cdmRepository = cdmRepository;
94
    }
95

  
96

  
97
    public String getPrefix() {
98
        return prefix;
99
    }
100

  
101

  
102
    public void setPrefix(String prefix) {
103
        this.prefix = prefix;
104
    }
105

  
106

  
107
    @Override
108
    public SpecimenImportReport getReport() {
109
        return report;
110
    }
111

  
112

  
113
    public void setReport(GbifImportReport report) {
114
        this.report = report;
115
    }
116

  
117

  
118
    @Override
119
    public Classification getClassification() {
120
        return classification;
121
    }
122

  
123

  
124
    @Override
125
    public void setClassification(Classification classification) {
126
        this.classification = classification;
127
    }
128

  
129

  
130
    @Override
131
    public Classification getDefaultClassification() {
132
        return defaultClassification;
133
    }
134

  
135

  
136
    @Override
137
    public void setDefaultClassification(Classification defaultClassification) {
138
        this.defaultClassification = defaultClassification;
139
    }
140

  
141

  
142
    @Override
143
    public Reference getRef() {
144
        return ref;
145
    }
146

  
147

  
148
    @Override
149
    public void setRef(Reference ref) {
150
        this.ref = ref;
151
    }
152

  
153

  
154
    @Override
155
    public GbifDataHolder getDataHolder() {
156
        return dataHolder;
157
    }
158

  
159

  
160
    public void setDataHolder(GbifDataHolder dataHolder) {
161
        this.dataHolder = dataHolder;
162
    }
163

  
164

  
165
    @Override
166
    public DerivedUnit getDerivedUnitBase() {
167
        return derivedUnitBase;
168
    }
169

  
170

  
171
    @Override
172
    public void setDerivedUnitBase(DerivedUnit derivedUnitBase) {
173
        this.derivedUnitBase = derivedUnitBase;
174
    }
175

  
176

  
177
    public List<OriginalSourceBase<?>> getAssociationRefs() {
178
        return associationRefs;
179
    }
180

  
181

  
182
    public void setAssociationRefs(List<OriginalSourceBase<?>> associationRefs) {
183
        this.associationRefs = associationRefs;
184
    }
185

  
186

  
187
    public boolean isAssociationSourcesSet() {
188
        return associationSourcesSet;
189
    }
190

  
191

  
192
    public void setAssociationSourcesSet(boolean associationSourcesSet) {
193
        this.associationSourcesSet = associationSourcesSet;
194
    }
195

  
196

  
197
    public List<OriginalSourceBase<?>> getDescriptionRefs() {
198
        return descriptionRefs;
199
    }
200

  
201

  
202
    public void setDescriptionRefs(List<OriginalSourceBase<?>> descriptionRefs) {
203
        this.descriptionRefs = descriptionRefs;
204
    }
205

  
206

  
207
    public boolean isDescriptionSourcesSet() {
208
        return descriptionSourcesSet;
209
    }
210

  
211

  
212
    public void setDescriptionSourcesSet(boolean descriptionSourcesSet) {
213
        this.descriptionSourcesSet = descriptionSourcesSet;
214
    }
215

  
216

  
217
    public List<OriginalSourceBase<?>> getDerivedUnitSources() {
218
        return derivedUnitSources;
219
    }
220

  
221

  
222
    public void setDerivedUnitSources(List<OriginalSourceBase<?>> derivedUnitSources) {
223
        this.derivedUnitSources = derivedUnitSources;
224
    }
225

  
226

  
227
    public boolean isDerivedUnitSourcesSet() {
228
        return derivedUnitSourcesSet;
229
    }
230

  
231

  
232
    public void setDerivedUnitSourcesSet(boolean derivedUnitSourcesSet) {
233
        this.derivedUnitSourcesSet = derivedUnitSourcesSet;
234
    }
235

  
236

  
237
    public boolean isDescriptionGroupSet() {
238
        return descriptionGroupSet;
239
    }
240

  
241

  
242
    public void setDescriptionGroupSet(boolean descriptionGroupSet) {
243
        this.descriptionGroupSet = descriptionGroupSet;
244
    }
245

  
246

  
247
    @Override
248
    public TaxonDescription getDescriptionGroup() {
249
        return descriptionGroup;
250
    }
251

  
252

  
253
    @Override
254
    public void setDescriptionGroup(TaxonDescription descriptionGroup) {
255
        this.descriptionGroup = descriptionGroup;
256
    }
257

  
258
    /* (non-Javadoc)
259
     * @see eu.etaxonomy.cdm.io.specimen.SpecimenImportStateBase#reset()
260
     */
261
    @Override
262
    public void reset() {
263
        // TODO Auto-generated method stub
264

  
265
    }
266

  
267
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)