\r
package eu.etaxonomy.cdm.io.common.mapping;\r
\r
-import java.sql.PreparedStatement;\r
import java.sql.ResultSet;\r
import java.sql.SQLException;\r
-import java.util.ArrayList;\r
-import java.util.HashSet;\r
-import java.util.List;\r
import java.util.Set;\r
\r
import org.apache.log4j.Logger;\r
\r
-import eu.etaxonomy.cdm.database.ICdmDataSource;\r
-import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper;\r
-import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IndexCounter;\r
-import eu.etaxonomy.cdm.io.common.DbExportStateBase;\r
import eu.etaxonomy.cdm.io.common.DbImportStateBase;\r
import eu.etaxonomy.cdm.io.common.IImportConfigurator;\r
+import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;\r
import eu.etaxonomy.cdm.model.common.CdmBase;\r
\r
/**\r
public class DbImportMapping<STATE extends DbImportStateBase, CONFIG extends IImportConfigurator> extends CdmIoMapping {\r
private static final Logger logger = Logger.getLogger(DbImportMapping.class);\r
\r
-// private PreparedStatement preparedStatement;\r
- private String dbTableName;\r
- //private List<CollectionExportMapping> collectionMappingList = new ArrayList<CollectionExportMapping>();\r
-\r
- public DbImportMapping(String tableName){\r
- this.dbTableName = tableName;\r
+ private boolean isInitialized = false;;\r
+ private Class<? extends CdmBase> destinationClass;\r
+ private DbImportMapping<STATE, CONFIG> secondPathMapping;\r
+ private boolean blankToNull = false;\r
+ \r
+ public DbImportMapping(){\r
+// this.dbTableName = tableName;\r
}\r
\r
- public boolean initialize(){\r
+ public boolean initialize(DbImportStateBase state, Class<? extends CdmBase> destinationClass){\r
+ if (!isInitialized){\r
+ // this.dbTableName = tableName;\r
+ this.destinationClass = destinationClass;\r
+ for (CdmMapperBase mapper: this.mapperList){\r
+ if (mapper instanceof IDbImportMapper){\r
+ ((IDbImportMapper) mapper).initialize(state, destinationClass);\r
+ }else{\r
+ logger.warn("Mapper type " + mapper.getClass().getSimpleName() + " not yet implemented for DB import mapping");\r
+ }\r
+ }\r
+ isInitialized = true;\r
+ if (secondPathMapping != null){\r
+ secondPathMapping.initialize(state, destinationClass);\r
+ }\r
+ }\r
return true;\r
}\r
- \r
-// public boolean initialize(STATE state) throws SQLException{\r
-// CONFIG config = (CONFIG)state.getConfig();\r
-// ICdmDataSource db = config.getDestination();\r
-// \r
-// try {\r
-// IndexCounter index;\r
-// String strPreparedStatement = prepareStatement();\r
-// logger.debug(strPreparedStatement);\r
-// this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);\r
-// index = new IndexCounter(1);\r
-// \r
-// for (CdmAttributeMapperBase mapper : this.mapperList){\r
-// if (mapper instanceof IDbExportMapper){\r
-// IDbExportMapper<DbExportStateBase<?>> dbMapper = (IDbExportMapper)mapper;\r
-// dbMapper.initialize(preparedStatement, index, state, dbTableName);\r
-// }else{\r
-// logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());\r
-// }\r
-// }\r
-// for (CollectionExportMapping collectionMapping : this.collectionMappingList ){\r
-// collectionMapping.initialize(state);\r
-// }\r
-// return true;\r
-// } catch (SQLException e) {\r
-// logger.warn("SQL Exception");\r
-// throw e;\r
-// }\r
-// }\r
\r
+ public void addMapper(CdmAttributeMapperBase mapper){\r
+ super.addMapper(mapper);\r
+ if (mapper instanceof DbStringMapper){\r
+ ((DbStringMapper)mapper).setBlankToNull(isBlankToNull());\r
+ }\r
+ }\r
\r
+ /**\r
+ * Invokes the second path mapping if one has been defined\r
+ * @param rs\r
+ * @param objectsToSave\r
+ * @return\r
+ * @throws SQLException\r
+ */\r
public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave) throws SQLException{\r
+ return invoke(rs, objectsToSave, false);\r
+ } \r
+ \r
+ /**\r
+ * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.\r
+ * @param rs\r
+ * @param objectsToSave\r
+ * @param secondPath\r
+ * @return\r
+ * @throws SQLException\r
+ */\r
+ public boolean invoke(ResultSet rs, Set<CdmBase> objectsToSave, boolean secondPath) throws SQLException{\r
boolean result = true;\r
- CdmBase objectToSave = null;\r
-// try {\r
- for (CdmAttributeMapperBase mapper : this.mapperList){\r
+ if (secondPath == true && secondPathMapping != null){\r
+ return secondPathMapping.invoke(rs, objectsToSave);\r
+ } else {\r
+ CdmBase objectToSave = null;\r
+ // try {\r
+ for (CdmMapperBase mapper : this.mapperList){\r
if (mapper instanceof IDbImportMapper){\r
- IDbImportMapper<DbImportStateBase<?>,CdmBase> dbMapper = (IDbImportMapper)mapper;\r
+ IDbImportMapper<DbImportStateBase<?,?>,CdmBase> dbMapper = (IDbImportMapper)mapper;\r
try {\r
objectToSave = dbMapper.invoke(rs, objectToSave);\r
} catch (Exception e) {\r
result = false;\r
- logger.error("Error occurred in mapping.invoke");\r
+ logger.error("Error occurred in mapping.invoke of mapper " + this.toString());\r
e.printStackTrace();\r
continue;\r
}\r
logger.warn("mapper is not of type " + IDbImportMapper.class.getSimpleName());\r
}\r
}\r
- objectsToSave.add(objectToSave);\r
-// int count = preparedStatement.executeUpdate();\r
-// if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);\r
-// for (CollectionExportMapping collectionMapping : this.collectionMappingList ){\r
-// result &= collectionMapping.invoke(cdmBase);\r
-// }\r
+ if (objectToSave != null){\r
+ objectsToSave.add(objectToSave);\r
+ }else{\r
+ logger.warn("The objectToSave was (null). Please check that your mappers work correctly.");\r
+ }\r
return result;\r
-// } catch(SQLException e){\r
-// e.printStackTrace();\r
-// logger.error(e.getMessage() + ": " + rs.toString());\r
-// return null;\r
-// }\r
+ }\r
}\r
\r
- \r
-// public void addCollectionMapping(CollectionExportMapping collectionMapping){\r
-// this.collectionMappingList.add(collectionMapping);\r
-// }\r
- \r
-// protected String prepareStatement(){\r
-// String sqlInsert = "INSERT INTO " + getDbTableName() + " (";\r
-// String sqlValues = ") VALUES(";\r
-// String sqlEnd = ")";\r
-// String attributes = "";\r
-// String values = "";\r
-// for (String attribute : this.getDestinationAttributeList()){\r
-// attributes += "," + attribute;\r
-// values += ",?";\r
-// }\r
-// attributes = attributes.substring(1); //delete first ','\r
-// values = values.substring(1); //delete first ','\r
-// String result = sqlInsert + attributes + sqlValues + values + sqlEnd;\r
-// return result;\r
-// }\r
+ public void setSecondPathMapping(DbImportMapping secondPathMapping){\r
+ this.secondPathMapping = secondPathMapping;\r
+ }\r
+\r
+ /**\r
+ * If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>\r
+ * @return\r
+ */\r
+ public boolean isBlankToNull() {\r
+ return blankToNull;\r
+ }\r
+\r
+ /**\r
+ * @see #isBlankToNull()\r
+ * @param blankToNull\r
+ */\r
+ public void setBlankToNull(boolean blankToNull) {\r
+ this.blankToNull = blankToNull;\r
+ }\r
\r
// /**\r
// * @return the berlinModelTableName\r
// this.dbTableName = dbTableName;\r
// }\r
//\r
-// /**\r
-// * @return the preparedStatement\r
-// */\r
-// protected PreparedStatement getPreparedStatement() {\r
-// return preparedStatement;\r
-// }\r
-//\r
-// /**\r
-// * @param preparedStatement the preparedStatement to set\r
-// */\r
-// protected void setPreparedStatement(PreparedStatement preparedStatement) {\r
-// this.preparedStatement = preparedStatement;\r
-// }\r
// \r
// protected List<CdmAttributeMapperBase> getAttributeMapperList(){\r
// List<CdmAttributeMapperBase> list = this.mapperList;\r