2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.cdm
.io
.eflora
.centralAfrica
.checklist
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang
.StringUtils
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.stereotype
.Component
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IClassificationService
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IOValidator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.ResultSetPartitioner
;
28 import eu
.etaxonomy
.cdm
.io
.common
.TdwgAreaProvider
;
29 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMapping
;
30 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportMarkerMapper
;
31 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportObjectCreationMapper
;
32 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.DbImportTaxIncludedInMapper
;
33 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.IMappingImport
;
34 import eu
.etaxonomy
.cdm
.io
.eflora
.centralAfrica
.checklist
.validation
.CentralAfricaChecklistTaxonImportValidator
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
37 import eu
.etaxonomy
.cdm
.model
.description
.Distribution
;
38 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
39 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
40 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
41 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
42 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
43 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
47 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
49 import eu
.etaxonomy
.cdm
.strategy
.parser
.NonViralNameParserImpl
;
57 public class CentralAfricaChecklistTaxonImport
extends CentralAfricaChecklistImportBase
<TaxonBase
> implements IMappingImport
<TaxonBase
, CentralAfricaChecklistImportState
>{
58 private static final Logger logger
= Logger
.getLogger(CentralAfricaChecklistTaxonImport
.class);
60 private NonViralNameParserImpl parser
= NonViralNameParserImpl
.NewInstance();
62 private Map
<UUID
, Taxon
> higherTaxonMap
;
64 private Integer TREE_ID
= null;
66 private DbImportMapping
<?
,?
> mapping
;
68 private int modCount
= 10000;
69 private static final String pluralString
= "taxa";
70 private static final String dbTableName
= "checklist";
71 private static final Class cdmTargetClass
= TaxonBase
.class;
72 private static final String strOrderBy
= " ORDER BY family, genus, species ";
74 public CentralAfricaChecklistTaxonImport(){
75 super(pluralString
, dbTableName
, cdmTargetClass
);
81 * @see eu.etaxonomy.cdm.io.erms.ErmsImportBase#getIdQuery()
84 protected String
getIdQuery() {
85 String strQuery
= " SELECT pk FROM " + dbTableName
+
92 * @see eu.etaxonomy.cdm.io.eflora.centralAfrica.checklist.CentralAfricaChecklistImportBase#getMapping()
94 protected DbImportMapping
getMapping() {
96 mapping
= new DbImportMapping();
98 mapping
.addMapper(DbImportObjectCreationMapper
.NewInstance(this, "pk", TAXON_NAMESPACE
)); //id + tu_status
100 UUID uuidKew
= CentralAfricaChecklistTransformer
.uuidAcceptedKew
;
101 mapping
.addMapper(DbImportMarkerMapper
.NewInstance("accepted kew", uuidKew
, "Accepted Kew", "Accepted Kew", "Kew", null));
103 UUID uuidGeneva
= CentralAfricaChecklistTransformer
.uuidAcceptedGeneva
;
104 mapping
.addMapper(DbImportMarkerMapper
.NewInstance("accepted geneva", uuidGeneva
, "Accepted Geneva", "Accepted Geneva", "Geneva", null));
106 UUID uuidItis
= CentralAfricaChecklistTransformer
.uuidAcceptedItis
;
107 mapping
.addMapper(DbImportMarkerMapper
.NewInstance("accepted itis", uuidItis
, "Accepted ITIS", "Accepted ITIS", "ITIS", null));
114 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
117 protected String
getRecordQuery(CentralAfricaChecklistImportConfigurator config
) {
118 String strSelect
= " SELECT * ";
119 String strFrom
= " FROM checklist";
120 String strWhere
= " WHERE ( pk IN (" + ID_LIST_TOKEN
+ ") )";
121 String strRecordQuery
= strSelect
+ strFrom
+ strWhere
+ strOrderBy
;
122 return strRecordQuery
;
127 public boolean doPartition(ResultSetPartitioner partitioner
, CentralAfricaChecklistImportState state
) {
128 higherTaxonMap
= new HashMap
<UUID
, Taxon
>();
129 Reference genevaReference
= getReferenceService().find(state
.getConfig().getUuidGenevaReference());
130 if (genevaReference
== null){
131 genevaReference
= makeGenevaReference(state
);
132 getReferenceService().save(genevaReference
);
134 state
.setGenevaReference(genevaReference
);
135 boolean success
= super.doPartition(partitioner
, state
);
136 higherTaxonMap
= new HashMap
<UUID
, Taxon
>();
137 state
.setGenevaReference(null);
143 public Map
<Object
, Map
<String
, ?
extends CdmBase
>> getRelatedObjectsForPartition(ResultSet rs
, CentralAfricaChecklistImportState state
) {
147 Set
<String
> referenceIdSet
= new HashSet
<String
>();
149 Map
<Object
, Map
<String
, ?
extends CdmBase
>> result
= new HashMap
<Object
, Map
<String
, ?
extends CdmBase
>>();
153 handleForeignKey(rs
, referenceIdSet
, "source");
157 nameSpace
= REFERENCE_NAMESPACE
;
158 cdmClass
= Reference
.class;
159 idSet
= referenceIdSet
;
160 Map
<String
, Reference
> referenceMap
= (Map
<String
, Reference
>)getCommonService().getSourcedObjectsByIdInSource(cdmClass
, referenceIdSet
, nameSpace
);
161 result
.put(REFERENCE_NAMESPACE
, referenceMap
);
163 } catch (SQLException e
) {
164 throw new RuntimeException(e
);
170 public TaxonBase
<?
> createObject(ResultSet rs
, CentralAfricaChecklistImportState state
) throws SQLException
{
171 BotanicalName speciesName
= BotanicalName
.NewInstance(Rank
.SPECIES());
173 Reference
<?
> sec
= state
.getConfig().getSourceReference();
174 getReferenceService().saveOrUpdate(sec
);
176 String familyString
= rs
.getString("family");
177 String genusString
= rs
.getString("genus");
178 String speciesString
= rs
.getString("species");
179 String authorityString
= rs
.getString("authority");
181 if (logger
.isDebugEnabled()){
182 System
.out
.println(familyString
+ " " + genusString
+ " " + speciesString
);
185 Taxon speciesTaxon
= Taxon
.NewInstance(speciesName
, sec
);;
186 speciesName
.setGenusOrUninomial(genusString
);
187 speciesName
.setSpecificEpithet(speciesString
);
188 parser
.handleAuthors(speciesName
, CdmUtils
.concat(" ", new String
[] {"", genusString
, speciesString
, authorityString
}), authorityString
);
191 Taxon familyTaxon
= null;
192 if (StringUtils
.isNotBlank(familyString
)){
193 familyTaxon
= getHigherTaxon(state
, familyString
, null);
194 if (familyTaxon
== null){
195 BotanicalName familyName
= BotanicalName
.NewInstance(Rank
.FAMILY());
196 familyName
.setGenusOrUninomial(familyString
);
197 familyTaxon
= Taxon
.NewInstance(familyName
, sec
);
198 saveHigherTaxon(state
, familyTaxon
, familyString
, null);
200 getTaxonService().saveOrUpdate(familyTaxon
);
205 Taxon genusTaxon
= getHigherTaxon(state
, familyString
, genusString
);
206 if (genusTaxon
== null){
207 BotanicalName genusName
= BotanicalName
.NewInstance(Rank
.GENUS());
208 genusName
.setGenusOrUninomial(genusString
);
209 genusTaxon
= Taxon
.NewInstance(genusName
, sec
);
210 saveHigherTaxon(state
, genusTaxon
, familyString
, genusString
);
211 if (familyTaxon
!= null){
212 makeTaxonomicallyIncluded(state
, TREE_ID
, genusTaxon
, familyTaxon
, null, null);
215 makeTaxonomicallyIncluded(state
, TREE_ID
, speciesTaxon
, genusTaxon
, null, null);
216 getTaxonService().saveOrUpdate(genusTaxon
);
218 String sourceString
= rs
.getString("source");
219 String sourceId
= rs
.getString("source_id");
221 Reference
<?
> sourceRef
= state
.getRelatedObject(REFERENCE_NAMESPACE
, sourceString
, Reference
.class);
222 speciesTaxon
.addSource(OriginalSourceType
.Import
, sourceId
, REFERENCE_NAMESPACE
, sourceRef
, null);
225 Reference
<?
> genevaReference
= state
.getGenevaReference();
226 Object genevaId
= rs
.getObject("geneva_ID");
227 speciesTaxon
.addSource(OriginalSourceType
.Import
, String
.valueOf(genevaId
), null, genevaReference
, null);
230 handleDistribution(rs
, speciesTaxon
);
235 private void handleDistribution(ResultSet rs
, Taxon speciesTaxon
) throws SQLException
{
236 TaxonDescription description
= TaxonDescription
.NewInstance(speciesTaxon
);
238 Boolean isCongo
= rs
.getBoolean("drc");
239 Boolean isBurundi
= rs
.getBoolean("burundi");
240 Boolean isRwanda
= rs
.getBoolean("rwanda");
242 addDistribution(description
, isCongo
, "ZAI");
243 addDistribution(description
, isBurundi
, "BUR");
244 addDistribution(description
, isRwanda
, "RWA");
254 private void addDistribution(TaxonDescription description
, Boolean exists
, String label
) {
256 NamedArea namedArea
= TdwgAreaProvider
.getAreaByTdwgAbbreviation(label
);
257 Distribution distribution
= Distribution
.NewInstance(namedArea
, PresenceAbsenceTerm
.PRESENT());
258 description
.addElement(distribution
);
264 private void saveHigherTaxon(CentralAfricaChecklistImportState state
, Taxon higherTaxon
, String family
, String genus
) {
265 String higherName
= normalizeHigherTaxonName(family
, genus
);
266 UUID uuid
= higherTaxon
.getUuid();
267 state
.putHigherTaxon(higherName
, uuid
);
268 higherTaxonMap
.put(uuid
, higherTaxon
);
273 private Taxon
getHigherTaxon(CentralAfricaChecklistImportState state
, String family
, String genus
) {
274 String higherName
= normalizeHigherTaxonName(family
, genus
);
275 UUID uuid
= state
.getHigherTaxon(higherName
);
279 taxon
= higherTaxonMap
.get(uuid
);
281 taxon
= CdmBase
.deproxy(getTaxonService().find(uuid
), Taxon
.class);
293 private String
normalizeHigherTaxonName(String family
, String genus
) {
294 return (CdmUtils
.Nz(family
) + "-" + CdmUtils
.Nz(genus
)).trim();
300 // private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state, Taxon parent, Taxon child, Reference citation, String microCitation){
301 // Reference sec = child.getSec();
302 // UUID uuid = state.getTreeUuid(sec);
303 // Classification tree;
304 // tree = state.getTree(sec);
306 // if (tree == null){
307 // tree = makeTreeMemSave(state, sec);
309 // TaxonNode childNode;
310 // if (parent != null){
311 // childNode = tree.addParentChild(parent, child, citation, microCitation);
313 // childNode = tree.addChildTaxon(child, citation, microCitation, null);
315 // return (childNode != null);
319 private boolean makeTaxonomicallyIncluded(CentralAfricaChecklistImportState state
, Integer treeRefFk
, Taxon child
, Taxon parent
, Reference
<?
> citation
, String microCitation
){
322 if (treeRefFk
== null){
323 treeKey
= "1"; // there is only one tree and it gets the map key '1'
324 treeUuid
= state
.getConfig().getClassificationUuid();
326 treeKey
=String
.valueOf(treeRefFk
);
327 treeUuid
= state
.getTreeUuidByTreeKey(treeKey
);
329 Classification tree
= (Classification
)state
.getRelatedObject(DbImportTaxIncludedInMapper
.TAXONOMIC_TREE_NAMESPACE
, treeKey
);
331 IClassificationService service
= state
.getCurrentIO().getClassificationService();
332 tree
= service
.find(treeUuid
);
334 String treeName
= state
.getConfig().getClassificationName();
335 tree
= Classification
.NewInstance(treeName
);
336 tree
.setUuid(treeUuid
);
337 //FIXME tree reference
338 //tree.setReference(ref);
341 state
.addRelatedObject(DbImportTaxIncludedInMapper
.TAXONOMIC_TREE_NAMESPACE
, treeKey
, tree
);
344 TaxonNode childNode
= tree
.addParentChild(parent
, child
, citation
, microCitation
);
345 return (childNode
!= null);
349 private Reference
<?
> makeGenevaReference(CentralAfricaChecklistImportState state
) {
350 Reference
<?
> result
= ReferenceFactory
.newDatabase();
351 result
.setTitleCache(state
.getConfig().getGenevaReferenceTitle(), true);
352 result
.setUuid(state
.getConfig().getUuidGenevaReference());
357 protected boolean doCheck(CentralAfricaChecklistImportState state
){
358 IOValidator
<CentralAfricaChecklistImportState
> validator
= new CentralAfricaChecklistTaxonImportValidator();
359 return validator
.validate(state
);
363 protected boolean isIgnore(CentralAfricaChecklistImportState state
){
364 return ! state
.getConfig().isDoTaxa();