Project

General

Profile

Download (9.18 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.reference.INomenclaturalReference;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
33
import eu.etaxonomy.cdm.strategy.parser.INonViralNameParser;
34
import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
35

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

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

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

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

    
51

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

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

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

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

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

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

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

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

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

    
142

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

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

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

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

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

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

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

    
194
    boolean referenceMapIsInitialized;
195

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

    
209
    }
210

    
211
    boolean agentMapIsInitialized = false;
212

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

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

    
252

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

    
270
    }
271

    
272

    
273
}
(8-8/9)