Project

General

Profile

Download (7.82 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.logging.log4j.LogManager;
18
import org.apache.logging.log4j.Logger;
19
import org.joda.time.DateTime;
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
 * @since 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 = LogManager.getLogger();
37

    
38
	protected DbExportMapperBase<STATE> exportMapperHelper = new DbExportMapperBase<>();
39
	private Integer precision = null;
40
	//if the source attribute (not the value) is obligatory, currently results in error logging only
41
	protected boolean obligatory = true;
42
	protected boolean notNull = false;
43

    
44
	protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue) {
45
		super(cdmAttributeString, dbAttributString, defaultValue);
46
	}
47

    
48
	/**
49
     * @param cdmAttributeString source attribute (CDM)
50
     * @param dbAttributString target attribute (export DB)
51
     * @param defaultValue default value if source value is <code>null</code>
52
     * @param obligatory if the source attribute is obligatory, but value may be <code>null</code>
53
     */
54
    protected DbSingleAttributeExportMapperBase(String cdmAttributeString, String dbAttributString, Object defaultValue, boolean obligatory, boolean notNull) {
55
        super(cdmAttributeString, dbAttributString, defaultValue);
56
        this.notNull = notNull;
57
        this.obligatory = obligatory;
58
    }
59

    
60
	@Override
61
    public void initialize(PreparedStatement stmt, IndexCounter index, STATE state, String tableName) {
62
		exportMapperHelper.initialize(stmt, index, state, tableName);
63
		this.precision = getDbColumnIntegerInfo("c.prec");
64
	}
65

    
66
	@Override
67
    public boolean invoke(CdmBase cdmBase) throws SQLException {
68
		if (exportMapperHelper.preparedStatement == null){
69
			logger.warn("PreparedStatement is null");
70
			return false;
71
		}else{
72
			return doInvoke(cdmBase);
73
		}
74
	}
75

    
76
	protected boolean doInvoke(CdmBase cdmBase) throws SQLException {
77
		try {
78
			Object value = getValue(cdmBase);
79
			int sqlType = getSqlType();
80
			//use default value if necessary
81
			if (value == null && defaultValue != null){
82
				value = defaultValue;
83
			}
84
			if (value == null){
85
			    if (notNull){
86
			        logger.error("Value for '"+this.getSourceAttribute()+"' is null but a value is required. Object: " + cdmBase.toString());
87
			        return false;
88
			    }else{
89
			        getPreparedStatement().setNull(getIndex(), sqlType);
90
			    }
91
			}else{
92
				if (sqlType == Types.INTEGER){
93
					try{
94
						getPreparedStatement().setInt(getIndex(), (Integer)value);
95
					}catch (Exception e) {
96
						logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
97
						value = getValue(cdmBase);
98
						throw new RuntimeException( e);
99
					}
100
				}else if (sqlType == Types.CLOB){
101
					getPreparedStatement().setString(getIndex(), (String)value);
102
				}else if (sqlType == Types.VARCHAR){
103
					String strValue = (String)value;
104
//					if (strValue.length() > 255){
105
//						logger.debug("String to long (" + strValue.length() + ") for object " + cdmBase.toString() + ": " + value);
106
//					}
107
					int precision = getPrecision();
108
					if (strValue.length() > 450){
109
						logger.debug(">450");
110
					}
111

    
112
					if (strValue.length() > precision && precision > 0 ){
113
						logger.warn("The length of the string to save ("+ getDestinationAttribute() + ") is longer than the database columns precision ("+precision+"). String will be truncated: " + strValue);
114
						if (precision >= 4) {
115
							strValue = strValue.substring(0, precision - 4 )+" ...";
116
						} else {
117
							strValue = strValue.substring(0, precision);
118
						}
119
					}
120
					getPreparedStatement().setString(getIndex(), strValue);
121
				}else if (sqlType == Types.BOOLEAN){
122
					getPreparedStatement().setBoolean(getIndex(), (Boolean)value);
123
				}else if (sqlType == Types.DATE){
124
				    java.sql.Timestamp sqlTimestamp;
125
				    if(value instanceof String){
126
				        String strDate = (String)value;
127
				        sqlTimestamp = java.sql.Timestamp.valueOf(strDate);
128
				    }else{
129
				        DateTime dateTime = (DateTime)value;
130
				        java.util.Date date = dateTime.toDate();
131
				        long t = date.getTime();
132
				        sqlTimestamp = new java.sql.Timestamp(t);
133
				    }
134
					getPreparedStatement().setTimestamp(getIndex(), sqlTimestamp);
135
				}else{
136
					throw new IllegalArgumentException("SqlType not yet supported yet: " + sqlType);
137
				}
138
			}
139
			return true;
140
		} catch (SQLException e) {
141
			logger.warn("SQL Exception: " + e.getLocalizedMessage());
142
			throw e;
143
		} catch (IllegalArgumentException e) {
144
			logger.error("IllegalArgumentException: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
145
			return false;
146
		} catch (Exception e) {
147
			logger.error("Exception: " + e.getLocalizedMessage() + ": " + cdmBase.toString());
148
			throw new RuntimeException( e);
149
		}
150
	}
151

    
152
	protected Object getValue(CdmBase cdmBase){
153
		boolean isBoolean = (this.getTypeClass() == boolean.class || this.getTypeClass() == Boolean.class);
154
		return ImportHelper.getValue(cdmBase, this.getSourceAttribute(), isBoolean, obligatory);
155
	}
156

    
157
	protected abstract int getSqlType();
158

    
159
	public PreparedStatement getPreparedStatement() {
160
		return exportMapperHelper.getPreparedStatement();
161
	}
162

    
163
	public int getIndex() {
164
		return exportMapperHelper.getIndex();
165
	}
166

    
167
	public STATE getState() {
168
		return exportMapperHelper.getState();
169
	}
170

    
171
	public String getTableName() {
172
		return exportMapperHelper.getTableName();
173
	}
174

    
175
	protected boolean checkSqlServerColumnExists(){
176
		Source source = getState().getConfig().getDestination();
177
		String strQuery = "SELECT  Count(t.id) as n " +
178
				" FROM sysobjects AS t " +
179
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
180
				" WHERE (t.xtype = 'U') AND " +
181
				" (t.name = '" + getTableName() + "') AND " +
182
				" (c.name = '" + getDestinationAttribute() + "')";
183
		ResultSet rs = source.getResultSet(strQuery) ;
184
		int n;
185
		try {
186
			rs.next();
187
			n = rs.getInt("n");
188
			return n>0;
189
		} catch (SQLException e) {
190
			e.printStackTrace();
191
			return false;
192
		}
193
	}
194

    
195
	protected int getPrecision(){
196
		return this.precision;
197
	}
198

    
199
	protected int getDbColumnIntegerInfo(String selectPart){
200
		Source source = getState().getConfig().getDestination();
201
		String strQuery = "SELECT  " + selectPart + " as result" +
202
				" FROM sysobjects AS t " +
203
				" INNER JOIN syscolumns AS c ON t.id = c.id " +
204
				" WHERE (t.xtype = 'U') AND " +
205
				" (t.name = '" + getTableName() + "') AND " +
206
				" (c.name = '" + getDestinationAttribute() + "')";
207
		ResultSet rs = source.getResultSet(strQuery) ;
208
		int n;
209
		try {
210
			rs.next();
211
			n = rs.getInt("result");
212
			return n;
213
		} catch (Exception e) {
214
			System.out.println(strQuery);
215
		    logger.error(e.getMessage() + ", selectPart: " + CdmUtils.Nz(selectPart) + ", table: " + CdmUtils.Nz(getTableName()) +", destination: " + CdmUtils.Nz(getDestinationAttribute()));
216
			e.printStackTrace();
217
			return -1;
218
		}
219
	}
220

    
221
	@Override
222
    public String toString(){
223
		String sourceAtt = CdmUtils.Nz(getSourceAttribute());
224
		String destAtt = CdmUtils.Nz(getDestinationAttribute());
225
		return this.getClass().getSimpleName() +"[" + sourceAtt + "->" + destAtt + "]";
226
	}
227
}
(30-30/44)