Project

General

Profile

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

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

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

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
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"
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().getSourceReference();
80
        Taxon taxon = Taxon.NewInstance(speciesName, sec);
81
        TaxonNode rubiaceae = getHighestNode(state);
82
//        rubiaceae.addChildTaxon(taxon, null, null);
83

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

    
87
        //add tropicos identifier
88
        String tropicosId = record.get("OutputNameID");
89
        if (isNotBlank(tropicosId)){
90
            String tropicosIdTypeLabel = "Tropicos Name Identifier";
91
            UUID uuid = MexicoBorhidiTransformer.uuidTropicosNameIdentifier;
92
            TermVocabulary<DefinedTerm> voc = null;  //for now it goes to user defined voc
93
            DefinedTerm identifierType = this.getIdentiferType(state, uuid, tropicosIdTypeLabel, tropicosIdTypeLabel, null, voc);
94
            Identifier<Taxon> identifier = Identifier.NewInstance(tropicosId, identifierType);
95
            taxon.addIdentifier(identifier);
96
        }
97

    
98
    }
99

    
100

    
101
    private Classification classification;
102
    private TaxonNode rubiaceaeNode;
103
    /**
104
     * @return
105
     */
106
    private TaxonNode getHighestNode(SimpleExcelTaxonImportState<CONFIG> state) {
107
        if (rubiaceaeNode == null){
108
            MexicoBorhidiImportConfigurator config = state.getConfig();
109
            classification = Classification.NewInstance(state.getConfig().getClassificationName());
110
            classification.setUuid(config.getClassificationUuid());
111
            classification.setReference(config.getSourceReference());
112
            BotanicalName nameRubiaceae = BotanicalName.NewInstance(Rank.FAMILY());
113
            nameRubiaceae.setGenusOrUninomial("Rubiaceae");
114
            Taxon rubiaceaeTaxon = Taxon.NewInstance(nameRubiaceae, classification.getReference());
115
            rubiaceaeNode = classification.addChildTaxon(rubiaceaeTaxon, null, null);
116
            getClassificationService().save(classification);
117
        }
118
        return rubiaceaeNode;
119
    }
120

    
121
    /**
122
     * @param record
123
     * @param state
124
     * @return
125
     */
126
    private BotanicalName makeName(HashMap<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
127
        String line = state.getCurrentLine() + ": ";
128

    
129
        String fullNameStr = getValue(record, "OutputFullNameWithAuthors");
130
        String volume = getValue(record, "OutputVolume");
131
        String issue = getValue(record, "OutputIssue");
132
        String page = getValue(record, "OutputPage");
133
        String titleYear = getValue(record, "OutputTitlePageYear");
134
        String publishedYear = getValue(record, "OutputYearPublished");
135
        String refAbbrevTitle = getValue(record, "OutputAbbreviatedTitle");
136
        String outputCollation = getValue(record, "OutputCollation");
137

    
138
        BotanicalName name = (BotanicalName)nameParser.parseFullName(fullNameStr, NomenclaturalCode.ICNAFP, Rank.SPECIES());
139
        if (name.isProtectedTitleCache()){
140
            logger.warn(line + "Name could not be parsed: " + fullNameStr );
141
        }else{
142
            replaceAuthorNames(state, name);
143
        }
144

    
145
        String[] volumeDetail = makeVolumeDetail(outputCollation);
146

    
147
        if (refAbbrevTitle != null){
148
            if (volume != null){
149
                Reference journal = state.getReference(refAbbrevTitle);
150
                if (journal == null){
151
                    journal = ReferenceFactory.newJournal();
152
                    journal.setAbbrevTitle(refAbbrevTitle);
153
                    state.putReference(refAbbrevTitle, journal);
154
                }
155
                Reference article = ReferenceFactory.newArticle();
156

    
157
                //volume + issue
158
    //            if (isNotBlank(issue)){
159
    //                volume = volume + "(" + issue + ")";
160
    //            }
161
                volume = volumeDetail[0];
162
                String detail = volumeDetail.length > 1 ? volumeDetail[1] : null;
163

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

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

    
170
                titleYear = (isBlank(publishedYear)? titleYear : "\"" + titleYear + "\"[" + publishedYear + "]");
171
                article.setDatePublished(TimePeriodParser.parseString(titleYear));
172

    
173
                Reference existingArticle = state.getReference(article.getTitleCache());
174
                if (existingArticle != null){
175
                    name.setNomenclaturalReference(existingArticle);
176
                }else{
177
                    name.setNomenclaturalReference(article);
178
                    state.putReference(article.getTitleCache(), article);
179
                }
180
            }else{
181

    
182
                Reference book = ReferenceFactory.newBook();
183
                book.setAbbrevTitle(refAbbrevTitle);
184

    
185
                if (volumeDetail.length > 1){
186
                    logger.warn(line + "Book outputCollation has volume part");
187
                }
188

    
189
                //year
190
                titleYear = (isBlank(publishedYear)? titleYear : "\"" + titleYear + "\"[" + publishedYear + "]");
191
                book.setDatePublished(TimePeriodParser.parseString(titleYear));
192

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

    
202
                //micro ref
203
                String detail = outputCollation;
204
                //String detail = page;
205
                name.setNomenclaturalMicroReference(detail);
206
            }
207
        }
208

    
209
        addNomRefExtension(state, name);
210

    
211

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

    
229
        return name;
230
    }
231

    
232
    /**
233
     * @param outputCollation
234
     * @return
235
     */
236
    private String[] makeVolumeDetail(String outputCollation) {
237
        if (outputCollation == null){
238
            return new String[0];
239
        }else{
240
            String[] split = outputCollation.split(":");
241
            return split;
242
        }
243
    }
244

    
245
    /**
246
     * @param state
247
     * @param referencedName
248
     */
249
    private void addNomRefExtension(SimpleExcelTaxonImportState<CONFIG> state, BotanicalName 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(name, newExtensionStr, extensionType);
262
    }
263

    
264

    
265
}
(1-1/11)