Project

General

Profile

Download (10.9 KB) Statistics
| Branch: | Tag: | Revision:
1 d427288b Andreas Müller
/**
2
 * 
3
 */
4 43b78563 Andreas Müller
package eu.etaxonomy.cdm.io.common;
5 d427288b Andreas Müller
6
import java.lang.reflect.InvocationTargetException;
7
import java.lang.reflect.Method;
8
import java.sql.ResultSet;
9
import java.sql.SQLException;
10 79bcb889 Andreas Müller
import java.util.Calendar;
11 e133b83b Andreas Müller
import java.util.List;
12 d427288b Andreas Müller
13
import org.apache.log4j.Logger;
14 43b78563 Andreas Müller
import org.jdom.Element;
15
import org.jdom.Namespace;
16 bcdc633e Andreas Müller
import org.joda.time.DateTime;
17 d427288b Andreas Müller
18 79bcb889 Andreas Müller
import eu.etaxonomy.cdm.common.CdmUtils;
19 b42eac28 Andreas Müller
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
20
import eu.etaxonomy.cdm.model.common.Annotation;
21 d427288b Andreas Müller
import eu.etaxonomy.cdm.model.common.CdmBase;
22 4e4722d3 Andreas Müller
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
23 b42eac28 Andreas Müller
import eu.etaxonomy.cdm.model.common.Language;
24 4e4722d3 Andreas Müller
import eu.etaxonomy.cdm.model.common.OriginalSource;
25 79bcb889 Andreas Müller
import eu.etaxonomy.cdm.model.common.TimePeriod;
26 4e4722d3 Andreas Müller
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
27 d427288b Andreas Müller
/**
28
 * @author a.mueller
29
 *
30
 */
31
public class ImportHelper {
32
	private static final Logger logger = Logger.getLogger(ImportHelper.class);
33
	
34 8160c112 Andreas Müller
	public static final boolean OVERWRITE = true;
35
	public static final boolean  NO_OVERWRITE = false;
36
	
37 79bcb889 Andreas Müller
	public static final boolean OBLIGATORY = true;
38
	public static final boolean  FACULTATIVE = false;
39
	
40 8160c112 Andreas Müller
	
41 57c46d2e Andreas Müller
42
	public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, long sourceId, String namespace){
43
		return setOriginalSource(idEntity, sourceReference, String.valueOf(sourceId), namespace);
44
	}
45
	
46
	/**
47
	 * Adds an original source object to the identifiable entity. 
48
	 * @param idEntity
49
	 * @param sourceReference
50
	 * @param sourceId
51
	 * @return
52
	 */
53
	public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, String sourceId, String namespace){
54 4e4722d3 Andreas Müller
		OriginalSource originalSource = new OriginalSource();
55 57c46d2e Andreas Müller
		originalSource.setIdInSource(sourceId);
56 43b78563 Andreas Müller
		originalSource.setCitation(sourceReference);
57 57c46d2e Andreas Müller
		originalSource.setIdNamespace(namespace);
58 4e4722d3 Andreas Müller
		idEntity.addSource(originalSource);
59
		return true;
60
	}
61
	
62 d427288b Andreas Müller
	
63
	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
64 8160c112 Andreas Müller
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);
65 d427288b Andreas Müller
	}
66
	
67 8160c112 Andreas Müller
	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean overwriteNull){
68
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, overwriteNull);
69
	}
70
		
71 d427288b Andreas Müller
	public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
72 8160c112 Andreas Müller
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE);
73 d427288b Andreas Müller
	}
74
75 8160c112 Andreas Müller
	public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull){
76 51c93ed3 Andreas Müller
		Object strValue;
77 d427288b Andreas Müller
		try {
78 51c93ed3 Andreas Müller
			strValue = rs.getObject(dbAttrName);
79 79bcb889 Andreas Müller
			return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
80 43b78563 Andreas Müller
		}catch (SQLException e) {
81
			logger.error("SQLException: " +  e);
82
			return false;
83
		}
84
85
	}
86
	
87
	
88
	public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
89
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, OVERWRITE);
90
	}
91
	
92
	public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, boolean overwriteNull){
93
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, overwriteNull);
94
	}
95
		
96
	public static boolean addXmlBooleanValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
97
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, boolean.class, OVERWRITE);
98
	}
99
100
101
	public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull){
102 79bcb889 Andreas Müller
		return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
103
	}
104
105
	public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
106 43b78563 Andreas Müller
		Object strValue;
107
		strValue = getXmlInputValue(root, xmlElementName, namespace);
108 79bcb889 Andreas Müller
		return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
109 43b78563 Andreas Müller
	}
110
	
111 79bcb889 Andreas Müller
	public static boolean addValue(Object sourceValue, CdmBase cdmBase, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
112 43b78563 Andreas Müller
		String methodName;
113
//		Object strValue;
114
		try {
115
			if (overwriteNull == NO_OVERWRITE && sourceValue == null ){
116 8160c112 Andreas Müller
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
117
				return true;
118
			}
119 43b78563 Andreas Müller
			if (logger.isDebugEnabled()) { logger.debug("addValue: " + sourceValue);}
120 d427288b Andreas Müller
			if (clazz == boolean.class || clazz == Boolean.class){
121 dd9ecdbd Andreas Müller
				if (cdmAttrName == null || cdmAttrName.length() < 1 ){
122
					throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
123
				}
124 d427288b Andreas Müller
				methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
125
			}else if(clazz == String.class) {
126 dd9ecdbd Andreas Müller
				if (cdmAttrName == null || cdmAttrName.length() < 1 ){
127
					throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
128
				}
129 d427288b Andreas Müller
				methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
130
			}else{
131
				logger.error("Class not supported: " + clazz.toString());
132
				return false;
133
			}
134
			Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
135 43b78563 Andreas Müller
			cdmMethod.invoke(cdmBase, sourceValue);
136 d427288b Andreas Müller
			return true;
137 4406daca Andreas Müller
		} catch (NullPointerException e) {
138
			logger.error("NullPointerException: " + e.getMessage());
139
			return false;
140 d427288b Andreas Müller
		} catch (IllegalArgumentException e) {
141
			logger.error("IllegalArgumentException: " + e.getMessage());
142
			return false;
143
		} catch (IllegalAccessException e) {
144
			logger.error("IllegalAccessException: " + e.getMessage());
145
			return false;
146
		} catch (InvocationTargetException e) {
147
			logger.error("InvocationTargetException: " + e.getMessage());
148
			return false;
149
		}catch (SecurityException e) {
150
			logger.error("SecurityException: " + e.getMessage());
151
			return false;
152
		} catch (NoSuchMethodException e) {
153 79bcb889 Andreas Müller
			if (obligat){
154
				logger.error("NoSuchMethod: " + e.getMessage());
155 b42eac28 Andreas Müller
				return false;
156 79bcb889 Andreas Müller
			}else{
157 b42eac28 Andreas Müller
				if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
158
				return true;
159 79bcb889 Andreas Müller
			}
160 d427288b Andreas Müller
		}
161 e133b83b Andreas Müller
		
162
	}
163
164
	private static boolean valuesAreNull(List<Object> values){
165
		for (Object sourceValue : values.toArray()){
166
			if (sourceValue != null){
167
				return false;
168
			}
169
		}
170
		return true;
171
	}
172
	
173
	public static boolean addMultipleValues(List<Object> sourceValues, CdmBase cdmBase, String cdmAttrName, List<Class> classes, boolean overwriteNull, boolean obligat){
174
		String methodName;
175
//		Object strValue;
176
		try {
177
			
178
			if (overwriteNull == NO_OVERWRITE && valuesAreNull(sourceValues)){
179
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
180
				return true;
181
			}
182
			if (logger.isDebugEnabled()) { logger.debug("addValues: " + sourceValues.toString());}
183
			
184
			
185
			if (cdmAttrName == null || cdmAttrName.length() < 1 ){
186
				throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
187
			}
188
			methodName = "add" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
189
			
190
			Class[] classArray = classes.toArray(new Class[0]);
191
			Method cdmMethod = cdmBase.getClass().getMethod(methodName, classArray);
192
			cdmMethod.invoke(cdmBase, sourceValues.toArray());
193
			return true;
194
		} catch (NullPointerException e) {
195
			logger.error("NullPointerException: " + e.getMessage());
196
			return false;
197
		} catch (IllegalArgumentException e) {
198
			logger.error("IllegalArgumentException: " + e.getMessage());
199
			return false;
200
		} catch (IllegalAccessException e) {
201
			logger.error("IllegalAccessException: " + e.getMessage());
202
			return false;
203
		} catch (InvocationTargetException e) {
204
			logger.error("InvocationTargetException: " + e.getMessage());
205
			return false;
206
		}catch (SecurityException e) {
207
			logger.error("SecurityException: " + e.getMessage());
208
			return false;
209
		} catch (NoSuchMethodException e) {
210
			if (obligat){
211
				logger.error("NoSuchMethod: " + e.getMessage());
212
				return false;
213
			}else{
214
				if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
215
				return true;
216
			}
217
		}
218
		
219 b42eac28 Andreas Müller
	}
220
	
221
	public static boolean addAnnotationFromResultSet(ResultSet rs, String attributeName, AnnotatableEntity cdmBase, Language language){
222
		try {
223
			String value = rs.getString(attributeName);
224
			if (CdmUtils.Nz(value).equals("")){
225
				String strAnnotation = attributeName + ": " + value;
226
				Annotation annoatation = Annotation.NewInstance(strAnnotation, language);
227
				cdmBase.addAnnotation(annoatation);
228 d427288b Andreas Müller
229 b42eac28 Andreas Müller
			}
230
			return true;
231
		} catch (SQLException e) {
232
			logger.warn(e);
233
			e.printStackTrace();
234
			return false;
235
		}
236 43b78563 Andreas Müller
	}
237
	
238 b42eac28 Andreas Müller
	
239
	
240 43b78563 Andreas Müller
	public static Object getXmlInputValue(Element root, String xmlElementName, Namespace namespace){
241
		Object result = null; 
242
		Element child = root.getChild(xmlElementName, namespace);
243
		if (child != null){
244
			result = child.getText().trim();
245
		}
246
		return result;
247
	}
248 79bcb889 Andreas Müller
	
249
	
250
	public static TimePeriod getDatePublished(String refYear){
251
		//FIXME until now only quick and dirty and wrong
252
		if (refYear == null){
253
			return null;
254
		}
255
		String[] years = refYear.split("-");
256 bcdc633e Andreas Müller
		DateTime dtStart = null;
257
		DateTime dtEnd = null;
258 79bcb889 Andreas Müller
		
259
		if (years.length > 2 || years.length <= 0){
260 b42eac28 Andreas Müller
			logger.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX getDatePublished");
261 79bcb889 Andreas Müller
		}else {
262 bcdc633e Andreas Müller
			dtStart = parseSingleDate(years[0]);
263
			
264 79bcb889 Andreas Müller
			if (years.length >= 2){
265 bcdc633e Andreas Müller
				dtEnd = parseSingleDate(years[1]);
266 79bcb889 Andreas Müller
			}
267
		}
268 bcdc633e Andreas Müller
		TimePeriod result = TimePeriod.NewInstance(dtStart, dtEnd);
269 79bcb889 Andreas Müller
		return result;
270
	}
271
	
272 bcdc633e Andreas Müller
	private static DateTime parseSingleDate(String singleDateString){
273 79bcb889 Andreas Müller
		//FIXME until now only quick and dirty and wrong
274 bcdc633e Andreas Müller
		DateTime dt = new DateTime(9999, 12, 30, 0,0,0,0);
275
		if (CdmUtils.isNumeric(singleDateString)){
276 79bcb889 Andreas Müller
			try {
277 bcdc633e Andreas Müller
				Integer year = Integer.valueOf(singleDateString.trim());
278
				if (year > 1750 && year < 2050){
279
					dt = dt.withYear(year);
280 79bcb889 Andreas Müller
				}
281
			} catch (NumberFormatException e) {
282
				logger.debug("Not a Integer format in getCalendar()");
283
			}
284
		}
285 bcdc633e Andreas Müller
		return dt;
286
287 79bcb889 Andreas Müller
	}
288 43b78563 Andreas Müller
289 bcdc633e Andreas Müller
	
290
	//******* old *****************
291
	
292
//	private static Calendar getCalendar(String strYear){
293
//		//FIXME until now only quick and dirty and wrong
294
//		Calendar cal = Calendar.getInstance();
295
//		cal.set(9999, Calendar.DECEMBER, 30, 0, 0, 0);
296
//		if (CdmUtils.isNumeric(strYear)){
297
//			try {
298
//				Integer year = Integer.valueOf(strYear.trim());
299
//				if (year > 1750 && year < 2030){
300
//					cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
301
//				}
302
//			} catch (NumberFormatException e) {
303
//				logger.debug("Not a Integer format in getCalendar()");
304
//			}
305
//		}
306
//		return cal;
307
//	}
308
309 d427288b Andreas Müller
310
}