Project

General

Profile

Download (9.26 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.mexico;
10

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

    
17
import org.apache.commons.lang.StringUtils;
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
22
import eu.etaxonomy.cdm.io.excel.common.ExcelImporterBase;
23
import eu.etaxonomy.cdm.model.agent.Person;
24
import eu.etaxonomy.cdm.model.agent.Team;
25
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
26
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
27
import eu.etaxonomy.cdm.model.name.BotanicalName;
28
import eu.etaxonomy.cdm.model.name.Rank;
29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
30
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
35
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
36

    
37
/**
38
 * Simple Excel import class that works without default state class
39
 * {@link SimpleExcelTaxonImportState}
40
 * @author a.mueller
41
 * @date 16.06.2016
42
 */
43
public abstract class SimpleExcelTaxonImport<CONFIG extends ExcelImportConfiguratorBase>
44
        extends ExcelImporterBase<SimpleExcelTaxonImportState<CONFIG>>{
45

    
46
    private static final long serialVersionUID = -4345647703312616421L;
47

    
48
    private static final Logger logger = Logger.getLogger(SimpleExcelTaxonImport.class);
49

    
50
    protected static INonViralNameParser<?> nameParser = NonViralNameParserImpl.NewInstance();
51

    
52

    
53
    @Override
54
    protected void analyzeRecord(HashMap<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
55
        //override only if needed
56
    }
57

    
58
    @Override
59
    protected void secondPass(SimpleExcelTaxonImportState<CONFIG> state) {
60
        //override only if needed
61
    }
62

    
63
    @Override
64
    protected boolean isIgnore(SimpleExcelTaxonImportState<CONFIG> state) {
65
        return false;
66
    }
67

    
68
//***************************** METHODS *********************************/
69
    /**
70
     * Returns the value of the record map for the given key.
71
     * The value is trimmed and empty values are set to <code>null</code>.
72
     * @param record
73
     * @param originalKey
74
     * @return the value
75
     */
76
    protected String getValue(Map<String, String> record, String originalKey) {
77
        String value = record.get(originalKey);
78
        if (! StringUtils.isBlank(value)) {
79
            if (logger.isDebugEnabled()) { logger.debug(originalKey + ": " + value); }
80
            value = CdmUtils.removeDuplicateWhitespace(value.trim()).toString();
81
            return value;
82
        }else{
83
            return null;
84
        }
85
    }
86

    
87
    /**
88
     * @param state
89
     * @return
90
     */
91
    protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
92
        return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, state.getConfig().getSourceReference());
93
    }
94

    
95
    /**
96
     * @param state
97
     * @param speciesName
98
     * @param sec
99
     * @param taxon
100
     * @param familyNode
101
     */
102
    protected void makeGenus(SimpleExcelTaxonImportState<CONFIG> state,
103
            BotanicalName speciesName,
104
            Reference sec,
105
            Taxon taxon,
106
            TaxonNode familyNode) {
107

    
108
        TaxonNode higherNode;
109
        if (speciesName.isProtectedTitleCache()){
110
            higherNode = familyNode;
111
        }else{
112
            String genusStr = speciesName.getGenusOrUninomial();
113
            Taxon genus = state.getHigherTaxon(genusStr);
114
            if (genus != null){
115
                higherNode = genus.getTaxonNodes().iterator().next();
116
            }else{
117
                BotanicalName genusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
118
                genusName.addSource(makeOriginalSource(state));
119
                genusName.setGenusOrUninomial(genusStr);
120
                genus = Taxon.NewInstance(genusName, sec);
121
                genus.addSource(makeOriginalSource(state));
122
                higherNode = familyNode.addChildTaxon(genus, null, null);
123
                state.putHigherTaxon(genusStr, genus);
124
            }
125
        }
126

    
127
        higherNode.addChildTaxon(taxon, null, null);
128
    }
129

    
130
    /**
131
     * @param line
132
     * @param keys
133
     * @param expectedKeys
134
     */
135
    protected void checkAllKeysExist(String line, Set<String> keys, List<String> expectedKeys) {
136
        for (String key: keys) {
137
            if (! expectedKeys.contains(key)){
138
                logger.warn(line + "Unexpected Key: " + key);
139
            }
140
        }
141
    }
142

    
143

    
144
    /**
145
     * @param state
146
     * @param name
147
     */
148
    protected void replaceAuthorNamesAndNomRef(SimpleExcelTaxonImportState<CONFIG> state,
149
            BotanicalName name) {
150
        TeamOrPersonBase<?> combAuthor = name.getCombinationAuthorship();
151
        name.setCombinationAuthorship(getExistingAuthor(state, combAuthor));
152

    
153
        TeamOrPersonBase<?> exAuthor = name.getExCombinationAuthorship();
154
        name.setExCombinationAuthorship(getExistingAuthor(state, exAuthor));
155

    
156
        TeamOrPersonBase<?> basioAuthor = name.getBasionymAuthorship();
157
        name.setBasionymAuthorship(getExistingAuthor(state, basioAuthor));
158

    
159
        TeamOrPersonBase<?> exBasioAuthor = name.getExBasionymAuthorship();
160
        name.setExBasionymAuthorship(getExistingAuthor(state, exBasioAuthor));
161

    
162
        INomenclaturalReference nomRef = name.getNomenclaturalReference();
163
        if (nomRef != null){
164
            TeamOrPersonBase<?> refAuthor = nomRef.getAuthorship();
165
            nomRef.setAuthorship(getExistingAuthor(state, refAuthor));
166

    
167
            Reference existingRef = getExistingReference(state, (Reference)nomRef);
168
            if (existingRef != null){
169
                name.setNomenclaturalReference(existingRef);
170
            }
171
        }
172
    }
173

    
174
     /**
175
     * @param state
176
     * @param nomRef
177
     */
178
    private Reference getExistingReference(SimpleExcelTaxonImportState<CONFIG> state, Reference ref) {
179
        if (ref == null){
180
            return null;
181
        }else{
182
            initRerenceMap(state);
183
            Reference result = state.getReference(ref.getTitleCache());
184
            if (result == null){
185
                result = ref;
186
                Reference inRef = result.getInReference();
187
                if (inRef != null){
188
                    result.setInReference(getExistingReference(state, result.getInReference()));
189
                }
190
                state.putReference(result.getTitleCache(), result);
191
            }
192
            return result;
193
        }
194
    }
195

    
196
    boolean referenceMapIsInitialized;
197

    
198
    /**
199
     * @param state
200
     */
201
    private void initRerenceMap(SimpleExcelTaxonImportState<CONFIG> state) {
202
        if (!referenceMapIsInitialized){
203
            List<String> propertyPaths = Arrays.asList("");
204
            List<Reference> existingReferences = this.getReferenceService().list(null, null, null, null, propertyPaths);
205
            for (Reference ref : existingReferences){
206
                state.putReference(ref.getTitleCache(), ref);
207
            }
208
            referenceMapIsInitialized = true;
209
        }
210

    
211
    }
212

    
213
    boolean agentMapIsInitialized = false;
214

    
215
    /**
216
     * @param state
217
     *
218
     */
219
    @SuppressWarnings("rawtypes")
220
    private void initAgentMap(SimpleExcelTaxonImportState<CONFIG> state) {
221
        if (!agentMapIsInitialized){
222
            List<String> propertyPaths = Arrays.asList("");
223
            List<TeamOrPersonBase> existingAgents = this.getAgentService().list(null, null, null, null, propertyPaths);
224
            for (TeamOrPersonBase agent : existingAgents){
225
                state.putAgentBase(agent.getTitleCache(), agent);
226
            }
227
            agentMapIsInitialized = true;
228
        }
229
    }
230

    
231
    /**
232
     * @param state
233
     * @param combAuthor
234
     * @return
235
     */
236
    protected TeamOrPersonBase<?> getExistingAuthor(SimpleExcelTaxonImportState<CONFIG> state,
237
            TeamOrPersonBase<?> author) {
238
        if (author == null){
239
            return null;
240
        }else{
241
            initAgentMap(state);
242
            TeamOrPersonBase<?> result = state.getAgentBase(author.getTitleCache());
243
            if (result == null){
244
                state.putAgentBase(author.getTitleCache(), author);
245
                if (author instanceof Team){
246
                    handleTeam(state, (Team)author);
247
                }
248
                result = author;
249
            }
250
            return result;
251
        }
252
    }
253

    
254

    
255
    /**
256
     * @param state
257
     * @param author
258
     */
259
    private void handleTeam(SimpleExcelTaxonImportState<CONFIG> state, Team team) {
260
        List<Person> members = team.getTeamMembers();
261
        for (int i =0; i< members.size(); i++){
262
            Person person = members.get(i);
263
            //FIXME cast find a better way to guarantee that only persons are returned
264
            Person existingPerson = (Person)state.getAgentBase(person.getTitleCache());
265
            if (existingPerson != null){
266
                members.set(i, existingPerson);
267
            }else{
268
                state.putAgentBase(person.getTitleCache(), person);
269
            }
270
        }
271

    
272
    }
273

    
274

    
275
}
(8-8/9)