Workaround for out of memory issues: Added logic to split the amount of taxa to be...
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ImportHelper.java
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.model.common.AnnotatableEntity;
24 import eu.etaxonomy.cdm.model.common.Annotation;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.common.ISourceable;
27 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
28 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.common.TimePeriod;
31 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
32 /**
33 * @author a.mueller
34 *
35 */
36 public class ImportHelper {
37 private static final Logger logger = Logger.getLogger(ImportHelper.class);
38
39 public static final boolean OVERWRITE = true;
40 public static final boolean NO_OVERWRITE = false;
41
42 public static final boolean OBLIGATORY = true;
43 public static final boolean FACULTATIVE = false;
44
45
46
47 public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, long sourceId, String namespace){
48 return setOriginalSource(idEntity, sourceReference, String.valueOf(sourceId), namespace);
49 }
50
51 /**
52 * Adds an original source object to the identifiable entity.
53 * @param idEntity
54 * @param sourceReference
55 * @param sourceId
56 * @return
57 */
58 public static boolean setOriginalSource(ISourceable idEntity, ReferenceBase sourceReference, String sourceId, String namespace){
59 IdentifiableSource originalSource = IdentifiableSource.NewInstance(sourceId, namespace, sourceReference, null);
60 idEntity.addSource(originalSource);
61 return true;
62 }
63
64
65 public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
66 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);
67 }
68
69 public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean overwriteNull){
70 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, overwriteNull);
71 }
72
73 public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
74 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE);
75 }
76
77 public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull){
78 Object strValue;
79 try {
80 strValue = rs.getObject(dbAttrName);
81 return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
82 }catch (SQLException e) {
83 logger.error("SQLException: " + e);
84 return false;
85 }
86
87 }
88
89
90 public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
91 return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, OVERWRITE);
92 }
93
94 public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, boolean overwriteNull){
95 return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, overwriteNull);
96 }
97
98 public static boolean addXmlBooleanValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
99 return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, boolean.class, OVERWRITE);
100 }
101
102
103 public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull){
104 return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
105 }
106
107 public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
108 Object strValue;
109 strValue = getXmlInputValue(root, xmlElementName, namespace);
110 return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
111 }
112
113 public static boolean addValue(Object sourceValue, CdmBase cdmBase, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
114 String methodName;
115 // Object strValue;
116 try {
117 if (overwriteNull == NO_OVERWRITE && sourceValue == null ){
118 if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
119 return true;
120 }
121 if (logger.isDebugEnabled()) { logger.debug("addValue: " + sourceValue);}
122 if (clazz == boolean.class || clazz == Boolean.class){
123 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
124 throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
125 }
126 methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
127 }else if(clazz == String.class) {
128 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
129 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
130 }
131 methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
132 }else{
133 logger.error("Class not supported: " + clazz.toString());
134 return false;
135 }
136 Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
137 cdmMethod.invoke(cdmBase, sourceValue);
138 return true;
139 } catch (NullPointerException e) {
140 logger.error("NullPointerException: " + e.getMessage());
141 return false;
142 } catch (IllegalArgumentException e) {
143 logger.error("IllegalArgumentException: " + e.getMessage());
144 return false;
145 } catch (IllegalAccessException e) {
146 logger.error("IllegalAccessException: " + e.getMessage());
147 return false;
148 } catch (InvocationTargetException e) {
149 logger.error("InvocationTargetException: " + e.getMessage());
150 return false;
151 }catch (SecurityException e) {
152 logger.error("SecurityException: " + e.getMessage());
153 return false;
154 } catch (NoSuchMethodException e) {
155 if (obligat){
156 logger.error("NoSuchMethod: " + e.getMessage());
157 return false;
158 }else{
159 if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
160 return true;
161 }
162 }
163
164 }
165
166 private static boolean valuesAreNull(List<Object> values){
167 for (Object sourceValue : values.toArray()){
168 if (sourceValue != null){
169 return false;
170 }
171 }
172 return true;
173 }
174
175 public static boolean addMultipleValues(List<Object> sourceValues, CdmBase cdmBase, String cdmAttrName, List<Class> classes, boolean overwriteNull, boolean obligat){
176 String methodName;
177 // Object strValue;
178 try {
179
180 if (overwriteNull == NO_OVERWRITE && valuesAreNull(sourceValues)){
181 if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
182 return true;
183 }
184 if (logger.isDebugEnabled()) { logger.debug("addValues: " + sourceValues.toString());}
185
186
187 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
188 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
189 }
190 methodName = "add" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
191
192 Class[] classArray = classes.toArray(new Class[0]);
193 Method cdmMethod = cdmBase.getClass().getMethod(methodName, classArray);
194 cdmMethod.invoke(cdmBase, sourceValues.toArray());
195 return true;
196 } catch (NullPointerException e) {
197 logger.error("NullPointerException: " + e.getMessage());
198 return false;
199 } catch (IllegalArgumentException e) {
200 logger.error("IllegalArgumentException: " + e.getMessage());
201 return false;
202 } catch (IllegalAccessException e) {
203 logger.error("IllegalAccessException: " + e.getMessage());
204 return false;
205 } catch (InvocationTargetException e) {
206 logger.error("InvocationTargetException: " + e.getMessage());
207 return false;
208 }catch (SecurityException e) {
209 logger.error("SecurityException: " + e.getMessage());
210 return false;
211 } catch (NoSuchMethodException e) {
212 if (obligat){
213 logger.error("NoSuchMethod: " + e.getMessage());
214 return false;
215 }else{
216 if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
217 return true;
218 }
219 }
220
221 }
222
223 public static boolean addAnnotationFromResultSet(ResultSet rs, String attributeName, AnnotatableEntity cdmBase, Language language){
224 try {
225 String value = rs.getString(attributeName);
226 if (CdmUtils.Nz(value).equals("")){
227 String strAnnotation = attributeName + ": " + value;
228 Annotation annoatation = Annotation.NewInstance(strAnnotation, language);
229 cdmBase.addAnnotation(annoatation);
230
231 }
232 return true;
233 } catch (SQLException e) {
234 logger.warn(e);
235 e.printStackTrace();
236 return false;
237 }
238 }
239
240
241
242 public static Object getXmlInputValue(Element root, String xmlElementName, Namespace namespace){
243 Object result = null;
244 Element child = root.getChild(xmlElementName, namespace);
245 if (child != null){
246 result = child.getText().trim();
247 }
248 return result;
249 }
250
251
252 public static TimePeriod getDatePublished(String refYear){
253 TimePeriod resultNew;
254 try {
255 resultNew = TimePeriod.parseString(refYear);
256 } catch (IllegalArgumentException e) {
257 logger.warn("RefYear could not be parsed: " + refYear);
258 resultNew = null;
259 }
260 return resultNew;
261 }
262
263 //************** EXPORT *******************/
264
265
266 public static<T extends Object> T getValue(CdmBase cdmBase, String cdmAttrName, boolean isBoolean, boolean obligatory){
267 String methodName;
268 T result;
269 try {
270 if (isBoolean){
271 if (cdmAttrName == null || cdmAttrName.length() < 3 || !( cdmAttrName.startsWith("is") || cdmAttrName.startsWith("use")) ){
272 throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters and start with 'is' or 'use': " + cdmAttrName);
273 }
274 methodName = cdmAttrName ;
275 }else {
276 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
277 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
278 }
279 methodName = "get" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
280 }
281 // else{
282 // logger.error("Class not supported: " + clazz.toString());
283 // return null;
284 // }
285 Method cdmMethod = cdmBase.getClass().getMethod(methodName);
286 result = (T)cdmMethod.invoke(cdmBase);
287 return result;
288 } catch (NullPointerException e) {
289 logger.error("NullPointerException: " + e.getMessage());
290 return null;
291 } catch (IllegalArgumentException e) {
292 logger.error("IllegalArgumentException: " + e.getMessage());
293 return null;
294 } catch (IllegalAccessException e) {
295 logger.error("IllegalAccessException: " + e.getMessage());
296 return null;
297 } catch (InvocationTargetException e) {
298 logger.error("InvocationTargetException: " + e.getMessage());
299 return null;
300 }catch (SecurityException e) {
301 logger.error("SecurityException: " + e.getMessage());
302 return null;
303 } catch (NoSuchMethodException e) {
304 if (obligatory){
305 logger.error("NoSuchMethod: " + e.getMessage());
306 return null;
307 }else{
308 if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
309 return null;
310 }
311 }
312
313 }
314
315
316
317
318
319 //******* old *****************
320
321 // private static Calendar getCalendar(String strYear){
322 // //FIXME until now only quick and dirty and wrong
323 // Calendar cal = Calendar.getInstance();
324 // cal.set(9999, Calendar.DECEMBER, 30, 0, 0, 0);
325 // if (CdmUtils.isNumeric(strYear)){
326 // try {
327 // Integer year = Integer.valueOf(strYear.trim());
328 // if (year > 1750 && year < 2030){
329 // cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
330 // }
331 // } catch (NumberFormatException e) {
332 // logger.debug("Not a Integer format in getCalendar()");
333 // }
334 // }
335 // return cal;
336 // }
337
338
339 }