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
.ResultSet
;
16 import java
.sql
.SQLException
;
18 import javax
.mail
.MethodNotSupportedException
;
20 import org
.apache
.log4j
.Logger
;
22 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
24 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
25 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 public abstract class DbSingleAttributeImportMapperBase
<STATE
extends DbImportStateBase
<?
,?
>, CDM_BASE
extends CdmBase
> extends CdmSingleAttributeMapperBase
implements IDbImportMapper
<STATE
, CDM_BASE
> {
33 private static final Logger logger
= Logger
.getLogger(DbSingleAttributeImportMapperBase
.class);
35 protected DbImportMapperBase
<STATE
> importMapperHelper
= new DbImportMapperBase
<STATE
>();
36 // private Integer precision = null;
37 protected boolean obligatory
= true;
38 protected boolean ignore
= false;;
40 protected Method destinationMethod
= null;
41 protected Class
<?
> targetClass
;
44 * @param dbAttributString
45 * @param cdmAttributeString
47 protected DbSingleAttributeImportMapperBase(String dbAttributString
, String cdmAttributeString
) {
48 super(dbAttributString
, cdmAttributeString
);
53 * @param dbAttributString
54 * @param cdmAttributeString
56 protected DbSingleAttributeImportMapperBase(String dbAttributString
, String cdmAttributeString
, Object defaultValue
) {
57 super(dbAttributString
, cdmAttributeString
, defaultValue
);
61 * @param dbAttributString
62 * @param cdmAttributeString
64 protected DbSingleAttributeImportMapperBase(String dbAttributeString
, String cdmAttributeString
, Object defaultValue
, boolean obligatory
) {
65 super(dbAttributeString
, cdmAttributeString
, defaultValue
);
66 this.obligatory
= obligatory
;
70 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.String)
72 public void initialize(STATE state
, Class
<?
extends CdmBase
> destinationClass
) {
73 importMapperHelper
.initialize(state
, destinationClass
);
75 targetClass
= getTargetClass(destinationClass
);
76 Class
<?
> parameterType
= getTypeClass();
77 String methodName
= getMethodName(parameterType
);
78 destinationMethod
= targetClass
.getMethod(methodName
, parameterType
);
79 } catch (SecurityException e
) {
80 throw new RuntimeException(e
);
81 } catch (NoSuchMethodException e
) {
82 throw new RuntimeException(e
);
88 * @param destinationClass
90 * @throws NoSuchMethodException
91 * @throws SecurityException
92 * @throws NoSuchMethodException
94 protected Class
getTargetClass(Class
<?
> destinationClass
) throws SecurityException
, NoSuchMethodException
{
95 Class result
= destinationClass
;
96 String destinationAttribute
= getDestinationAttribute();
97 if (destinationAttribute
== null){
100 String
[] splits
= destinationAttribute
.split("\\.");
102 for (int i
= 0; i
< splits
.length
- 1; i
++){
103 String split
= splits
[i
];
104 String castedResultClass
= getCastedResultClass(split
);
105 split
= removeCast(split
);
106 String methodName
= ImportHelper
.getGetterMethodName(split
, false);
109 getterMethod
= result
.getMethod(methodName
, null);
110 } catch (NoSuchMethodException e1
) {
113 result
= getterMethod
.getReturnType();
114 if (castedResultClass
!= null){
116 //casting works only if the subclass is in the same package
117 String packageName
= result
.getPackage().getName();
118 castedResultClass
= packageName
+ "." + castedResultClass
;
119 result
= Class
.forName(castedResultClass
);
120 } catch (ClassNotFoundException e
) {
134 private String
removeCast(String split
) {
135 int index
= split
.lastIndexOf(")");
136 if (split
.length() > index
){
137 split
= split
.substring(index
+ 1);
149 private String
getCastedResultClass(String split
) {
150 String castString
= null;
151 split
= split
.trim();
152 int index
= split
.lastIndexOf(")");
153 if (split
.startsWith("(") && index
> -1 ){
154 castString
= split
.substring(1, index
);
155 castString
= castString
.trim();
165 private String
getMethodName(Class clazz
) {
166 String cdmAttributeName
= getTargetClassAttribute(getDestinationAttribute());
167 String result
= ImportHelper
.getSetterMethodName(clazz
, cdmAttributeName
);
173 * @param destinationAttribute
176 private String
getTargetClassAttribute(String destinationAttribute
) {
177 if (destinationAttribute
== null){
180 String
[] splitted
= destinationAttribute
.split("\\.");
181 String result
= splitted
[splitted
.length
- 1]; //get last attribute
187 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.IDbExportMapper#invoke(eu.etaxonomy.cdm.model.common.CdmBase)
189 public CDM_BASE
invoke(ResultSet rs
, CDM_BASE cdmBase
) throws SQLException
{
193 Object dbValue
= getValue(rs
);
195 // String dbValue = rs.getString(getSourceAttribute());
196 return doInvoke(cdmBase
, dbValue
);
199 protected CDM_BASE
doInvoke(CDM_BASE cdmBase
, Object value
) throws SQLException
{
200 Method method
= getMethod();
202 Object objectToInvoke
= getObjectToInvoke(cdmBase
);
203 if (objectToInvoke
== null){
204 logger
.warn("No object defined for invoke. Method will not be invoked");
206 method
.invoke(objectToInvoke
, value
);
209 } catch (IllegalArgumentException e
) {
211 } catch (IllegalAccessException e
) {
213 } catch (InvocationTargetException e
) {
215 } catch (SecurityException e
) {
217 } catch (NoSuchMethodException e
) {
220 throw new RuntimeException("Problems when invoking target method");
226 * @throws NoSuchMethodException
227 * @throws SecurityException
228 * @throws InvocationTargetException
229 * @throws IllegalAccessException
230 * @throws IllegalArgumentException
232 private Object
getObjectToInvoke(CDM_BASE cdmBase
) throws SecurityException
, NoSuchMethodException
, IllegalArgumentException
, IllegalAccessException
, InvocationTargetException
{
233 Object objectToInvoke
= cdmBase
;
234 String destinationAttribute
= getDestinationAttribute();
235 String
[] splits
= destinationAttribute
.split("\\.");
236 for (int i
= 0; i
< splits
.length
- 1; i
++){
237 String split
= splits
[i
];
238 split
= removeCast(split
);
239 String methodName
= ImportHelper
.getGetterMethodName(split
, false);
240 Method method
= objectToInvoke
.getClass().getMethod(methodName
, null);
241 objectToInvoke
= method
.invoke(cdmBase
, null);
243 return objectToInvoke
;
250 private Method
getMethod() {
251 if (destinationMethod
!= null){
252 return destinationMethod
;
254 throw new RuntimeException("Missing destinationMethod not yet implemented");
259 protected Object
getValue(ResultSet rs
) throws SQLException
{
260 return getDbValue(rs
);
264 * Returns the database value for the attribute
267 * @throws SQLException
269 protected Object
getDbValue(ResultSet rs
) throws SQLException
{
270 String columnLabel
= getSourceAttribute();
271 Object value
= rs
.getObject(columnLabel
);
277 // * @return the index
279 // public int getIndex() {
280 // return exportMapperHelper.getIndex();
286 protected STATE
getState() {
287 return importMapperHelper
.getState();
294 protected String
getTableName() {
295 return importMapperHelper
.getTableName();
298 protected boolean checkDbColumnExists() throws MethodNotSupportedException
{
299 // //TODO remove cast
300 // Source source = (Source)getState().getConfig().getSource();
301 // String tableName = getTableName();
302 // String attributeName = getSourceAttribute();
303 // return source.checkColumnExists(tableName, attributeName);
304 //TODO not possible as long as tableName is not initialized
308 // protected int getPrecision(){
309 // return this.precision;
312 protected int getDbColumnIntegerInfo(String selectPart
){
314 Source source
= (Source
)getState().getConfig().getSource();
315 String strQuery
= "SELECT " + selectPart
+ " as result" +
316 " FROM sysobjects AS t " +
317 " INNER JOIN syscolumns AS c ON t.id = c.id " +
318 " WHERE (t.xtype = 'U') AND " +
319 " (t.name = '" + getTableName() + "') AND " +
320 " (c.name = '" + getSourceAttribute() + "')";
321 ResultSet rs
= source
.getResultSet(strQuery
) ;
325 n
= rs
.getInt("result");
327 } catch (SQLException e
) {
338 * @throws SQLException
340 protected String
getStringDbValue(ResultSet rs
, String attribute
) throws SQLException
{
341 Object oId
= rs
.getObject(attribute
);
342 String id
= String
.valueOf(oId
);
345 // public String toString(){
346 // String sourceAtt = CdmUtils.Nz(getSourceAttribute());
347 // String destAtt = CdmUtils.Nz(getDestinationAttribute());
348 // return this.getClass().getSimpleName() +"[" + sourceAtt + "->" + destAtt + "]";