Project

General

Profile

Download (4.6 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.SQLException;
15

    
16
import org.apache.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
19
import eu.etaxonomy.cdm.io.common.ImportHelper;
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21

    
22
/**
23
 * @author a.mueller
24
 * @since 24.02.2010
25
 * @version 1.0
26
 */
27
public class DbImportTruncatedStringMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, CdmBase>{
28
	
29
	@SuppressWarnings("unused")
30
	private static final Logger logger = Logger.getLogger(DbImportTruncatedStringMapper.class);
31
	
32
	/**
33
	 * @param dbAttributString
34
	 * @param cdmAttributeString
35
	 * @return
36
	 */
37
	public static DbImportTruncatedStringMapper NewInstance (String dbAttributeString, String cdmAttributeString, String longTextAttribute) {
38
		boolean obligatory = false;
39
		Object defaultValue = null;
40
		return new DbImportTruncatedStringMapper(dbAttributeString, cdmAttributeString, defaultValue, obligatory, longTextAttribute);
41
	}
42
	
43
	
44
//	/**
45
//	 * @param cdmAttributeString
46
//	 * @param dbAttributString
47
//	 * @param defaultValue
48
//	 */
49
//	public static DbImportTruncatedStringMapper NewInstance (String dbAttributString, String cdmAttributeString, Object defaultValue) {
50
//		boolean obligatory = false;
51
//		return new  DbImportTruncatedStringMapper(dbAttributString, cdmAttributeString, defaultValue, obligatory);
52
//	}
53
//
54
//	/**
55
//	 * @param cdmAttributeString
56
//	 * @param dbAttributString
57
//	 * @param defaultValue
58
//	 */
59
//	public static DbImportTruncatedStringMapper NewInstance (String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory) {
60
//		return new  DbImportTruncatedStringMapper(dbAttributeString, cdmAttributeString, defaultValue, obligatory);
61
//	}
62

    
63
	private String longTextAttribute;
64
	private int truncatedLength = 255;
65
	private Method longTextMethod;
66
	
67
	/**
68
	 * @param cdmAttributeString
69
	 * @param dbAttributString
70
	 * @param defaultValue
71
	 */
72
	protected DbImportTruncatedStringMapper(String dbAttributeString, String cdmAttributeString, Object defaultValue, boolean obligatory, String longTextAttribute) {
73
		super(dbAttributeString, cdmAttributeString, defaultValue, obligatory);
74
		this.longTextAttribute = longTextAttribute;
75
	}
76
	
77
	
78
	
79

    
80
	/* (non-Javadoc)
81
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
82
	 */
83
	@Override
84
	public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
85
		super.initialize(state, destinationClass);
86
		Class<?> parameterType = getTypeClass();
87
		String methodName = ImportHelper.getSetterMethodName(parameterType, longTextAttribute);
88
		try {
89
			longTextMethod = targetClass.getMethod(methodName, parameterType);
90
		} catch (SecurityException e) {
91
			throw new RuntimeException(e);
92
		} catch (NoSuchMethodException e) {
93
			throw new RuntimeException(e);
94
		}
95
	}
96
	
97
	/* (non-Javadoc)
98
	 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase)
99
	 */
100
	@Override
101
	protected CdmBase doInvoke(CdmBase cdmBase, Object value) throws SQLException {
102
		CdmBase result;
103
		String strValue = (String)value;
104
		if (strValue != null && strValue.length()>255){
105
			return invokeTruncatedValue(cdmBase, value);
106
		}else{
107
			return super.doInvoke(cdmBase, value);
108
		}
109
	}
110

    
111

    
112

    
113

    
114
	/**
115
	 * @param cdmBase
116
	 * @param value
117
	 * @throws SQLException
118
	 */
119
	private CdmBase invokeTruncatedValue(CdmBase cdmBase, Object value) throws SQLException {
120
		CdmBase result;
121
		String truncatedValue = ((String)value).substring(0, truncatedLength - 3) + "...";
122
		result = super.doInvoke(cdmBase, truncatedValue);
123
		try {
124
			longTextMethod.invoke(cdmBase, value);
125
			return result;
126
		} catch (IllegalArgumentException e) {
127
			e.printStackTrace();
128
		} catch (IllegalAccessException e) {
129
			e.printStackTrace();
130
		} catch (InvocationTargetException e) {
131
			e.printStackTrace();
132
		}
133
		throw new RuntimeException("Problems when invoking long text method");
134
	}
135

    
136

    
137
	/* (non-Javadoc)
138
	 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
139
	 */
140
	@Override
141
	public Class getTypeClass() {
142
		return String.class;
143
	}
144
	
145

    
146
}
(37-37/51)