Project

General

Profile

Download (6.33 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.HashMap;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
19
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImport;
20
import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
21
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
22
import eu.etaxonomy.cdm.model.description.TaxonDescription;
23
import eu.etaxonomy.cdm.model.name.IBotanicalName;
24
import eu.etaxonomy.cdm.model.name.INonViralName;
25
import eu.etaxonomy.cdm.model.name.Rank;
26
import eu.etaxonomy.cdm.model.name.TaxonName;
27
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28
import eu.etaxonomy.cdm.model.reference.Reference;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30

    
31
/**
32
 * @author a.mueller
33
 * @since 14.12.2016
34
 */
35

    
36
public abstract class FloraHellenicaImportBase<CONFIG extends FloraHellenicaImportConfigurator>
37
            extends SimpleExcelTaxonImport<CONFIG>{
38

    
39
    private static final long serialVersionUID = 2593130403213346396L;
40
    private static final Logger logger = Logger.getLogger(FloraHellenicaImportBase.class);
41

    
42
    private Map<UUID, Taxon> acceptedTaxonMap = new HashMap<>();
43
    private Reference sourceReference;
44
    private Reference secReference;
45
    private Reference secReference2;
46

    
47
    @SuppressWarnings("unchecked")
48
    private ImportDeduplicationHelper<SimpleExcelTaxonImportState<?>> deduplicationHelper = (ImportDeduplicationHelper<SimpleExcelTaxonImportState<?>>)ImportDeduplicationHelper.NewInstance(this);
49

    
50

    
51

    
52
    /**
53
     * @param taxon
54
     * @return
55
     */
56
    protected TaxonDescription getTaxonDescription(Taxon taxon) {
57
        if (!taxon.getDescriptions().isEmpty()){
58
            return taxon.getDescriptions().iterator().next();
59
        }else{
60
            TaxonDescription desc = TaxonDescription.NewInstance(taxon);
61
            desc.setDefault(true);
62
            return desc;
63
        }
64
    }
65

    
66

    
67
    /**
68
     * @param state
69
     * @return
70
     */
71
    protected Reference getSourceCitation(SimpleExcelTaxonImportState<CONFIG> state) {
72
        if (this.sourceReference == null){
73
            this.sourceReference = getPersistentReference(state.getConfig().getSourceReference());
74
        }
75
        return this.sourceReference;
76
    }
77

    
78

    
79
    protected Reference getSecReference(SimpleExcelTaxonImportState<CONFIG> state) {
80
        if (this.secReference == null){
81
            this.secReference = getPersistentReference(state.getConfig().getSecReference());
82
        }
83
        return this.secReference;
84
    }
85

    
86
    protected Reference getSecReference2(SimpleExcelTaxonImportState<CONFIG> state) {
87
        if (this.secReference2 == null){
88
            this.secReference2 = getPersistentReference(state.getConfig().getSecReference2());
89
        }
90
        return this.secReference2;
91
    }
92

    
93
    /**
94
     * @param reference
95
     * @return
96
     */
97
    private Reference getPersistentReference(Reference reference) {
98
        Reference result = getReferenceService().find(reference.getUuid());
99
        if (result == null){
100
            result = reference;
101
        }
102
        return result;
103
    }
104

    
105

    
106
    /**
107
     * @param record
108
     * @param state
109
     * @return
110
     */
111
    protected Taxon getAcceptedTaxon(Map<String, String> record,
112
            SimpleExcelTaxonImportState<CONFIG> state, String key) {
113

    
114
        String accStr = getValue(record, key);
115
        if (accStr == null){
116
            return null;
117
        }
118
        accStr = accStr.trim();
119

    
120
        Taxon accTaxon = state.getTaxon(accStr);
121
        if (accTaxon == null){
122
            String message = state.getCurrentLine()+  ": Accepted taxon could not be found: " + accStr;
123
            logger.warn(message);
124
            return null;
125
        }else{
126
            initAcceptedTaxonMap();
127
//            accTaxon = (Taxon)getTaxonService().find(accTaxon.getUuid());
128
            accTaxon = acceptedTaxonMap.get(accTaxon.getUuid());
129
        }
130
        return accTaxon;
131
    }
132

    
133
    private void initAcceptedTaxonMap() {
134
        if (acceptedTaxonMap.isEmpty()){
135
            List<Taxon> list = getTaxonService().list(Taxon.class, null, null, null, null);
136
            for (Taxon taxon : list){
137
                acceptedTaxonMap.put(taxon.getUuid(), taxon);
138
            }
139
        }
140
    }
141

    
142
    /**
143
     * @param record
144
     * @param state
145
     * @return
146
     */
147
    protected Taxon getHigherTaxon(Map<String, String> record,
148
            SimpleExcelTaxonImportState<CONFIG> state, String key) {
149

    
150
        String accStr = getValue(record, key);
151
        if (accStr == null){
152
            return null;
153
        }
154
        accStr = accStr.trim();
155

    
156
        Taxon accTaxon = state.getHigherTaxon(accStr);
157
        if (accTaxon == null){
158
            String message = state.getCurrentLine()+  ": Higher taxon could not be found: " + accStr;
159
            logger.info(message); //not critical
160
            return null;
161
        }else{
162
            initAcceptedTaxonMap();
163
//            accTaxon = (Taxon)getTaxonService().find(accTaxon.getUuid());
164
            accTaxon = acceptedTaxonMap.get(accTaxon.getUuid());
165
        }
166
        return accTaxon;
167
    }
168

    
169

    
170
    protected IBotanicalName makeFamilyName(SimpleExcelTaxonImportState<CONFIG> state,
171
            String famStr) {
172
        TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.FAMILY());
173
        famStr = famStr.substring(0,1).toUpperCase() + famStr.substring(1).toLowerCase();
174
        name.setGenusOrUninomial(famStr);
175
        name.addSource(makeOriginalSource(state));
176
        return name;
177
    }
178

    
179

    
180
    /**
181
     * @param state
182
     * @param name
183
     * @return
184
     */
185
    protected <NAME extends INonViralName> NAME replaceNameAuthorsAndReferences(SimpleExcelTaxonImportState<CONFIG> state, NAME name) {
186
        NAME result = deduplicationHelper.getExistingName(state, name);
187
        deduplicationHelper.replaceAuthorNamesAndNomRef(state, result);
188
        return result;
189
    }
190

    
191

    
192
    /**
193
     * @param state
194
     * @return
195
     */
196
    @Override
197
    protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
198
        return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, getSourceCitation(state));
199
    }
200

    
201
}
(6-6/16)