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
import org.springframework.transaction.TransactionStatus;
19

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

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

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

    
44

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

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

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

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

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

    
64
    private boolean isFirst = true;
65
    private TransactionStatus tx = null;
66
    /**
67
     * {@inheritDoc}
68
     */
69
    @Override
70
    protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
71
        if (isFirst){
72
            tx = this.startTransaction();
73
            isFirst = false;
74
        }
75

    
76
        String line = state.getCurrentLine() + ": ";
77
        HashMap<String, String> record = state.getOriginalRecord();
78

    
79
        Set<String> keys = record.keySet();
80
        for (String key: keys) {
81
            if (! expectedKeys.contains(key)){
82
                logger.warn(line + "Unexpected Key: " + key);
83
            }
84
        }
85

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

    
93
    @Override
94
    protected void secondPass(SimpleExcelTaxonImportState<CONFIG> state) {
95
        if (tx != null){
96
            this.commitTransaction(tx);
97
            tx = null;
98
        }
99
    }
100

    
101

    
102
    /**
103
     * @param state
104
     * @param line
105
     * @param record
106
     * @param noStr
107
     * @return
108
     */
109
    private TaxonNode makeTaxon(SimpleExcelTaxonImportState<CONFIG> state, String line,
110
            HashMap<String, String> record,
111
            String noStr) {
112

    
113
        TaxonNode familyTaxon = getFamilyTaxon(record, state);
114
        if (familyTaxon == null){
115
            logger.warn(line + "Family not created, can't add excluded taxon: " + record.get(FAMILY));
116
            return null;
117
        }
118

    
119
        String taxonStr = getValue(record, TAXON);
120
        INonViralName name = parser.parseFullName(taxonStr, NomenclaturalCode.ICNAFP, null);
121
        if (name.isProtectedTitleCache()){
122
            logger.warn(line + "Name could not be parsed: " + taxonStr);
123
        }
124

    
125
        Taxon taxon = Taxon.NewInstance(name, getSecReference(state));
126
        taxon.addImportSource(noStr, getWorksheetName(), getSourceCitation(state), null);
127
        TaxonNode excludedNode = familyTaxon.addChildTaxon(taxon, getSecReference(state), null);
128
        excludedNode.setExcluded(true);
129
        getTaxonNodeService().saveOrUpdate(excludedNode);
130
        return excludedNode;
131
    }
132

    
133

    
134
   /**
135
     * @param record
136
     * @param state
137
     * @return
138
     */
139
    private TaxonNode getFamilyTaxon(HashMap<String, String> record,
140
            SimpleExcelTaxonImportState<CONFIG> state) {
141

    
142
        String familyStr = getValue(record, FAMILY);
143
        if (familyStr == null){
144
            return null;
145
        }
146
        familyStr = familyStr.trim();
147

    
148
        Taxon family = state.getHigherTaxon(familyStr);
149
        TaxonNode familyNode;
150
        if (family != null){
151
            familyNode = family.getTaxonNodes().iterator().next();
152
        }else{
153
            BotanicalName name = makeFamilyName(state, familyStr);
154
            Reference sec = getSecReference(state);
155
            family = Taxon.NewInstance(name, sec);
156

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

    
166

    
167
    private ITaxonTreeNode getExcludedFamilyTaxon(
168
            SimpleExcelTaxonImportState<CONFIG> state) {
169

    
170
        if (excludedFamilyNode != null){
171
            return this.excludedFamilyNode;
172
        }
173
        Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
174
        TaxonNode plantae = classification.getChildNodes().iterator().next();
175

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

    
185
}
(2-2/8)