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