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.name.TaxonNameFactory;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
40
import eu.etaxonomy.cdm.model.taxon.Classification;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
44

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

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

    
62

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

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

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

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

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

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

    
86

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

    
91

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

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

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

    
130

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

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

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

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

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

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

    
171

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

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

    
178
                titleYear = (isBlank(publishedYear)? titleYear : UTF8.QUOT_DBL_LOW9 + titleYear + UTF8.QUOT_DBL_HIGH_REV9 + "[" + publishedYear + "]");
179
                article.setDatePublished(TimePeriodParser.parseString(titleYear));
180

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

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

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

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

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

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

    
214
                book.setVolume(volume);
215

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

    
221
        addNomRefExtension(state, name);
222

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

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

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

    
253

    
254
        return name;
255
    }
256

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

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

    
289

    
290
}
(1-1/9)