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 org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
29 //TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
30 public class DbImportMethodMapper
<CDMBASE
extends VersionableEntity
, STATE
extends DbImportStateBase
<?
,?
>> extends DbImportMultiAttributeMapperBase
<CDMBASE
, STATE
> {
31 private static final Logger logger
= Logger
.getLogger(DbImportMethodMapper
.class);
33 //******************************* ATTRIBUTES ***************************************/
35 private Method method
;
36 private Class
<?
>[] parameterTypes
;
37 private Object objectToInvoke
;
40 // **************************** FACTORY METHODS ***************************************************/
42 // public static <T extends DbImportStateBase> DbImportMethodMapperBase NewInstance(DbImportStateBase importBase, String methodName){
44 //// Class<?> parameterTypes = importBase.getStandardMethodParameter();
45 // Class<?> parameterType1 = ResultSet.class;
46 // Class<?> parameterType2 = DbImportStateBase.class;
48 // DbImportMethodMapperBase result = new DbImportMethodMapperBase(importBase.getClass(), methodName, parameterType1, parameterType2);
52 public static <T
extends DbImportStateBase
> DbImportMethodMapper
NewInstance(Class
<?
> clazz
, String methodName
, Class parameterTypes
){
53 DbImportMethodMapper result
= new DbImportMethodMapper(clazz
, null, methodName
, parameterTypes
);
57 public static <T
extends DbImportStateBase
> DbImportMethodMapper
NewInstance(Object objectToInvoke
, String methodName
, Class
<?
> parameterType1
, Class
<?
> parameterType2
){
58 DbImportMethodMapper result
= new DbImportMethodMapper(objectToInvoke
.getClass(), objectToInvoke
, methodName
, parameterType1
,parameterType2
);
62 public static <T
extends DbImportStateBase
> DbImportMethodMapper
NewInstance(Object objectToInvoke
, String methodName
, Class
<?
>... parameterTypes
){
63 DbImportMethodMapper result
= new DbImportMethodMapper(objectToInvoke
.getClass(), objectToInvoke
, methodName
, parameterTypes
);
67 //********************************* CONSTRUCTOR ****************************************/
72 * @param parameterTypes
74 protected DbImportMethodMapper(Class
<?
> clazz
, Object objectToInoke
, String methodName
, Class
<?
>... parameterTypes
) {
76 this.objectToInvoke
= objectToInoke
;
78 this.parameterTypes
= parameterTypes
;
79 method
= clazz
.getDeclaredMethod(methodName
, parameterTypes
);
80 method
.setAccessible(true);
81 } catch (SecurityException e
) {
82 logger
.error("SecurityException", e
);
83 } catch (NoSuchMethodException e
) {
84 logger
.error("NoSuchMethodException", e
);
87 //************************************ METHODS *******************************************/
90 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
92 public void initialize(STATE state
, Class
<?
extends CdmBase
> destinationClass
) {
93 super.initialize(state
, destinationClass
);
94 //initialize when this logging is not needed anymore
99 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
101 public CDMBASE
invoke(ResultSet rs
, CDMBASE cdmBase
) throws SQLException
{
103 // if (this.parameterTypes.length > 1 && DbExportStateBase.class.isAssignableFrom(parameterTypes[1])){
104 getState().addRelatedObject(getState().CURRENT_OBJECT_NAMESPACE
, getState().CURRENT_OBJECT_ID
, cdmBase
);
105 CDMBASE result
= (CDMBASE
)method
.invoke(objectToInvoke
, rs
, getState());
107 // return (CDMBASE)method.invoke(null, rs);
110 // CDMBASE result = doInvoke(rs, result);
112 } catch (InvocationTargetException e
) {
113 logger
.error("InvocationTargetException: " + e
.getLocalizedMessage() + " in method " + this.method
.getName());
116 } catch (IllegalAccessException e
) {
117 logger
.error("IllegalAccessException: " + e
.getLocalizedMessage());
124 * Returns the transformer from the configuration
127 protected IInputTransformer
getTransformer(){
128 return getState().getConfig().getTransformer();