Project

General

Profile

Download (10 KB) Statistics
| Branch: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2017 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.redlist.gefaesspflanzen.excel;
11

    
12
import java.util.Map;
13
import java.util.UUID;
14

    
15
import org.apache.commons.lang3.StringUtils;
16
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
22
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
23
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
24
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
25
import eu.etaxonomy.cdm.model.common.Language;
26
import eu.etaxonomy.cdm.model.name.IBotanicalName;
27
import eu.etaxonomy.cdm.model.name.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonName;
29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.Classification;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.cdm.strategy.homotypicgroup.BasionymRelationCreator;
37
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
38

    
39
/**
40
 * Import for German red list checklist for plantae.
41
 * @author a.mueller
42
 * @since 13.06.2019
43
 */
44
@Component
45
public class RedListGefaesspflanzenTaxonExcelImport<CONFIG extends RedListGefaesspflanzenExcelImportConfigurator>
46
        extends SimpleExcelTaxonImport<CONFIG> {
47

    
48
    private static final long serialVersionUID = -884838817884874228L;
49
    private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenTaxonExcelImport.class);
50

    
51
    private static final String ID_COL = "SORT_ID";
52
    private static final String UUID_COL = "TAXON_UUID";
53
    private static final String SYN_FLAG_COL = "SYN_FLAG";
54
    private static final String VOLLNAME_COL = "VOLLNAME";
55
    private static final String WISS_NAME_COL = "WISS_NAME";
56
    private static final String AUTHOR_COL = "AUTOR";
57
    private static final String RANK_COL = "RANG";
58
    private static final String ZUSATZ_COL = "ZUSATZ";
59

    
60

    
61
    private static UUID rootUuid = UUID.fromString("235ae474-227f-438a-b132-4508053fcb1c");
62
    private static UUID plantaeUuid = UUID.fromString("31bd1b7c-245a-416d-b076-aa090c7469ce");
63

    
64
    @SuppressWarnings("unchecked")
65
    private ImportDeduplicationHelper<SimpleExcelTaxonImportState<?>> deduplicationHelper
66
           = (ImportDeduplicationHelper<SimpleExcelTaxonImportState<?>>)ImportDeduplicationHelper.NewStandaloneInstance();
67

    
68
    private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
69
    private BasionymRelationCreator basionymCreator = new BasionymRelationCreator();
70

    
71

    
72
    @Override
73
    protected String getWorksheetName(CONFIG config) {
74
        return "Florenliste";
75
    }
76

    
77
    private boolean isFirst = true;
78
    private TransactionStatus tx = null;
79

    
80
    /**
81
     * {@inheritDoc}
82
     */
83
    @Override
84
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
85
        if (isFirst){
86
            tx = this.startTransaction();
87
            isFirst = false;
88
        }
89

    
90
        String line = state.getCurrentLine() + ": ";
91
        Map<String, String> record = state.getOriginalRecord();
92

    
93
        String noStr = getValue(record, ID_COL);
94

    
95
        //species
96
        TaxonBase<?> taxon = makeTaxon(state, line, record, noStr);
97

    
98
        getTaxonService().save(taxon);
99
        saveNameRelations(taxon.getName());
100
    }
101

    
102

    
103
    @Override
104
    protected void secondPass(SimpleExcelTaxonImportState<CONFIG> state) {
105
        if (tx != null){
106
            this.commitTransaction(tx);
107
            tx = null;
108
        }
109
    }
110

    
111

    
112

    
113

    
114
    /**
115
     * @param col
116
     * @return
117
     */
118
    private String getNamespace(CONFIG config) {
119
        return getWorksheetName(config)+"."+ ID_COL;
120
    }
121

    
122

    
123

    
124
    /**
125
     * @param state
126
     * @param line
127
     * @param record
128
     * @param noStr
129
     * @return
130
     */
131
    private TaxonBase<?> makeTaxon(SimpleExcelTaxonImportState<CONFIG> state, String line, Map<String, String> record,
132
            String noStr) {
133

    
134
//        TaxonNode familyTaxon = getFamilyTaxon(record, state);
135
//        if (familyTaxon == null){
136
//            logger.warn(line + "Family not created: " + record.get(FAMILIA));
137
//        }
138

    
139
        String nameStr = getValue(record, WISS_NAME_COL);
140
        String authorStr = getValue(record, AUTHOR_COL);
141
        String synFlag = getValue(record, SYN_FLAG_COL);
142
        String uuidTaxon = getValue(record, UUID_COL);
143
        String vollName = getValue(record, VOLLNAME_COL);
144
        String zusatz = getValue(record, ZUSATZ_COL);
145

    
146
        String sensuStr;
147
        if (StringUtils.isNotEmpty(zusatz) && zusatz.startsWith("s. ")){
148
            sensuStr = zusatz.split(",")[0].trim();
149
        }else {
150
           sensuStr = null;
151
        }
152
        String nomStatusStr;
153
        if (StringUtils.isNotEmpty(zusatz) && !zusatz.trim().equals(sensuStr)){
154
            nomStatusStr = sensuStr == null? zusatz.trim():zusatz.split(",")[1].trim();
155
        }else{
156
            nomStatusStr = null;
157
        }
158

    
159

    
160
        nameStr = CdmUtils.concat(" ", nameStr, authorStr);
161
        boolean isAuct = nameStr.endsWith("auct.");
162
        nameStr = normalizeNameStr(nameStr);
163

    
164
        Rank rank = Rank.SPECIES();
165
        IBotanicalName name = (IBotanicalName)parser.parseFullName(nameStr, state.getConfig().getNomenclaturalCode(), rank);
166
        name.addImportSource(noStr, getNamespace(state.getConfig()), getSourceCitation(state), null);
167
        name = deduplicationHelper.getExistingName(state, name);
168
        if (name.isProtectedTitleCache()){
169
            logger.warn(line + "Name could not be parsed: " + nameStr);
170
        }
171

    
172
        deduplicationHelper.replaceAuthorNamesAndNomRef(state, name);
173

    
174
        TaxonBase<?> taxon;
175
        if ("1".equals(synFlag) || isAuct){
176
            taxon = Taxon.NewInstance(name, getSecReference(state));
177
        }else if ("b".equals(synFlag)||"x".equals(synFlag)){
178
            taxon = Synonym.NewInstance(name, getSecReference(state));
179
        }else{
180
            logger.warn("Unknown synFlag: " + synFlag);
181
            return null;
182
        }
183
        taxon.setUuid(UUID.fromString(uuidTaxon));
184
        if (isAuct){
185
            taxon.setAppendedPhrase("auct.");  //TODO
186
        }
187
        if (sensuStr != null){
188
            taxon.setAppendedPhrase(sensuStr);
189
        }
190

    
191
        taxon.addImportSource(noStr, getNamespace(state.getConfig()), getSourceCitation(state), null);
192

    
193
        checkVollname(state, taxon, vollName, sensuStr, isAuct);
194
        return taxon;
195
    }
196

    
197

    
198

    
199
    /**
200
     * @param state
201
     * @param taxon
202
     * @param sensuStr
203
     * @param isAuct
204
     * @param vollName
205
     */
206
    private void checkVollname(SimpleExcelTaxonImportState<CONFIG> state, TaxonBase<?> taxon, String vollName, String sensuStr, boolean isAuct) {
207
        TaxonName name = taxon.getName();
208
        String titleCache = (sensuStr == null && !isAuct) ? name.getTitleCache() : taxon.getTitleCache();
209
        vollName = vollName.replace(" agg.", " aggr.").replace(" (E)", "");
210
        if (!titleCache.equals(vollName)){
211
            logger.warn("Vollname weicht ab: " + vollName +" <-> " +  titleCache);
212
        }
213
    }
214

    
215

    
216
    /**
217
     * @param nameStr
218
     */
219
    private String normalizeNameStr(String nameStr) {
220
        String result = nameStr.replace(" agg.", " aggr.").replaceAll(" auct.$", "")
221
                .replaceAll(" grex ", " subsp. ").replaceAll(" sublusus ", " subsp. ");
222
        return result;
223
    }
224

    
225
    private TaxonNode rootNode;
226
    private TaxonNode getClassification(SimpleExcelTaxonImportState<CONFIG> state) {
227
        if (rootNode == null){
228
            Reference sec = getSecReference(state);
229
            String classificationName = state.getConfig().getClassificationName();
230
            Language language = Language.DEFAULT();
231
            Classification classification = Classification.NewInstance(classificationName, sec, language);
232
            classification.setUuid(state.getConfig().getClassificationUuid());
233
            classification.getRootNode().setUuid(rootUuid);
234

    
235
            IBotanicalName plantaeName = TaxonNameFactory.NewBotanicalInstance(Rank.KINGDOM());
236
            plantaeName.setGenusOrUninomial("Plantae");
237
            Taxon plantae = Taxon.NewInstance(plantaeName, sec);
238
            TaxonNode plantaeNode = classification.addChildTaxon(plantae, null, null);
239
            plantaeNode.setUuid(plantaeUuid);
240
            getClassificationService().save(classification);
241

    
242
            rootNode = plantaeNode;
243
        }
244
        return rootNode;
245
    }
246

    
247

    
248
//    protected IBotanicalName makeFamilyName(SimpleExcelTaxonImportState<CONFIG> state, String famStr) {
249
//        IBotanicalName name = TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY());
250
//        famStr = decapitalize(famStr);
251
//        name.setGenusOrUninomial(famStr);
252
//        name.addSource(makeOriginalSource(state));
253
//        return name;
254
//    }
255

    
256
    /**
257
     * @param state
258
     * @return
259
     */
260
    @Override
261
    protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
262
        return IdentifiableSource.NewDataImportInstance(getValue(state.getOriginalRecord(),ID_COL),
263
                getNamespace(state.getConfig()), state.getConfig().getSourceReference());
264
    }
265

    
266

    
267
    protected Reference getSecReference(SimpleExcelTaxonImportState<CONFIG> state) {
268
        return state.getConfig().getSecReference();
269
    }
270

    
271
    /**
272
     * @param state
273
     * @return
274
     */
275
    protected Reference getSourceCitation(SimpleExcelTaxonImportState<CONFIG> state) {
276
        return state.getConfig().getSourceReference();
277
    }
278

    
279
    /**
280
     * @param state
281
     * @param parentStr
282
     * @return
283
     */
284
    private TaxonNode getParent(SimpleExcelTaxonImportState<CONFIG> state, String parentStr) {
285
        Taxon taxon = state.getHigherTaxon(parentStr);
286

    
287
        return taxon == null ? null : taxon.getTaxonNodes().iterator().next();
288
    }
289

    
290
}
(2-2/2)