1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common.mapping.out;
|
11
|
|
12
|
import java.sql.PreparedStatement;
|
13
|
import java.sql.ResultSet;
|
14
|
import java.sql.SQLException;
|
15
|
import java.sql.Types;
|
16
|
import java.time.LocalDateTime;
|
17
|
import java.time.ZonedDateTime;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
22
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
23
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
24
|
import eu.etaxonomy.cdm.io.common.Source;
|
25
|
import eu.etaxonomy.cdm.io.common.mapping.CdmSingleAttributeMapperBase;
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
* @created 12.05.2009
|
31
|
*/
|
32
|
public abstract class DbSingleAttributeExportMapperBase<STATE extends DbExportStateBase<?, IExportTransformer>>
|
33
|
extends CdmSingleAttributeMapperBase
|
34
|
implements IDbExportMapper<STATE, IExportTransformer> {
|
35
|
|
36
|
private static final Logger logger = Logger.getLogger(DbSingleAttributeExportMapperBase.class);
|
37
|
|
38
|
protected DbExportMapperBase<STATE> exportMapperHelper = new DbExportMapperBase<STATE>();
|
39
|
private Integer precision = null;
|
40
|
protected boolean obligatory = true;
|
41
|
|
42
|
/**
|
43
|
* @param dbAttributString
|
44
|
* @param cdmAttributeString
|
45
|
*/
|
46
|
protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue) {
|
47
|
super(cdmAttributeString, dbAttributString, defaultValue);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* @param dbAttributString
|
52
|
* @param cdmAttributeString
|
53
|
*/
|
54
|
protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue, boolean obligatory) {
|
55
|
super(cdmAttributeString, dbAttributString, defaultValue);
|
56
|
this.obligatory = obligatory;
|
57
|
}
|
58
|
|
59
|
/* (non-Javadoc)
|
60
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IStatefulDbExportMapper#initialize(java.sql.PreparedStatement, eu.etaxonomy.cdm.io.berlinModel.out.mapper.IndexCounter, eu.etaxonomy.cdm.io.berlinModel.out.DbExportState)
|
61
|
*/
|
62
|
@Override
|
63
|
public void initialize(PreparedStatement stmt, IndexCounter index, STATE state, String tableName) {
|
64
|
exportMapperHelper.initialize(stmt, index, state, tableName);
|
65
|
this.precision = getDbColumnIntegerInfo("c.prec");
|
66
|
}
|
67
|
|
68
|
|
69
|
/* (non-Javadoc)
|
70
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper#invoke(eu.etaxonomy.cdm.model.common.CdmBase)
|
71
|
*/
|
72
|
@Override
|
73
|
public boolean invoke(CdmBase cdmBase) throws SQLException {
|
74
|
if (exportMapperHelper.preparedStatement == null){
|
75
|
logger.warn("PreparedStatement is null");
|
76
|
return false;
|
77
|
}else{
|
78
|
return doInvoke(cdmBase);
|
79
|
}
|
80
|
}
|
81
|
|
82
|
protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
|
83
|
try {
|
84
|
Object value = getValue(cdmBase);
|
85
|
int sqlType = getSqlType();
|
86
|
//use default value if necessary
|
87
|
if (value == null && defaultValue != null){
|
88
|
value = defaultValue;
|
89
|
}
|
90
|
if (value == null){
|
91
|
getPreparedStatement().setNull(getIndex(), sqlType);
|
92
|
}else{
|
93
|
if (sqlType == Types.INTEGER){
|
94
|
try{
|
95
|
getPreparedStatement().setInt(getIndex(), (Integer)value);
|
96
|
}catch (Exception e) {
|
97
|
logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
|
98
|
value = getValue(cdmBase);
|
99
|
throw new RuntimeException( e);
|
100
|
}
|
101
|
}else if (sqlType == Types.CLOB){
|
102
|
getPreparedStatement().setString(getIndex(), (String)value);
|
103
|
}else if (sqlType == Types.VARCHAR){
|
104
|
String strValue = (String)value;
|
105
|
// if (strValue.length() > 255){
|
106
|
// logger.debug("String to long (" + strValue.length() + ") for object " + cdmBase.toString() + ": " + value);
|
107
|
// }
|
108
|
int precision = getPrecision();
|
109
|
if (strValue.length() > 450){
|
110
|
logger.debug(">450");
|
111
|
}
|
112
|
|
113
|
if (strValue.length() > precision && precision > 0 ){
|
114
|
logger.warn("The length of the string to save ("+ getDestinationAttribute() + ") is longer than the database columns precision ("+precision+"). String will be truncated: " + strValue);
|
115
|
if (precision >= 4) {
|
116
|
strValue = strValue.substring(0, precision - 4 )+" ...";
|
117
|
} else {
|
118
|
strValue = strValue.substring(0, precision);
|
119
|
}
|
120
|
}
|
121
|
getPreparedStatement().setString(getIndex(), strValue);
|
122
|
}else if (sqlType == Types.BOOLEAN){
|
123
|
getPreparedStatement().setBoolean(getIndex(), (Boolean)value);
|
124
|
}else if (sqlType == Types.DATE){
|
125
|
LocalDateTime date = ((ZonedDateTime)value).toLocalDateTime();
|
126
|
java.sql.Timestamp sqlTimestamp = java.sql.Timestamp.valueOf(date);
|
127
|
getPreparedStatement().setTimestamp(getIndex(), sqlTimestamp);
|
128
|
}else{
|
129
|
throw new IllegalArgumentException("SqlType not yet supported yet: " + sqlType);
|
130
|
}
|
131
|
}
|
132
|
return true;
|
133
|
} catch (SQLException e) {
|
134
|
logger.warn("SQL Exception: " + e.getLocalizedMessage());
|
135
|
throw e;
|
136
|
} catch (IllegalArgumentException e) {
|
137
|
logger.error("IllegalArgumentException: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
|
138
|
return false;
|
139
|
} catch (Exception e) {
|
140
|
logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
|
141
|
throw new RuntimeException( e);
|
142
|
}
|
143
|
}
|
144
|
|
145
|
protected Object getValue(CdmBase cdmBase){
|
146
|
boolean isBoolean = (this.getTypeClass() == boolean.class || this.getTypeClass() == Boolean.class);
|
147
|
return ImportHelper.getValue(cdmBase, this.getSourceAttribute(), isBoolean, obligatory);
|
148
|
}
|
149
|
|
150
|
protected abstract int getSqlType();
|
151
|
|
152
|
/**
|
153
|
* @return the preparedStatement
|
154
|
*/
|
155
|
public PreparedStatement getPreparedStatement() {
|
156
|
return exportMapperHelper.getPreparedStatement();
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* @return the index
|
161
|
*/
|
162
|
public int getIndex() {
|
163
|
return exportMapperHelper.getIndex();
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* @return the state
|
168
|
*/
|
169
|
public STATE getState() {
|
170
|
return exportMapperHelper.getState();
|
171
|
}
|
172
|
|
173
|
|
174
|
/**
|
175
|
* @return the state
|
176
|
*/
|
177
|
public String getTableName() {
|
178
|
return exportMapperHelper.getTableName();
|
179
|
}
|
180
|
|
181
|
protected boolean checkSqlServerColumnExists(){
|
182
|
Source source = getState().getConfig().getDestination();
|
183
|
String strQuery = "SELECT Count(t.id) as n " +
|
184
|
" FROM sysobjects AS t " +
|
185
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
186
|
" WHERE (t.xtype = 'U') AND " +
|
187
|
" (t.name = '" + getTableName() + "') AND " +
|
188
|
" (c.name = '" + getDestinationAttribute() + "')";
|
189
|
ResultSet rs = source.getResultSet(strQuery) ;
|
190
|
int n;
|
191
|
try {
|
192
|
rs.next();
|
193
|
n = rs.getInt("n");
|
194
|
return n>0;
|
195
|
} catch (SQLException e) {
|
196
|
e.printStackTrace();
|
197
|
return false;
|
198
|
}
|
199
|
|
200
|
}
|
201
|
|
202
|
|
203
|
protected int getPrecision(){
|
204
|
return this.precision;
|
205
|
}
|
206
|
|
207
|
protected int getDbColumnIntegerInfo(String selectPart){
|
208
|
Source source = getState().getConfig().getDestination();
|
209
|
String strQuery = "SELECT " + selectPart + " as result" +
|
210
|
" FROM sysobjects AS t " +
|
211
|
" INNER JOIN syscolumns AS c ON t.id = c.id " +
|
212
|
" WHERE (t.xtype = 'U') AND " +
|
213
|
" (t.name = '" + getTableName() + "') AND " +
|
214
|
" (c.name = '" + getDestinationAttribute() + "')";
|
215
|
ResultSet rs = source.getResultSet(strQuery) ;
|
216
|
int n;
|
217
|
try {
|
218
|
rs.next();
|
219
|
n = rs.getInt("result");
|
220
|
return n;
|
221
|
} catch (SQLException e) {
|
222
|
logger.error(e.getMessage() + ", selectPart: " + CdmUtils.Nz(selectPart) + ", table: " + CdmUtils.Nz(getTableName()) +", destination: " + CdmUtils.Nz(getDestinationAttribute()));
|
223
|
e.printStackTrace();
|
224
|
return -1;
|
225
|
}
|
226
|
|
227
|
}
|
228
|
|
229
|
|
230
|
@Override
|
231
|
public String toString(){
|
232
|
String sourceAtt = CdmUtils.Nz(getSourceAttribute());
|
233
|
String destAtt = CdmUtils.Nz(getDestinationAttribute());
|
234
|
return this.getClass().getSimpleName() +"[" + sourceAtt + "->" + destAtt + "]";
|
235
|
}
|
236
|
|
237
|
}
|