remove "NEW" comment
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportTruncatedStringMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.sql.SQLException;
16
17 import org.apache.log4j.Logger;
18
19 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20 import eu.etaxonomy.cdm.io.common.ImportHelper;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22
23 /**
24 * @author a.mueller
25 * @created 24.02.2010
26 * @version 1.0
27 */
28 public class DbImportTruncatedStringMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, CdmBase>{
29
30 @SuppressWarnings("unused")
31 private static final Logger logger = Logger.getLogger(DbImportTruncatedStringMapper.class);
32
33 /**
34 * @param dbAttributString
35 * @param cdmAttributeString
36 * @return
37 */
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);
42 }
43
44
45 // /**
46 // * @param cdmAttributeString
47 // * @param dbAttributString
48 // * @param defaultValue
49 // */
50 // public static DbImportTruncatedStringMapper NewInstance (String dbAttributString, String cdmAttributeString, Object defaultValue) {
51 // boolean obligatory = false;
52 // return new DbImportTruncatedStringMapper(dbAttributString, cdmAttributeString, defaultValue, obligatory);
53 // }
54 //
55 // /**
56 // * @param cdmAttributeString
57 // * @param dbAttributString
58 // * @param defaultValue
59 // */
60 // public static DbImportTruncatedStringMapper NewInstance (String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory) {
61 // return new DbImportTruncatedStringMapper(dbAttributeString, cdmAttributeString, defaultValue, obligatory);
62 // }
63
64 private String longTextAttribute;
65 private int truncatedLength = 255;
66 private Method longTextMethod;
67
68 /**
69 * @param cdmAttributeString
70 * @param dbAttributString
71 * @param defaultValue
72 */
73 protected DbImportTruncatedStringMapper(String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory, String longTextAttribute) {
74 super(dbAttributeString, cdmAttributeString, defaultValue, obligatory);
75 this.longTextAttribute = longTextAttribute;
76 }
77
78
79
80
81 /* (non-Javadoc)
82 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
83 */
84 @Override
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);
89 try {
90 longTextMethod = targetClass.getMethod(methodName, parameterType);
91 } catch (SecurityException e) {
92 throw new RuntimeException(e);
93 } catch (NoSuchMethodException e) {
94 throw new RuntimeException(e);
95 }
96 }
97
98 /* (non-Javadoc)
99 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase)
100 */
101 @Override
102 protected CdmBase doInvoke(CdmBase cdmBase, Object value) throws SQLException {
103 CdmBase result;
104 String strValue = (String)value;
105 if (strValue != null && strValue.length()>255){
106 return invokeTruncatedValue(cdmBase, value);
107 }else{
108 return super.doInvoke(cdmBase, value);
109 }
110 }
111
112
113
114
115 /**
116 * @param cdmBase
117 * @param value
118 * @throws SQLException
119 */
120 private CdmBase invokeTruncatedValue(CdmBase cdmBase, Object value) throws SQLException {
121 CdmBase result;
122 String truncatedValue = ((String)value).substring(0, truncatedLength - 3) + "...";
123 result = super.doInvoke(cdmBase, truncatedValue);
124 try {
125 longTextMethod.invoke(cdmBase, value);
126 return result;
127 } catch (IllegalArgumentException e) {
128 e.printStackTrace();
129 } catch (IllegalAccessException e) {
130 e.printStackTrace();
131 } catch (InvocationTargetException e) {
132 e.printStackTrace();
133 }
134 throw new RuntimeException("Problems when invoking long text method");
135 }
136
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
140 */
141 @Override
142 public Class getTypeClass() {
143 return String.class;
144 }
145
146
147 }