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.Types;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
|
18
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
19
|
|
20
|
/**
|
21
|
* @author a.mueller
|
22
|
* @since 12.05.2009
|
23
|
* @version 1.0
|
24
|
*/
|
25
|
public class DbStringMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?,IExportTransformer>, IExportTransformer> {
|
26
|
private static final Logger logger = Logger.getLogger(DbStringMapper.class);
|
27
|
|
28
|
private static final int MAX_PRECISION = -1; //precision for datatype nvarchar(max) == clob (SQL Server 2008)
|
29
|
private boolean blankToNull = false;
|
30
|
|
31
|
public static DbStringMapper NewInstance(String cdmAttributeString, String dbAttributeString){
|
32
|
return new DbStringMapper(cdmAttributeString, dbAttributeString, null, true);
|
33
|
}
|
34
|
|
35
|
public static DbStringMapper NewFacultativeInstance(String cdmAttributeString, String dbAttributeString){
|
36
|
return new DbStringMapper(cdmAttributeString, dbAttributeString, null, false);
|
37
|
}
|
38
|
|
39
|
public static DbStringMapper NewInstance(String cdmAttributeString, String dbAttributeString, String defaultValue){
|
40
|
return new DbStringMapper(cdmAttributeString, dbAttributeString, defaultValue, false);
|
41
|
}
|
42
|
|
43
|
public static DbStringMapper NewInstance(String cdmAttributeString, String dbAttributeString, String defaultValue, boolean obligatory){
|
44
|
return new DbStringMapper(cdmAttributeString, dbAttributeString, defaultValue, obligatory);
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* @param dbAttributeString
|
49
|
* @param cdmAttributeString
|
50
|
*/
|
51
|
private DbStringMapper(String cdmAttributeString, String dbAttributeString, String defaultValue, boolean obligatory) {
|
52
|
super(cdmAttributeString, dbAttributeString, defaultValue, obligatory);
|
53
|
}
|
54
|
|
55
|
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue(eu.etaxonomy.cdm.model.common.CdmBase)
|
59
|
*/
|
60
|
@Override
|
61
|
protected Object getValue(CdmBase cdmBase) {
|
62
|
String result = (String)super.getValue(cdmBase);
|
63
|
if (isBlankToNull() && StringUtils.isBlank(result)){
|
64
|
result = null;
|
65
|
}
|
66
|
if (result != null){
|
67
|
if (result.startsWith(" ") || result.endsWith(" ")){
|
68
|
result = result.trim();
|
69
|
}
|
70
|
//truncate if needed
|
71
|
if (result.length() > getPrecision() && getPrecision() != MAX_PRECISION && getPrecision() > 0){
|
72
|
logger.warn("Truncation (" + result.length() + "->" + getPrecision() + ") needed for Attribute " + getDestinationAttribute() + " in " + cdmBase + "." );
|
73
|
result = result.substring(0, getPrecision());
|
74
|
}
|
75
|
}
|
76
|
return result;
|
77
|
}
|
78
|
|
79
|
|
80
|
/**
|
81
|
* If <code>true</code> all {@link DbStringMapper} map blank strings to <code>null</code>
|
82
|
* @return
|
83
|
*/
|
84
|
public boolean isBlankToNull() {
|
85
|
return blankToNull;
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* @see #isBlankToNull()
|
90
|
* @param blankToNull
|
91
|
*/
|
92
|
public DbStringMapper setBlankToNull(boolean blankToNull) {
|
93
|
this.blankToNull = blankToNull;
|
94
|
return this;
|
95
|
}
|
96
|
|
97
|
|
98
|
|
99
|
/* (non-Javadoc)
|
100
|
* @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
|
101
|
*/
|
102
|
@Override
|
103
|
protected int getSqlType() {
|
104
|
return Types.VARCHAR;
|
105
|
}
|
106
|
|
107
|
|
108
|
/* (non-Javadoc)
|
109
|
* @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
|
110
|
*/
|
111
|
@Override
|
112
|
public Class<?> getTypeClass() {
|
113
|
return String.class;
|
114
|
}
|
115
|
|
116
|
}
|