Project

General

Profile

Download (10.6 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.URISyntaxException;
12
import java.util.Arrays;
13
import java.util.List;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.UUID;
17

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

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.common.URI;
24
import eu.etaxonomy.cdm.common.UTF8;
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.media.ExternalLinkType;
29
import eu.etaxonomy.cdm.model.name.IBotanicalName;
30
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
31
import eu.etaxonomy.cdm.model.name.Rank;
32
import eu.etaxonomy.cdm.model.name.TaxonName;
33
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
36
import eu.etaxonomy.cdm.model.taxon.Classification;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
39
import eu.etaxonomy.cdm.model.term.IdentifierType;
40
import eu.etaxonomy.cdm.model.term.TermVocabulary;
41
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
42

    
43
/**
44
 * @author a.mueller
45
 * @since 16.06.2016
46
 */
47
@Component
48
public class MexicoBorhidiExcelImport<CONFIG extends MexicoBorhidiImportConfigurator>
49
        extends SimpleExcelTaxonImport<CONFIG>{
50

    
51
    private static final Logger logger = LogManager.getLogger();
52
    private static final long serialVersionUID = -3607776356577606657L;
53

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

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

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

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

    
73
        //Name
74
        IBotanicalName speciesName = makeName(record, state);
75

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

    
81
        taxon.addSource(makeOriginalSource(state));
82

    
83
        //make genus
84
        makeGenus(state, speciesName, sec, taxon, rubiaceae);
85
    }
86

    
87

    
88
    private Classification classification;
89
    private TaxonNode rubiaceaeNode;
90

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

    
106
    private IBotanicalName makeName(Map<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
107
        String line = state.getCurrentLine() + ": ";
108

    
109
        String fullNameStr = getValue(record, "OutputFullNameWithAuthors");
110
//        String volume = getValue(record, "OutputVolume");
111
//        String issue = getValue(record, "OutputIssue");
112
//        String page = getValue(record, "OutputPage");
113
        String titleYear = getValue(record, "OutputTitlePageYear");
114
        String publishedYear = getValue(record, "OutputYearPublished");
115
        String refAbbrevTitle = getValue(record, "OutputAbbreviatedTitle");
116
        String outputCollation = getValue(record, "OutputCollation");
117
        String refType = getValue(record, "RefType");
118

    
119

    
120
        TaxonName name = (TaxonName)nameParser.parseFullName(fullNameStr, NomenclaturalCode.ICNAFP, Rank.SPECIES());
121
        if (name.isProtectedTitleCache()){
122
            //for the 2 ined. names
123
            name = (TaxonName)nameParser.parseReferencedName(fullNameStr, NomenclaturalCode.ICNAFP, Rank.SPECIES());
124
        }
125
        if (name.isProtectedTitleCache()){
126
            logger.warn(line + "Name could not be parsed: " + fullNameStr );
127
        }else{
128
            replaceAuthorNamesAndNomRef(state, name);
129
        }
130

    
131
        if (refAbbrevTitle != null){
132
            String[] volumeDetail = makeVolumeDetail(outputCollation);
133
            String detail;
134
            String volume = null;
135
            if (volumeDetail.length > 1){
136
                volume = volumeDetail[0].trim();
137
                detail = volumeDetail[1].trim();
138
            }else{
139
                detail = volumeDetail[0].trim();
140
            }
141

    
142
            refAbbrevTitle = refAbbrevTitle.trim();
143
            boolean isArticle = "A".equalsIgnoreCase(refType);
144

    
145
            if (isArticle){
146
                if (! "A".equalsIgnoreCase(refType)){
147
                    logger.warn(line + "RefType problem with article " + refType);
148
                }
149

    
150
                Reference journal = state.getReference(refAbbrevTitle);
151
                if (journal == null){
152
                    journal = ReferenceFactory.newJournal();
153
                    journal.setAbbrevTitle(refAbbrevTitle);
154
                    state.putReference(refAbbrevTitle, journal);
155
                    journal.addSource(makeOriginalSource(state));
156

    
157
                }
158
                Reference article = ReferenceFactory.newArticle();
159

    
160

    
161
                //            String detail = page;
162
                name.setNomenclaturalMicroReference(detail);
163

    
164
                article.setVolume(CdmUtils.Ne(volume));
165
                article.setInReference(journal);
166

    
167
                titleYear = (isBlank(publishedYear)? titleYear : UTF8.QUOT_DBL_LOW9 + titleYear + UTF8.QUOT_DBL_HIGH_REV9 + "[" + publishedYear + "]");
168
                article.setDatePublished(TimePeriodParser.parseStringVerbatim(titleYear));
169

    
170
                article.setAuthorship(name.getCombinationAuthorship());
171

    
172
                Reference existingArticle = state.getReference(article.getTitleCache());
173
                if (existingArticle != null){
174
                    name.setNomenclaturalReference(existingArticle);
175
                }else{
176
                    name.setNomenclaturalReference(article);
177
                    state.putReference(article.getTitleCache(), article);
178
                    article.addSource(makeOriginalSource(state));
179
                }
180
            }else{
181
                if (! "B".equalsIgnoreCase(refType)){
182
                    logger.warn(line + "RefType problem with book" + refType);
183
                }
184

    
185
                Reference book = ReferenceFactory.newBook();
186
                book.setAbbrevTitle(refAbbrevTitle);
187

    
188
                //year
189
                titleYear = (isBlank(publishedYear)? titleYear : UTF8.QUOT_DBL_LOW9 + titleYear + UTF8.QUOT_DBL_HIGH_REV9 + "[" + publishedYear + "]");
190
                book.setDatePublished(TimePeriodParser.parseStringVerbatim(titleYear));
191

    
192
                book.setAuthorship(name.getCombinationAuthorship());
193

    
194
                //deduplicate
195
                Reference existingBook = state.getReference(book.getTitleCache());
196
                if (existingBook != null){
197
                    name.setNomenclaturalReference(existingBook);
198
                }else{
199
                    name.setNomenclaturalReference(book);
200
                    state.putReference(book.getTitleCache(), book);
201
                }
202

    
203
                book.setVolume(volume);
204

    
205
                //String detail = page;
206
                name.setNomenclaturalMicroReference(detail);
207
            }
208
        }
209

    
210
        addNomRefExtension(state, name);
211

    
212
        //add protologue
213
        String bhlLink = record.get("OutputBHLLink");
214
        if (isNotBlank(bhlLink)){
215
            try {
216
                URI uri = new URI(bhlLink);
217
                name.addProtologue(uri, null, ExternalLinkType.WebSite);
218
            } catch (URISyntaxException e) {
219
                logger.warn(line + "URI could not be parsed: " + e.getMessage());
220
            }
221
        }
222

    
223
        //add tropicos identifier
224
        String tropicosId = record.get("OutputNameID");
225
        if (isNotBlank(tropicosId)){
226
            String tropicosIdTypeLabel = "Tropicos Name Identifier";
227
            UUID uuid = IdentifierType.uuidTropicosNameIdentifier;
228
            TermVocabulary<IdentifierType> voc = null;  //for now it goes to user defined voc
229
            IdentifierType identifierType = this.getIdentiferType(state, uuid, tropicosIdTypeLabel, tropicosIdTypeLabel, null, voc);
230
            Identifier identifier = Identifier.NewInstance(tropicosId, identifierType);
231
            name.addIdentifier(identifier);
232
        }
233

    
234
        name.addSource(makeOriginalSource(state));
235

    
236

    
237
        return name;
238
    }
239

    
240
    private String[] makeVolumeDetail(String outputCollation) {
241
        if (outputCollation == null){
242
            return new String[0];
243
        }else{
244
            String[] split = outputCollation.split(":");
245
            return split;
246
        }
247
    }
248

    
249
    private void addNomRefExtension(SimpleExcelTaxonImportState<CONFIG> state, IBotanicalName name) {
250
        String newExtensionStr = name.getFullTitleCache() + " - BORHIDI";
251
        UUID uuidNomRefExtension = MexicoConabioTransformer.uuidNomRefExtension;
252
        for (Extension extension : name.getExtensions()){
253
            if (extension.getType().getUuid().equals(uuidNomRefExtension)){
254
                extension.setValue(extension.getValue() + "\n" + newExtensionStr);
255
                return;
256
            }
257
        }
258
        String label = "Nomenclatural reference in Sources";
259
        String abbrev = "Nom. ref. src.";
260
        ExtensionType extensionType = getExtensionType(state, uuidNomRefExtension, label, label, abbrev);
261
        Extension.NewInstance((TaxonName)name, newExtensionStr, extensionType);
262
    }
263
}
(1-1/26)