3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.io
.common
.mapping
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.sql
.SQLException
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
20 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
21 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 public class DbImportTruncatedStringMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, CdmBase
>{
30 @SuppressWarnings("unused")
31 private static final Logger logger
= Logger
.getLogger(DbImportTruncatedStringMapper
.class);
34 * @param dbAttributString
35 * @param cdmAttributeString
38 public static DbImportTruncatedStringMapper
NewInstance (String dbAttributeString
, String cdmAttributeString
, String longTextAttribute
) {
39 boolean obligatory
= false;
40 Object defaultValue
= null;
41 return new DbImportTruncatedStringMapper(dbAttributeString
, cdmAttributeString
, defaultValue
, obligatory
, longTextAttribute
);
46 // * @param cdmAttributeString
47 // * @param dbAttributString
48 // * @param defaultValue
50 // public static DbImportTruncatedStringMapper NewInstance (String dbAttributString, String cdmAttributeString, Object defaultValue) {
51 // boolean obligatory = false;
52 // return new DbImportTruncatedStringMapper(dbAttributString, cdmAttributeString, defaultValue, obligatory);
56 // * @param cdmAttributeString
57 // * @param dbAttributString
58 // * @param defaultValue
60 // public static DbImportTruncatedStringMapper NewInstance (String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory) {
61 // return new DbImportTruncatedStringMapper(dbAttributeString, cdmAttributeString, defaultValue, obligatory);
64 private String longTextAttribute
;
65 private int truncatedLength
= 255;
66 private Method longTextMethod
;
69 * @param cdmAttributeString
70 * @param dbAttributString
73 protected DbImportTruncatedStringMapper(String dbAttributeString
, String cdmAttributeString
, Object defaultValue
, boolean obligatory
, String longTextAttribute
) {
74 super(dbAttributeString
, cdmAttributeString
, defaultValue
, obligatory
);
75 this.longTextAttribute
= longTextAttribute
;
82 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
85 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
86 super.initialize(state
, destinationClass
);
87 Class
<?
> parameterType
= getTypeClass();
88 String methodName
= ImportHelper
.getSetterMethodName(parameterType
, longTextAttribute
);
90 longTextMethod
= targetClass
.getMethod(methodName
, parameterType
);
91 } catch (SecurityException e
) {
92 throw new RuntimeException(e
);
93 } catch (NoSuchMethodException e
) {
94 throw new RuntimeException(e
);
99 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase)
102 protected CdmBase
doInvoke(CdmBase cdmBase
, Object value
) throws SQLException
{
104 String strValue
= (String
)value
;
105 if (strValue
!= null && strValue
.length()>255){
106 return invokeTruncatedValue(cdmBase
, value
);
108 return super.doInvoke(cdmBase
, value
);
118 * @throws SQLException
120 private CdmBase
invokeTruncatedValue(CdmBase cdmBase
, Object value
) throws SQLException
{
122 String truncatedValue
= ((String
)value
).substring(0, truncatedLength
- 3) + "...";
123 result
= super.doInvoke(cdmBase
, truncatedValue
);
125 longTextMethod
.invoke(cdmBase
, value
);
127 } catch (IllegalArgumentException e
) {
129 } catch (IllegalAccessException e
) {
131 } catch (InvocationTargetException e
) {
134 throw new RuntimeException("Problems when invoking long text method");
139 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
142 public Class
getTypeClass() {