Project

General

Profile

Download (14 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.ICdmBase;
28
import eu.etaxonomy.cdm.model.common.IOriginalSource;
29
import eu.etaxonomy.cdm.model.common.ISourceable;
30
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
31
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
32
import eu.etaxonomy.cdm.model.common.Language;
33
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
34
import eu.etaxonomy.cdm.model.common.TimePeriod;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
36
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
37
import eu.etaxonomy.cdm.model.name.TaxonName;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
40
/**
41
 * @author a.mueller
42
 *
43
 */
44
public class ImportHelper {
45
	private static final Logger logger = Logger.getLogger(ImportHelper.class);
46

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

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

    
53

    
54

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

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

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

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

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

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

    
108
	}
109

    
110

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

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

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

    
123

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

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

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

    
172
	}
173

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

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

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

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

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

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

    
236

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

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

    
271
	}
272

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

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

    
290

    
291

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

    
301

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

    
313
	//************** EXPORT *******************/
314

    
315

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

    
352
	}
353

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

    
375
}
(45-45/71)