Project

General

Profile

Download (6.15 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.greece;
10

    
11
import java.util.Arrays;
12
import java.util.HashMap;
13
import java.util.List;
14
import java.util.Set;
15

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

    
19
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
20
import eu.etaxonomy.cdm.model.name.IBotanicalName;
21
import eu.etaxonomy.cdm.model.name.INonViralName;
22
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
23
import eu.etaxonomy.cdm.model.name.Rank;
24
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Classification;
28
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
32

    
33
/**
34
 * @author a.mueller
35
 * @date 14.12.2016
36
 *
37
 */
38

    
39
@Component
40
public class FloraHellenicaExcludedTaxonImport<CONFIG extends FloraHellenicaImportConfigurator>
41
            extends FloraHellenicaImportBase<CONFIG>{
42

    
43

    
44
    private static final long serialVersionUID = 2629253144140992196L;
45
    private static final Logger logger = Logger.getLogger(FloraHellenicaExcludedTaxonImport.class);
46

    
47
    private static final String TAXON = "Taxon";
48
    private static final String UNIQUE_ID = "Unique ID";
49
    private static final String FAMILY = "Family";
50

    
51
    private  static List<String> expectedKeys= Arrays.asList(new String[]{
52
            UNIQUE_ID,FAMILY,TAXON
53
    });
54

    
55
    private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
56
    private TaxonNode excludedFamilyNode;
57

    
58
    @Override
59
    protected String getWorksheetName() {
60
        return "excluded taxa";
61
    }
62

    
63
    boolean isFirst = true;
64
    /**
65
     * {@inheritDoc}
66
     */
67
    @Override
68
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
69

    
70
        String line = state.getCurrentLine() + ": ";
71
        HashMap<String, String> record = state.getOriginalRecord();
72

    
73
        Set<String> keys = record.keySet();
74
        for (String key: keys) {
75
            if (! expectedKeys.contains(key)){
76
                logger.warn(line + "Unexpected Key: " + key);
77
            }
78
        }
79
        if (isFirst){
80
            System.out.println("Start excluded taxa");
81
            isFirst = false;
82
        }
83

    
84
        String noStr = getValue(record, UNIQUE_ID);
85
        TaxonNode taxonNode = makeTaxon(state, line, record, noStr);
86
        if (taxonNode != null){
87
            state.putTaxon(noStr, taxonNode.getTaxon());
88
        }
89
    }
90

    
91

    
92
    /**
93
     * @param state
94
     * @param line
95
     * @param record
96
     * @param noStr
97
     * @return
98
     */
99
    private TaxonNode makeTaxon(SimpleExcelTaxonImportState<CONFIG> state, String line,
100
            HashMap<String, String> record,
101
            String noStr) {
102

    
103
        TaxonNode familyTaxonNode = getFamilyTaxon(record, state);
104
        familyTaxonNode = getTaxonNodeService().find(familyTaxonNode.getUuid());
105
        if (familyTaxonNode == null){
106
            logger.warn(line + "Family not created, can't add excluded taxon: " + record.get(FAMILY));
107
            return null;
108
        }
109

    
110
        String taxonStr = getValue(record, TAXON);
111
        INonViralName name = parser.parseFullName(taxonStr, NomenclaturalCode.ICNAFP, null);
112
        name = replaceNameAuthorsAndReferences(state, name);
113
        if (name.isProtectedTitleCache()){
114
            logger.warn(line + "Name could not be parsed: " + taxonStr);
115
        }
116

    
117
        Taxon taxon = Taxon.NewInstance(name, getSecReference(state));
118
        taxon.addImportSource(noStr, getWorksheetName(), getSourceCitation(state), null);
119
        TaxonNode excludedNode = familyTaxonNode.addChildTaxon(taxon, getSecReference(state), null);
120
        excludedNode.setExcluded(true);
121
        getTaxonNodeService().saveOrUpdate(excludedNode);
122
        return excludedNode;
123
    }
124

    
125

    
126

    
127
   /**
128
     * @param record
129
     * @param state
130
     * @return
131
     */
132
    private TaxonNode getFamilyTaxon(HashMap<String, String> record,
133
            SimpleExcelTaxonImportState<CONFIG> state) {
134

    
135
        String familyStr = getValue(record, FAMILY);
136
        if (familyStr == null){
137
            return null;
138
        }
139
        familyStr = familyStr.trim();
140

    
141
//        Taxon family = state.getHigherTaxon(familyStr);
142
        Taxon family = this.getHigherTaxon(record, state, FAMILY);
143
        TaxonNode familyNode;
144
        if (family != null){
145
            familyNode = family.getTaxonNodes().iterator().next();
146
        }else{
147
            IBotanicalName name = makeFamilyName(state, familyStr);
148
            name = replaceNameAuthorsAndReferences(state, name);
149

    
150
            Reference sec = getSecReference(state);
151
            family = Taxon.NewInstance(name, sec);
152

    
153
            ITaxonTreeNode groupNode = getExcludedFamilyTaxon(state);
154
            familyNode = groupNode.addChildTaxon(family, sec, null);
155
            state.putHigherTaxon(familyStr, family);
156
            getTaxonNodeService().saveOrUpdate(familyNode);
157
//            logger.warn(state.getCurrentLine() +": " + "Family not found for excluded taxon");
158
        }
159
        return familyNode;
160
    }
161

    
162

    
163
    private ITaxonTreeNode getExcludedFamilyTaxon(
164
            SimpleExcelTaxonImportState<CONFIG> state) {
165

    
166
        if (excludedFamilyNode != null){
167
            return this.excludedFamilyNode;
168
        }
169
        Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
170
        TaxonNode plantae = classification.getChildNodes().iterator().next();
171

    
172
        TaxonNameBase<?,?> name = TaxonNameFactory.NewBotanicalInstance(Rank.SUPERFAMILY());
173
        name.setTitleCache("Excluded", true);
174
        Taxon taxon = Taxon.NewInstance(name, getSecReference(state));
175
        excludedFamilyNode = plantae.addChildTaxon(taxon, getSourceCitation(state), null);
176
        excludedFamilyNode.setExcluded(true);
177
        getTaxonNodeService().saveOrUpdate(excludedFamilyNode);
178
        return excludedFamilyNode;
179
    }
180

    
181
}
(3-3/11)