3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.io
.common
.mapping
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonTreeService
;
20 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
21 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.ImportConfiguratorBase
;
23 import eu
.etaxonomy
.cdm
.io
.erms
.ICheckIgnoreMapper
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
42 public class DbImportTaxIncludedInMapper
<STATE
extends DbImportStateBase
<ImportConfiguratorBase
,?
>> extends DbImportMultiAttributeMapperBase
<CdmBase
, STATE
> {
43 private static final Logger logger
= Logger
.getLogger(DbImportTaxIncludedInMapper
.class);
45 //******************************** FACTORY METHOD ***************************************************/
47 public static DbImportTaxIncludedInMapper
<?
> NewInstance(String dbChildAttribute
, String dbChildNamespace
, String dbParentAttribute
, String parentNamespace
, String dbAlternativeParentAttribute
, String alternativeParentNamespace
, String dbTreeAttribute
){
48 String citationNamespace
= null;
49 String citationAttribute
= null;
50 return new DbImportTaxIncludedInMapper(dbChildAttribute
, dbChildNamespace
, dbParentAttribute
, parentNamespace
, dbAlternativeParentAttribute
, alternativeParentNamespace
, dbTreeAttribute
, citationAttribute
, citationNamespace
);
53 //******************************* ATTRIBUTES ***************************************/
54 private String fromAttribute
;
55 private String toAttribute
;
57 private String fromNamespace
;
58 private String toNamespace
;
60 private String citationAttribute
;
61 private String citationNamespace
;
63 private String microCitationAttribute
;
64 private String treeAttribute
;
65 private String alternativeAttribute
;
66 private String alternativeNamespace
;
69 //********************************* CONSTRUCTOR ****************************************/
71 * @param relatedObjectNamespace
72 * @param mappingImport
74 protected DbImportTaxIncludedInMapper(String fromAttribute
, String fromNamespace
, String toAttribute
, String toNamespace
, String alternativeAttribute
, String alternativeNamespace
, String treeAttribute
, String citationAttribute
, String citationNamespace
) {
76 //TODO make it a single attribute mapper
77 this.fromAttribute
= fromAttribute
;
78 this.fromNamespace
= fromNamespace
;
79 this.toAttribute
= toAttribute
;
80 this.toNamespace
= toNamespace
;
81 this.treeAttribute
= treeAttribute
;
82 this.alternativeAttribute
= alternativeAttribute
;
83 this.alternativeNamespace
= alternativeNamespace
;
84 this.citationAttribute
= citationAttribute
;
85 this.citationNamespace
= citationNamespace
;
88 //************************************ METHODS *******************************************/
92 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
94 public CdmBase
invoke(ResultSet rs
, CdmBase cdmBase
) throws SQLException
{
95 STATE state
= getState();
96 CdmImportBase currentImport
= state
.getCurrentIO();
97 if (currentImport
instanceof ICheckIgnoreMapper
){
98 boolean ignoreRecord
= ((ICheckIgnoreMapper
)currentImport
).checkIgnoreMapper(this, rs
);
104 TaxonBase fromObject
= (TaxonBase
)getRelatedObject(rs
, fromAttribute
, fromNamespace
);
105 TaxonBase toObject
= (TaxonBase
)getRelatedObject(rs
, toAttribute
, toNamespace
);
106 TaxonBase alternativeToObject
= (TaxonBase
)getRelatedObject(rs
, alternativeAttribute
, alternativeNamespace
);
108 String fromId
= String
.valueOf(rs
.getObject(fromAttribute
));
109 String toId
= String
.valueOf(rs
.getObject(toAttribute
));
110 String alternativeToId
= String
.valueOf(rs
.getObject(alternativeAttribute
));
112 ReferenceBase citation
= (ReferenceBase
)getRelatedObject(rs
, citationAttribute
, citationNamespace
);
113 String microCitation
= null;
114 if (citationAttribute
!= null){
115 microCitation
= rs
.getString(microCitationAttribute
);
118 Integer treeFk
= null;
119 if (treeAttribute
!= null){
120 treeFk
= rs
.getInt(treeAttribute
);
123 if (fromObject
== null){
124 String warning
= "The child taxon could not be found. Child taxon not added to the tree";
125 logger
.warn(warning
);
130 fromTaxon
= checkTaxonType(fromObject
, "Child", fromId
);
131 } catch (IllegalArgumentException e2
) {
136 if (toObject
== null){
137 String warning
= "The parent taxon could not be found. Child taxon not added to the tree";
138 logger
.warn(warning
);
144 toTaxon
= checkTaxonType(toObject
, "Parent", toId
);
145 } catch (IllegalArgumentException e
) {
146 if (alternativeToObject
!= null){
148 toTaxon
= checkTaxonType(alternativeToObject
, "Alternative parent", alternativeToId
);
149 } catch (IllegalArgumentException e1
) {
158 if (fromTaxon
.equals(toTaxon
)){
159 String warning
= "A taxon may not be a child of itself. Taxon not added to the tree";
160 logger
.warn(warning
);
164 makeTaxonomicallyIncluded(state
, treeFk
, fromTaxon
, toTaxon
, citation
, microCitation
);
173 * TODO copied from BM import. May be more generic
175 * @param taxonTreeMap
180 * @param microCitation
184 public static final String TAXONOMIC_TREE_NAMESPACE
= "TaxonomicTree";
186 private boolean makeTaxonomicallyIncluded(STATE state
, Integer treeRefFk
, Taxon child
, Taxon parent
, ReferenceBase citation
, String microCitation
){
189 if (treeRefFk
== null){
190 treeKey
= "1"; // there is only one tree and it gets the key '1'
191 treeUuid
= state
.getConfig().getTaxonomicTreeUuid();
193 treeKey
=String
.valueOf(treeRefFk
);
194 treeUuid
= state
.getTreeUuidByTreeKey(treeKey
);
196 TaxonomicTree tree
= (TaxonomicTree
)state
.getRelatedObject(TAXONOMIC_TREE_NAMESPACE
, treeKey
);
198 ITaxonTreeService service
= state
.getCurrentIO().getTaxonTreeService();
199 tree
= service
.getTaxonomicTreeByUuid(treeUuid
);
201 String treeName
= state
.getConfig().getTaxonomicTreeName();
202 tree
= TaxonomicTree
.NewInstance(treeName
);
203 tree
.setUuid(treeUuid
);
204 //FIXME tree reference
205 //tree.setReference(ref);
208 state
.addRelatedObject(TAXONOMIC_TREE_NAMESPACE
, treeKey
, tree
);
211 tree
.addParentChild(parent
, child
, citation
, microCitation
);
216 // private boolean makeTaxonomicallyIncluded_OLD(STATE state, Integer treeRefFk, Taxon child, Taxon parent, ReferenceBase citation, String microCitation){
217 // Map<Integer, TaxonomicTree> taxonTreeMap = state.getPartitionTaxonTreeMap();
220 // TaxonomicTree tree = taxonTreeMap.get(treeRefFk);
221 // if (tree == null){
222 // UUID treeUuid = state.getTreeUuidByTreeKey(treeRefFk);
223 // tree = state.getCurrentImport().getTaxonTreeService().getTaxonomicTreeByUuid(treeUuid);
224 // if (tree == null){
225 // //FIXME FIXME FIXME
226 // String treeName = "TaxonTree - No Name";
227 // tree = TaxonomicTree.NewInstance(treeName);
228 // //tree.setReference(ref);
229 // //throw new IllegalStateException("Tree for ToTaxon reference does not exist.");
231 // taxonTreeMap.put(treeRefFk, tree);
233 // return tree.addParentChild(parent, child, citation, microCitation);
238 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
242 * @throws SQLException
244 protected CdmBase
getRelatedObject(ResultSet rs
, String dbAttribute
, String namespace
) throws SQLException
{
245 CdmBase result
= null;
246 if (dbAttribute
!= null){
247 Object dbValue
= rs
.getObject(dbAttribute
);
248 String id
= String
.valueOf(dbValue
);
249 DbImportStateBase state
= importMapperHelper
.getState();
250 result
= state
.getRelatedObject(namespace
, id
);
257 * Checks if cdmBase is of type Taxon
263 private Taxon
checkTaxonType(TaxonBase taxonBase
, String typeString
, String id
) throws IllegalArgumentException
{
264 if (! taxonBase
.isInstanceOf(Taxon
.class)){
265 String warning
= typeString
+ " (" + id
+ ") is not of type Taxon but of type " + taxonBase
.getClass().getSimpleName();
266 logger
.warn(warning
);
267 throw new IllegalArgumentException(warning
);
269 return (taxonBase
.deproxy(taxonBase
, Taxon
.class));