slightly better handling of precision
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMapping.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.Set;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
21 import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23
24 /**
25 * @author a.mueller
26 * @created 12.05.2009
27 * @version 1.0
28 */
29 public class DbImportMapping<STATE extends DbImportStateBase, CONFIG extends IImportConfigurator> extends CdmIoMapping {
30 private static final Logger logger = Logger.getLogger(DbImportMapping.class);
31
32 private boolean isInitialized = false;;
33 private Class<? extends CdmBase> destinationClass;
34 private DbImportMapping<STATE, CONFIG> secondPathMapping;
35 private boolean blankToNull = false;
36
37 public DbImportMapping(){
38 // this.dbTableName = tableName;
39 }
40
41 public boolean initialize(DbImportStateBase state, Class<? extends CdmBase> destinationClass){
42 if (!isInitialized){
43 // this.dbTableName = tableName;
44 this.destinationClass = destinationClass;
45 for (CdmMapperBase mapper: this.mapperList){
46 if (mapper instanceof IDbImportMapper){
47 ((IDbImportMapper) mapper).initialize(state, destinationClass);
48 }else{
49 logger.warn("Mapper type " + mapper.getClass().getSimpleName() + " not yet implemented for DB import mapping");
50 }
51 }
52 isInitialized = true;
53 if (secondPathMapping != null){
54 secondPathMapping.initialize(state, destinationClass);
55 }
56 }
57 return true;
58 }
59
60 public void addMapper(CdmAttributeMapperBase mapper){
61 super.addMapper(mapper);
62 if (mapper instanceof DbStringMapper){
63 ((DbStringMapper)mapper).setBlankToNull(isBlankToNull());
64 }
65 }
66
67 /**
68 * Invokes the second path mapping if one has been defined
69 * @param rs
70 * @param objectsToSave
71 * @return
72 * @throws SQLException
73 */
74 public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave) throws SQLException{
75 return invoke(rs, objectsToSave, false);
76 }
77
78 /**
79 * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
80 * @param rs
81 * @param objectsToSave
82 * @param secondPath
83 * @return
84 * @throws SQLException
85 */
86 public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave, boolean secondPath) throws SQLException{
87 boolean result = true;
88 if (secondPath == true && secondPathMapping != null){
89 return secondPathMapping.invoke(rs, objectsToSave);
90 } else {
91 CdmBase objectToSave = null;
92 // try {
93 for (CdmMapperBase mapper : this.mapperList){
94 if (mapper instanceof IDbImportMapper){
95 IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper)mapper;
96 try {
97 objectToSave = dbMapper.invoke(rs, objectToSave);
98 } catch (Exception e) {
99 result = false;
100 logger.error("Error occurred in mapping.invoke of mapper " + this.toString());
101 e.printStackTrace();
102 continue;
103 }
104 }else{
105 logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());
106 }
107 }
108 if (objectToSave != null){
109 objectsToSave.add(objectToSave);
110 }else{
111 logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");
112 }
113 return result;
114 }
115 }
116
117 public void setSecondPathMapping(DbImportMapping secondPathMapping){
118 this.secondPathMapping = secondPathMapping;
119 }
120
121 /**
122 * If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
123 * @return
124 */
125 public boolean isBlankToNull() {
126 return blankToNull;
127 }
128
129 /**
130 * @see #isBlankToNull()
131 * @param blankToNull
132 */
133 public void setBlankToNull(boolean blankToNull) {
134 this.blankToNull = blankToNull;
135 }
136
137 // /**
138 // * @return the berlinModelTableName
139 // */
140 // public String getDbTableName() {
141 // return dbTableName;
142 // }
143 //
144 // /**
145 // * @param berlinModelTableName the berlinModelTableName to set
146 // */
147 // public void setDbTableName(String dbTableName) {
148 // this.dbTableName = dbTableName;
149 // }
150 //
151 //
152 // protected List<CdmAttributeMapperBase> getAttributeMapperList(){
153 // List<CdmAttributeMapperBase> list = this.mapperList;
154 // return list;
155 // }
156
157
158 }