Project

General

Profile

Download (11.4 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.Map;
12
import java.util.UUID;
13

    
14
import org.apache.logging.log4j.LogManager;
15
import org.apache.logging.log4j.Logger;
16
import org.springframework.stereotype.Component;
17

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

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

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

    
46
    private static final UUID uuidSileneaeInfoNameIdType = UUID.fromString("95ecbf6d-521d-447f-bae5-d82585ff3617");
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
    @Override
76
    protected String getWorksheetName(CaryoSileneaeImportConfigurator config) {
77
        return "Names";
78
    }
79

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

    
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 publicationStr = getValue(record, PUBLICATION);
97
        String publDateStr = getValue(record, PUBL_DATE);
98
        String authorsStr = getValue(record, AUTHORS);
99
        String ipniId = getValue(record, IPNI_ID);
100
        String notes = getValue(record, NOTES);
101
        String nomInvalFlag = getValue(record, NOMINVAL_FLAG);
102
        String typeSpecimenStr = getValue(record, TYPE_SPECIMEN);
103

    
104
        String row = String.valueOf(line) + "("+nomenId+"): ";
105

    
106
        try {
107
            //create name
108
            Rank rank = state.getTransformer().getRankByKey(rankStr);
109
            if (rank == null) {
110
                logger.warn(row + "rank not recognized: " + rankStr);
111
            }
112
            TaxonName name = TaxonNameFactory.NewBotanicalInstance(rank);
113

    
114
            //fill simple
115
            name.setGenusOrUninomial(Ne(genusStr));
116
            name.setInfraGenericEpithet(Ne(infragenStr));
117
            if (speciesStr != null && speciesStr.startsWith("×")) {
118
                name.setBinomHybrid(true);
119
                speciesStr = speciesStr.replace("×", "").trim();
120
            }
121
            name.setSpecificEpithet(Ne(speciesStr));
122
            name.setInfraSpecificEpithet(Ne(infraspStr));
123
            try {
124
                NonViralNameParserImpl.NewInstance().parseAuthors(name, authorsStr);
125
            } catch (Exception e) {
126
                name.setAuthorshipCache(authorsStr, true);
127
                logger.warn(row + "authorship not parsable: " + authorsStr);
128
            }
129

    
130
            //publication
131
            publicationStr = normalizePublication(publicationStr);
132
            if (isNotBlank(publicationStr) || isNotBlank(publDateStr)) {
133
                DoubleResult<String, String> publ = new DoubleResult<>(publicationStr, publDateStr);
134
                origPublicationMap.put(nomenId, publ);
135
            }
136

    
137
            //ipni ID
138
            if (isNotBlank(ipniId)) {
139
                name.addIdentifier(ipniId, IdentifierType.IDENTIFIER_NAME_IPNI());
140
            }
141

    
142
            //add ID
143
            IdentifierType sileneaeInfoNameIdType = getIdentiferType(state,
144
                    uuidSileneaeInfoNameIdType, null, null, null, null);
145
            name.addIdentifier(nomenId.toString(), sileneaeInfoNameIdType);
146

    
147
            //notes
148
            if (isNotBlank(notes)) {
149
                handleNotes(name, notes, row);
150
            }
151

    
152
            //nom.inval flag
153
            if (nomInvalFlag != null && nomInvalFlag.trim().equalsIgnoreCase("yes")) {
154
                Reference ref = null;
155
                name.addStatus(NomenclaturalStatusType.INVALID(), ref, null);
156
            }
157

    
158
            //type specimen
159
            if (isNotBlank(typeSpecimenStr)) {
160
                DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
161
                facade.setCollector(null);  //just to create field unit and gathering event
162
                facade.innerFieldUnit().setTitleCache("Field Unit for: " + typeSpecimenStr, true);
163
                facade.innerDerivedUnit().setTitleCache(typeSpecimenStr, true);
164
                SpecimenTypeDesignationStatus status = SpecimenTypeDesignationStatus.UNSPECIFIC();
165
                Reference ref = null;
166
                String originalInfo = null;
167
                name.addSpecimenTypeDesignation(facade.innerDerivedUnit(), status, ref, null, originalInfo, false, false);
168
                // save ??
169
            }
170

    
171
            Reference sourceRef = getSourceReference(state);
172
            name.addImportSource(nomenId.toString(), "Names.nomen_ID", sourceRef, "row " + String.valueOf(line));
173

    
174
            putToNameMap(nomenId, name);
175

    
176
        } catch (UndefinedTransformerMethodException e) {
177
            e.printStackTrace();
178
        }
179
    }
180

    
181
    private String normalizePublication(String publicationStr) {
182
        if (isBlank(publicationStr)) {
183
            return null;
184
        }
185
        if ("-".equalsIgnoreCase(publicationStr)) {
186
            return null;
187
        }
188
        if ("?".equalsIgnoreCase(publicationStr)) {
189
            return null;
190
        }
191
        if ("??".equalsIgnoreCase(publicationStr)) {
192
            return null;
193
        }
194
        if ("none".equalsIgnoreCase(publicationStr)) {
195
            return null;
196
        }
197
        return publicationStr;
198
    }
199

    
200
    private String Ne(String genusStr) {
201
        return CdmUtils.Ne(genusStr);
202
    }
203

    
204
    private String handleNotes(TaxonName name, String notes, String row) {
205

    
206
        NomenclaturalStatusType remarkType = null;
207
        if (notes == null){
208
           //nothing to do
209
        }else if ("ined".equals(notes)){
210
            remarkType = NomenclaturalStatusType.INED();
211
            notes = null;
212
        }else if ("nom. utique rej.".equals(notes)){
213
            remarkType = NomenclaturalStatusType.UTIQUE_REJECTED();
214
            notes = null;
215
        }else if ("nomen nudum".equalsIgnoreCase(notes)){
216
            remarkType = NomenclaturalStatusType.NUDUM();
217
            notes = null;
218
        }else if (notes.startsWith("nomen nudum") || notes.startsWith("Nomen nudum") ){
219
            remarkType = NomenclaturalStatusType.NUDUM();
220
        }else if (notes.startsWith("nom. illeg.")){
221
            remarkType = NomenclaturalStatusType.ILLEGITIMATE();
222
        }else if (notes.startsWith("nom. inval")){
223
            remarkType = NomenclaturalStatusType.INVALID();
224
        }else if ("Nom. rej.".equals(notes)){
225
            remarkType = NomenclaturalStatusType.REJECTED();
226
            notes = null;
227
        }
228

    
229
        //annotation
230
        if (isNotBlank(notes)) {
231
            Annotation annotation = Annotation.NewDefaultLanguageInstance(notes);
232
            annotation.setAnnotationType(AnnotationType.TECHNICAL());
233
            name.addAnnotation(annotation);
234
        }
235

    
236
        //nom. status.
237
        if (remarkType != null) {
238
            if (!name.hasStatus(remarkType)) {
239

    
240
                Reference ref = null;
241
                name.addStatus(remarkType, ref, null);
242
                if (name.getStatus().size() > 1) {
243
                    logger.warn(row + "name has >1 status: " + name.getFullTitleCache());
244
                }
245
            }
246
        }
247

    
248
        return notes;
249
    }
250

    
251
    @Override
252
    protected void secondPass(SimpleExcelTaxonImportState<CaryoSileneaeImportConfigurator> state) {
253
        Map<String, String> record = state.getOriginalRecord();
254
        int line = state.getCurrentLine();
255

    
256
        Integer nomenId = Integer.valueOf(getValue(record, NOMEN_ID));
257
        Integer basionymId = getInt(getValue(record, BASIONYM_LINK));
258
        Integer typeSpeciesId = getInt(getValue(record, TYPE_SPECIES_LINK));
259

    
260
        String row = String.valueOf(line) + "("+nomenId+"): ";
261
//        if ((line % 500) == 0){
262
//            newTransaction(state);
263
//            System.out.println(line);
264
//        }
265

    
266
        TaxonName name = getName(nomenId);
267
        if (name == null) {
268
            logger.warn(row + "Name does not exist");
269
            return;
270
        }
271

    
272
        //basionym
273
        if (basionymId != null && !basionymId.equals(nomenId)) {
274
            TaxonName basionym = getName(basionymId);
275
            if (basionym == null) {
276
                logger.warn(row + "basionym does not exist");
277
            }else {
278
                name.addBasionym(basionym);
279
            }
280
        }
281

    
282
        //type name
283
        if (typeSpeciesId != null) {
284
            TaxonName typeSpecies = getName(typeSpeciesId);
285
            if (typeSpecies == null) {
286
                logger.warn(row + "typeSpecies does not exist");
287
            }else {
288
                Reference ref = null;
289
                NameTypeDesignationStatus status = null; // NameTypeDesignationStatus.NOT_APPLICABLE(); //TODO minor NameTypeDesignationStatus
290
                name.addNameTypeDesignation(typeSpecies, ref, null, null, status, false);
291
            }
292
        }
293
    }
294
}
(8-8/18)