Project

General

Profile

Download (5.38 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.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.CdmImportBase;
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
 * @since 12.05.2009
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 = LogManager.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 NewDefaultInstance(CdmImportBase<?, ?> cdmImport, String methodName,
52
	        Class<? extends DbImportStateBase> importStateClass){
53
	    DbImportMethodMapper<?,?> result = new DbImportMethodMapper<VersionableEntity, T>(cdmImport.getClass(), cdmImport, methodName, ResultSet.class, importStateClass);
54
	    return result;
55
	}
56

    
57
	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper NewInstance(Class<?> clazz, String methodName, Class parameterType){
58
		DbImportMethodMapper<?,T> result = new DbImportMethodMapper<VersionableEntity,T>(clazz, null, methodName, parameterType);
59
		return result;
60
	}
61

    
62

    
63
//	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper NewInstance(Object objectToInvoke, String methodName, Class<?> parameterType1, Class<?> parameterType2){
64
//		DbImportMethodMapper<?,?> result = new DbImportMethodMapper<VersionableEntity, T>(objectToInvoke.getClass(), objectToInvoke, methodName, parameterType1,parameterType2);
65
//		return result;
66
//	}
67

    
68
	@Deprecated //not sure if it works correctly any more, was already commented but is used in CentralAfricaFernsXXXImport
69
	//can be removed if this dependency is removed
70
	public static <T extends DbImportStateBase<?,?>> DbImportMethodMapper<VersionableEntity,T> NewInstance(Object objectToInvoke, String methodName, Class<?>... parameterTypes){
71
		DbImportMethodMapper<VersionableEntity,T> result = new DbImportMethodMapper<VersionableEntity,T>(objectToInvoke.getClass(), objectToInvoke, methodName, parameterTypes);
72
		return result;
73
	}
74

    
75
//********************************* CONSTRUCTOR ****************************************/
76

    
77
	protected DbImportMethodMapper(Class<?> clazz, Object objectToInoke, String methodName, Class<?>... parameterTypes) {
78
		super();
79
		this.objectToInvoke = objectToInoke;
80
		try {
81
			this.parameterTypes = parameterTypes;
82
			method = clazz.getDeclaredMethod(methodName, parameterTypes);
83
			method.setAccessible(true);
84
		} catch (SecurityException e) {
85
			logger.error("SecurityException", e);
86
		} catch (NoSuchMethodException e) {
87
			logger.error("NoSuchMethodException", e);
88
		}
89
	}
90

    
91
//************************************ METHODS *******************************************/
92

    
93
	@Override
94
    public void initialize(STATE state, Class<? extends CdmBase> destinationClass) {
95
		super.initialize(state, destinationClass);
96
		//initialize when this logging is not needed anymore
97
	}
98

    
99
	@Override
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(DbImportStateBase.CURRENT_OBJECT_NAMESPACE,
104
			        DbImportStateBase.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
	 * Returns the transformer from the configuration
124
	 */
125
	protected IInputTransformer getTransformer(){
126
		return getState().getTransformer();
127
	}
128
}
(26-26/53)