Project

General

Profile

Revision d3c53a58

IDd3c53a5845ad832d414ec12958098ed75844dae8
Parent 7f3f7703
Child 80d86f0e

Added by Katja Luther almost 3 years ago

specimen import changes

View differences:

cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/occurrence/OccurenceQuery.java
9 9
*/
10 10
package eu.etaxonomy.cdm.ext.occurrence;
11 11

  
12
import java.io.Serializable;
12 13
import java.text.SimpleDateFormat;
13 14
import java.util.Calendar;
14 15
import java.util.Set;
......
19 20
 * @date 17.09.2013
20 21
 *
21 22
 */
22
public class OccurenceQuery {
23
public class OccurenceQuery implements Serializable{
23 24
    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
24 25

  
25 26
    public final Set<String[]> tripleIds;
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/occurrence/bioCase/BioCaseQueryGenerator.java
92 92
    private static final String COLLECTOR_NUMBER_PATH_ABCD_2_0 = UNIT_PATH + "/CollectorsFieldNumber";
93 93
    private static final String COLLECTOR_PATH_ABCD_2_0 = UNIT_PATH + "/Gathering/Agents/GatheringAgentsText";
94 94
    private static final String ACCESSION_NUMBER_PATH_ABCD_2_0 = UNIT_PATH + "/SpecimenUnit/Accessions/AccessionNumber";
95
    private static final String CAT_PATH_ABCD_2_0 = UNIT_PATH + "/CAT";
95 96

  
96 97
    /**
97 98
     * Generates an XML query according to the BioCASe protocol.
......
133 134
        elFilter.addContent(elAnd);
134 135

  
135 136
        if(query.tripleIds!=null ){
136
            Element elOr = new Element(OR);;
137

  
138
            for (String[] unitId: query.tripleIds){
139
                addEqualsFilter(elOr, unitId[0], UNIT_ID_PATH_ABCD_2_0);
140

  
137
            if (query.tripleIds.size() == 1){
138
                String unitId[] = query.tripleIds.iterator().next();
139
                addEqualsFilter(elAnd, unitId[0], UNIT_ID_PATH_ABCD_2_0);
140
            }else{
141
                Element elOr = new Element(OR);
142
                for (String[] unitId: query.tripleIds){
143
                    addEqualsFilter(elOr, unitId[0], UNIT_ID_PATH_ABCD_2_0);
144
                }
145
                elAnd.addContent(elOr);
141 146
            }
142

  
143
            elAnd.addContent(elOr);
144

  
145 147
        }
146 148
        if(query.accessionNumber!=null && !query.accessionNumber.trim().isEmpty()){
147 149
            addLikeFilter(elAnd, query.accessionNumber, ACCESSION_NUMBER_PATH_ABCD_2_0);
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/occurrence/gbif/GbifJsonOccurrenceParser.java
28 28

  
29 29
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
30 30
import eu.etaxonomy.cdm.common.UriUtils;
31
import eu.etaxonomy.cdm.model.agent.Institution;
31 32
import eu.etaxonomy.cdm.model.agent.Person;
33
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
32 34
import eu.etaxonomy.cdm.model.common.TimePeriod;
33 35
import eu.etaxonomy.cdm.model.location.Country;
34 36
import eu.etaxonomy.cdm.model.location.Point;
35 37
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
38
import eu.etaxonomy.cdm.model.name.BacterialName;
39
import eu.etaxonomy.cdm.model.name.BotanicalName;
40
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
41
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
36 42
import eu.etaxonomy.cdm.model.name.NonViralName;
37 43
import eu.etaxonomy.cdm.model.name.Rank;
44
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45
import eu.etaxonomy.cdm.model.name.ViralName;
46
import eu.etaxonomy.cdm.model.name.ZoologicalName;
47
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
38 48
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
39 49
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
40 50

  
......
140 150
    protected static final String IDENTIFIED_BY = "identifiedBy";
141 151
    protected static final String COLLECTION_ID = "collectionID";
142 152

  
153
    private static final String PLANTAE = "Plantae";
154

  
155
    private static final String ANIMALIA = "Animalia";
156

  
157
    private static final String FUNGI = "Fungi";
158

  
159
    private static final String BACTERIA = "Bacteria";
160

  
143 161

  
144 162

  
145 163

  
......
183 201
    private static Collection<GbifResponse> parseJsonRecords(JSONArray jsonArray) {
184 202
        Collection<GbifResponse> results = new ArrayList<GbifResponse>();
185 203
        String[] tripleId = new String[3];
204
        String string;
186 205
        for(Object o:jsonArray){
187 206
            //parse every record
207
            tripleId = new String[3];
188 208
            if(o instanceof JSONObject){
189 209
                String dataSetKey = null;
190 210
                GbifDataSetProtocol dataSetProtocol = null;
191 211
                DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
192
                NonViralName<NonViralName> name = null;
212
                TaxonNameBase name = null;
193 213
                JSONObject record = (JSONObject)o;
194 214

  
195 215
                if(record.has(DATASET_PROTOCOL)){
......
199 219
                    dataSetKey = record.getString(DATASET_KEY);
200 220
                }
201 221
                if(record.has(COUNTRY_CODE)){
202
                    String string = record.getString(COUNTRY_CODE);
222
                    string = record.getString(COUNTRY_CODE);
203 223
                    Country country = Country.getCountryByIso3166A2(string);
204 224
                    if(country!=null){
205 225
                        derivedUnitFacade.setCountry(country);
206 226
                    }
207 227
                }
208 228
                if(record.has(LOCALITY)){
209
                    String string = record.getString(LOCALITY);
229
                    string = record.getString(LOCALITY);
210 230
                    derivedUnitFacade.setLocality(string);
211 231
                }
212 232

  
213 233
                if (record.has("species")){
214 234
                    Rank rank = null;
215
                    String string;
235

  
216 236
                    if (record.has(TAXON_RANK)){
217 237
                        string= record.getString(TAXON_RANK);
218 238
                        try {
......
223 243
                        }
224 244
                    }
225 245
                    if (rank != null){
226
                        name = NonViralName.NewInstance(rank);
246
                        if (record.has(NOMENCLATURALCODE)){
247
                            string = record.getString(NOMENCLATURALCODE);
248

  
249
                            if (string.equals(NomenclaturalCode.ICZN.getTitleCache())){
250
                                name = ZoologicalName.NewInstance(rank);
251
                            } else if (string.equals(NomenclaturalCode.ICNAFP.getTitleCache())) {
252
                                name = BotanicalName.NewInstance(rank);
253
                            } else if (string.equals(NomenclaturalCode.ICNB.getTitleCache())){
254
                                name = BacterialName.NewInstance(rank);
255
                            } else if (string.equals(NomenclaturalCode.ICNCP.getTitleCache())){
256
                                name = CultivarPlantName.NewInstance(rank);
257
                            } else if (string.equals(NomenclaturalCode.ICVCN.getTitleCache())){
258
                                name = ViralName.NewInstance(rank);
259
                            } else {
260
                                name = NonViralName.NewInstance(rank);
261
                            }
262
                        }else {
263
                            if (record.has(KINGDOM)){
264
                                if (record.getString(KINGDOM).equals(PLANTAE)){
265
                                    name = BotanicalName.NewInstance(rank);
266
                                } else if (record.getString(KINGDOM).equals(ANIMALIA)){
267
                                    name = ZoologicalName.NewInstance(rank);
268
                                } else if (record.getString(KINGDOM).equals(FUNGI)){
269
                                    name = NonViralName.NewInstance(rank);
270
                                } else if (record.getString(KINGDOM).equals(BACTERIA)){
271
                                    name = BacterialName.NewInstance(rank);
272
                                } else{
273
                                    name = NonViralName.NewInstance(rank);
274
                                }
275
                            } else{
276
                                name = NonViralName.NewInstance(rank);
277
                            }
278
                        }
227 279
                        if (record.has(GENUS)){
228
                            name.setGenusOrUninomial(record.getString(GENUS));
280
                            ((NonViralName)name).setGenusOrUninomial(record.getString(GENUS));
229 281
                        }
230 282
                        if (record.has(SPECIFIC_EPITHET)){
231
                            name.setSpecificEpithet(record.getString(SPECIFIC_EPITHET));
283
                            ((NonViralName)name).setSpecificEpithet(record.getString(SPECIFIC_EPITHET));
232 284
                        }
233 285
                        if (record.has(INFRASPECIFIC_EPITHET)){
234
                            name.setSpecificEpithet(record.getString(INFRASPECIFIC_EPITHET));
286
                            ((NonViralName)name).setInfraSpecificEpithet(record.getString(INFRASPECIFIC_EPITHET));
287
                        }
288
                        if (record.has(SCIENTIFIC_NAME)){
289
                            name.setTitleCache(record.getString(SCIENTIFIC_NAME), true);
235 290
                        }
291

  
292
                    }
293
                    DeterminationEvent detEvent = DeterminationEvent.NewInstance();
294

  
295
                    if (record.has(IDENTIFIED_BY)){
296
                        Person determiner = Person.NewTitledInstance(record.getString(IDENTIFIED_BY));
297
                        detEvent.setDeterminer(determiner);
298

  
236 299
                    }
300
                    detEvent.setTaxonName(name);
301
                    detEvent.setPreferredFlag(true);
302
                    derivedUnitFacade.addDetermination(detEvent);
303

  
237 304
                }
238 305

  
239 306

  
......
273 340
                if(record.has(ELEVATION)){
274 341
                    try {
275 342
                        //parse integer and strip of unit
276
                        String string = record.getString(ELEVATION);
343
                        string = record.getString(ELEVATION);
277 344
                        int length = string.length();
278 345
                        StringBuilder builder = new StringBuilder();
279 346
                        for(int i=0;i<length;i++){
......
330 397
                    //FIXME: check data base for existing collections
331 398
                    eu.etaxonomy.cdm.model.occurrence.Collection collection = eu.etaxonomy.cdm.model.occurrence.Collection.NewInstance();
332 399
                    collection.setCode(collectionCode);
400
                    if(record.has(INSTITUTION_CODE)){
401
                        Institution institution = Institution.NewNamedInstance(record.getString(INSTITUTION_CODE));
402
                        institution.setCode(record.getString(INSTITUTION_CODE));
403
                        collection.setInstitute(institution);
404
                    }
333 405
                    derivedUnitFacade.setCollection(collection);
334 406
                }
335 407
                if(record.has(CATALOG_NUMBER)){
408
                    derivedUnitFacade.setCatalogNumber(record.getString(CATALOG_NUMBER));
336 409
                    derivedUnitFacade.setAccessionNumber(record.getString(CATALOG_NUMBER));
337 410
                    tripleId[0]= record.getString(CATALOG_NUMBER);
338 411
                }
......
341 414
                    tripleId[1]= record.getString(INSTITUTION_CODE);
342 415
                }
343 416

  
417
                if (record.has(OCCURENCE_ID)){
418
                    IdentifiableSource source = IdentifiableSource.NewDataImportInstance((record.getString(OCCURENCE_ID)));
419
                    derivedUnitFacade.addSource(source);
420
                }
421

  
344 422
                // create dataset URL
345 423
                URI uri = null;
346 424
                try {
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/occurrence/gbif/GbifQueryGenerator.java
34 34
    public List<NameValuePair> generateQueryParams(OccurenceQuery query){
35 35
        List<NameValuePair> queryParamsGET = new ArrayList<NameValuePair>();
36 36
        // only look for preserved specimens
37
        ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "basisOfRecord", "PRESERVED_SPECIMEN");
38
        ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "limit", "100");
39
        if (query.tripleIds != null){
40
            for (String[] tripleId:query.tripleIds){
41
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber",tripleId[0]);
42
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "InstitutionCode",tripleId[1]);
43
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CollectionCode", tripleId[2]);
44
            }
45
        }else{
46
            if((query.accessionNumber!=null && !query.accessionNumber.isEmpty()) || query.tripleIds != null){
47
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber", query.accessionNumber);
48 37

  
38
        if (checkForValidQuery(query)) {
39
            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "basisOfRecord", "PRESERVED_SPECIMEN");
40

  
41
            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "limit", "100");
42
            if (query.tripleIds != null){
43
                for (String[] tripleId:query.tripleIds){
44
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber",tripleId[0]);
45
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "InstitutionCode",tripleId[1]);
46
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CollectionCode", tripleId[2]);
47
                }
48
            }else{
49
                if((query.accessionNumber!=null && !query.accessionNumber.isEmpty()) ){
50
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber", query.accessionNumber);
51

  
52

  
53
                }
54
                if(query.collector!=null && !query.collector.isEmpty()){
55
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordedBy", query.collector);
56
                }
57
                if(query.collectorsNumber!=null && !query.collectorsNumber.isEmpty()){
58
                    // TODO refine parameter
59
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "fieldNumber", query.collectorsNumber);
60
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordNumber", query.collectorsNumber);
61
                }
62
                if(query.country!=null && !query.country.isEmpty()){
63
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "country", query.country);
64
                }
65
                /**
66
                       Date Range: January – June 1899
67
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&year=1899&month=1,6
68
                       Exact Date: june 20th 1901
69
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1901-6-20
70
                       Date range exact: Jan. 02.1899 to june 03. 1902
71
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1899-1-2,1902-6-3
72
                 */
73
                // FIXME: currently ONLY year is handled by the query (see GbifServiceWrapper)
74
                if(query.herbarium!=null && !query.herbarium.isEmpty()){
75
                    // TODO refine parameter
76
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "institutionCode", query.herbarium);
77
                }
78
                if(query.locality!=null && !query.locality.isEmpty()){
79
                    //TODO not yet available at GBIF
80
        //            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, param, query.locality);
81
                }
82
                if(query.taxonName!=null && !query.taxonName.isEmpty()){
83
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "scientificName", query.taxonName);
84
                }
49 85
            }
50
            if(query.collector!=null && !query.collector.isEmpty()){
51
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordedBy", query.collector);
52
            }
53
            if(query.collectorsNumber!=null && !query.collectorsNumber.isEmpty()){
54
                // TODO refine parameter
55
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "fieldNumber", query.collectorsNumber);
56
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordNumber", query.collectorsNumber);
57
            }
58
            if(query.country!=null && !query.country.isEmpty()){
59
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "country", query.country);
60
            }
61
            /**
62
                   Date Range: January – June 1899
63
                   http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&year=1899&month=1,6
64
                   Exact Date: june 20th 1901
65
                   http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1901-6-20
66
                   Date range exact: Jan. 02.1899 to june 03. 1902
67
                   http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1899-1-2,1902-6-3
68
             */
69
            // FIXME: currently ONLY year is handled by the query (see GbifServiceWrapper)
70
            if(query.herbarium!=null && !query.herbarium.isEmpty()){
71
                // TODO refine parameter
72
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "institutionCode", query.herbarium);
73
            }
74
            if(query.locality!=null && !query.locality.isEmpty()){
75
                //TODO not yet available at GBIF
76
    //            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, param, query.locality);
77
            }
78
            if(query.taxonName!=null && !query.taxonName.isEmpty()){
79
                ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "scientificName", query.taxonName);
80
            }
86
            return queryParamsGET;
81 87
        }
82
        return queryParamsGET;
88
        return null;
89
    }
90

  
91
    /**
92
     * @param query
93
     */
94
    private boolean checkForValidQuery(OccurenceQuery query) {
95
       boolean valid = false;
96
       if (query.tripleIds != null) {
97
           for (String[] tripleId:query.tripleIds){
98
               if (tripleId[0] != null || tripleId[1] != null || tripleId[2] != null){
99
                   valid = true;
100
               }
101
           }
102
       } else if((query.accessionNumber!=null && !query.accessionNumber.isEmpty()) ){
103
           valid = true;
104
       } else if (query.collector!=null && !query.collector.isEmpty()){
105
           valid = true;
106
       }else if(query.collectorsNumber!=null && !query.collectorsNumber.isEmpty()){
107
           valid = true;
108
       } else if (query.country!=null && !query.country.isEmpty()){
109
           valid = true;
110
       } else if(query.herbarium!=null && !query.herbarium.isEmpty()){
111
           valid = true;
112
       } else if (query.taxonName!=null && !query.taxonName.isEmpty()){
113
          valid = true;
114
       } else if (query.locality!=null && !query.locality.isEmpty()){
115
           //TODO not yet available at GBIF
116
       }
117

  
118
       return valid;
83 119
    }
84 120

  
121

  
85 122
}
cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/occurrence/gbif/GbifQueryServiceWrapper.java
61 61
            //TODO date is not supported by GBIF
62 62
        }
63 63
        List<NameValuePair> queryParamsGET = new GbifQueryGenerator().generateQueryParams(query);
64
        URI uri = createUri(SUB_PATH, queryParamsGET);
64
        if (queryParamsGET != null){
65
            URI uri = createUri(SUB_PATH, queryParamsGET);
66
            URIBuilder builder = new URIBuilder(uri.toString()+yearUri);
65 67

  
66
        URIBuilder builder = new URIBuilder(uri.toString()+yearUri);
67

  
68
        if(UriUtils.isServiceAvailable(uri, 10000)){
69
            logger.info("Querying GBIF service with " + builder.build());
70
            return GbifJsonOccurrenceParser.parseJsonRecords(executeHttpGet(builder.build(), null));
71
        }
72
        else{
73
            logger.error("Querying " + uri + " got a timeout!");
68
            if(UriUtils.isServiceAvailable(uri, 10000)){
69
                logger.info("Querying GBIF service with " + builder.build());
70
                return GbifJsonOccurrenceParser.parseJsonRecords(executeHttpGet(builder.build(), null));
71
            }
72
            else{
73
                logger.error("Querying " + uri + " got a timeout!");
74
                return null;
75
            }
76
        } else{
77
            logger.info("Querying GBIF service was skipped because of missing get parameters.");
74 78
            return null;
75 79
        }
76 80
    }
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/service/IIOService.java
9 9
*/
10 10
package eu.etaxonomy.cdm.io.service;
11 11

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

  
14 15
import eu.etaxonomy.cdm.io.common.ExportResult;
......
16 17
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
17 18
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
18 19
import eu.etaxonomy.cdm.io.common.ImportResult;
20
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
21
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
19 22

  
20 23
/**
21 24
 * @author cmathew
......
58 61
    public ImportResult importDataFromInputStream(IImportConfigurator configurator, byte[] importData);
59 62

  
60 63

  
64
    /**
65
     * @param configurator
66
     * @return
67
     */
68
    ImportResult importDataFromStream(SpecimenImportConfiguratorBase configurator);
69

  
70

  
71
    /**
72
     * @param configurators
73
     * @return
74
     */
75
    ImportResult importDataFromStream(List<Abcd206ImportConfigurator> configurators);
76

  
61 77
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/service/IOServiceImpl.java
15 15
import java.io.Serializable;
16 16
import java.nio.file.Files;
17 17
import java.nio.file.Path;
18
import java.util.List;
18 19
import java.util.UUID;
19 20

  
20 21
import org.springframework.beans.factory.annotation.Autowired;
......
25 26
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
26 27
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
27 28
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
29
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
28 30
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
29 31
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
30 32
import eu.etaxonomy.cdm.io.common.ExportResult;
......
34 36
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
35 37
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
36 38
import eu.etaxonomy.cdm.io.common.ImportResult;
39
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
40
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
37 41

  
38 42
/**
39 43
 * @author cmathew
......
141 145
        return result;
142 146
    }
143 147

  
148

  
149
    @Override
150
    public ImportResult importDataFromStream(SpecimenImportConfiguratorBase configurator) {
151
        ImportResult result = new ImportResult();
152

  
153
            OccurenceQuery query;
154
            result = cdmImport.execute(configurator);
155
            return result;
156
    }
157

  
158
    @Override
159
    public ImportResult importDataFromStream(List<Abcd206ImportConfigurator> configurators) {
160
        ImportResult result = new ImportResult();
161

  
162
            OccurenceQuery query;
163
            for (SpecimenImportConfiguratorBase configurator:configurators){
164
                result = cdmImport.execute(configurator);
165
            }
166
            return result;
167
    }
168

  
169

  
170

  
171

  
172

  
144 173
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/SpecimenDataHolder.java
9 9
*/
10 10
package eu.etaxonomy.cdm.io.specimen;
11 11

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

  
17
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Identification;
18
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
14 19

  
15 20
/**
16 21
 * @author k.luther
......
19 24
 */
20 25
public class SpecimenDataHolder {
21 26

  
22
    private String nomenclatureCode;
23
    private List<HashMap<String, String>> atomisedIdentificationList;
27
    protected String nomenclatureCode;
28
    protected List<HashMap<String, String>> atomisedIdentificationList;
29
    private String recordBasis;
30
    protected String gatheringElevationText;
31
    private String gatheringElevationMin;
32
    private String gatheringElevationMax;
33
    private String gatheringNotes;
34
    private String gatheringDateText;
35

  
36
    protected String gatheringElevation;
37

  
38
    private String gatheringElevationUnit;
39
    private String gatheringSpatialDatum;
40
    private String gatheringCoordinateErrorMethod;
41
    private String kindOfUnit;
42

  
43
    private Map<String, String> namedAreaList;
44
    private String fieldNumber;
45
    private String unitNotes; //  occurenceRemarks(DwCA)
46

  
47
    private List<String> multimediaObjects;
48
    private List<Identification> identificationList;
49

  
50
    private List<SpecimenTypeDesignationStatus> statusList;
51

  
52
    private List<String[]> referenceList;
53
    private List<String> docSources;
54
    private String unitID;
55

  
56

  
24 57

  
25 58

  
26 59

  
......
57 90
    public void setAtomisedIdentificationList(List<HashMap<String, String>> atomisedIdentificationList) {
58 91
        this.atomisedIdentificationList = atomisedIdentificationList;
59 92
    }
93

  
94

  
95

  
96
    /**
97
     * @return the gatheringElevationText
98
     */
99
    public String getGatheringElevationText() {
100
        return gatheringElevationText;
101
    }
102

  
103

  
104

  
105
    /**
106
     * @param gatheringElevationText the gatheringElevationText to set
107
     */
108
    public void setGatheringElevationText(String gatheringElevationText) {
109
        this.gatheringElevationText = gatheringElevationText;
110
    }
111

  
112

  
113

  
114
    /**
115
     * @return the gatheringElevationMax
116
     */
117
    public String getGatheringElevationMax() {
118
        return gatheringElevationMax;
119
    }
120

  
121

  
122

  
123
    /**
124
     * @param gatheringElevationMax the gatheringElevationMax to set
125
     */
126
    public void setGatheringElevationMax(String gatheringElevationMax) {
127
        this.gatheringElevationMax = gatheringElevationMax;
128
    }
129

  
130

  
131

  
132
    /**
133
     * @return the gatheringElevationMin
134
     */
135
    public String getGatheringElevationMin() {
136
        return gatheringElevationMin;
137
    }
138

  
139

  
140

  
141
    /**
142
     * @param gatheringElevationMin the gatheringElevationMin to set
143
     */
144
    public void setGatheringElevationMin(String gatheringElevationMin) {
145
        this.gatheringElevationMin = gatheringElevationMin;
146
    }
147

  
148

  
149

  
150
    /**
151
     * @return the kindOfUnit
152
     */
153
    public String getKindOfUnit() {
154
        return kindOfUnit;
155
    }
156

  
157

  
158

  
159
    /**
160
     * @param kindOfUnit the kindOfUnit to set
161
     */
162
    public void setKindOfUnit(String kindOfUnit) {
163
        this.kindOfUnit = kindOfUnit;
164
    }
165

  
166

  
167

  
168
    /**
169
     * @return the gatheringElevationUnit
170
     */
171
    public String getGatheringElevationUnit() {
172
        return gatheringElevationUnit;
173
    }
174

  
175

  
176

  
177
    /**
178
     * @param gatheringElevationUnit the gatheringElevationUnit to set
179
     */
180
    public void setGatheringElevationUnit(String gatheringElevationUnit) {
181
        this.gatheringElevationUnit = gatheringElevationUnit;
182
    }
183

  
184

  
185

  
186
    /**
187
     * @return the gatheringDateText
188
     */
189
    public String getGatheringDateText() {
190
        return gatheringDateText;
191
    }
192

  
193

  
194

  
195
    /**
196
     * @param gatheringDateText the gatheringDateText to set
197
     */
198
    public void setGatheringDateText(String gatheringDateText) {
199
        this.gatheringDateText = gatheringDateText;
200
    }
201

  
202

  
203

  
204
    /**
205
     * @return the gatheringNotes
206
     */
207
    public String getGatheringNotes() {
208
        return gatheringNotes;
209
    }
210

  
211

  
212

  
213
    /**
214
     * @param gatheringNotes the gatheringNotes to set
215
     */
216
    public void setGatheringNotes(String gatheringNotes) {
217
        this.gatheringNotes = gatheringNotes;
218
    }
219

  
220

  
221

  
222
    /**
223
     * @return the gatheringSpatialDatum
224
     */
225
    public String getGatheringSpatialDatum() {
226
        return gatheringSpatialDatum;
227
    }
228

  
229

  
230

  
231
    /**
232
     * @param gatheringSpatialDatum the gatheringSpatialDatum to set
233
     */
234
    public void setGatheringSpatialDatum(String gatheringSpatialDatum) {
235
        this.gatheringSpatialDatum = gatheringSpatialDatum;
236
    }
237

  
238

  
239

  
240
    /**
241
     * @return the namedAreaList
242
     */
243
    public Map<String, String> getNamedAreaList() {
244
        return namedAreaList;
245
    }
246

  
247

  
248

  
249
    /**
250
     * @param namedAreaList the namedAreaList to set
251
     */
252
    public void setNamedAreaList(Map<String, String> namedAreaList) {
253
        this.namedAreaList = namedAreaList;
254
    }
255

  
256

  
257

  
258
    /**
259
     * @return the gatheringCoordinateErrorMethod
260
     */
261
    public String getGatheringCoordinateErrorMethod() {
262
        return gatheringCoordinateErrorMethod;
263
    }
264

  
265

  
266

  
267
    /**
268
     * @param gatheringCoordinateErrorMethod the gatheringCoordinateErrorMethod to set
269
     */
270
    public void setGatheringCoordinateErrorMethod(String gatheringCoordinateErrorMethod) {
271
        this.gatheringCoordinateErrorMethod = gatheringCoordinateErrorMethod;
272
    }
273

  
274

  
275

  
276
    /**
277
     *
278
     */
279
    public void reset() {
280
        nomenclatureCode = null;
281
        atomisedIdentificationList = new ArrayList<HashMap<String,String>>();
282
        gatheringDateText = null;
283
        gatheringNotes = null;
284
        kindOfUnit = null;
285

  
286
        setRecordBasis(null);
287
        gatheringElevationText = null;
288
        gatheringElevationMin = null;
289
        gatheringElevationMax = null;
290
        gatheringNotes = null;
291
        gatheringDateText = null;
292

  
293
        gatheringElevation = null;
294

  
295
        gatheringElevationUnit = null;
296
        gatheringSpatialDatum = null;
297
       gatheringCoordinateErrorMethod = null;
298

  
299

  
300
    }
301

  
302

  
303

  
304
    /**
305
     * @return the fieldNumber
306
     */
307
    public String getFieldNumber() {
308
        return fieldNumber;
309
    }
310

  
311

  
312

  
313
    /**
314
     * @param fieldNumber the fieldNumber to set
315
     */
316
    public void setFieldNumber(String fieldNumber) {
317
        this.fieldNumber = fieldNumber;
318
    }
319

  
320

  
321

  
322
    /**
323
     * @return the unitNotes
324
     */
325
    public String getUnitNotes() {
326
        return unitNotes;
327
    }
328

  
329

  
330

  
331
    /**
332
     * @param unitNotes the unitNotes to set
333
     */
334
    public void setUnitNotes(String unitNotes) {
335
        this.unitNotes = unitNotes;
336
    }
337

  
338

  
339

  
340
    /**
341
     * @return the multimediaObjects
342
     */
343
    public List<String> getMultimediaObjects() {
344
        return multimediaObjects;
345
    }
346

  
347

  
348

  
349
    /**
350
     * @param multimediaObjects the multimediaObjects to set
351
     */
352
    public void setMultimediaObjects(List<String> multimediaObjects) {
353
        this.multimediaObjects = multimediaObjects;
354
    }
355

  
356

  
357

  
358
    /**
359
     * @return the identificationList
360
     */
361
    public List<Identification> getIdentificationList() {
362
        return identificationList;
363
    }
364

  
365

  
366

  
367
    /**
368
     * @param identificationList the identificationList to set
369
     */
370
    public void setIdentificationList(List<Identification> identificationList) {
371
        this.identificationList = identificationList;
372
    }
373

  
374

  
375

  
376
    /**
377
     * @return the statusList
378
     */
379
    public List<SpecimenTypeDesignationStatus> getStatusList() {
380
        return statusList;
381
    }
382

  
383

  
384

  
385
    /**
386
     * @param statusList the statusList to set
387
     */
388
    public void setStatusList(List<SpecimenTypeDesignationStatus> statusList) {
389
        this.statusList = statusList;
390
    }
391

  
392

  
393

  
394
    /**
395
     * @return the referenceList
396
     */
397
    public List<String[]> getReferenceList() {
398
        return referenceList;
399
    }
400

  
401

  
402

  
403
    /**
404
     * @param referenceList the referenceList to set
405
     */
406
    public void setReferenceList(List<String[]> referenceList) {
407
        this.referenceList = referenceList;
408
    }
409

  
410

  
411

  
412
    /**
413
     * @return the docSources
414
     */
415
    public List<String> getDocSources() {
416
        return docSources;
417
    }
418

  
419

  
420

  
421
    /**
422
     * @param docSources the docSources to set
423
     */
424
    public void setDocSources(List<String> docSources) {
425
        this.docSources = docSources;
426
    }
427

  
428

  
429

  
430
    /**
431
     * @return the unitID
432
     */
433
    public String getUnitID() {
434
        return unitID;
435
    }
436

  
437

  
438

  
439
    /**
440
     * @param unitID the unitID to set
441
     */
442
    public void setUnitID(String unitID) {
443
        this.unitID = unitID;
444
    }
445

  
446

  
447

  
448
    /**
449
     * @return the recordBasis
450
     */
451
    public String getRecordBasis() {
452
        return recordBasis;
453
    }
454

  
455

  
456

  
457
    /**
458
     * @param recordBasis the recordBasis to set
459
     */
460
    public void setRecordBasis(String recordBasis) {
461
        this.recordBasis = recordBasis;
462
    }
60 463
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/specimen/SpecimenImportBase.java
12 12

  
13 13
import java.util.ArrayList;
14 14
import java.util.HashMap;
15
import java.util.HashSet;
15 16
import java.util.List;
17
import java.util.Map;
16 18
import java.util.Set;
19
import java.util.UUID;
17 20

  
18 21
import org.apache.log4j.Logger;
22
import org.unitils.spring.annotation.SpringBeanByType;
19 23

  
20 24
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
21 25
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
26
import eu.etaxonomy.cdm.api.service.IClassificationService;
27
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28
import eu.etaxonomy.cdm.api.service.ITaxonService;
22 29
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
23 30
import eu.etaxonomy.cdm.api.service.pager.Pager;
24 31
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25 32
import eu.etaxonomy.cdm.io.common.CdmImportBase;
26 33
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
34
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Identification;
27 35
import eu.etaxonomy.cdm.io.specimen.abcd206.in.SpecimenImportReport;
28 36
import eu.etaxonomy.cdm.model.agent.AgentBase;
37
import eu.etaxonomy.cdm.model.agent.Institution;
29 38
import eu.etaxonomy.cdm.model.agent.Team;
30 39
import eu.etaxonomy.cdm.model.common.CdmBase;
40
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
31 41
import eu.etaxonomy.cdm.model.common.LanguageString;
42
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
43
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
32 44
import eu.etaxonomy.cdm.model.description.DescriptionBase;
45
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
46
import eu.etaxonomy.cdm.model.description.Feature;
47
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
48
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33 49
import eu.etaxonomy.cdm.model.name.BacterialName;
34 50
import eu.etaxonomy.cdm.model.name.BotanicalName;
35 51
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
36 52
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
37 53
import eu.etaxonomy.cdm.model.name.NonViralName;
38 54
import eu.etaxonomy.cdm.model.name.Rank;
55
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
56
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
39 57
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40 58
import eu.etaxonomy.cdm.model.name.ZoologicalName;
41 59
import eu.etaxonomy.cdm.model.occurrence.Collection;
60
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
61
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
42 62
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
63
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
43 64
import eu.etaxonomy.cdm.model.reference.Reference;
65
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
44 66
import eu.etaxonomy.cdm.model.taxon.Classification;
45 67
import eu.etaxonomy.cdm.model.taxon.Synonym;
46 68
import eu.etaxonomy.cdm.model.taxon.Taxon;
47 69
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
70
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
71
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
48 72
import eu.etaxonomy.cdm.persistence.query.MatchMode;
49 73
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
50 74
import eu.etaxonomy.cdm.strategy.parser.ParserProblem;
......
60 84
	private static final Logger logger = Logger.getLogger(SpecimenImportBase.class);
61 85
	protected final boolean DEBUG = true;
62 86

  
87
	protected static final UUID SPECIMEN_SCAN_TERM = UUID.fromString("acda15be-c0e2-4ea8-8783-b9b0c4ad7f03");
88

  
89
	private static final String COLON = ":";
90
	@SpringBeanByType
91
	private ITaxonNodeService nodeService;
92

  
93
	@SpringBeanByType
94
    private ITaxonService taxonService;
95

  
96
	@SpringBeanByType
97
    private IClassificationService classificationService;
98

  
63 99
	@Override
64 100
    protected abstract void doInvoke(STATE state);
65 101

  
......
75 111

  
76 112
	protected TaxonNameBase<?, ?> getOrCreateTaxonName(String scientificName, Rank rank, boolean preferredFlag, STATE state, int unitIndexInAbcdFile){
77 113
        TaxonNameBase<?, ?> taxonName = null;
78
        SpecimenImportConfiguratorBase config = (SpecimenImportConfiguratorBase) state.getConfig();
114
        SpecimenImportConfiguratorBase config = state.getConfig();
79 115

  
80 116
        //check atomised name data for rank
81 117
        //new name will be created
82 118
        NonViralName<?> atomisedTaxonName = null;
83
        if (rank==null && unitIndexInAbcdFile>=0 && (state.getDataHolder().getAtomisedIdentificationList() != null || state.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
119
        if (rank==null && unitIndexInAbcdFile>=0 && ((state.getDataHolder().getAtomisedIdentificationList() != null && !state.getDataHolder().getAtomisedIdentificationList().isEmpty())|| state.getDataHolder().getAtomisedIdentificationList().size() > 0)) {
84 120
            atomisedTaxonName = setTaxonNameByType(state.getDataHolder().getAtomisedIdentificationList().get(unitIndexInAbcdFile), scientificName, state);
85 121
            if(atomisedTaxonName!=null){
86 122
                rank = atomisedTaxonName.getRank();
......
96 132
                // to be created for the determination history
97 133
                String nameCache = parsedName.getNameCache();
98 134
                List<NonViralName> names = getNameService().findNamesByNameCache(nameCache, MatchMode.EXACT, null);
99
                taxonName = getBestMatchingName(scientificName, new ArrayList<TaxonNameBase>(names), state);
100
            }
101
            else {
135
                if (!names.isEmpty()){
136
                     return getBestMatchingName(scientificName, new ArrayList<TaxonNameBase>(names), state);
137
                }
138
            } else {
102 139
                //search for existing names
103 140
                List<TaxonNameBase> names = getNameService().listByTitle(TaxonNameBase.class, scientificName, MatchMode.EXACT, null, null, null, null, null);
104 141
                taxonName = getBestMatchingName(scientificName, names, state);
......
112 149
                        taxonName = getBestMatchingName(atomisedTaxonName.getTitleCache(), names, state);
113 150
                    }
114 151
                }
152

  
115 153
            }
154

  
116 155
        }
117 156

  
118 157
        if(taxonName==null && atomisedTaxonName!=null){
......
128 167
        }
129 168
        else if(taxonName==null){
130 169
            //create new taxon name
131
            taxonName = NonViralName.NewInstance(rank);
170
            if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICNAFP)){
171
                taxonName = BotanicalName.NewInstance(rank);
172
            }else if (state.getDataHolder().getNomenclatureCode().equals(NomenclaturalCode.ICZN)){
173
                taxonName = ZoologicalName.NewInstance(rank);
174
            }else{
175
                taxonName = NonViralName.NewInstance(rank);
176
            }
132 177
            taxonName.setFullTitleCache(scientificName,true);
133 178
            taxonName.setTitleCache(scientificName, true);
134 179
            state.getReport().addName(taxonName);
......
471 516
	     * @param cdmBase
472 517
	     * @param state
473 518
	     */
474
	    protected void save(CdmBase cdmBase, SpecimenImportStateBase state) {
519
	    protected UUID save(CdmBase cdmBase, SpecimenImportStateBase state) {
475 520
	        ICdmApplicationConfiguration cdmRepository = state.getConfig().getCdmAppController();
476 521
	        if (cdmRepository == null){
477 522
	            cdmRepository = this;
478 523
	        }
479 524

  
480 525
	        if (cdmBase.isInstanceOf(LanguageString.class)){
481
	            cdmRepository.getTermService().saveLanguageData(CdmBase.deproxy(cdmBase, LanguageString.class));
526
	            return cdmRepository.getTermService().saveLanguageData(CdmBase.deproxy(cdmBase, LanguageString.class));
482 527
	        }else if (cdmBase.isInstanceOf(SpecimenOrObservationBase.class)){
483
	            cdmRepository.getOccurrenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class));
528
	            return cdmRepository.getOccurrenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, SpecimenOrObservationBase.class));
484 529
	        }else if (cdmBase.isInstanceOf(Reference.class)){
485
	            cdmRepository.getReferenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, Reference.class));
530
	            return cdmRepository.getReferenceService().saveOrUpdate(CdmBase.deproxy(cdmBase, Reference.class));
486 531
	        }else if (cdmBase.isInstanceOf(Classification.class)){
487
	            cdmRepository.getClassificationService().saveOrUpdate(CdmBase.deproxy(cdmBase, Classification.class));
532
	            return cdmRepository.getClassificationService().saveOrUpdate(CdmBase.deproxy(cdmBase, Classification.class));
488 533
	        }else if (cdmBase.isInstanceOf(AgentBase.class)){
489
	            cdmRepository.getAgentService().saveOrUpdate(CdmBase.deproxy(cdmBase, AgentBase.class));
534
	            return cdmRepository.getAgentService().saveOrUpdate(CdmBase.deproxy(cdmBase, AgentBase.class));
490 535
	        }else if (cdmBase.isInstanceOf(Collection.class)){
491
	            cdmRepository.getCollectionService().saveOrUpdate(CdmBase.deproxy(cdmBase, Collection.class));
536
	            return cdmRepository.getCollectionService().saveOrUpdate(CdmBase.deproxy(cdmBase, Collection.class));
492 537
	        }else if (cdmBase.isInstanceOf(DescriptionBase.class)){
493
	            cdmRepository.getDescriptionService().saveOrUpdate(CdmBase.deproxy(cdmBase, DescriptionBase.class));
538
	            return cdmRepository.getDescriptionService().saveOrUpdate(CdmBase.deproxy(cdmBase, DescriptionBase.class));
494 539
	        }else if (cdmBase.isInstanceOf(TaxonBase.class)){
495
	            cdmRepository.getTaxonService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonBase.class));
540
	            return cdmRepository.getTaxonService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonBase.class));
496 541
	        }else if (cdmBase.isInstanceOf(TaxonNameBase.class)){
497
	            cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNameBase.class));
542
	            return cdmRepository.getNameService().saveOrUpdate(CdmBase.deproxy(cdmBase, TaxonNameBase.class));
498 543
	        }else{
499 544
	            throw new IllegalArgumentException("Class not supported in save method: " + CdmBase.deproxy(cdmBase, CdmBase.class).getClass().getSimpleName());
500 545
	        }
......
520 565

  
521 566
	    protected abstract void importAssociatedUnits(STATE state, Object item, DerivedUnitFacade derivedUnitFacade);
522 567

  
568
	    /**
569
	     * getFacade : get the DerivedUnitFacade based on the recordBasis
570
	     * @param state
571
	     *
572
	     * @return DerivedUnitFacade
573
	     */
574
	    protected DerivedUnitFacade getFacade(STATE state) {
575
	        if(DEBUG) {
576
	            logger.info("getFacade()");
577
	        }
578
	        SpecimenOrObservationType type = null;
579

  
580
	        // create specimen
581
	        if (NB((state.getDataHolder().getRecordBasis())) != null) {
582
	            if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("s") || state.getDataHolder().getRecordBasis().toLowerCase().indexOf("specimen")>-1) {// specimen
583
	                type = SpecimenOrObservationType.PreservedSpecimen;
584
	            }
585
	            if (state.getDataHolder().getRecordBasis().toLowerCase().startsWith("o") ||state.getDataHolder().getRecordBasis().toLowerCase().indexOf("observation")>-1 ) {
586
	                type = SpecimenOrObservationType.Observation;
587
	            }
588
	            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("fossil")>-1){
589
	                type = SpecimenOrObservationType.Fossil;
590
	            }
591
	            if (state.getDataHolder().getRecordBasis().toLowerCase().indexOf("living")>-1) {
592
	                type = SpecimenOrObservationType.LivingSpecimen;
593
	            }
594
	            if (type == null) {
595
	                logger.info("The basis of record does not seem to be known: " + state.getDataHolder().getRecordBasis());
596
	                type = SpecimenOrObservationType.DerivedUnit;
597
	            }
598
	            // TODO fossils?
599
	        } else {
600
	            logger.info("The basis of record is null");
601
	            type = SpecimenOrObservationType.DerivedUnit;
602
	        }
603
	        DerivedUnitFacade derivedUnitFacade = DerivedUnitFacade.NewInstance(type);
604
	        return derivedUnitFacade;
605
	    }
606

  
607
	    /**
608
	     * Look if the Institution does already exist
609
	     * @param institutionCode: a string with the institutioncode
610
	     * @param config : the configurator
611
	     * @return the Institution (existing or new)
612
	     */
613
	    protected Institution getInstitution(String institutionCode, STATE state) {
614
	        SpecimenImportConfiguratorBase config = state.getConfig();
615
	        Institution institution=null;
616
	        institution = (Institution)state.institutions.get(institutionCode);
617
	        if (institution != null){
618
	            return institution;
619
	        }
620
	        List<Institution> institutions;
621
	        try {
622
	            institutions = getAgentService().searchInstitutionByCode(institutionCode);
623

  
624
	        } catch (Exception e) {
625
	            institutions = new ArrayList<Institution>();
626
	            logger.warn(e);
627
	        }
628
	        if (institutions.size() > 0 && config.isReuseExistingMetaData()) {
629
	            for (Institution institut:institutions){
630
	                try{
631
	                    if (institut.getCode().equalsIgnoreCase(institutionCode)) {
632
	                        institution=institut;
633
	                        break;
634
	                    }
635
	                }catch(Exception e){logger.warn("no institution code in the db");}
636
	            }
637
	        }
638
	        if(DEBUG) {
639
	            if(institution !=null) {
640
	                logger.info("getinstitution " + institution.toString());
641
	            }
642
	        }
643
	        if (institution == null){
644
	            // create institution
645
	            institution = Institution.NewInstance();
646
	            institution.setCode(institutionCode);
647
	            institution.setTitleCache(institutionCode, true);
648
	            UUID uuid = save(institution, state);
649
	        }
650

  
651

  
652
	        state.institutions.put(institutionCode, institution);
653
	        return institution;
654
	    }
655

  
656
	    /**
657
	     * Look if the Collection does already exist
658
	     * @param collectionCode
659
	     * @param collectionCode: a string
660
	     * @param config : the configurator
661
	     * @return the Collection (existing or new)
662
	     */
663
	    protected Collection getCollection(Institution institution, String collectionCode, STATE state) {
664
	        SpecimenImportConfiguratorBase config = state.getConfig();
665
	        Collection collection = null;
666
	        List<Collection> collections;
667
	        collection = (Collection) state.collections.get(collectionCode);
668
	        if (collection != null){
669
	            return collection;
670
	        }
671
	        try {
672
	            collections = getCollectionService().searchByCode(collectionCode);
673
	        } catch (Exception e) {
674
	            collections = new ArrayList<Collection>();
675
	        }
676
	        if (collections.size() > 0 && config.isReuseExistingMetaData()) {
677
	            for (Collection coll:collections){
678
	                if (coll.getCode() != null && coll.getInstitute() != null
679
	                        && coll.getCode().equalsIgnoreCase(collectionCode) && coll.getInstitute().equals(institution)) {
680
	                    collection = coll;
681
	                    break;
682
	                }
683
	            }
684
	        }
685

  
686
	        if(collection == null){
687
	            collection =Collection.NewInstance();
688
	            collection.setCode(collectionCode);
689
	            collection.setInstitute(institution);
690
	            collection.setTitleCache(collectionCode);
691
	            UUID uuid = save(collection, state);
692
	        }
693

  
694

  
695

  
696
	        state.collections.put(collectionCode, collection);
697

  
698
	        return collection;
699
	    }
700

  
701
	    /**
702
	     * @param reference
703
	     * @param citationDetail
704
	     * @return
705
	     */
706
	    //FIXME this method is highly critical, because
707
	    //  * it will have serious performance and memory problems with large databases
708
	    //        (databases may easily have >1 Mio source records)
709
	    //  * it does not make sense to search for existing sources and then clone them
710
	    //    we need to search for existing references instead and use them (if exist)
711
	    //    for our new source.
712
	    protected IdentifiableSource getIdentifiableSource(Reference reference, String citationDetail) {
713

  
714
	      /*  List<IdentifiableSource> issTmp = getCommonService().list(IdentifiableSource.class, null, null, null, null);
715

  
716

  
717
	        if (reference != null){
718
	            try {
719
	                for (OriginalSourceBase<?> osb: issTmp){
720
	                    if (osb.getCitation() != null && osb.getCitation().getTitleCache().equalsIgnoreCase(reference.getTitleCache())){
721
	                        String osbDetail = osb.getCitationMicroReference();
722
	                        if ((StringUtils.isBlank(osbDetail) && StringUtils.isBlank(citationDetail))
723
	                                || (osbDetail != null && osbDetail.equalsIgnoreCase(citationDetail)) ) {
724
//	                            System.out.println("REFERENCE FOUND RETURN EXISTING SOURCE");
725
	                            return (IdentifiableSource) osb.clone();
726
	                        }
727
	                    }
728
	                }
729
	            } catch (CloneNotSupportedException e) {
730
	                throw new RuntimeException(e);
731
	            } catch (Exception e1){
732
	                e1.printStackTrace();
733
	            }
734
	        }
735
	*/
736
	        IdentifiableSource sour = IdentifiableSource.NewInstance(OriginalSourceType.Import,null,null, reference,citationDetail);
737
	        return sour;
738
	    }
739

  
740
	    /**
741
	     * Add the hierarchy for a Taxon(add higher taxa)
742
	     * @param classification
743
	     * @param taxon: a taxon to add as a node
744
	     * @param state: the ABCD import state
745
	     */
746
	    protected void addParentTaxon(Taxon taxon, STATE state, boolean preferredFlag, Classification classification){
747
	        NonViralName<?>  nvname = CdmBase.deproxy(taxon.getName(), NonViralName.class);
748
	        Rank rank = nvname.getRank();
749
	        Taxon genus =null;
750
	        Taxon subgenus =null;
751
	        Taxon species = null;
752
	        Taxon subspecies = null;
753
	        Taxon parent = null;
754
	        if(rank!=null){
755
	            if (rank.isLower(Rank.GENUS() )){
756
	                String genusOrUninomial = nvname.getGenusOrUninomial();
757
	                TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(genusOrUninomial, Rank.GENUS(), preferredFlag, state, -1);
758
	                genus = getOrCreateTaxonForName(taxonName, state);
759
	                if (preferredFlag) {
760
	                    parent = linkParentChildNode(null, genus, classification, state);
761
	                }
762

  
763
	            }
764
	            if (rank.isLower(Rank.SUBGENUS())){
765
	                String prefix = nvname.getGenusOrUninomial();
766
	                String name = nvname.getInfraGenericEpithet();
767
	                if (name != null){
768
	                    TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SUBGENUS(), preferredFlag, state, -1);
769
	                    subgenus = getOrCreateTaxonForName(taxonName, state);
770
	                    if (preferredFlag) {
771
	                        parent = linkParentChildNode(genus, subgenus, classification, state);
772
	                    }            }
773
	            }
774
	            if (rank.isLower(Rank.SPECIES())){
775
	                if (subgenus!=null){
776
	                    String prefix = nvname.getGenusOrUninomial();
777
	                    String name = nvname.getInfraGenericEpithet();
778
	                    String spe = nvname.getSpecificEpithet();
779
	                    if (spe != null){
780
	                        TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name+" "+spe, Rank.SPECIES(), preferredFlag, state, -1);
781
	                        species = getOrCreateTaxonForName(taxonName, state);
782
	                        if (preferredFlag) {
783
	                            parent = linkParentChildNode(subgenus, species, classification, state);
784
	                        }
785
	                    }
786
	                }
787
	                else{
788
	                    String prefix = nvname.getGenusOrUninomial();
789
	                    String name = nvname.getSpecificEpithet();
790
	                    if (name != null){
791
	                        TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(prefix+" "+name, Rank.SPECIES(), preferredFlag, state, -1);
792
	                        species = getOrCreateTaxonForName(taxonName, state);
793
	                        if (preferredFlag) {
794
	                            parent = linkParentChildNode(genus, species, classification, state);
795
	                        }
796
	                    }
797
	                }
798
	            }
799
	            if (rank.isLower(Rank.INFRASPECIES())){
800
	                TaxonNameBase<?,?> taxonName = getOrCreateTaxonName(nvname.getFullTitleCache(), Rank.SUBSPECIES(), preferredFlag, state, -1);
801
	                subspecies = getOrCreateTaxonForName(taxonName, state);
802
	                if (preferredFlag) {
803
	                    parent = linkParentChildNode(species, subspecies, classification, state);
804
	                }
805
	            }
806
	        }
807
	        if (preferredFlag && parent!=taxon) {
808
	            linkParentChildNode(parent, taxon, classification, state);
809
	        }
810
	    }
811

  
812
	    /**
813
	     * Link a parent to a child and save it in the current classification
814
	     * @param parent: the higher Taxon
815
	     * @param child : the lower (or current) Taxon
816
	     * return the Taxon from the new created Node
817
	     * @param classification
818
	     * @param state
819
	     */
820
	    protected Taxon linkParentChildNode(Taxon parent, Taxon child, Classification classification, STATE state) {
821
	        TaxonNode node =null;
822
	        if (parent != null) {
823
	            parent = (Taxon) getTaxonService().find(parent.getUuid());
824
	            child = (Taxon) getTaxonService().find(child.getUuid());
825
	            //here we do not have to check if the taxon nodes already exists
826
	            //this is done by classification.addParentChild()
827
	            //do not add child node if it already exists
828
	            if(hasTaxonNodeInClassification(child, classification)){
829
	                return child;
830
	            }
831
	            else{
832
	                node = classification.addParentChild(parent, child, state.getRef(), "");
833
	                save(classification, state);
834
	            }
835
	        }
836
	        else {
837
	            child = (Taxon) getTaxonService().find(child.getUuid());
838
	            //do not add child node if it already exists
839
	            if(hasTaxonNodeInClassification(child, classification)){
840
	                return child;
841
	            }
842
	            else{
843
	                node = classification.addChildTaxon(child, state.getRef(), null);
844
	                save(classification, state);
845
	            }
846
	        }
847
	        if(node!=null){
848
	            state.getReport().addTaxonNode(node);
849
	            return node.getTaxon();
850
	        }
851
	        String message = "Could not create taxon node for " +child;
852
	        state.getReport().addInfoMessage(message);
853
	        logger.warn(message);
854
	        return null;
855
	    }
856

  
857
	    protected Taxon getOrCreateTaxonForName(TaxonNameBase<?, ?> taxonNameBase, STATE state){
858

  
859
	        Set<Taxon> acceptedTaxa = taxonNameBase.getTaxa();
860
	        if(acceptedTaxa.size()>0){
861
	            Taxon firstAcceptedTaxon = acceptedTaxa.iterator().next();
862
	            if(acceptedTaxa.size()>1){
863
	                String message = "More than one accepted taxon was found for taxon name: "
864
	                        + taxonNameBase.getTitleCache() + "!\n" + firstAcceptedTaxon + "was chosen for "+state.getDerivedUnitBase();
865
	                state.getReport().addInfoMessage(message);
866
	                logger.warn(message);
867
	            }
868
	            else{
869
	                return firstAcceptedTaxon;
870
	            }
871
	        }
872
	        else{
873
	            Set<TaxonBase> taxonAndSynonyms = taxonNameBase.getTaxonBases();
874
	            for (TaxonBase taxonBase : taxonAndSynonyms) {
875
	                if(taxonBase.isInstanceOf(Synonym.class)){
876
	                    Synonym synonym = HibernateProxyHelper.deproxy(taxonBase, Synonym.class);
877
	                    Set<Taxon> acceptedTaxaOfSynonym = synonym.getAcceptedTaxa();
878
	                    if(acceptedTaxaOfSynonym.size()!=1){
879
	                        String message = "No accepted taxa could be found for taxon name: "
880
	                                + taxonNameBase.getTitleCache()
881
	                                + "!\nEither it is a pro parte synonym or has no accepted taxa";
882
	                        state.getReport().addInfoMessage(message);
883
	                        logger.warn(message);
884
	                    }
885
	                    else{
886
	                        return acceptedTaxaOfSynonym.iterator().next();
887
	                    }
888
	                }
889
	            }
890
	        }
891
	        Taxon taxon = Taxon.NewInstance(taxonNameBase, state.getRef());
892
	        save(taxon, state);
893
	        state.getReport().addTaxon(taxon);
894
	        logger.info("Created new taxon "+ taxon);
895
	        return taxon;
896
	    }
897

  
898
	    private boolean hasTaxonNodeInClassification(Taxon taxon, Classification classification){
899
	        if(taxon.getTaxonNodes()!=null){
900
	            for (TaxonNode node : taxon.getTaxonNodes()){
901
	                if(node.getClassification().equals(classification)){
902
	                    return true;
903
	                }
904
	            }
905
	        }
906
	        return false;
907
	    }
908

  
909
	    /**
910
	     * HandleIdentifications : get the scientific names present in the ABCD
911
	     * document and store link them with the observation/specimen data
912
	     * @param state: the current ABCD import state
913
	     * @param derivedUnitFacade : the current derivedunitfacade
914
	     */
915
	    protected void handleIdentifications(STATE state, DerivedUnitFacade derivedUnitFacade) {
916
	       SpecimenImportConfiguratorBase config = state.getConfig();
917

  
918

  
919
	        String scientificName = "";
920
	        boolean preferredFlag = false;
921

  
922
	        if (state.getDataHolder().getNomenclatureCode() == ""){
923
	            if (config.getNomenclaturalCode() != null){
924
	                if (config.getNomenclaturalCode() != null){
925
	                    state.getDataHolder().setNomenclatureCode(config.getNomenclaturalCode().toString());
926
	                }
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)