2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.mexico
;
11 import java
.util
.Arrays
;
12 import java
.util
.HashMap
;
13 import java
.util
.List
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.apache
.log4j
.Logger
;
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
;
37 * Simple Excel import class that works without default state class
38 * {@link SimpleExcelTaxonImportState}
42 public abstract class SimpleExcelTaxonImport
<CONFIG
extends ExcelImportConfiguratorBase
>
43 extends ExcelImporterBase
<SimpleExcelTaxonImportState
<CONFIG
>>{
45 private static final long serialVersionUID
= -4345647703312616421L;
47 private static final Logger logger
= Logger
.getLogger(SimpleExcelTaxonImport
.class);
49 protected static INonViralNameParser
<?
> nameParser
= NonViralNameParserImpl
.NewInstance();
53 protected void analyzeRecord(HashMap
<String
, String
> record
, SimpleExcelTaxonImportState
<CONFIG
> state
) {
54 //override only if needed
58 protected void secondPass(SimpleExcelTaxonImportState
<CONFIG
> state
) {
59 //override only if needed
63 protected boolean isIgnore(SimpleExcelTaxonImportState
<CONFIG
> state
) {
67 //***************************** METHODS *********************************/
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>.
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();
90 protected IdentifiableSource
makeOriginalSource(SimpleExcelTaxonImportState
<CONFIG
> state
) {
91 return IdentifiableSource
.NewDataImportInstance("line: " + state
.getCurrentLine(), null, state
.getConfig().getSourceReference());
101 protected void makeGenus(SimpleExcelTaxonImportState
<CONFIG
> state
,
102 BotanicalName speciesName
,
105 TaxonNode familyNode
) {
107 TaxonNode higherNode
;
108 if (speciesName
.isProtectedTitleCache()){
109 higherNode
= familyNode
;
111 String genusStr
= speciesName
.getGenusOrUninomial();
112 Taxon genus
= state
.getHigherTaxon(genusStr
);
114 higherNode
= genus
.getTaxonNodes().iterator().next();
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
);
126 higherNode
.addChildTaxon(taxon
, null, null);
132 * @param expectedKeys
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
);
147 protected void replaceAuthorNamesAndNomRef(SimpleExcelTaxonImportState
<CONFIG
> state
, BotanicalName name
) {
148 TeamOrPersonBase
<?
> combAuthor
= name
.getCombinationAuthorship();
149 name
.setCombinationAuthorship(getExistingAuthor(state
, combAuthor
));
151 TeamOrPersonBase
<?
> exAuthor
= name
.getExCombinationAuthorship();
152 name
.setExCombinationAuthorship(getExistingAuthor(state
, exAuthor
));
154 TeamOrPersonBase
<?
> basioAuthor
= name
.getBasionymAuthorship();
155 name
.setBasionymAuthorship(getExistingAuthor(state
, basioAuthor
));
157 TeamOrPersonBase
<?
> exBasioAuthor
= name
.getExBasionymAuthorship();
158 name
.setExBasionymAuthorship(getExistingAuthor(state
, exBasioAuthor
));
160 INomenclaturalReference nomRef
= name
.getNomenclaturalReference();
162 TeamOrPersonBase
<?
> refAuthor
= nomRef
.getAuthorship();
163 nomRef
.setAuthorship(getExistingAuthor(state
, refAuthor
));
165 Reference existingRef
= getExistingReference(state
, (Reference
)nomRef
);
166 if (existingRef
!= null){
167 name
.setNomenclaturalReference(existingRef
);
176 private Reference
getExistingReference(SimpleExcelTaxonImportState
<CONFIG
> state
, Reference ref
) {
180 initRerenceMap(state
);
181 Reference result
= state
.getReference(ref
.getTitleCache());
184 Reference inRef
= result
.getInReference();
186 result
.setInReference(getExistingReference(state
, result
.getInReference()));
188 state
.putReference(result
.getTitleCache(), result
);
194 boolean referenceMapIsInitialized
;
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
);
206 referenceMapIsInitialized
= true;
211 boolean agentMapIsInitialized
= false;
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
);
225 agentMapIsInitialized
= true;
234 protected TeamOrPersonBase
<?
> getExistingAuthor(SimpleExcelTaxonImportState
<CONFIG
> state
,
235 TeamOrPersonBase
<?
> author
) {
240 TeamOrPersonBase
<?
> result
= state
.getAgentBase(author
.getTitleCache());
242 state
.putAgentBase(author
.getTitleCache(), author
);
243 if (author
instanceof Team
){
244 handleTeam(state
, (Team
)author
);
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
);
266 state
.putAgentBase(person
.getTitleCache(), person
);