Project

General

Profile

Download (8.53 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.Set;
15

    
16
import org.apache.log4j.Logger;
17

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

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

    
43
    private static final long serialVersionUID = -4345647703312616421L;
44

    
45
    private static final Logger logger = Logger.getLogger(SimpleExcelTaxonImport.class);
46

    
47
    protected static INonViralNameParser<?> nameParser = NonViralNameParserImpl.NewInstance();
48

    
49

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

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

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

    
65
//***************************** METHODS *********************************/
66

    
67

    
68
    /**
69
     * @param state
70
     * @return
71
     */
72
    protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
73
        return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, state.getConfig().getSourceReference());
74
    }
75

    
76
    /**
77
     * @param state
78
     * @param speciesName
79
     * @param sec
80
     * @param taxon
81
     * @param familyNode
82
     */
83
    protected void makeGenus(SimpleExcelTaxonImportState<CONFIG> state,
84
            BotanicalName speciesName,
85
            Reference sec,
86
            Taxon taxon,
87
            TaxonNode familyNode) {
88

    
89
        TaxonNode higherNode;
90
        if (speciesName.isProtectedTitleCache()){
91
            higherNode = familyNode;
92
        }else{
93
            String genusStr = speciesName.getGenusOrUninomial();
94
            Taxon genus = state.getHigherTaxon(genusStr);
95
            if (genus != null){
96
                higherNode = genus.getTaxonNodes().iterator().next();
97
            }else{
98
                BotanicalName genusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
99
                genusName.addSource(makeOriginalSource(state));
100
                genusName.setGenusOrUninomial(genusStr);
101
                genus = Taxon.NewInstance(genusName, sec);
102
                genus.addSource(makeOriginalSource(state));
103
                higherNode = familyNode.addChildTaxon(genus, null, null);
104
                state.putHigherTaxon(genusStr, genus);
105
            }
106
        }
107

    
108
        higherNode.addChildTaxon(taxon, null, null);
109
    }
110

    
111
    /**
112
     * @param line
113
     * @param keys
114
     * @param expectedKeys
115
     */
116
    protected void checkAllKeysExist(String line, Set<String> keys, List<String> expectedKeys) {
117
        for (String key: keys) {
118
            if (! expectedKeys.contains(key)){
119
                logger.warn(line + "Unexpected Key: " + key);
120
            }
121
        }
122
    }
123

    
124

    
125
    /**
126
     * @param state
127
     * @param name
128
     */
129
    protected void replaceAuthorNamesAndNomRef(SimpleExcelTaxonImportState<CONFIG> state,
130
            BotanicalName name) {
131
        TeamOrPersonBase<?> combAuthor = name.getCombinationAuthorship();
132
        name.setCombinationAuthorship(getExistingAuthor(state, combAuthor));
133

    
134
        TeamOrPersonBase<?> exAuthor = name.getExCombinationAuthorship();
135
        name.setExCombinationAuthorship(getExistingAuthor(state, exAuthor));
136

    
137
        TeamOrPersonBase<?> basioAuthor = name.getBasionymAuthorship();
138
        name.setBasionymAuthorship(getExistingAuthor(state, basioAuthor));
139

    
140
        TeamOrPersonBase<?> exBasioAuthor = name.getExBasionymAuthorship();
141
        name.setExBasionymAuthorship(getExistingAuthor(state, exBasioAuthor));
142

    
143
        INomenclaturalReference nomRef = name.getNomenclaturalReference();
144
        if (nomRef != null){
145
            TeamOrPersonBase<?> refAuthor = nomRef.getAuthorship();
146
            nomRef.setAuthorship(getExistingAuthor(state, refAuthor));
147

    
148
            Reference existingRef = getExistingReference(state, (Reference)nomRef);
149
            if (existingRef != null){
150
                name.setNomenclaturalReference(existingRef);
151
            }
152
        }
153
    }
154

    
155
     /**
156
     * @param state
157
     * @param nomRef
158
     */
159
    private Reference getExistingReference(SimpleExcelTaxonImportState<CONFIG> state, Reference ref) {
160
        if (ref == null){
161
            return null;
162
        }else{
163
            initRerenceMap(state);
164
            Reference result = state.getReference(ref.getTitleCache());
165
            if (result == null){
166
                result = ref;
167
                Reference inRef = result.getInReference();
168
                if (inRef != null){
169
                    result.setInReference(getExistingReference(state, result.getInReference()));
170
                }
171
                state.putReference(result.getTitleCache(), result);
172
            }
173
            return result;
174
        }
175
    }
176

    
177
    boolean referenceMapIsInitialized;
178

    
179
    /**
180
     * @param state
181
     */
182
    private void initRerenceMap(SimpleExcelTaxonImportState<CONFIG> state) {
183
        if (!referenceMapIsInitialized){
184
            List<String> propertyPaths = Arrays.asList("");
185
            List<Reference> existingReferences = this.getReferenceService().list(null, null, null, null, propertyPaths);
186
            for (Reference ref : existingReferences){
187
                state.putReference(ref.getTitleCache(), ref);
188
            }
189
            referenceMapIsInitialized = true;
190
        }
191

    
192
    }
193

    
194
    boolean agentMapIsInitialized = false;
195

    
196
    /**
197
     * @param state
198
     *
199
     */
200
    @SuppressWarnings("rawtypes")
201
    private void initAgentMap(SimpleExcelTaxonImportState<CONFIG> state) {
202
        if (!agentMapIsInitialized){
203
            List<String> propertyPaths = Arrays.asList("");
204
            List<TeamOrPersonBase> existingAgents = this.getAgentService().list(null, null, null, null, propertyPaths);
205
            for (TeamOrPersonBase agent : existingAgents){
206
                state.putAgentBase(agent.getTitleCache(), agent);
207
            }
208
            agentMapIsInitialized = true;
209
        }
210
    }
211

    
212
    /**
213
     * @param state
214
     * @param combAuthor
215
     * @return
216
     */
217
    protected TeamOrPersonBase<?> getExistingAuthor(SimpleExcelTaxonImportState<CONFIG> state,
218
            TeamOrPersonBase<?> author) {
219
        if (author == null){
220
            return null;
221
        }else{
222
            initAgentMap(state);
223
            TeamOrPersonBase<?> result = state.getAgentBase(author.getTitleCache());
224
            if (result == null){
225
                state.putAgentBase(author.getTitleCache(), author);
226
                if (author instanceof Team){
227
                    handleTeam(state, (Team)author);
228
                }
229
                result = author;
230
            }
231
            return result;
232
        }
233
    }
234

    
235

    
236
    /**
237
     * @param state
238
     * @param author
239
     */
240
    private void handleTeam(SimpleExcelTaxonImportState<CONFIG> state, Team team) {
241
        List<Person> members = team.getTeamMembers();
242
        for (int i =0; i< members.size(); i++){
243
            Person person = members.get(i);
244
            //FIXME cast find a better way to guarantee that only persons are returned
245
            Person existingPerson = (Person)state.getAgentBase(person.getTitleCache());
246
            if (existingPerson != null){
247
                members.set(i, existingPerson);
248
            }else{
249
                state.putAgentBase(person.getTitleCache(), person);
250
            }
251
        }
252

    
253
    }
254

    
255

    
256
}
(8-8/9)