Project

General

Profile

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

    
11
import java.net.MalformedURLException;
12
import java.net.URI;
13
import java.text.ParseException;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.UUID;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21
import eu.etaxonomy.cdm.api.service.config.MatchingTaxonConfigurator;
22
import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
23
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
24
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
25
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.TimePeriod;
28
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
29
import eu.etaxonomy.cdm.model.description.TaxonDescription;
30
import eu.etaxonomy.cdm.model.location.Country;
31
import eu.etaxonomy.cdm.model.media.Media;
32
import eu.etaxonomy.cdm.model.occurrence.Collection;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
36

    
37
/**
38
 * @author a.mueller
39
 * @since 21.08.2018
40
 *
41
 */
42
public class GreeceWillingImport
43
        extends SimpleExcelTaxonImport<GreeceWillingImportConfigurator>{
44

    
45
    private static final long serialVersionUID = 8258914747643501550L;
46

    
47
    private static final Logger logger = Logger.getLogger(GreeceWillingImport.class);
48

    
49
    private ImportDeduplicationHelper<SimpleExcelTaxonImportState> dedupHelper;
50

    
51

    
52
    /**
53
     * {@inheritDoc}
54
     */
55
    @Override
56
    protected void firstPass(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state) {
57
        try {
58
            Map<String, String> record = state.getOriginalRecord();
59
            String scientificName = record.get("ScientificName");
60
            String stableIdentifier = record.get("ObjectURI");
61
            String title = record.get("Title");
62
            String titleDescription = record.get("TitleDescription");
63

    
64
            String collector = record.get("Collector");
65
            String collectorNumber = record.get("CollectorNumber");
66

    
67
            String collectionDate = record.get("CollectionDate");
68
//            String CollDateISO = record.get("CollDateISO");
69
            String catalogNumber = record.get("CatalogNumber");
70
            String locality = record.get("Locality");
71
            String image = record.get("Image");
72

    
73
            String latitude = record.get("Latitude");
74
            String longitude = record.get("Longitude");
75

    
76
            String rdfId = record.get("rdfID");
77

    
78
            TimePeriod date = TimePeriodParser.parseEnglishDate(collectionDate, null);
79

    
80
            validate(state, "BaseOfRecords", "Specimen");
81
            validate(state, "InstitutionCode", "BGBM");
82
            validate(state, "CollectionCode", "B");
83
            validate(state, "HigherGeography", "Greece");
84
            validate(state, "Country", "Greece");
85
            validate(state, "CountryCode", "GR");
86

    
87

    
88
//        HerbariumID
89
//        CollDateISO
90

    
91

    
92
            Taxon taxon = getTaxonByName(state, scientificName);
93
            DerivedUnitFacade facade = DerivedUnitFacade.NewPreservedSpecimenInstance();
94

    
95
            facade.setPreferredStableUri(URI.create(stableIdentifier));
96
            facade.setFieldNumber(collectorNumber);
97
            facade.setBarcode(catalogNumber);
98
            facade.setCountry(Country.GREECEHELLENICREPUBLIC());
99

    
100
            facade.setLocality(locality);
101
            try {
102
                facade.setExactLocationByParsing(longitude, latitude, null, null);
103
            } catch (ParseException e) {
104
                e.printStackTrace();
105
            }
106
            facade.setCollector(getCollector(state, collector));
107
            facade.getGatheringEvent(true).setTimeperiod(date);
108

    
109
            facade.setCollection(getCollection(state));
110

    
111
            Media media = getMedia(state,title, titleDescription, image);
112
            facade.addFieldObjectMedia(media);
113

    
114
            Reference sourceReference = state.getSourceReference();
115
            String sourceId = rdfId;
116
            String namespace = "rdfID";
117
            this.addOriginalSource(facade.innerFieldUnit(), sourceId, namespace, sourceReference);
118
            this.addOriginalSource(facade.innerDerivedUnit(), sourceId, namespace, sourceReference);
119
            this.addOriginalSource(media, sourceId, namespace, sourceReference);
120

    
121
            TaxonDescription description = getTaxonDescription(taxon, sourceReference, false, CREATE);
122
            IndividualsAssociation specimen = IndividualsAssociation.NewInstance(facade.innerDerivedUnit());
123
            description.addElement(specimen);
124

    
125
            //        getDedupHelper(state).replaceAuthorNamesAndNomRef(state, name);
126

    
127
        } catch (MalformedURLException e) {
128
            logger.warn("An error occurred during import");
129
        }
130

    
131

    
132

    
133

    
134
    }
135

    
136
    private Collection bgbm;
137
    /**
138
     * @param state
139
     * @return
140
     */
141
    private Collection getCollection(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state) {
142
        if (bgbm == null){
143
            List<Collection> results = getCollectionService().searchByCode("B");
144
            if (results.size()> 1){
145
                throw new RuntimeException("More then 1 collection found for 'B'");
146
            }else if (results.isEmpty()){
147
                throw new RuntimeException("No collection found for 'B'");
148
            }
149
            bgbm = results.get(0);
150
        }
151
        return bgbm;
152
    }
153

    
154
    /**
155
     * @param state
156
     * @param string
157
     * @param string2
158
     */
159
    private void validate(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state, String attr,
160
            String expectedValue) {
161
        Map<String, String> record = state.getOriginalRecord();
162
        String attrValue = record.get(attr);
163
        if (!expectedValue.equalsIgnoreCase(attrValue)){
164
            throw new RuntimeException("Attribute " + attr + " has not expected value " + expectedValue + " but "+ attrValue);
165
        }
166
    }
167

    
168
    /**
169
     * @param state
170
     * @param title
171
     * @param titleDescription
172
     * @param image
173
     * @return
174
     * @throws MalformedURLException
175
     */
176
    private Media getMedia(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state, String title,
177
            String titleDescription, String image) throws MalformedURLException {
178

    
179
        //TODO
180
        String thumbnail = image;
181
        Media media = getImageMedia(image, thumbnail, true);
182

    
183
        if (isNotBlank(title)){
184
            media.putTitle(Language.ENGLISH(), title);
185
        }
186
        if (isNotBlank(titleDescription)){
187
            media.putDescription(Language.ENGLISH(), titleDescription);
188
        }
189

    
190

    
191
        //TODO thumbnails etc.
192

    
193
//        ImageInfo info = ImageInfo.NewInstanceWithMetaData(URI.create(image), 60);
194
//        Media media = Media.NewInstance(uri, info.getLength(), info.getMimeType(), info.getSuffix());
195
        return media;
196
    }
197

    
198

    
199
    Team willingCollector;
200
    /**
201
     * @param state
202
     * @param collector
203
     * @return
204
     */
205
    private Team getCollector(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state,
206
            String collector) {
207
        if (!"Willing,R. & Willing,E.".equals(collector)){
208
            throw new RuntimeException("Unexpected collector: " + collector);
209
        }
210
        if (willingCollector == null){
211
            UUID willingTeamUuid = UUID.fromString("ab3594a5-304f-4f19-bc8b-4a38c8abfad7");
212
            willingCollector = (Team)getAgentService().find(willingTeamUuid);
213
        }
214
        return willingCollector;
215
    }
216

    
217
    /**
218
     * @param state
219
     * @param scientificName
220
     * @return
221
     */
222
    private Taxon getTaxonByName(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state,
223
            String scientificName) {
224
        MatchingTaxonConfigurator config = MatchingTaxonConfigurator.NewInstance();
225
        config.setTaxonNameTitle(scientificName);
226
        config.setIncludeSynonyms(false);
227
        Taxon result = getTaxonService().findBestMatchingTaxon(config);
228
        return result;
229
    }
230

    
231
    /**
232
     * @param state
233
     * @return
234
     */
235
    private ImportDeduplicationHelper<SimpleExcelTaxonImportState> getDedupHelper(SimpleExcelTaxonImportState<GreeceWillingImportConfigurator> state) {
236
        if (this.dedupHelper == null){
237
            dedupHelper = ImportDeduplicationHelper.NewInstance(this, state);
238
        }
239
        return this.dedupHelper;
240
    }
241
}
(14-14/16)