2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.common
;
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
;
18 import org
.apache
.log4j
.Logger
;
19 import org
.jdom
.Element
;
20 import org
.jdom
.Namespace
;
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
;
41 public class ImportHelper
{
42 private static final Logger logger
= Logger
.getLogger(ImportHelper
.class);
44 public static final boolean OVERWRITE
= true;
45 public static final boolean NO_OVERWRITE
= false;
47 public static final boolean OBLIGATORY
= true;
48 public static final boolean FACULTATIVE
= false;
52 public static boolean setOriginalSource(ISourceable sourceable
, Reference sourceReference
, long sourceId
, String namespace
){
53 return setOriginalSource(sourceable
, sourceReference
, String
.valueOf(sourceId
), namespace
);
57 * Adds an original source object to the identifiable entity.
59 * @param sourceReference
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);
70 throw new ClassCastException("Unknown implementing class for ISourceable "+ sourceable
.getClass() + " . Not supported bei ImportHelper.");
72 sourceable
.addSource(originalSource
);
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
);
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);
84 public static boolean addBooleanValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
){
85 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, boolean.class, OVERWRITE
, false);
88 public static boolean addValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean blankToNull
){
91 strValue
= rs
.getObject(dbAttrName
);
92 if (strValue
instanceof String
&& strValue
!= null){
93 strValue
= ((String
)strValue
).trim();
94 if (blankToNull
&& strValue
.equals("")){
98 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, OBLIGATORY
);
99 }catch (SQLException e
) {
100 logger
.error("SQLException: " + e
);
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
);
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
);
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
);
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
);
124 public static boolean addXmlValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean obligat
){
126 strValue
= getXmlInputValue(root
, xmlElementName
, namespace
);
127 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, obligat
);
130 public static boolean addValue(Object sourceValue
, CdmBase cdmBase
, String cdmAttrName
, Class
<?
> clazz
, boolean overwriteNull
, boolean obligat
){
134 if (overwriteNull
== NO_OVERWRITE
&& sourceValue
== null ){
135 if (logger
.isDebugEnabled()) { logger
.debug("no overwrite for NULL-value");}
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
);
143 } catch (NullPointerException e
) {
144 logger
.error("NullPointerException: " + e
.getMessage());
146 } catch (IllegalArgumentException e
) {
147 logger
.error("IllegalArgumentException: " + e
.getMessage());
149 } catch (IllegalAccessException e
) {
150 logger
.error("IllegalAccessException: " + e
.getMessage());
152 } catch (InvocationTargetException e
) {
153 logger
.error("InvocationTargetException: " + e
.getMessage());
155 }catch (SecurityException e
) {
156 logger
.error("SecurityException: " + e
.getMessage());
158 } catch (NoSuchMethodException e
) {
160 logger
.error("NoSuchMethod: " + e
.getMessage());
163 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
171 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
174 // * @throws IllegalArgumentException if a clazz is not yet supported
176 public static String
getSetterMethodName(Class
<?
> clazz
, String cdmAttrName
) {
177 return getSetterPutterMethodName(clazz
, cdmAttrName
, "set");
181 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
184 // * @throws IllegalArgumentException if a clazz is not yet supported
186 public static String
getPutterMethodName(Class
<?
> clazz
, String cdmAttrName
) {
187 return getSetterPutterMethodName(clazz
, cdmAttrName
, "put");
191 * @param clazz either boolean or other class (for boolean the naming strategy is different !)
194 // * @throws IllegalArgumentException if a clazz is not yet supported
196 private static String
getSetterPutterMethodName(Class
<?
> clazz
, String cdmAttrName
, String prefix
) {
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");
202 methodName
= prefix
+ cdmAttrName
.substring(2, 3).toUpperCase() + cdmAttrName
.substring(3) ;
204 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
205 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
207 methodName
= prefix
+ cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
212 private static boolean valuesAreNull(List
<Object
> values
){
213 for (Object sourceValue
: values
.toArray()){
214 if (sourceValue
!= null){
221 public static boolean addMultipleValues(List
<Object
> sourceValues
, CdmBase cdmBase
, String cdmAttrName
, List
<Class
> classes
, boolean overwriteNull
, boolean obligat
){
226 if (overwriteNull
== NO_OVERWRITE
&& valuesAreNull(sourceValues
)){
227 if (logger
.isDebugEnabled()) { logger
.debug("no overwrite for NULL-value");}
230 if (logger
.isDebugEnabled()) { logger
.debug("addValues: " + sourceValues
.toString());}
233 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
234 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
236 methodName
= "add" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
238 Class
<?
>[] classArray
= classes
.toArray(new Class
[0]);
239 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
, classArray
);
240 cdmMethod
.invoke(cdmBase
, sourceValues
.toArray());
242 } catch (NullPointerException e
) {
243 logger
.error("NullPointerException: " + e
.getMessage());
245 } catch (IllegalArgumentException e
) {
246 logger
.error("IllegalArgumentException: " + e
.getMessage());
248 } catch (IllegalAccessException e
) {
249 logger
.error("IllegalAccessException: " + e
.getMessage());
251 } catch (InvocationTargetException e
) {
252 logger
.error("InvocationTargetException: " + e
.getMessage());
254 }catch (SecurityException e
) {
255 logger
.error("SecurityException: " + e
.getMessage());
257 } catch (NoSuchMethodException e
) {
259 logger
.error("NoSuchMethod: " + e
.getMessage());
262 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
269 public static boolean addAnnotationFromResultSet(ResultSet rs
, String attributeName
, AnnotatableEntity cdmBase
, Language language
){
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
);
279 } catch (SQLException e
) {
288 public static Object
getXmlInputValue(Element root
, String xmlElementName
, Namespace namespace
){
289 Object result
= null;
290 Element child
= root
.getChild(xmlElementName
, namespace
);
292 result
= child
.getText().trim();
298 public static TimePeriod
getDatePublished(String refYear
){
299 TimePeriod resultNew
;
301 resultNew
= TimePeriod
.parseString(refYear
);
302 } catch (IllegalArgumentException e
) {
303 logger
.warn("RefYear could not be parsed: " + refYear
);
309 //************** EXPORT *******************/
312 public static<T
extends Object
> T
getValue(CdmBase cdmBase
, String cdmAttrName
, boolean isBoolean
, boolean obligatory
){
316 methodName
= getGetterMethodName(cdmAttrName
, isBoolean
);
317 if (cdmBase
.isInstanceOf(NonViralName
.class)){
318 cdmBase
= CdmBase
.deproxy(cdmBase
, NonViralName
.class);
320 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
);
321 result
= (T
)cdmMethod
.invoke(cdmBase
);
323 } catch (NullPointerException e
) {
324 logger
.error("NullPointerException: " + e
.getMessage());
326 } catch (IllegalArgumentException e
) {
327 logger
.error("IllegalArgumentException: " + e
.getMessage());
329 } catch (IllegalAccessException e
) {
330 logger
.error("IllegalAccessException: " + e
.getMessage());
332 } catch (InvocationTargetException e
) {
333 logger
.error("InvocationTargetException: " + e
.getMessage());
335 }catch (SecurityException e
) {
336 logger
.error("SecurityException: " + e
.getMessage());
338 } catch (NoSuchMethodException e
) {
340 logger
.error("NoSuchMethod: " + e
.getMessage());
343 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
355 public static String
getGetterMethodName(String cdmAttrName
, boolean 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
);
361 methodName
= cdmAttrName
;
363 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
364 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
366 methodName
= "get" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;