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
.model
.common
.CdmBase
;
28 public class DbImportMapping
<STATE
extends DbImportStateBase
, CONFIG
extends IImportConfigurator
> extends CdmIoMapping
{
29 private static final Logger logger
= Logger
.getLogger(DbImportMapping
.class);
31 private boolean isInitialized
= false;;
32 private Class
<?
extends CdmBase
> destinationClass
;
33 private DbImportMapping
<STATE
, CONFIG
> secondPathMapping
;
35 public DbImportMapping(){
36 // this.dbTableName = tableName;
39 public boolean initialize(DbImportStateBase state
, Class
<?
extends CdmBase
> destinationClass
){
41 // this.dbTableName = tableName;
42 this.destinationClass
= destinationClass
;
43 for (CdmAttributeMapperBase mapper
: this.mapperList
){
44 if (mapper
instanceof IDbImportMapper
){
45 ((IDbImportMapper
) mapper
).initialize(state
, destinationClass
);
49 if (secondPathMapping
!= null){
50 secondPathMapping
.initialize(state
, destinationClass
);
57 * Invokes the second path mapping if one has been defined
59 * @param objectsToSave
61 * @throws SQLException
63 public boolean invoke(ResultSet rs
, Set
<CdmBase
> objectsToSave
) throws SQLException
{
64 return invoke(rs
, objectsToSave
, false);
68 * Invokes the mapping. If secondPath is true, the secondPath mapping is invoked if it exists.
70 * @param objectsToSave
73 * @throws SQLException
75 public boolean invoke(ResultSet rs
, Set
<CdmBase
> objectsToSave
, boolean secondPath
) throws SQLException
{
76 boolean result
= true;
77 if (secondPath
== true && secondPathMapping
!= null){
78 return secondPathMapping
.invoke(rs
, objectsToSave
);
80 CdmBase objectToSave
= null;
82 for (CdmAttributeMapperBase mapper
: this.mapperList
){
83 if (mapper
instanceof IDbImportMapper
){
84 IDbImportMapper
<DbImportStateBase
<?
,?
>,CdmBase
> dbMapper
= (IDbImportMapper
)mapper
;
86 objectToSave
= dbMapper
.invoke(rs
, objectToSave
);
87 } catch (Exception e
) {
89 logger
.error("Error occurred in mapping.invoke of mapper " + this.toString());
94 logger
.warn("mapper is not of type " + IDbImportMapper
.class.getSimpleName());
97 if (objectToSave
!= null){
98 objectsToSave
.add(objectToSave
);
100 logger
.warn("The objectToSave was (null). Please check that your mappers work correctly.");
106 public void setSecondPathMapping(DbImportMapping secondPathMapping
){
107 this.secondPathMapping
= secondPathMapping
;
111 // * @return the berlinModelTableName
113 // public String getDbTableName() {
114 // return dbTableName;
118 // * @param berlinModelTableName the berlinModelTableName to set
120 // public void setDbTableName(String dbTableName) {
121 // this.dbTableName = dbTableName;
125 // protected List<CdmAttributeMapperBase> getAttributeMapperList(){
126 // List<CdmAttributeMapperBase> list = this.mapperList;