Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2016 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.mexico;
10

    
11
import java.net.URI;
12
import java.net.URISyntaxException;
13
import java.util.Arrays;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.common.UTF8;
24
import eu.etaxonomy.cdm.model.common.DefinedTerm;
25
import eu.etaxonomy.cdm.model.common.Extension;
26
import eu.etaxonomy.cdm.model.common.ExtensionType;
27
import eu.etaxonomy.cdm.model.common.Identifier;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
32
import eu.etaxonomy.cdm.model.description.TextData;
33
import eu.etaxonomy.cdm.model.media.Media;
34
import eu.etaxonomy.cdm.model.name.BotanicalName;
35
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
42
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
43

    
44
/**
45
 * @author a.mueller
46
 * @date 16.06.2016
47
 *
48
 */
49
@Component
50
public class MexicoBorhidiExcelImport<CONFIG extends MexicoBorhidiImportConfigurator>
51
        extends SimpleExcelTaxonImport<CONFIG>{
52
    private static final Logger logger = Logger.getLogger(MexicoBorhidiExcelImport.class);
53
    private static final long serialVersionUID = -3607776356577606657L;
54

    
55
    private  static List<String> expectedKeys= Arrays.asList(new String[]{
56
            "FullnameNoAuthors","OutputNameID","OutputFullNameWithAuthors","RefType"
57
            ,"OutputAbbreviatedTitle","OutputCollation","OutputVolume",
58
            "OutputIssue","OutputPage","OutputTitlePageYear","OutputYearPublished",
59
            "OutputBHLLink"});
60

    
61

    
62
    @Override
63
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
64
        String line = state.getCurrentLine() + ": ";
65
        HashMap<String, String> record = state.getOriginalRecord();
66

    
67
        Set<String> keys = record.keySet();
68
        checkAllKeysExist(line, keys, expectedKeys);
69

    
70
        if (record.get("FullnameNoAuthors") == null ){
71
            logger.warn("No FullnameNoAuthors given: " + line);
72
            return;
73
        }
74

    
75
        //Name
76
        BotanicalName speciesName = makeName(record, state);
77

    
78
        //Taxon
79
        Reference sec = state.getConfig().getSecReference();
80
        Taxon taxon = Taxon.NewInstance(speciesName, sec);
81
        TaxonNode rubiaceae = getHighestNode(state);
82

    
83
        taxon.addSource(makeOriginalSource(state));
84

    
85

    
86
        //make genus
87
        makeGenus(state, speciesName, sec, taxon, rubiaceae);
88
    }
89

    
90

    
91
    private Classification classification;
92
    private TaxonNode rubiaceaeNode;
93
    /**
94
     * @return
95
     */
96
    private TaxonNode getHighestNode(SimpleExcelTaxonImportState<CONFIG> state) {
97
        if (rubiaceaeNode == null){
98
            MexicoBorhidiImportConfigurator config = state.getConfig();
99
            classification = Classification.NewInstance(state.getConfig().getClassificationName());
100
            classification.setUuid(config.getClassificationUuid());
101
            classification.setReference(config.getSecReference());
102
            BotanicalName nameRubiaceae = BotanicalName.NewInstance(Rank.FAMILY());
103
            nameRubiaceae.setGenusOrUninomial("Rubiaceae");
104
            Taxon rubiaceaeTaxon = Taxon.NewInstance(nameRubiaceae, classification.getReference());
105
            rubiaceaeNode = classification.addChildTaxon(rubiaceaeTaxon, null, null);
106
            getClassificationService().save(classification);
107
        }
108
        return rubiaceaeNode;
109
    }
110

    
111
    /**
112
     * @param record
113
     * @param state
114
     * @return
115
     */
116
    private BotanicalName makeName(HashMap<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
117
        String line = state.getCurrentLine() + ": ";
118

    
119
        String fullNameStr = getValue(record, "OutputFullNameWithAuthors");
120
//        String volume = getValue(record, "OutputVolume");
121
//        String issue = getValue(record, "OutputIssue");
122
//        String page = getValue(record, "OutputPage");
123
        String titleYear = getValue(record, "OutputTitlePageYear");
124
        String publishedYear = getValue(record, "OutputYearPublished");
125
        String refAbbrevTitle = getValue(record, "OutputAbbreviatedTitle");
126
        String outputCollation = getValue(record, "OutputCollation");
127
        String refType = getValue(record, "RefType");
128

    
129

    
130
        BotanicalName name = (BotanicalName)nameParser.parseFullName(fullNameStr, NomenclaturalCode.ICNAFP, Rank.SPECIES());
131
        if (name.isProtectedTitleCache()){
132
            //for the 2 ined. names
133
            name = (BotanicalName)nameParser.parseReferencedName(fullNameStr, NomenclaturalCode.ICNAFP, Rank.SPECIES());
134
        }
135
        if (name.isProtectedTitleCache()){
136
            logger.warn(line + "Name could not be parsed: " + fullNameStr );
137
        }else{
138
            replaceAuthorNamesAndNomRef(state, name);
139
        }
140

    
141
        if (refAbbrevTitle != null){
142
            String[] volumeDetail = makeVolumeDetail(outputCollation);
143
            String detail;
144
            String volume = null;
145
            if (volumeDetail.length > 1){
146
                volume = volumeDetail[0].trim();
147
                detail = volumeDetail[1].trim();
148
            }else{
149
                detail = volumeDetail[0].trim();
150
            }
151

    
152
            refAbbrevTitle = refAbbrevTitle.trim();
153
            boolean isArticle = "A".equalsIgnoreCase(refType);
154

    
155
            if (isArticle){
156
                if (! "A".equalsIgnoreCase(refType)){
157
                    logger.warn(line + "RefType problem with article " + refType);
158
                }
159

    
160
                Reference journal = state.getReference(refAbbrevTitle);
161
                if (journal == null){
162
                    journal = ReferenceFactory.newJournal();
163
                    journal.setAbbrevTitle(refAbbrevTitle);
164
                    state.putReference(refAbbrevTitle, journal);
165
                    journal.addSource(makeOriginalSource(state));
166

    
167
                }
168
                Reference article = ReferenceFactory.newArticle();
169

    
170

    
171
                //            String detail = page;
172
                name.setNomenclaturalMicroReference(detail);
173

    
174
                article.setVolume(CdmUtils.Ne(volume));
175
                article.setInReference(journal);
176

    
177
                titleYear = (isBlank(publishedYear)? titleYear : UTF8.ENGLISH_QUOT_START_LOW + titleYear + UTF8.ENGLISH_QUOT_END_HIGH + "[" + publishedYear + "]");
178
                article.setDatePublished(TimePeriodParser.parseString(titleYear));
179

    
180
                article.setAuthorship(name.getCombinationAuthorship());
181

    
182
                Reference existingArticle = state.getReference(article.getTitleCache());
183
                if (existingArticle != null){
184
                    name.setNomenclaturalReference(existingArticle);
185
                }else{
186
                    name.setNomenclaturalReference(article);
187
                    state.putReference(article.getTitleCache(), article);
188
                    article.addSource(makeOriginalSource(state));
189
                }
190
            }else{
191
                if (! "B".equalsIgnoreCase(refType)){
192
                    logger.warn(line + "RefType problem with book" + refType);
193
                }
194

    
195
                Reference book = ReferenceFactory.newBook();
196
                book.setAbbrevTitle(refAbbrevTitle);
197

    
198
                //year
199
                titleYear = (isBlank(publishedYear)? titleYear : UTF8.ENGLISH_QUOT_START_LOW + titleYear + UTF8.ENGLISH_QUOT_END_HIGH + "[" + publishedYear + "]");
200
                book.setDatePublished(TimePeriodParser.parseString(titleYear));
201

    
202
                book.setAuthorship(name.getCombinationAuthorship());
203

    
204
                //deduplicate
205
                Reference existingBook = state.getReference(book.getTitleCache());
206
                if (existingBook != null){
207
                    name.setNomenclaturalReference(existingBook);
208
                }else{
209
                    name.setNomenclaturalReference(book);
210
                    state.putReference(book.getTitleCache(), book);
211
                }
212

    
213
                book.setVolume(volume);
214

    
215
                //String detail = page;
216
                name.setNomenclaturalMicroReference(detail);
217
            }
218
        }
219

    
220
        addNomRefExtension(state, name);
221

    
222
        //add protologue
223
        String bhlLink = record.get("OutputBHLLink");
224
        if (isNotBlank(bhlLink)){
225
            URI uri;
226
            try {
227
                uri = new URI(bhlLink);
228
                Media media = Media.NewInstance(uri, null, null, null);
229
                TaxonNameDescription desc = TaxonNameDescription.NewInstance(name);
230
                desc.setTitleCache("Protologue for " + name.getNameCache(), true);
231
                DescriptionElementBase elem = TextData.NewInstance(Feature.PROTOLOGUE());
232
                elem.addMedia(media);
233
                desc.addElement(elem);
234
            } catch (URISyntaxException e) {
235
                logger.warn(line + "URI could not be parsed: " + e.getMessage());
236
            }
237
        }
238

    
239
        //add tropicos identifier
240
        String tropicosId = record.get("OutputNameID");
241
        if (isNotBlank(tropicosId)){
242
            String tropicosIdTypeLabel = "Tropicos Name Identifier";
243
            UUID uuid = MexicoConabioTransformer.uuidTropicosNameIdentifier;
244
            TermVocabulary<DefinedTerm> voc = null;  //for now it goes to user defined voc
245
            DefinedTerm identifierType = this.getIdentiferType(state, uuid, tropicosIdTypeLabel, tropicosIdTypeLabel, null, voc);
246
            Identifier<Taxon> identifier = Identifier.NewInstance(tropicosId, identifierType);
247
            name.addIdentifier(identifier);
248
        }
249

    
250
        name.addSource(makeOriginalSource(state));
251

    
252

    
253
        return name;
254
    }
255

    
256
    /**
257
     * @param outputCollation
258
     * @return
259
     */
260
    private String[] makeVolumeDetail(String outputCollation) {
261
        if (outputCollation == null){
262
            return new String[0];
263
        }else{
264
            String[] split = outputCollation.split(":");
265
            return split;
266
        }
267
    }
268

    
269
    /**
270
     * @param state
271
     * @param referencedName
272
     */
273
    private void addNomRefExtension(SimpleExcelTaxonImportState<CONFIG> state, BotanicalName name) {
274
        String newExtensionStr = name.getFullTitleCache() + " - BORHIDI";
275
        UUID uuidNomRefExtension = MexicoConabioTransformer.uuidNomRefExtension;
276
        for (Extension extension : name.getExtensions()){
277
            if (extension.getType().getUuid().equals(uuidNomRefExtension)){
278
                extension.setValue(extension.getValue() + "\n" + newExtensionStr);
279
                return;
280
            }
281
        }
282
        String label = "Nomenclatural reference in Sources";
283
        String abbrev = "Nom. ref. src.";
284
        ExtensionType extensionType = getExtensionType(state, uuidNomRefExtension, label, label, abbrev);
285
        Extension.NewInstance(name, newExtensionStr, extensionType);
286
    }
287

    
288

    
289
}
(1-1/9)