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.berlinModel.out;
|
12
|
|
13
|
import java.sql.PreparedStatement;
|
14
|
import java.sql.SQLException;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.List;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
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.DbExportStateBase;
|
23
|
import eu.etaxonomy.cdm.io.common.Source;
|
24
|
import eu.etaxonomy.cdm.io.common.mapping.CdmAttributeMapperBase;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.CdmIoMapping;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
* @created 12.05.2009
|
31
|
* @version 1.0
|
32
|
*/
|
33
|
public class BerlinModelExportMapping extends CdmIoMapping {
|
34
|
private static final Logger logger = Logger.getLogger(BerlinModelExportMapping.class);
|
35
|
|
36
|
private PreparedStatement preparedStatement;
|
37
|
private String dbTableName;
|
38
|
private List<CollectionExportMapping> collectionMappingList = new ArrayList<CollectionExportMapping>();
|
39
|
|
40
|
|
41
|
public BerlinModelExportMapping(String tableName){
|
42
|
this.dbTableName = tableName;
|
43
|
}
|
44
|
|
45
|
public boolean initialize(BerlinModelExportState state) throws SQLException{
|
46
|
BerlinModelExportConfigurator bmeConfig = (BerlinModelExportConfigurator)state.getConfig();
|
47
|
Source db = bmeConfig.getDestination();
|
48
|
|
49
|
try {
|
50
|
IndexCounter index;
|
51
|
String strPreparedStatement = prepareStatement();
|
52
|
logger.debug(strPreparedStatement);
|
53
|
this.preparedStatement = db.getConnection().prepareStatement(strPreparedStatement);
|
54
|
index = new IndexCounter(1);
|
55
|
|
56
|
for (CdmAttributeMapperBase mapper : this.mapperList){
|
57
|
if (mapper instanceof IDbExportMapper){
|
58
|
IDbExportMapper<DbExportStateBase<?>> dbMapper = (IDbExportMapper)mapper;
|
59
|
dbMapper.initialize(preparedStatement, index, state, dbTableName);
|
60
|
}else{
|
61
|
logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());
|
62
|
}
|
63
|
}
|
64
|
for (CollectionExportMapping collectionMapping : this.collectionMappingList ){
|
65
|
collectionMapping.initialize(state);
|
66
|
}
|
67
|
return true;
|
68
|
} catch (SQLException e) {
|
69
|
logger.warn("SQL Exception");
|
70
|
throw e;
|
71
|
}
|
72
|
}
|
73
|
|
74
|
|
75
|
public boolean invoke(CdmBase cdmBase) throws SQLException{
|
76
|
try {
|
77
|
boolean result = true;
|
78
|
for (CdmAttributeMapperBase mapper : this.mapperList){
|
79
|
if (mapper instanceof IDbExportMapper){
|
80
|
IDbExportMapper<DbExportStateBase<?>> dbMapper = (IDbExportMapper)mapper;
|
81
|
try {
|
82
|
result &= dbMapper.invoke(cdmBase);
|
83
|
} catch (Exception e) {
|
84
|
result = false;
|
85
|
logger.error("Error occurred in mapping.invoke");
|
86
|
e.printStackTrace();
|
87
|
continue;
|
88
|
}
|
89
|
}else{
|
90
|
logger.warn("mapper is not of type " + IDbExportMapper.class.getSimpleName());
|
91
|
}
|
92
|
}
|
93
|
int count = preparedStatement.executeUpdate();
|
94
|
if (logger.isDebugEnabled())logger.debug("Number of rows affected: " + count);
|
95
|
for (CollectionExportMapping collectionMapping : this.collectionMappingList ){
|
96
|
result &= collectionMapping.invoke(cdmBase);
|
97
|
}
|
98
|
return result;
|
99
|
} catch(SQLException e){
|
100
|
e.printStackTrace();
|
101
|
logger.error(e.getMessage() + ": " + cdmBase.toString());
|
102
|
return false;
|
103
|
}
|
104
|
}
|
105
|
|
106
|
|
107
|
public void addCollectionMapping(CollectionExportMapping collectionMapping){
|
108
|
this.collectionMappingList.add(collectionMapping);
|
109
|
}
|
110
|
|
111
|
protected String prepareStatement(){
|
112
|
String sqlInsert = "INSERT INTO " + getDbTableName() + " (";
|
113
|
String sqlValues = ") VALUES(";
|
114
|
String sqlEnd = ")";
|
115
|
String attributes = "";
|
116
|
String values = "";
|
117
|
for (String attribute : this.getDestinationAttributeList()){
|
118
|
attributes += "," + attribute;
|
119
|
values += ",?";
|
120
|
}
|
121
|
attributes = attributes.substring(1); //delete first ','
|
122
|
values = values.substring(1); //delete first ','
|
123
|
String result = sqlInsert + attributes + sqlValues + values + sqlEnd;
|
124
|
return result;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* @return the berlinModelTableName
|
129
|
*/
|
130
|
public String getDbTableName() {
|
131
|
return dbTableName;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* @param berlinModelTableName the berlinModelTableName to set
|
136
|
*/
|
137
|
public void setDbTableName(String dbTableName) {
|
138
|
this.dbTableName = dbTableName;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* @return the preparedStatement
|
143
|
*/
|
144
|
protected PreparedStatement getPreparedStatement() {
|
145
|
return preparedStatement;
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* @param preparedStatement the preparedStatement to set
|
150
|
*/
|
151
|
protected void setPreparedStatement(PreparedStatement preparedStatement) {
|
152
|
this.preparedStatement = preparedStatement;
|
153
|
}
|
154
|
|
155
|
protected List<CdmAttributeMapperBase> getAttributeMapperList(){
|
156
|
List<CdmAttributeMapperBase> list = this.mapperList;
|
157
|
return list;
|
158
|
}
|
159
|
|
160
|
|
161
|
}
|