Project

General

Profile

Download (13.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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;
11

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

    
18
import org.apache.log4j.Logger;
19
import org.jdom.Element;
20
import org.jdom.Namespace;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
25
import eu.etaxonomy.cdm.model.common.Annotation;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.IOriginalSource;
28
import eu.etaxonomy.cdm.model.common.ISourceable;
29
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
30
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
31
import eu.etaxonomy.cdm.model.common.Language;
32
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
33
import eu.etaxonomy.cdm.model.common.TimePeriod;
34
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
36
import eu.etaxonomy.cdm.model.name.NonViralName;
37
import eu.etaxonomy.cdm.model.reference.Reference;
38
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
39
/**
40
 * @author a.mueller
41
 *
42
 */
43
public class ImportHelper {
44
	private static final Logger logger = Logger.getLogger(ImportHelper.class);
45

    
46
	public static final boolean OVERWRITE = true;
47
	public static final boolean  NO_OVERWRITE = false;
48

    
49
	public static final boolean OBLIGATORY = true;
50
	public static final boolean  FACULTATIVE = false;
51

    
52

    
53

    
54
	public static boolean setOriginalSource(ISourceable sourceable, Reference sourceReference, long sourceId, String namespace){
55
		return setOriginalSource(sourceable, sourceReference, String.valueOf(sourceId), namespace);
56
	}
57

    
58
	/**
59
	 * Adds an original source object to the identifiable entity.
60
	 * @param idEntity
61
	 * @param sourceReference
62
	 * @param sourceId
63
	 * @return
64
	 */
65
	public static boolean setOriginalSource(ISourceable sourceable, Reference sourceReference, String sourceId, String namespace){
66
		IOriginalSource<?> originalSource;
67
		OriginalSourceType type = OriginalSourceType.Import;
68
		if (HibernateProxyHelper.isInstanceOf(sourceable, IdentifiableEntity.class)){
69
			originalSource = IdentifiableSource.NewInstance(type, sourceId, namespace, sourceReference, null);
70
		}else if (HibernateProxyHelper.isInstanceOf(sourceable, DescriptionElementBase.class)){
71
			originalSource = DescriptionElementSource.NewInstance(type, sourceId, namespace, sourceReference, null);
72
		}else{
73
			throw new ClassCastException("Unknown implementing class for ISourceable "+ sourceable.getClass() + " . Not supported bei ImportHelper.");
74
		}
75
		sourceable.addSource(originalSource);
76
		return true;
77
	}
78

    
79
	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean blankToNull){
80
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE, blankToNull);
81
	}
82

    
83
//	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean overwriteNull){
84
//		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, overwriteNull);
85
//	}
86

    
87
	public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
88
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE, false);
89
	}
90

    
91
	public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull, boolean blankToNull){
92
		Object strValue;
93
		try {
94
			strValue = rs.getObject(dbAttrName);
95
			if (strValue instanceof String && strValue != null){
96
				strValue = ((String)strValue).trim();
97
				if (blankToNull &&  strValue.equals("")){
98
					strValue = null;
99
				}
100
			}
101
			return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
102
		}catch (SQLException e) {
103
			logger.error("SQLException: " +  e);
104
			return false;
105
		}
106

    
107
	}
108

    
109

    
110
	public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
111
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, OVERWRITE);
112
	}
113

    
114
	public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, boolean overwriteNull){
115
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, overwriteNull);
116
	}
117

    
118
	public static boolean addXmlBooleanValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
119
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, boolean.class, OVERWRITE);
120
	}
121

    
122

    
123
	public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull){
124
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
125
	}
126

    
127
	public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
128
		Object strValue;
129
		strValue = getXmlInputValue(root, xmlElementName, namespace);
130
		return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
131
	}
132

    
133
	public static boolean addValue(Object sourceValue, CdmBase cdmBase, String cdmAttrName, Class<?> clazz, boolean overwriteNull, boolean obligat){
134
		String methodName;
135
//		Object strValue;
136
		try {
137
			if (overwriteNull == NO_OVERWRITE && sourceValue == null ){
138
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
139
				return true;
140
			}
141
			if (logger.isDebugEnabled()) { logger.debug("addValue: " + sourceValue);}
142
			methodName = getSetterMethodName(clazz, cdmAttrName);
143
			Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
144
			cdmMethod.invoke(cdmBase, sourceValue);
145
			return true;
146
		} catch (NullPointerException e) {
147
			logger.error("NullPointerException: " + e.getMessage());
148
			return false;
149
		} catch (IllegalArgumentException e) {
150
			logger.error("IllegalArgumentException: " + e.getMessage());
151
			return false;
152
		} catch (IllegalAccessException e) {
153
			logger.error("IllegalAccessException: " + e.getMessage());
154
			return false;
155
		} catch (InvocationTargetException e) {
156
			logger.error("InvocationTargetException: " + e.getMessage());
157
			return false;
158
		}catch (SecurityException e) {
159
			logger.error("SecurityException: " + e.getMessage());
160
			return false;
161
		} catch (NoSuchMethodException e) {
162
			if (obligat){
163
				logger.error("NoSuchMethod: " + e.getMessage());
164
				return false;
165
			}else{
166
				if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
167
				return true;
168
			}
169
		}
170

    
171
	}
172

    
173
	/**
174
	 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
175
	 * @param cdmAttrName
176
	 * @return
177
//	 * @throws IllegalArgumentException if a clazz is not yet supported
178
	 */
179
	public static String getSetterMethodName(Class<?> clazz, String cdmAttrName) {
180
		return getSetterPutterMethodName(clazz, cdmAttrName, "set");
181
	}
182

    
183
	/**
184
	 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
185
	 * @param cdmAttrName
186
	 * @return
187
//	 * @throws IllegalArgumentException if a clazz is not yet supported
188
	 */
189
	public static String getPutterMethodName(Class<?> clazz, String cdmAttrName) {
190
		return getSetterPutterMethodName(clazz, cdmAttrName, "put");
191
	}
192

    
193
	/**
194
	 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
195
	 * @param cdmAttrName
196
	 * @return
197
//	 * @throws IllegalArgumentException if a clazz is not yet supported
198
	 */
199
	private static String getSetterPutterMethodName(Class<?> clazz, String cdmAttrName, String prefix) {
200
		String methodName;
201
		if (clazz == boolean.class || clazz == Boolean.class){
202
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
203
				throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
204
			}
205
			methodName = prefix + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
206
		}else  {
207
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
208
				throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
209
			}
210
			methodName = prefix + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
211
		}
212
		return methodName;
213
	}
214

    
215
	private static boolean valuesAreNull(List<Object> values){
216
		for (Object sourceValue : values.toArray()){
217
			if (sourceValue != null){
218
				return false;
219
			}
220
		}
221
		return true;
222
	}
223

    
224
	public static boolean addMultipleValues(List<Object> sourceValues, CdmBase cdmBase, String cdmAttrName, List<Class> classes, boolean overwriteNull, boolean obligat){
225
		String methodName;
226
//		Object strValue;
227
		try {
228

    
229
			if (overwriteNull == NO_OVERWRITE && valuesAreNull(sourceValues)){
230
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
231
				return true;
232
			}
233
			if (logger.isDebugEnabled()) { logger.debug("addValues: " + sourceValues.toString());}
234

    
235

    
236
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
237
				throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
238
			}
239
			methodName = "add" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
240

    
241
			Class<?>[] classArray = classes.toArray(new Class[0]);
242
			Method cdmMethod = cdmBase.getClass().getMethod(methodName, classArray);
243
			cdmMethod.invoke(cdmBase, sourceValues.toArray());
244
			return true;
245
		} catch (NullPointerException e) {
246
			logger.error("NullPointerException: " + e.getMessage());
247
			return false;
248
		} catch (IllegalArgumentException e) {
249
			logger.error("IllegalArgumentException: " + e.getMessage());
250
			return false;
251
		} catch (IllegalAccessException e) {
252
			logger.error("IllegalAccessException: " + e.getMessage());
253
			return false;
254
		} catch (InvocationTargetException e) {
255
			logger.error("InvocationTargetException: " + e.getMessage());
256
			return false;
257
		}catch (SecurityException e) {
258
			logger.error("SecurityException: " + e.getMessage());
259
			return false;
260
		} catch (NoSuchMethodException e) {
261
			if (obligat){
262
				logger.error("NoSuchMethod: " + e.getMessage());
263
				return false;
264
			}else{
265
				if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
266
				return true;
267
			}
268
		}
269

    
270
	}
271

    
272
	public static boolean addAnnotationFromResultSet(ResultSet rs, String attributeName, AnnotatableEntity cdmBase, Language language){
273
		try {
274
			String value = rs.getString(attributeName);
275
			if (CdmUtils.Nz(value).equals("")){
276
				String strAnnotation = attributeName + ": " + value;
277
				Annotation annoatation = Annotation.NewInstance(strAnnotation, language);
278
				cdmBase.addAnnotation(annoatation);
279

    
280
			}
281
			return true;
282
		} catch (SQLException e) {
283
			logger.warn(e);
284
			e.printStackTrace();
285
			return false;
286
		}
287
	}
288

    
289

    
290

    
291
	public static Object getXmlInputValue(Element root, String xmlElementName, Namespace namespace){
292
		Object result = null;
293
		Element child = root.getChild(xmlElementName, namespace);
294
		if (child != null){
295
			result = child.getText().trim();
296
		}
297
		return result;
298
	}
299

    
300

    
301
	public static TimePeriod getDatePublished(String refYear){
302
		TimePeriod resultNew;
303
		try {
304
			resultNew = TimePeriodParser.parseString(refYear);
305
		} catch (IllegalArgumentException e) {
306
			logger.warn("RefYear could not be parsed: " + refYear);
307
			resultNew = null;
308
		}
309
		return resultNew;
310
	}
311

    
312
	//************** EXPORT *******************/
313

    
314

    
315
	public static<T extends Object> T getValue(CdmBase cdmBase, String cdmAttrName, boolean isBoolean, boolean obligatory){
316
		String methodName;
317
		T result;
318
		try {
319
			methodName = getGetterMethodName(cdmAttrName, isBoolean);
320
			if (cdmBase.isInstanceOf(NonViralName.class)){
321
				cdmBase = CdmBase.deproxy(cdmBase);
322
			}
323
			Method cdmMethod = cdmBase.getClass().getMethod(methodName);
324
			result = (T)cdmMethod.invoke(cdmBase);
325
			return result;
326
		} catch (NullPointerException e) {
327
			logger.error("NullPointerException: " + e.getMessage());
328
			return null;
329
		} catch (IllegalArgumentException e) {
330
			logger.error("IllegalArgumentException: " + e.getMessage());
331
			return null;
332
		} catch (IllegalAccessException e) {
333
			logger.error("IllegalAccessException: " + e.getMessage());
334
			return null;
335
		} catch (InvocationTargetException e) {
336
			logger.error("InvocationTargetException: " + e.getMessage());
337
			return null;
338
		}catch (SecurityException e) {
339
			logger.error("SecurityException: " + e.getMessage());
340
			return null;
341
		} catch (NoSuchMethodException e) {
342
			if (obligatory){
343
				logger.error("NoSuchMethod: " + e.getMessage());
344
				return null;
345
			}else{
346
				if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
347
				return null;
348
			}
349
		}
350

    
351
	}
352

    
353
	/**
354
	 * @param cdmAttrName
355
	 * @param isBoolean
356
	 * @return
357
	 */
358
	public static String getGetterMethodName(String cdmAttrName, boolean isBoolean) {
359
		String methodName;
360
		if (isBoolean){
361
			if (cdmAttrName == null || cdmAttrName.length() < 3 ||  !( cdmAttrName.startsWith("is") || cdmAttrName.startsWith("use"))     ){
362
				throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters and start with 'is' or 'use': " + cdmAttrName);
363
			}
364
			methodName = cdmAttrName ;
365
		}else {
366
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
367
				throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
368
			}
369
			methodName = "get" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
370
		}
371
		return methodName;
372
	}
373

    
374
}
(41-41/62)