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.List;
13
import java.util.Map;
14
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.excel.common.ExcelImportBase;
19
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
20
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase;
21
import eu.etaxonomy.cdm.model.agent.AgentBase;
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.agent.Team;
24
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
25
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
26
import eu.etaxonomy.cdm.model.name.IBotanicalName;
27
import eu.etaxonomy.cdm.model.name.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
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
 * @since 16.06.2016
41
 */
42
public abstract class SimpleExcelTaxonImport<CONFIG extends ExcelImportConfiguratorBase>
43
        extends ExcelImportBase<SimpleExcelTaxonImportState<CONFIG>, CONFIG, ExcelRowBase>{
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
    @Override
52
    protected void analyzeRecord(Map<String, String> record, SimpleExcelTaxonImportState<CONFIG> state) {
53
        //override only if needed
54
    }
55

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

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

    
66
//***************************** METHODS *********************************/
67

    
68
    protected IdentifiableSource makeOriginalSource(SimpleExcelTaxonImportState<CONFIG> state) {
69
        return IdentifiableSource.NewDataImportInstance("line: " + state.getCurrentLine(), null, state.getConfig().getSourceReference());
70
    }
71

    
72
    protected void makeGenus(SimpleExcelTaxonImportState<CONFIG> state,
73
            IBotanicalName speciesName,
74
            Reference sec,
75
            Taxon taxon,
76
            TaxonNode familyNode) {
77

    
78
        TaxonNode higherNode;
79
        if (speciesName.isProtectedTitleCache()){
80
            higherNode = familyNode;
81
        }else{
82
            String genusStr = speciesName.getGenusOrUninomial();
83
            Taxon genus = state.getHigherTaxon(genusStr);
84
            if (genus != null){
85
                higherNode = genus.getTaxonNodes().iterator().next();
86
            }else{
87
                IBotanicalName genusName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
88
                genusName.addSource(makeOriginalSource(state));
89
                genusName.setGenusOrUninomial(genusStr);
90
                genus = Taxon.NewInstance(genusName, sec);
91
                genus.addSource(makeOriginalSource(state));
92
                higherNode = familyNode.addChildTaxon(genus, null, null);
93
                state.putHigherTaxon(genusStr, genus);
94
            }
95
        }
96

    
97
        higherNode.addChildTaxon(taxon, null, null);
98
    }
99

    
100
    protected void checkAllKeysExist(String line, Set<String> keys, List<String> expectedKeys) {
101
        for (String key: keys) {
102
            if (! expectedKeys.contains(key)){
103
                logger.warn(line + "Unexpected Key: " + key);
104
            }
105
        }
106
    }
107

    
108
    protected void replaceAuthorNamesAndNomRef(SimpleExcelTaxonImportState<CONFIG> state,
109
            IBotanicalName name) {
110
        TeamOrPersonBase<?> combAuthor = name.getCombinationAuthorship();
111
        name.setCombinationAuthorship(getExistingAuthor(state, combAuthor));
112

    
113
        TeamOrPersonBase<?> exAuthor = name.getExCombinationAuthorship();
114
        name.setExCombinationAuthorship(getExistingAuthor(state, exAuthor));
115

    
116
        TeamOrPersonBase<?> basioAuthor = name.getBasionymAuthorship();
117
        name.setBasionymAuthorship(getExistingAuthor(state, basioAuthor));
118

    
119
        TeamOrPersonBase<?> exBasioAuthor = name.getExBasionymAuthorship();
120
        name.setExBasionymAuthorship(getExistingAuthor(state, exBasioAuthor));
121

    
122
        INomenclaturalReference nomRef = name.getNomenclaturalReference();
123
        if (nomRef != null){
124
            TeamOrPersonBase<?> refAuthor = nomRef.getAuthorship();
125
            nomRef.setAuthorship(getExistingAuthor(state, refAuthor));
126

    
127
            Reference existingRef = getExistingReference(state, (Reference)nomRef);
128
            if (existingRef != null){
129
                name.setNomenclaturalReference(existingRef);
130
            }
131
        }
132
    }
133

    
134
    private Reference getExistingReference(SimpleExcelTaxonImportState<CONFIG> state, Reference ref) {
135
        if (ref == null){
136
            return null;
137
        }else{
138
            initRerenceMap(state);
139
            Reference result = state.getReference(ref.getTitleCache());
140
            if (result == null){
141
                result = ref;
142
                Reference inRef = result.getInReference();
143
                if (inRef != null){
144
                    result.setInReference(getExistingReference(state, result.getInReference()));
145
                }
146
                state.putReference(result.getTitleCache(), result);
147
            }
148
            return result;
149
        }
150
    }
151

    
152
    boolean referenceMapIsInitialized;
153

    
154
    private void initRerenceMap(SimpleExcelTaxonImportState<CONFIG> state) {
155
        if (!referenceMapIsInitialized){
156
            List<String> propertyPaths = Arrays.asList("");
157
            List<Reference> existingReferences = this.getReferenceService().list(null, null, null, null, propertyPaths);
158
            for (Reference ref : existingReferences){
159
                state.putReference(ref.getTitleCache(), ref);
160
            }
161
            referenceMapIsInitialized = true;
162
        }
163

    
164
    }
165

    
166
    boolean agentMapIsInitialized = false;
167

    
168
    @SuppressWarnings("rawtypes")
169
    private void initAgentMap(SimpleExcelTaxonImportState<CONFIG> state) {
170
        if (!agentMapIsInitialized){
171
            List<String> propertyPaths = Arrays.asList("");
172
            List<AgentBase<?>> existingAgents = this.getAgentService().list(null, null, null, null, propertyPaths);
173
            for (AgentBase<?> agent : existingAgents){
174
                if (agent.isInstanceOf(TeamOrPersonBase.class)){
175
                    state.putAgentBase(agent.getTitleCache(), (TeamOrPersonBase)agent);
176
                }
177
            }
178
            agentMapIsInitialized = true;
179
        }
180
    }
181

    
182
    protected TeamOrPersonBase<?> getExistingAuthor(SimpleExcelTaxonImportState<CONFIG> state,
183
            TeamOrPersonBase<?> author) {
184
        if (author == null){
185
            return null;
186
        }else{
187
            initAgentMap(state);
188
            TeamOrPersonBase<?> result = state.getAgentBase(author.getTitleCache());
189
            if (result == null){
190
                state.putAgentBase(author.getTitleCache(), author);
191
                if (author instanceof Team){
192
                    handleTeam(state, (Team)author);
193
                }
194
                result = author;
195
            }
196
            return result;
197
        }
198
    }
199

    
200
    private void handleTeam(SimpleExcelTaxonImportState<CONFIG> state, Team team) {
201
        List<Person> members = team.getTeamMembers();
202
        for (int i =0; i< members.size(); i++){
203
            Person person = members.get(i);
204
            //FIXME cast find a better way to guarantee that only persons are returned
205
            Person existingPerson = (Person)state.getAgentBase(person.getTitleCache());
206
            if (existingPerson != null){
207
                members.set(i, existingPerson);
208
            }else{
209
                state.putAgentBase(person.getTitleCache(), person);
210
            }
211
        }
212
    }
213

    
214
    /**
215
     * Returns the line string and prints it on syso for each count-th line
216
     */
217
    protected String getLine(SimpleExcelTaxonImportState<CONFIG> state, Integer count) {
218
        String line = state.getCurrentLine() + ": ";
219
        if (count != null && (state.getCurrentLine() % count) == 0){
220
            System.out.println(line);
221
        }
222
        return line;
223
    }
224
}
(8-8/9)