Project

General

Profile

Download (13.8 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.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
20
import org.jdom.Element;
21
import org.jdom.Namespace;
22

    
23
import eu.etaxonomy.cdm.common.CdmUtils;
24
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
26
import eu.etaxonomy.cdm.model.common.Annotation;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.ICdmBase;
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.VerbatimTimePeriod;
33
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
34
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
35
import eu.etaxonomy.cdm.model.name.TaxonName;
36
import eu.etaxonomy.cdm.model.reference.IOriginalSource;
37
import eu.etaxonomy.cdm.model.reference.ISourceable;
38
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
41
/**
42
 * @author a.mueller
43
 *
44
 */
45
public class ImportHelper {
46
	private static final Logger logger = LogManager.getLogger(ImportHelper.class);
47

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

    
51
	public static final boolean OBLIGATORY = true;
52
	public static final boolean  FACULTATIVE = false;
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(@SuppressWarnings("rawtypes") ISourceable sourceable,
67
	        Reference sourceReference, String sourceId, String namespace){
68
		IOriginalSource originalSource;
69
		OriginalSourceType type = OriginalSourceType.Import;
70
		if (HibernateProxyHelper.isInstanceOf(sourceable, IdentifiableEntity.class)){
71
			originalSource = IdentifiableSource.NewInstance(type, sourceId, namespace, sourceReference, null);
72
		}else if (HibernateProxyHelper.isInstanceOf(sourceable, DescriptionElementBase.class)){
73
			originalSource = DescriptionElementSource.NewInstance(type, sourceId, namespace, sourceReference, null);
74
		}else{
75
			throw new ClassCastException("Unknown implementing class for ISourceable "+ sourceable.getClass() + " . Not supported bei ImportHelper.");
76
		}
77
		sourceable.addSource(originalSource);
78
		return true;
79
	}
80

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

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

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

    
93
	public static boolean addValue(ResultSet rs, ICdmBase cdmBase, String dbAttrName, String cdmAttrName,
94
	        Class<?> clazz, boolean overwriteNull, boolean blankToNull){
95
		Object strValue;
96
		try {
97
			strValue = rs.getObject(dbAttrName);
98
			if (strValue instanceof String){
99
				strValue = ((String)strValue).trim();
100
				if (blankToNull &&  strValue.equals("")){
101
					strValue = null;
102
				}
103
			}
104
			return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
105
		}catch (SQLException e) {
106
			logger.error("SQLException: " +  e);
107
			return false;
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,
125
	        String cdmAttrName, Class<?> clazz, boolean overwriteNull){
126
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
127
	}
128

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

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

    
174
	}
175

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

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

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

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

    
227
	public static boolean addMultipleValues(List<Object> sourceValues, CdmBase cdmBase, String cdmAttrName,
228
	        @SuppressWarnings("rawtypes") List<Class> classes, boolean overwriteNull, boolean obligat){
229

    
230
		String methodName;
231
//		Object strValue;
232
		try {
233

    
234
			if (overwriteNull == NO_OVERWRITE && valuesAreNull(sourceValues)){
235
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
236
				return true;
237
			}
238
			if (logger.isDebugEnabled()) { logger.debug("addValues: " + sourceValues.toString());}
239

    
240

    
241
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
242
				throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
243
			}
244
			methodName = "add" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
245

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

    
275
	}
276

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

    
285
			}
286
			return true;
287
		} catch (SQLException e) {
288
			logger.warn(e);
289
			e.printStackTrace();
290
			return false;
291
		}
292
	}
293

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

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

    
314
	//************** EXPORT *******************/
315

    
316

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