Project

General

Profile

Download (3.14 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.out;
11

    
12
import java.lang.reflect.Method;
13
import java.sql.Types;
14

    
15
import org.apache.logging.log4j.LogManager;
16
import org.apache.logging.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.location.NamedArea;
21

    
22
/**
23
 * Maps an area to a database key or cache field.
24
 * @author a.mueller
25
 * @since 06.02.2012
26
 */
27
public class DbAreaMapper extends DbSingleAttributeExportMapperBase<DbExportStateBase<?, IExportTransformer>> implements IDbExportMapper<DbExportStateBase<?, IExportTransformer>, IExportTransformer>{
28

    
29
    private static final Logger logger = LogManager.getLogger();
30

    
31
	private Method method;
32
	private boolean isCache;
33
	private Class<? extends CdmBase> cdmClass;
34

    
35
	public static DbAreaMapper NewInstance(Class<? extends CdmBase> cdmClass, String cdmAttribute, String dbAttributeString, boolean isCache){
36
		String methodName = "get" + cdmAttribute;
37
		return new DbAreaMapper(cdmClass, methodName,  (Class<?>[])null, cdmAttribute, dbAttributeString, isCache, null);
38
	}
39

    
40
	/**
41
	 * @param clazz
42
	 * @param parameterTypes
43
	 * @param dbAttributeString
44
	 * @param cdmAttributeString
45
	 */
46
	protected DbAreaMapper(Class<? extends CdmBase> clazz, String methodName, Class<?>[] parameterTypes, String cdmAttribute, String dbAttributeString, boolean isCache, Object defaultValue) {
47
		super(cdmAttribute, dbAttributeString, defaultValue);
48
		cdmClass =  clazz;
49
		this.isCache = isCache;
50
		try {
51
//			this.parameterTypes = parameterTypes;
52
			method = clazz.getDeclaredMethod(methodName, parameterTypes);
53
			method.setAccessible(true);
54
			cdmClass =  clazz;
55
		} catch (NoSuchMethodException e) {
56
			try {
57
				method = clazz.getMethod(methodName, parameterTypes);
58
			} catch (NoSuchMethodException e1) {
59
				logger.error("NoSuchMethodException", e);
60
				return;
61
			}
62
		} catch (SecurityException e) {
63
			logger.error("SecurityException", e);
64
			return;
65
		}
66
		method.setAccessible(true);
67
		cdmClass =  clazz;
68
	}
69

    
70
	@Override
71
	protected Object getValue(CdmBase cdmBase) {
72
		if (cdmBase.isInstanceOf(cdmClass)){
73
			try {
74
				NamedArea area = (NamedArea)method.invoke(cdmBase, (Object[])null);
75
				IExportTransformer transformer = getState().getTransformer();
76
				if (isCache){
77
					return transformer.getCacheByNamedArea(area);
78
				}else{
79
					return transformer.getKeyByNamedArea(area);
80
				}
81
			} catch (Exception e) {
82
				logger.error("Exception when invoking method: " + e.getMessage());
83
				return null;
84
			}
85

    
86
		}else{
87
			throw new ClassCastException("CdmBase for "+this.getClass().getName() +" must be of type "+cdmClass.getName()+", but was " + cdmBase.getClass());
88
		}
89
	}
90

    
91
	@Override
92
	protected int getSqlType() {
93
		if (isCache){
94
			return Types.VARCHAR;
95
		}else{
96
			return Types.INTEGER;
97
		}
98
	}
99

    
100

    
101
	@Override
102
	public Class<?> getTypeClass() {
103
		if (isCache){
104
			return String.class;
105
		}else{
106
			return Integer.class;
107
		}
108
	}
109
}
(5-5/44)