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
;
17 import org
.apache
.log4j
.Logger
;
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
;
29 public class DbImportMapping
<STATE
extends DbImportStateBase
, CONFIG
extends IImportConfigurator
> extends CdmIoMapping
{
30 private static final Logger logger
= Logger
.getLogger(DbImportMapping
.class);
32 private boolean isInitialized
= false;;
33 private Class
<?
extends CdmBase
> destinationClass
;
34 private DbImportMapping
<STATE
, CONFIG
> secondPathMapping
;
35 private boolean blankToNull
= false;
37 public DbImportMapping(){
38 // this.dbTableName = tableName;
41 public boolean initialize(DbImportStateBase state
, Class
<?
extends CdmBase
> destinationClass
){
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
);
49 logger
.warn("Mapper type " + mapper
.getClass().getSimpleName() + " not yet implemented for DB import mapping");
53 if (secondPathMapping
!= null){
54 secondPathMapping
.initialize(state
, destinationClass
);
60 public void addMapper(CdmAttributeMapperBase mapper
){
61 super.addMapper(mapper
);
62 if (mapper
instanceof DbStringMapper
){
63 ((DbStringMapper
)mapper
).setBlankToNull(isBlankToNull());
68 * Invokes the second path mapping if one has been defined
70 * @param objectsToSave
72 * @throws SQLException
74 public boolean invoke(ResultSet rs
, Set
<CdmBase
> objectsToSave
) throws SQLException
{
75 return invoke(rs
, objectsToSave
, false);
79 * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
81 * @param objectsToSave
84 * @throws SQLException
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
);
91 CdmBase objectToSave
= null;
93 for (CdmMapperBase mapper
: this.mapperList
){
94 if (mapper
instanceof IDbImportMapper
){
95 IDbImportMapper
<DbImportStateBase
<?
,?
>,CdmBase
> dbMapper
= (IDbImportMapper
)mapper
;
97 objectToSave
= dbMapper
.invoke(rs
, objectToSave
);
98 } catch (Exception e
) {
100 logger
.error("Error occurred in mapping.invoke of mapper " + this.toString());
105 logger
.warn("mapper is not of type " + IDbImportMapper
.class.getSimpleName());
108 if (objectToSave
!= null){
109 objectsToSave
.add(objectToSave
);
111 logger
.warn("The objectToSave was (null). Please check that your mappers work correctly.");
117 public void setSecondPathMapping(DbImportMapping secondPathMapping
){
118 this.secondPathMapping
= secondPathMapping
;
122 * If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
125 public boolean isBlankToNull() {
130 * @see #isBlankToNull()
133 public void setBlankToNull(boolean blankToNull
) {
134 this.blankToNull
= blankToNull
;
138 // * @return the berlinModelTableName
140 // public String getDbTableName() {
141 // return dbTableName;
145 // * @param berlinModelTableName the berlinModelTableName to set
147 // public void setDbTableName(String dbTableName) {
148 // this.dbTableName = dbTableName;
152 // protected List<CdmAttributeMapperBase> getAttributeMapperList(){
153 // List<CdmAttributeMapperBase> list = this.mapperList;