Project

General

Profile

Download (5.05 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.io.common.mapping;
11

    
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.common.VersionableEntity;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 12.05.2009
26
 * @version 1.0
27
 */
28
//TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
29
public class DbImportMethodMapper<CDMBASE extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CDMBASE, STATE>  {
30
	private static final Logger logger = Logger.getLogger(DbImportMethodMapper.class);
31
	
32
	//******************************* ATTRIBUTES ***************************************/
33
	
34
	private Method method;
35
	private Class<?>[] parameterTypes;
36
	private Object objectToInvoke;
37
	
38

    
39
// **************************** FACTORY METHODS ***************************************************/
40

    
41
//	public static <T extends DbImportStateBase> DbImportMethodMapperBase NewInstance(DbImportStateBase importBase, String methodName){
42
//		
43
////		Class<?> parameterTypes = importBase.getStandardMethodParameter();
44
//		Class<?> parameterType1 = ResultSet.class;
45
//		Class<?> parameterType2 = DbImportStateBase.class;
46
//		
47
//		DbImportMethodMapperBase result = new DbImportMethodMapperBase(importBase.getClass(), methodName, parameterType1, parameterType2);
48
//		return result;
49
//	}
50

    
51
	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper NewInstance(Class<?> clazz, String methodName, Class parameterTypes){
52
		DbImportMethodMapper<?,T> result = new DbImportMethodMapper<VersionableEntity,T>(clazz, null, methodName, parameterTypes);
53
		return result;
54
	}
55
	
56
	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?> parameterType1, Class<?> parameterType2){
57
		DbImportMethodMapper<?,?> result = new DbImportMethodMapper<VersionableEntity, T>(objectToInvoke.getClass(), objectToInvoke, methodName, parameterType1,parameterType2);
58
		return result;
59
	}
60
	
61
	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?>... parameterTypes){
62
		DbImportMethodMapper<?,?> result = new DbImportMethodMapper<VersionableEntity,T>(objectToInvoke.getClass(), objectToInvoke, methodName, parameterTypes);
63
		return result;
64
	}
65
	
66
//********************************* CONSTRUCTOR ****************************************/
67
	
68
	/**
69
	 * @param clazz
70
	 * @param methodName
71
	 * @param parameterTypes
72
	 */
73
	protected DbImportMethodMapper(Class<?> clazz, Object objectToInoke, String methodName, Class<?>... parameterTypes) {
74
		super();
75
		this.objectToInvoke = objectToInoke;
76
		try {
77
			this.parameterTypes = parameterTypes;
78
			method = clazz.getDeclaredMethod(methodName, parameterTypes);
79
			method.setAccessible(true);
80
		} catch (SecurityException e) {
81
			logger.error("SecurityException", e);
82
		} catch (NoSuchMethodException e) {
83
			logger.error("NoSuchMethodException", e);
84
		}
85
	}
86
//************************************ METHODS *******************************************/
87

    
88
	/* (non-Javadoc)
89
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
90
	 */
91
	public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
92
		super.initialize(state, destinationClass);
93
		//initialize when this logging is not needed anymore
94
	}
95
	
96
	
97
	/* (non-Javadoc)
98
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
99
	 */
100
	public CDMBASE invoke(ResultSet rs, CDMBASE cdmBase) throws SQLException {
101
		try{
102
	//		if (this.parameterTypes.length > 1 && DbExportStateBase.class.isAssignableFrom(parameterTypes[1])){
103
			getState().addRelatedObject(getState().CURRENT_OBJECT_NAMESPACE, getState().CURRENT_OBJECT_ID, cdmBase);
104
			CDMBASE result = (CDMBASE)method.invoke(objectToInvoke, rs, getState());
105
	//		}else{
106
	//			return (CDMBASE)method.invoke(null, rs);
107
	//		}
108
			
109
	//		CDMBASE result = doInvoke(rs, result);
110
			return result;
111
		} catch (InvocationTargetException e) {
112
			logger.error("InvocationTargetException: " + e.getLocalizedMessage() + " in method " + this.method.getName());
113
			e.printStackTrace();
114
			return null;
115
		} catch (IllegalAccessException e) {
116
			logger.error("IllegalAccessException: " + e.getLocalizedMessage());
117
			return null;
118
		}
119
	}
120
	
121
	
122
	/**
123
	 * Returns the transformer from the configuration
124
	 * @return
125
	 */
126
	protected IInputTransformer getTransformer(){
127
		return getState().getTransformer();
128
	}
129
	
130
}
(25-25/51)