minor changes in io
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / out / MethodMapper.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.out;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.sql.Types;
16
17 import org.apache.log4j.Logger;
18 import org.joda.time.DateTime;
19
20 import eu.etaxonomy.cdm.io.common.DbExportBase;
21 import eu.etaxonomy.cdm.io.common.DbExportStateBase;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23
24 /**
25 * @author a.mueller
26 * @created 12.05.2009
27 * @version 1.0
28 */
29 public class MethodMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?>> implements IDbExportMapper<DbExportStateBase<?>> {
30 private static final Logger logger = Logger.getLogger(MethodMapper.class);
31
32 private Method method;
33 private Class<?>[] parameterTypes;
34
35 public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase){
36 String methodName = "get" + dbAttributeString;
37 return NewInstance(dbAttributeString, exportBase, methodName);
38 }
39
40 public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, Class<?> parameterTypes){
41 String methodName = "get" + dbAttributeString;
42 return new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
43 }
44
45 public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, DbExportBase exportBase, String methodName){
46 Class<?> parameterTypes = exportBase.getStandardMethodParameter();
47 MethodMapper result = new MethodMapper(dbAttributeString, exportBase.getClass(), methodName, parameterTypes);
48 return result;
49 }
50
51 public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class parameterTypes){
52 MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterTypes);
53 return result;
54 }
55
56 public static <T extends DbExportBase> MethodMapper NewInstance(String dbAttributeString, Class<?> clazz, String methodName, Class<?> parameterType1, Class<?> parameterType2){
57 MethodMapper result = new MethodMapper(dbAttributeString, clazz, methodName, parameterType1,parameterType2);
58 return result;
59 }
60
61 /**
62 * @param parameterTypes
63 * @param dbIdAttributString
64 */
65 protected MethodMapper(String dbAttributeString, Class<?> clazz, String methodName, Class<?>... parameterTypes) {
66 super(null, dbAttributeString, null);
67 try {
68 this.parameterTypes = parameterTypes;
69 method = clazz.getDeclaredMethod(methodName, parameterTypes);
70 method.setAccessible(true);
71 } catch (SecurityException e) {
72 logger.error("SecurityException", e);
73 } catch (NoSuchMethodException e) {
74 logger.error("NoSuchMethodException", e);
75 }
76 }
77
78 /* (non-Javadoc)
79 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
80 */
81 @Override
82 public Class<?> getTypeClass() {
83 return method.getReturnType();
84 }
85
86
87 /* (non-Javadoc)
88 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue()
89 */
90 @Override
91 protected Object getValue(CdmBase cdmBase) {
92 try{
93 if (this.parameterTypes.length > 1 && DbExportStateBase.class.isAssignableFrom(parameterTypes[1])){
94 return method.invoke(null, cdmBase, getState());
95 }else{
96 return method.invoke(null, cdmBase);
97 }
98
99 } catch (IllegalAccessException e) {
100 logger.error("IllegalAccessException: " + e.getLocalizedMessage());
101 return false;
102 } catch (InvocationTargetException e) {
103 logger.error("InvocationTargetException: " + e.getLocalizedMessage());
104 return false;
105 } catch (Exception e) {
106 logger.error("Any Exception: " + e.getLocalizedMessage());
107 throw new RuntimeException(e);
108 }
109 }
110
111
112
113 /* (non-Javadoc)
114 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
115 */
116 @Override
117 protected int getSqlType() {
118 Class<?> returnType = method.getReturnType();
119 if (returnType == Integer.class){
120 return Types.INTEGER;
121 }else if (returnType == String.class){
122 return Types.VARCHAR;
123 }else if (returnType == Boolean.class){
124 return Types.BOOLEAN;
125 }else if (returnType == DateTime.class){
126 return Types.DATE;
127 }else{
128 logger.warn("Return type not supported yet: " + returnType.getSimpleName());
129 throw new IllegalArgumentException("Return type not supported yet: " + returnType.getSimpleName());
130 }
131 }
132
133 }