Project

General

Profile

Download (14.2 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;import org.apache.logging.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.IdentifiableEntity;
29
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
32
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
33
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
34
import eu.etaxonomy.cdm.model.name.TaxonName;
35
import eu.etaxonomy.cdm.model.reference.IOriginalSource;
36
import eu.etaxonomy.cdm.model.reference.ISourceable;
37
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
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 = LogManager.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
	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(@SuppressWarnings("rawtypes") ISourceable sourceable,
66
	        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,
93
	        Class<?> clazz, boolean overwriteNull, boolean blankToNull){
94
		Object strValue;
95
		try {
96
			strValue = rs.getObject(dbAttrName);
97
			if (strValue instanceof String){
98
				strValue = ((String)strValue).trim();
99
				if (blankToNull &&  strValue.equals("")){
100
					strValue = null;
101
				}
102
			}
103
			return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
104
		}catch (SQLException e) {
105
			logger.error("SQLException: " +  e);
106
			return false;
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,
124
	        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,
129
	        String cdmAttrName, Class<?> clazz, boolean overwriteNull, boolean obligat){
130
		Object strValue;
131
		strValue = getXmlInputValue(root, xmlElementName, namespace);
132
		return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
133
	}
134

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

    
173
	}
174

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

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

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

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

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

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

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

    
239

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

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

    
274
	}
275

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

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

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

    
302
	public static VerbatimTimePeriod getDatePublished(String refYear){
303
	    VerbatimTimePeriod resultNew;
304
		try {
305
			resultNew = TimePeriodParser.parseStringVerbatim(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,
317
	        boolean isBoolean, boolean obligatory){
318
		try {
319
		    String methodName = getGetterMethodName(cdmAttrName, isBoolean);
320
			if (cdmBase.isInstanceOf(TaxonName.class)){
321
				cdmBase = CdmBase.deproxy(cdmBase);
322
			}
323
			Method cdmMethod = cdmBase.getClass().getMethod(methodName);
324
			@SuppressWarnings("unchecked")
325
            T 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
	 * @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/65)