Project

General

Profile

Download (7.7 KB) Statistics
| Branch: | Tag: | Revision:
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

    
17
import org.apache.log4j.Logger;
18
import org.joda.time.DateTime;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
22
import eu.etaxonomy.cdm.io.common.ImportHelper;
23
import eu.etaxonomy.cdm.io.common.Source;
24
import eu.etaxonomy.cdm.io.common.mapping.CdmSingleAttributeMapperBase;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26

    
27
/**
28
 * @author a.mueller
29
 * @created 12.05.2009
30
 * @version 1.0
31
 */
32
public abstract class DbSingleAttributeExportMapperBase<STATE extends DbExportStateBase<?, IExportTransformer>> extends CdmSingleAttributeMapperBase implements IDbExportMapper<STATE, IExportTransformer>  {
33
	private static final Logger logger = Logger.getLogger(DbSingleAttributeExportMapperBase.class);
34
	
35
	protected DbExportMapperBase<STATE> exportMapperHelper = new DbExportMapperBase<STATE>();
36
	private Integer precision = null;
37
	protected boolean obligatory = true;
38
	
39
	/**
40
	 * @param dbAttributString
41
	 * @param cdmAttributeString
42
	 */
43
	protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue) {
44
		super(cdmAttributeString, dbAttributString, defaultValue);
45
	}
46
	
47
	/**
48
	 * @param dbAttributString
49
	 * @param cdmAttributeString
50
	 */
51
	protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue, boolean obligatory) {
52
		super(cdmAttributeString, dbAttributString, defaultValue);
53
		this.obligatory = obligatory;
54
	}
55
	
56
	/* (non-Javadoc)
57
	 * @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)
58
	 */
59
	public void initialize(PreparedStatement stmt, IndexCounter index, STATE state, String tableName) {
60
		exportMapperHelper.initialize(stmt, index, state, tableName);
61
		this.precision = getDbColumnIntegerInfo("c.prec");	
62
	}
63

    
64

    
65
	/* (non-Javadoc)
66
	 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper#invoke(eu.etaxonomy.cdm.model.common.CdmBase)
67
	 */
68
	public boolean invoke(CdmBase cdmBase) throws SQLException {
69
		if (exportMapperHelper.preparedStatement == null){
70
			logger.warn("PreparedStatement is null");
71
			return false;
72
		}else{
73
			return doInvoke(cdmBase);
74
		}
75
	}
76
	
77
	protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
78
		try {
79
			Object value = getValue(cdmBase);
80
			int sqlType = getSqlType();
81
			//use default value if necessary
82
			if (value == null && defaultValue != null){
83
				value = defaultValue;
84
			}
85
			if (value == null){
86
				getPreparedStatement().setNull(getIndex(), sqlType);
87
			}else{
88
				if (sqlType == Types.INTEGER){
89
					try{
90
						getPreparedStatement().setInt(getIndex(), (Integer)value);
91
					}catch (Exception e) {
92
						logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
93
						value = getValue(cdmBase);
94
						throw new RuntimeException( e);
95
					}
96
				}else if (sqlType == Types.CLOB){
97
					getPreparedStatement().setString(getIndex(), (String)value);
98
				}else if (sqlType == Types.VARCHAR){
99
					String strValue = (String)value;
100
//					if (strValue.length() > 255){
101
//						logger.debug("String to long (" + strValue.length() + ") for object " + cdmBase.toString() + ": " + value);
102
//					}
103
					int precision = getPrecision();
104
					if (strValue.length() > 450){
105
						logger.debug(">450");
106
					}
107
					
108
					if (strValue.length() > precision && precision > 0 ){
109
						logger.warn("The length of the string to save ("+ getDestinationAttribute() + ") is longer than the database columns precision ("+precision+"). String will be truncated: " + strValue);
110
						if (precision >= 4) {
111
							strValue = strValue.substring(0, precision - 4 )+" ...";
112
						} else {
113
							strValue = strValue.substring(0, precision);
114
						}
115
					}
116
					getPreparedStatement().setString(getIndex(), strValue);
117
				}else if (sqlType == Types.BOOLEAN){
118
					getPreparedStatement().setBoolean(getIndex(), (Boolean)value);
119
				}else if (sqlType == Types.DATE){
120
					java.util.Date date = ((DateTime)value).toDate();
121
					long t = date.getTime();
122
					java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);
123
					getPreparedStatement().setTimestamp(getIndex(), sqlTimestamp);
124
				}else{
125
					throw new IllegalArgumentException("SqlType not yet supported yet: " + sqlType);
126
				}
127
			}
128
			return true;
129
		} catch (SQLException e) {
130
			logger.warn("SQL Exception: " + e.getLocalizedMessage());
131
			throw e;
132
		} catch (IllegalArgumentException e) {
133
			logger.error("IllegalArgumentException: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
134
			return false;
135
		} catch (Exception e) {
136
			logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
137
			throw new RuntimeException( e);
138
		}
139
	}
140
	
141
	protected Object getValue(CdmBase cdmBase){
142
		boolean isBoolean = (this.getTypeClass() == boolean.class || this.getTypeClass() == Boolean.class);
143
		return ImportHelper.getValue(cdmBase, this.getSourceAttribute(), isBoolean, obligatory);
144
	}
145
	
146
	protected abstract int getSqlType();
147
	
148
	/**
149
	 * @return the preparedStatement
150
	 */
151
	public PreparedStatement getPreparedStatement() {
152
		return exportMapperHelper.getPreparedStatement();
153
	}
154
	
155
	/**
156
	 * @return the index
157
	 */
158
	public int getIndex() {
159
		return exportMapperHelper.getIndex();
160
	}
161
	
162
	/**
163
	 * @return the state
164
	 */
165
	public STATE getState() {
166
		return exportMapperHelper.getState();
167
	}
168
	
169
	
170
	/**
171
	 * @return the state
172
	 */
173
	public String getTableName() {
174
		return exportMapperHelper.getTableName();
175
	}
176
	
177
	protected boolean checkSqlServerColumnExists(){
178
		Source source = getState().getConfig().getDestination();
179
		String strQuery = "SELECT  Count(t.id) as n " +
180
				" FROM sysobjects AS t " +
181
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
182
				" WHERE (t.xtype = 'U') AND " + 
183
				" (t.name = '" + getTableName() + "') AND " + 
184
				" (c.name = '" + getDestinationAttribute() + "')";
185
		ResultSet rs = source.getResultSet(strQuery) ;		
186
		int n;
187
		try {
188
			rs.next();
189
			n = rs.getInt("n");
190
			return n>0;
191
		} catch (SQLException e) {
192
			e.printStackTrace();
193
			return false;
194
		}
195
		
196
	}
197
	
198
	
199
	protected int getPrecision(){
200
		return this.precision;
201
	}
202
	
203
	protected int getDbColumnIntegerInfo(String selectPart){
204
		Source source = getState().getConfig().getDestination();
205
		String strQuery = "SELECT  " + selectPart + " as result" +
206
				" FROM sysobjects AS t " +
207
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
208
				" WHERE (t.xtype = 'U') AND " + 
209
				" (t.name = '" + getTableName() + "') AND " + 
210
				" (c.name = '" + getDestinationAttribute() + "')";
211
		ResultSet rs = source.getResultSet(strQuery) ;		
212
		int n;
213
		try {
214
			rs.next();
215
			n = rs.getInt("result");
216
			return n;
217
		} catch (SQLException e) {
218
			logger.error(e.getMessage() + ", selectPart: " + CdmUtils.Nz(selectPart) + ", table: " + CdmUtils.Nz(getTableName()) +", destination: " + CdmUtils.Nz(getDestinationAttribute()));
219
			e.printStackTrace();
220
			return -1;
221
		}
222
			
223
	}
224
	
225
	
226
	public String toString(){
227
		String sourceAtt = CdmUtils.Nz(getSourceAttribute());
228
		String destAtt = CdmUtils.Nz(getDestinationAttribute());
229
		return this.getClass().getSimpleName() +"[" + sourceAtt + "->" + destAtt + "]";
230
	}
231
	
232
}
(27-27/40)