Project

General

Profile

Download (11.1 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.List;
15
import java.util.Map;
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.Extension;
25
import eu.etaxonomy.cdm.model.common.ExtensionType;
26
import eu.etaxonomy.cdm.model.common.Identifier;
27
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
30
import eu.etaxonomy.cdm.model.description.TextData;
31
import eu.etaxonomy.cdm.model.media.Media;
32
import eu.etaxonomy.cdm.model.name.IBotanicalName;
33
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
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.model.term.DefinedTerm;
43
import eu.etaxonomy.cdm.model.term.TermVocabulary;
44
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
45

    
46
/**
47
 * @author a.mueller
48
 * @since 16.06.2016
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
    @Override
63
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
64
        String line = state.getCurrentLine() + ": ";
65
        Map<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
        IBotanicalName 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
        //make genus
86
        makeGenus(state, speciesName, sec, taxon, rubiaceae);
87
    }
88

    
89

    
90
    private Classification classification;
91
    private TaxonNode rubiaceaeNode;
92

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

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

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

    
121

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

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

    
144
            refAbbrevTitle = refAbbrevTitle.trim();
145
            boolean isArticle = "A".equalsIgnoreCase(refType);
146

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

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

    
159
                }
160
                Reference article = ReferenceFactory.newArticle();
161

    
162

    
163
                //            String detail = page;
164
                name.setNomenclaturalMicroReference(detail);
165

    
166
                article.setVolume(CdmUtils.Ne(volume));
167
                article.setInReference(journal);
168

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

    
172
                article.setAuthorship(name.getCombinationAuthorship());
173

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

    
187
                Reference book = ReferenceFactory.newBook();
188
                book.setAbbrevTitle(refAbbrevTitle);
189

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

    
194
                book.setAuthorship(name.getCombinationAuthorship());
195

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

    
205
                book.setVolume(volume);
206

    
207
                //String detail = page;
208
                name.setNomenclaturalMicroReference(detail);
209
            }
210
        }
211

    
212
        addNomRefExtension(state, name);
213

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

    
231
        //add tropicos identifier
232
        String tropicosId = record.get("OutputNameID");
233
        if (isNotBlank(tropicosId)){
234
            String tropicosIdTypeLabel = "Tropicos Name Identifier";
235
            UUID uuid = DefinedTerm.uuidTropicosNameIdentifier;
236
            TermVocabulary<DefinedTerm> voc = null;  //for now it goes to user defined voc
237
            DefinedTerm identifierType = this.getIdentiferType(state, uuid, tropicosIdTypeLabel, tropicosIdTypeLabel, null, voc);
238
            Identifier<Taxon> identifier = Identifier.NewInstance(tropicosId, identifierType);
239
            name.addIdentifier(identifier);
240
        }
241

    
242
        name.addSource(makeOriginalSource(state));
243

    
244

    
245
        return name;
246
    }
247

    
248
    private String[] makeVolumeDetail(String outputCollation) {
249
        if (outputCollation == null){
250
            return new String[0];
251
        }else{
252
            String[] split = outputCollation.split(":");
253
            return split;
254
        }
255
    }
256

    
257
    private void addNomRefExtension(SimpleExcelTaxonImportState<CONFIG> state, IBotanicalName name) {
258
        String newExtensionStr = name.getFullTitleCache() + " - BORHIDI";
259
        UUID uuidNomRefExtension = MexicoConabioTransformer.uuidNomRefExtension;
260
        for (Extension extension : name.getExtensions()){
261
            if (extension.getType().getUuid().equals(uuidNomRefExtension)){
262
                extension.setValue(extension.getValue() + "\n" + newExtensionStr);
263
                return;
264
            }
265
        }
266
        String label = "Nomenclatural reference in Sources";
267
        String abbrev = "Nom. ref. src.";
268
        ExtensionType extensionType = getExtensionType(state, uuidNomRefExtension, label, label, abbrev);
269
        Extension.NewInstance((TaxonName)name, newExtensionStr, extensionType);
270
    }
271
}
(1-1/9)