3 * Copyright (C) 2009 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.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
12 import java
.sql
.PreparedStatement
;
13 import java
.sql
.SQLException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.CollectionExportMapping
;
20 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IDbExportMapper
;
21 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.IndexCounter
;
22 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.CdmAttributeMapperBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.CdmIoMapping
;
24 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 public class PesiExportMapping
extends CdmIoMapping
{
34 private static final Logger logger
= Logger
.getLogger(PesiExportMapping
.class);
36 private PreparedStatement preparedStatement
;
37 private String dbTableName
;
38 private List
<PesiCollectionExportMapping
> collectionMappingList
= new ArrayList
<PesiCollectionExportMapping
>();
41 public PesiExportMapping(String tableName
){
42 this.dbTableName
= tableName
;
45 public boolean initialize(PesiExportState state
) throws SQLException
{
46 PesiExportConfigurator bmeConfig
= (PesiExportConfigurator
)state
.getConfig();
47 Source db
= bmeConfig
.getDestination();
51 String strPreparedStatement
= prepareStatement();
52 logger
.debug(strPreparedStatement
);
53 this.preparedStatement
= db
.getConnection().prepareStatement(strPreparedStatement
);
54 index
= new IndexCounter(1);
56 for (CdmAttributeMapperBase mapper
: this.mapperList
){
57 if (mapper
instanceof IDbExportMapper
){
58 IDbExportMapper
<DbExportStateBase
<?
>> dbMapper
= (IDbExportMapper
)mapper
;
59 dbMapper
.initialize(preparedStatement
, index
, state
, dbTableName
);
61 logger
.warn("mapper is not of type " + IDbExportMapper
.class.getSimpleName());
64 for (PesiCollectionExportMapping collectionMapping
: this.collectionMappingList
){
65 collectionMapping
.initialize(state
);
68 } catch (SQLException e
) {
69 logger
.warn("SQL Exception");
75 public boolean invoke(CdmBase cdmBase
) throws SQLException
{
77 boolean result
= true;
78 for (CdmAttributeMapperBase mapper
: this.mapperList
){
79 if (mapper
instanceof IDbExportMapper
){
80 IDbExportMapper
<DbExportStateBase
<?
>> dbMapper
= (IDbExportMapper
<DbExportStateBase
<?
>>)mapper
;
82 result
&= dbMapper
.invoke(cdmBase
);
83 } catch (Exception e
) {
85 logger
.error("Error occurred in mapping.invoke");
90 logger
.warn("mapper is not of type " + IDbExportMapper
.class.getSimpleName());
93 int count
= preparedStatement
.executeUpdate();
94 if (logger
.isDebugEnabled())logger
.debug("Number of rows affected: " + count
);
95 for (PesiCollectionExportMapping collectionMapping
: this.collectionMappingList
){
96 result
&= collectionMapping
.invoke(cdmBase
);
99 } catch(SQLException e
){
101 logger
.error(e
.getMessage() + ": " + cdmBase
.toString());
107 public void addCollectionMapping(PesiCollectionExportMapping collectionMapping
){
108 this.collectionMappingList
.add(collectionMapping
);
111 protected String
prepareStatement(){
112 String sqlInsert
= "INSERT INTO " + getDbTableName() + " (";
113 String sqlValues
= ") VALUES(";
115 String attributes
= "";
117 for (String attribute
: this.getDestinationAttributeList()){
118 attributes
+= "," + attribute
;
121 attributes
= attributes
.substring(1); //delete first ','
122 values
= values
.substring(1); //delete first ','
123 String result
= sqlInsert
+ attributes
+ sqlValues
+ values
+ sqlEnd
;
128 * @return the pesiTableName
130 public String
getDbTableName() {
135 * @param pesiTableName the pesiTableName to set
137 public void setDbTableName(String dbTableName
) {
138 this.dbTableName
= dbTableName
;
142 * @return the preparedStatement
144 protected PreparedStatement
getPreparedStatement() {
145 return preparedStatement
;
149 * @param preparedStatement the preparedStatement to set
151 protected void setPreparedStatement(PreparedStatement preparedStatement
) {
152 this.preparedStatement
= preparedStatement
;
155 protected List
<CdmAttributeMapperBase
> getAttributeMapperList(){
156 List
<CdmAttributeMapperBase
> list
= this.mapperList
;