exception handling (print stacktrace)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportMethodMapper.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.ResultSet;
16 import java.sql.SQLException;
17
18 import org.apache.log4j.Logger;
19
20 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.common.VersionableEntity;
23
24 /**
25 * @author a.mueller
26 * @created 12.05.2009
27 * @version 1.0
28 */
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);
32
33 //******************************* ATTRIBUTES ***************************************/
34
35 private Method method;
36 private Class<?>[] parameterTypes;
37 private Object objectToInvoke;
38
39
40 // **************************** FACTORY METHODS ***************************************************/
41
42 // public static <T extends DbImportStateBase> DbImportMethodMapperBase NewInstance(DbImportStateBase importBase, String methodName){
43 //
44 //// Class<?> parameterTypes = importBase.getStandardMethodParameter();
45 // Class<?> parameterType1 = ResultSet.class;
46 // Class<?> parameterType2 = DbImportStateBase.class;
47 //
48 // DbImportMethodMapperBase result = new DbImportMethodMapperBase(importBase.getClass(), methodName, parameterType1, parameterType2);
49 // return result;
50 // }
51
52 public static <T extends DbImportStateBase> DbImportMethodMapper NewInstance(Class<?> clazz, String methodName, Class parameterTypes){
53 DbImportMethodMapper result = new DbImportMethodMapper(clazz, null, methodName, parameterTypes);
54 return result;
55 }
56
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);
59 return result;
60 }
61
62 public static <T extends DbImportStateBase> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?>... parameterTypes){
63 DbImportMethodMapper result = new DbImportMethodMapper(objectToInvoke.getClass(), objectToInvoke, methodName, parameterTypes);
64 return result;
65 }
66
67 //********************************* CONSTRUCTOR ****************************************/
68
69 /**
70 * @param clazz
71 * @param methodName
72 * @param parameterTypes
73 */
74 protected DbImportMethodMapper(Class<?> clazz, Object objectToInoke, String methodName, Class<?>... parameterTypes) {
75 super();
76 this.objectToInvoke = objectToInoke;
77 try {
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);
85 }
86 }
87 //************************************ METHODS *******************************************/
88
89 /* (non-Javadoc)
90 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
91 */
92 public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
93 super.initialize(state, destinationClass);
94 //initialize when this logging is not needed anymore
95 }
96
97
98 /* (non-Javadoc)
99 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
100 */
101 public CDMBASE invoke(ResultSet rs, CDMBASE cdmBase) throws SQLException {
102 try{
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());
106 // }else{
107 // return (CDMBASE)method.invoke(null, rs);
108 // }
109
110 // CDMBASE result = doInvoke(rs, result);
111 return result;
112 } catch (InvocationTargetException e) {
113 logger.error("InvocationTargetException: " + e.getLocalizedMessage() + " in method " + this.method.getName());
114 e.printStackTrace();
115 return null;
116 } catch (IllegalAccessException e) {
117 logger.error("IllegalAccessException: " + e.getLocalizedMessage());
118 return null;
119 }
120 }
121
122
123 /**
124 * Returns the transformer from the configuration
125 * @return
126 */
127 protected IInputTransformer getTransformer(){
128 return getState().getConfig().getTransformer();
129 }
130
131 }