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
.out
;
13 import java
.lang
.reflect
.InvocationTargetException
;
14 import java
.lang
.reflect
.Method
;
15 import java
.sql
.Types
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.joda
.time
.DateTime
;
20 import eu
.etaxonomy
.cdm
.io
.common
.DbExportBase
;
21 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 public class MethodMapper
extends DbSingleAttributeExportMapperBase
<DbExportStateBase
<?
>> implements IDbExportMapper
<DbExportStateBase
<?
>> {
30 private static final Logger logger
= Logger
.getLogger(MethodMapper
.class);
32 private Method method
;
33 private Class
<?
>[] parameterTypes
;
35 public static <T
extends DbExportBase
> MethodMapper
NewInstance(String dbAttributeString
, DbExportBase exportBase
){
36 String methodName
= "get" + dbAttributeString
;
37 return NewInstance(dbAttributeString
, exportBase
, methodName
);
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
);
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
);
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
);
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
);
62 * @param parameterTypes
63 * @param dbIdAttributString
65 protected MethodMapper(String dbAttributeString
, Class
<?
> clazz
, String methodName
, Class
<?
>... parameterTypes
) {
66 super(null, dbAttributeString
, null);
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
);
79 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
82 public Class
<?
> getTypeClass() {
83 return method
.getReturnType();
88 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValue()
91 protected Object
getValue(CdmBase cdmBase
) {
93 if (this.parameterTypes
.length
> 1 && DbExportStateBase
.class.isAssignableFrom(parameterTypes
[1])){
94 return method
.invoke(null, cdmBase
, getState());
96 return method
.invoke(null, cdmBase
);
99 } catch (IllegalAccessException e
) {
100 logger
.error("IllegalAccessException: " + e
.getLocalizedMessage());
102 } catch (InvocationTargetException e
) {
103 logger
.error("InvocationTargetException: " + e
.getLocalizedMessage());
105 } catch (Exception e
) {
106 logger
.error("Any Exception: " + e
.getLocalizedMessage());
107 throw new RuntimeException(e
);
114 * @see eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbSingleAttributeExportMapperBase#getValueType()
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){
128 logger
.warn("Return type not supported yet: " + returnType
.getSimpleName());
129 throw new IllegalArgumentException("Return type not supported yet: " + returnType
.getSimpleName());