Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2020 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.caryo;
10

    
11
import java.util.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.UUID;
15

    
16
import org.apache.logging.log4j.LogManager;
17
import org.apache.logging.log4j.Logger;
18
import org.springframework.stereotype.Component;
19

    
20
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
21
import eu.etaxonomy.cdm.common.CdmUtils;
22
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
23
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
24
import eu.etaxonomy.cdm.model.common.Annotation;
25
import eu.etaxonomy.cdm.model.common.AnnotationType;
26
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
27
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
28
import eu.etaxonomy.cdm.model.name.Rank;
29
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
32
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.term.DefinedTerm;
35
import eu.etaxonomy.cdm.strategy.exceptions.StringNotParsableException;
36
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
37

    
38
/**
39
 * @author a.mueller
40
 * @since 02.02.2023
41
 */
42
@Component
43
public class CaryoSileneaeNameImport extends CaryoSileneaeImportBase {
44

    
45
    private static final long serialVersionUID = 8931253645038029899L;
46
    private static final Logger logger = LogManager.getLogger();
47

    
48
    private static final String NOMEN_ID = "nomen_id";
49
    private static final String RANK = "rank";
50
    private static final String GENUS = "Genus";
51
    private static final String INFRAGEN_NAME = "Infragen_name";
52
    private static final String SPECIES = "Species";
53
    private static final String INFRASP_EPITHET = "Infrasp_epithet";
54
    private static final String AUTHORS = "Authors";
55

    
56
    private static final String BASIONYM_LINK = "Basionym_link";
57
    private static final String IPNI_ID = "IPNI_ID";
58
    private static final String NOTES = "Notes";
59
    private static final String NOMINVAL_FLAG = "NomInval_Flag";
60
    private static final String TYPE_SPECIMEN = "Type_specimen";
61
    private static final String TYPE_SPECIES_LINK = "TypeSpecies_link";
62

    
63
    //maybe not used
64
    private static final String PUBLICATION = "Publication";
65
    private static final String PUBL_DATE = "PublDate";
66

    
67
    //not used
68
    @SuppressWarnings("unused")
69
    private static final String HOMONYM_FLAG = "Homonym_Flag";
70
    @SuppressWarnings("unused")
71
    private static final String MISAPPLIED_FLAG = "Misapplied_Flag";
72
    @SuppressWarnings("unused")
73
    private static final String SynonymyReference_link = "SynonymyReference_link";
74

    
75
    private Map<Integer, UUID> nameMapping = new HashMap<>();
76

    
77
    private SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state;
78

    
79
    @Override
80
    protected void firstPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
81
        int line = state.getCurrentLine();
82
        if ((line % 500) == 0){
83
            newTransaction(state);
84
            System.out.println(line);
85
        }
86

    
87
        this.state = state;
88
        Map<String, String> record = state.getOriginalRecord();
89

    
90
        Integer nomenId = Integer.valueOf(getValue(record, NOMEN_ID));
91
        String rankStr = getValue(record, RANK);
92
        String genusStr = getValue(record, GENUS);
93
        String infragenStr = getValue(record, INFRAGEN_NAME);
94
        String speciesStr = getValue(record, SPECIES);
95
        String infraspStr = getValue(record, INFRASP_EPITHET);
96
        String authorsStr = getValue(record, AUTHORS);
97
        String ipniId = getValue(record, IPNI_ID);
98
        String notes = getValue(record, NOTES);
99
        String nomInvalFlag = getValue(record, NOMINVAL_FLAG);
100
        String typeSpecimenStr = getValue(record, TYPE_SPECIMEN);
101

    
102
        String row = String.valueOf(line) + "("+nomenId+"): ";
103

    
104
        try {
105
            //create name
106
            Rank rank = state.getTransformer().getRankByKey(rankStr);
107
            TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
108

    
109
            //fill simple
110
            name.setGenusOrUninomial(Ne(genusStr));
111
            name.setInfraGenericEpithet(Ne(infragenStr));
112
            name.setSpecificEpithet(Ne(speciesStr));
113
            name.setInfraSpecificEpithet(Ne(infraspStr));
114
            NonViralNameParserImpl.NewInstance().parseAuthors(name, authorsStr);
115

    
116
            //TODO ??publication + PublDate
117

    
118
            //ipni ID
119
            if (isNotBlank(ipniId)) {
120
                name.addIdentifier(ipniId, DefinedTerm.IDENTIFIER_NAME_IPNI());
121
            }
122

    
123
            //notes
124
            if (isNotBlank(notes)) {
125
                Annotation annotation = Annotation.NewDefaultLanguageInstance(notes);
126
                annotation.setAnnotationType(AnnotationType.TECHNICAL());
127
                name.addAnnotation(annotation);
128
            }
129

    
130
            //nominval flag
131
            if (nomInvalFlag != null && nomInvalFlag.trim().equalsIgnoreCase("yes")) {
132
                Reference ref = null;
133
                name.addStatus(NomenclaturalStatusType.INVALID(), ref, null);
134
            }
135

    
136
            //type specimen
137
            if (isNotBlank(typeSpecimenStr)) {
138
                DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
139
                facade.setCollector(null);  //just to create field unit and gathering event
140
                facade.innerDerivedUnit().setTitleCache(typeSpecimenStr, true);
141
                SpecimenTypeDesignationStatus status = null; //TODO
142
                Reference ref = null;
143
                String originalInfo = null;
144
                name.addSpecimenTypeDesignation(facade.innerDerivedUnit(), status, ref, null, originalInfo, false, false);
145
                // save ??
146
            }
147

    
148

    
149
        } catch (UndefinedTransformerMethodException | StringNotParsableException e) {
150
            e.printStackTrace();
151
        }
152
    }
153

    
154
    private String Ne(String genusStr) {
155
        return CdmUtils.Ne(genusStr);
156
    }
157

    
158
    private TaxonName dedupliateNameParts(TaxonName name) {
159
        if (state.getConfig().isDoDeduplicate()){
160
            state.getDeduplicationHelper().replaceAuthorNamesAndNomRef(name);
161
        }
162
        return name;
163
    }
164

    
165
    private String getOtherAuthors(List<TaxonName> otherNames) {
166
        String result = "";
167
        for (TaxonName name : otherNames){
168
            result = CdmUtils.concat(";", result, name.getAuthorshipCache());
169
        }
170
        return result;
171
    }
172

    
173

    
174
    private void handleNomenclRemarkAndNameStatus(String nomenclaturalRemarks, String row, boolean isNewName, TaxonName name,
175
            List<NomenclaturalStatusType> statusTypes) {
176

    
177
        NomenclaturalStatusType remarkType = null;
178
        NomenclaturalStatusType statusType = statusTypes.isEmpty()? null: statusTypes.iterator().next();
179
        if (nomenclaturalRemarks == null){
180
           //nothing to do
181
        }else if (", nom. illeg.".equals(nomenclaturalRemarks)){
182
            remarkType = NomenclaturalStatusType.ILLEGITIMATE();
183
        }else if (", nom. cons.".equals(nomenclaturalRemarks)){
184
            remarkType = NomenclaturalStatusType.CONSERVED();
185
        }else if (", nom. nud.".equals(nomenclaturalRemarks)){
186
            remarkType = NomenclaturalStatusType.NUDUM();
187
        }else if (", nom. provis.".equals(nomenclaturalRemarks)){
188
            remarkType = NomenclaturalStatusType.PROVISIONAL();
189
        }else if (", nom. rej.".equals(nomenclaturalRemarks)){
190
            remarkType = NomenclaturalStatusType.REJECTED();
191
        }else if (", nom. subnud.".equals(nomenclaturalRemarks)){
192
            remarkType = NomenclaturalStatusType.SUBNUDUM();
193
        }else if (", nom. superfl.".equals(nomenclaturalRemarks)){
194
            remarkType = NomenclaturalStatusType.SUPERFLUOUS();
195
        }else if (", not validly publ.".equals(nomenclaturalRemarks)){
196
            statusTypes.add(NomenclaturalStatusType.INVALID());
197
        }else if (", opus utique oppr.".equals(nomenclaturalRemarks)){
198
            statusTypes.add(NomenclaturalStatusType.OPUS_UTIQUE_OPPR());
199
        }else {
200
            logger.warn(row + "Unhandled nomenclatural remark: " + nomenclaturalRemarks);
201
        }
202

    
203
        NomenclaturalStatusType kewType = remarkType != null? remarkType : statusType;
204
        if (isNewName){
205
            if(remarkType != null && statusType != null && !remarkType.equals(statusType)){
206
                logger.warn(row + "Kew suggests 2 different nom. status. types for new name. The status from nomenclatural_remarks was taken.");
207
            }
208
            if (kewType != null){
209
                name.addStatus(kewType, getSecRef(state), null);
210
            }
211
        }else{
212
            NomenclaturalStatusType existingType = null;
213
            if (!name.getStatus().isEmpty()){
214
                existingType = name.getStatus().iterator().next().getType();
215
            }
216
            if (existingType != null && kewType != null){
217
                if (!existingType.equals(kewType)){
218
                    logger.warn(row + "Existing name status "+existingType.getTitleCache()+" differs from Kew status " + kewType.getTitleCache() + ". Key status ignored");
219
                }
220
            }else if (existingType != null && kewType == null){
221
                logger.warn(row + "Info: Existing name has a name status "+existingType.getTitleCache()+" but Kew name has no status. Existing status kept.");
222
            }else if (existingType == null && kewType != null){
223
                if(remarkType != null && statusType != null && !remarkType.equals(statusType)){
224
                    logger.warn(row + "Existing name has no status while Kew name suggests a status (but 2 different status form status and nomenclatural_remarks field).");
225
                }else{
226
                    logger.warn(row + "Existing name has no status while Kew name suggests a status ("+kewType.getTitleCache()+"). Kew status ignored.");
227
                }
228
            }
229
        }
230
    }
231

    
232
    @Override
233
    protected void secondPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
234
        Map<String, String> record = state.getOriginalRecord();
235
        int line = state.getCurrentLine();
236

    
237
        Integer nomenId = Integer.valueOf(getValue(record, NOMEN_ID));
238
        Integer basionymId = getInt(getValue(record, BASIONYM_LINK));
239
        Integer typeSpeciesId = getInt(getValue(record, TYPE_SPECIES_LINK));
240

    
241
        String row = String.valueOf(line) + "("+nomenId+"): ";
242
        if ((line % 500) == 0){
243
            newTransaction(state);
244
            System.out.println(line);
245
        }
246

    
247
        TaxonName name = getName(state, nomenId);
248
        if (name == null) {
249
            logger.warn(row + "Name does not exist");
250
            return;
251
        }
252

    
253
        //basionym
254
        if (basionymId != null) {
255
            TaxonName basionym = getName(state, basionymId);
256
            if (basionym == null) {
257
                logger.warn(row + "basionym does not exist");
258
            }else {
259
                name.addBasionym(basionym);
260
            }
261
        }
262

    
263
        //type name
264
        if (typeSpeciesId != null) {
265
            TaxonName typeSpecies = getName(state, typeSpeciesId);
266
            if (typeSpecies == null) {
267
                logger.warn(row + "typeSpecies does not exist");
268
            }else {
269
                Reference ref = null;
270
                NameTypeDesignationStatus status = null; //TODO
271
                name.addNameTypeDesignation(typeSpecies, ref, null, null, status, false);
272
            }
273
        }
274
    }
275
}
(8-8/15)