merge pesi->trunk
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportTaxIncludedInMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18
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;
29
30 /**
31 * @author a.mueller
32 * @created 12.05.2009
33 * @version 1.0
34 */
35 /**
36 * @author a.mueller
37 * @created 02.03.2010
38 * @version 1.0
39 * @param <CDM_BASE>
40 * @param <STATE>
41 */
42 public class DbImportTaxIncludedInMapper<STATE extends DbImportStateBase<ImportConfiguratorBase,?>> extends DbImportMultiAttributeMapperBase<CdmBase, STATE> {
43 private static final Logger logger = Logger.getLogger(DbImportTaxIncludedInMapper.class);
44
45 //******************************** FACTORY METHOD ***************************************************/
46
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);
51 }
52
53 //******************************* ATTRIBUTES ***************************************/
54 private String fromAttribute;
55 private String toAttribute;
56
57 private String fromNamespace;
58 private String toNamespace;
59
60 private String citationAttribute;
61 private String citationNamespace;
62
63 private String microCitationAttribute;
64 private String treeAttribute;
65 private String alternativeAttribute;
66 private String alternativeNamespace;
67
68
69 //********************************* CONSTRUCTOR ****************************************/
70 /**
71 * @param relatedObjectNamespace
72 * @param mappingImport
73 */
74 protected DbImportTaxIncludedInMapper(String fromAttribute, String fromNamespace, String toAttribute, String toNamespace, String alternativeAttribute, String alternativeNamespace, String treeAttribute, String citationAttribute, String citationNamespace) {
75 super();
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;
86 }
87
88 //************************************ METHODS *******************************************/
89
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
93 */
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);
99 if (ignoreRecord){
100 return cdmBase;
101 }
102 }
103
104 TaxonBase fromObject = (TaxonBase)getRelatedObject(rs, fromAttribute, fromNamespace);
105 TaxonBase toObject = (TaxonBase)getRelatedObject(rs, toAttribute, toNamespace);
106 TaxonBase alternativeToObject = (TaxonBase)getRelatedObject(rs, alternativeAttribute, alternativeNamespace);
107
108 String fromId = String.valueOf(rs.getObject(fromAttribute));
109 String toId = String.valueOf(rs.getObject(toAttribute));
110 String alternativeToId = String.valueOf(rs.getObject(alternativeAttribute));
111
112 ReferenceBase citation = (ReferenceBase)getRelatedObject(rs, citationAttribute, citationNamespace);
113 String microCitation = null;
114 if (citationAttribute != null){
115 microCitation = rs.getString(microCitationAttribute);
116 }
117 //TODO check int
118 Integer treeFk = null;
119 if (treeAttribute != null){
120 treeFk = rs.getInt(treeAttribute);
121 }
122
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);
126 return cdmBase;
127 }
128 Taxon fromTaxon;
129 try {
130 fromTaxon = checkTaxonType(fromObject, "Child", fromId);
131 } catch (IllegalArgumentException e2) {
132 //fromTaxon is null
133 return cdmBase;
134 }
135
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);
139 return cdmBase;
140 }
141
142 Taxon toTaxon;
143 try {
144 toTaxon = checkTaxonType(toObject, "Parent", toId);
145 } catch (IllegalArgumentException e) {
146 if (alternativeToObject != null){
147 try {
148 toTaxon = checkTaxonType(alternativeToObject, "Alternative parent", alternativeToId);
149 } catch (IllegalArgumentException e1) {
150 return cdmBase;
151 }
152 }else{
153
154 return cdmBase;
155 }
156 }
157
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);
161 return cdmBase;
162 }
163 //maps the reference
164 makeTaxonomicallyIncluded(state, treeFk, fromTaxon, toTaxon, citation, microCitation);
165 return fromTaxon;
166 }
167
168
169
170
171
172 /**
173 * TODO copied from BM import. May be more generic
174 * @param state
175 * @param taxonTreeMap
176 * @param treeRefFk
177 * @param child
178 * @param parent
179 * @param citation
180 * @param microCitation
181 * @return
182 */
183
184 public static final String TAXONOMIC_TREE_NAMESPACE = "TaxonomicTree";
185
186 private boolean makeTaxonomicallyIncluded(STATE state, Integer treeRefFk, Taxon child, Taxon parent, ReferenceBase citation, String microCitation){
187 String treeKey;
188 UUID treeUuid;
189 if (treeRefFk == null){
190 treeKey = "1"; // there is only one tree and it gets the key '1'
191 treeUuid = state.getConfig().getTaxonomicTreeUuid();
192 }else{
193 treeKey =String.valueOf(treeRefFk);
194 treeUuid = state.getTreeUuidByTreeKey(treeKey);
195 }
196 TaxonomicTree tree = (TaxonomicTree)state.getRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey);
197 if (tree == null){
198 ITaxonTreeService service = state.getCurrentIO().getTaxonTreeService();
199 tree = service.getTaxonomicTreeByUuid(treeUuid);
200 if (tree == null){
201 String treeName = state.getConfig().getTaxonomicTreeName();
202 tree = TaxonomicTree.NewInstance(treeName);
203 tree.setUuid(treeUuid);
204 //FIXME tree reference
205 //tree.setReference(ref);
206 service.save(tree);
207 }
208 state.addRelatedObject(TAXONOMIC_TREE_NAMESPACE, treeKey, tree);
209 }
210
211 tree.addParentChild(parent, child, citation, microCitation);
212 return true;
213 }
214
215 //
216 // private boolean makeTaxonomicallyIncluded_OLD(STATE state, Integer treeRefFk, Taxon child, Taxon parent, ReferenceBase citation, String microCitation){
217 // Map<Integer, TaxonomicTree> taxonTreeMap = state.getPartitionTaxonTreeMap();
218 //
219 //
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.");
230 // }
231 // taxonTreeMap.put(treeRefFk, tree);
232 // }
233 // return tree.addParentChild(parent, child, citation, microCitation);
234 // }
235
236
237 /**
238 * //TODO copied from DbImportObjectMapper. Maybe these can be merged again in future
239 * @param rs
240 * @param dbAttribute
241 * @return
242 * @throws SQLException
243 */
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);
251 }
252 return result;
253 }
254
255
256 /**
257 * Checks if cdmBase is of type Taxon
258 * @param taxonBase
259 * @param typeString
260 * @param id
261 * @return
262 */
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);
268 }
269 return (taxonBase.deproxy(taxonBase, Taxon.class));
270 }
271
272
273 }