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
.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
;
36 public class ImportHelper
{
37 private static final Logger logger
= Logger
.getLogger(ImportHelper
.class);
39 public static final boolean OVERWRITE
= true;
40 public static final boolean NO_OVERWRITE
= false;
42 public static final boolean OBLIGATORY
= true;
43 public static final boolean FACULTATIVE
= false;
47 public static boolean setOriginalSource(IdentifiableEntity idEntity
, ReferenceBase sourceReference
, long sourceId
, String namespace
){
48 return setOriginalSource(idEntity
, sourceReference
, String
.valueOf(sourceId
), namespace
);
52 * Adds an original source object to the identifiable entity.
54 * @param sourceReference
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
);
65 public static boolean addStringValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
){
66 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, String
.class, OVERWRITE
);
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
);
73 public static boolean addBooleanValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
){
74 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, boolean.class, OVERWRITE
);
77 public static boolean addValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
, Class clazz
, boolean overwriteNull
){
80 strValue
= rs
.getObject(dbAttrName
);
81 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, OBLIGATORY
);
82 }catch (SQLException e
) {
83 logger
.error("SQLException: " + e
);
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
);
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
);
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
);
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
);
107 public static boolean addXmlValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean obligat
){
109 strValue
= getXmlInputValue(root
, xmlElementName
, namespace
);
110 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, obligat
);
113 public static boolean addValue(Object sourceValue
, CdmBase cdmBase
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean obligat
){
117 if (overwriteNull
== NO_OVERWRITE
&& sourceValue
== null ){
118 if (logger
.isDebugEnabled()) { logger
.debug("no overwrite for NULL-value");}
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");
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");
131 methodName
= "set" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
133 logger
.error("Class not supported: " + clazz
.toString());
136 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
, clazz
);
137 cdmMethod
.invoke(cdmBase
, sourceValue
);
139 } catch (NullPointerException e
) {
140 logger
.error("NullPointerException: " + e
.getMessage());
142 } catch (IllegalArgumentException e
) {
143 logger
.error("IllegalArgumentException: " + e
.getMessage());
145 } catch (IllegalAccessException e
) {
146 logger
.error("IllegalAccessException: " + e
.getMessage());
148 } catch (InvocationTargetException e
) {
149 logger
.error("InvocationTargetException: " + e
.getMessage());
151 }catch (SecurityException e
) {
152 logger
.error("SecurityException: " + e
.getMessage());
154 } catch (NoSuchMethodException e
) {
156 logger
.error("NoSuchMethod: " + e
.getMessage());
159 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
166 private static boolean valuesAreNull(List
<Object
> values
){
167 for (Object sourceValue
: values
.toArray()){
168 if (sourceValue
!= null){
175 public static boolean addMultipleValues(List
<Object
> sourceValues
, CdmBase cdmBase
, String cdmAttrName
, List
<Class
> classes
, boolean overwriteNull
, boolean obligat
){
180 if (overwriteNull
== NO_OVERWRITE
&& valuesAreNull(sourceValues
)){
181 if (logger
.isDebugEnabled()) { logger
.debug("no overwrite for NULL-value");}
184 if (logger
.isDebugEnabled()) { logger
.debug("addValues: " + sourceValues
.toString());}
187 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
188 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
190 methodName
= "add" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
192 Class
[] classArray
= classes
.toArray(new Class
[0]);
193 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
, classArray
);
194 cdmMethod
.invoke(cdmBase
, sourceValues
.toArray());
196 } catch (NullPointerException e
) {
197 logger
.error("NullPointerException: " + e
.getMessage());
199 } catch (IllegalArgumentException e
) {
200 logger
.error("IllegalArgumentException: " + e
.getMessage());
202 } catch (IllegalAccessException e
) {
203 logger
.error("IllegalAccessException: " + e
.getMessage());
205 } catch (InvocationTargetException e
) {
206 logger
.error("InvocationTargetException: " + e
.getMessage());
208 }catch (SecurityException e
) {
209 logger
.error("SecurityException: " + e
.getMessage());
211 } catch (NoSuchMethodException e
) {
213 logger
.error("NoSuchMethod: " + e
.getMessage());
216 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
223 public static boolean addAnnotationFromResultSet(ResultSet rs
, String attributeName
, AnnotatableEntity cdmBase
, Language language
){
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
);
233 } catch (SQLException e
) {
242 public static Object
getXmlInputValue(Element root
, String xmlElementName
, Namespace namespace
){
243 Object result
= null;
244 Element child
= root
.getChild(xmlElementName
, namespace
);
246 result
= child
.getText().trim();
252 public static TimePeriod
getDatePublished(String refYear
){
253 TimePeriod resultNew
;
255 resultNew
= TimePeriod
.parseString(refYear
);
256 } catch (IllegalArgumentException e
) {
257 logger
.warn("RefYear could not be parsed: " + refYear
);
263 //************** EXPORT *******************/
266 public static<T
extends Object
> T
getValue(CdmBase cdmBase
, String cdmAttrName
, boolean isBoolean
, boolean obligatory
){
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
);
274 methodName
= cdmAttrName
;
276 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
277 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
279 methodName
= "get" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
282 // logger.error("Class not supported: " + clazz.toString());
285 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
);
286 result
= (T
)cdmMethod
.invoke(cdmBase
);
288 } catch (NullPointerException e
) {
289 logger
.error("NullPointerException: " + e
.getMessage());
291 } catch (IllegalArgumentException e
) {
292 logger
.error("IllegalArgumentException: " + e
.getMessage());
294 } catch (IllegalAccessException e
) {
295 logger
.error("IllegalAccessException: " + e
.getMessage());
297 } catch (InvocationTargetException e
) {
298 logger
.error("InvocationTargetException: " + e
.getMessage());
300 }catch (SecurityException e
) {
301 logger
.error("SecurityException: " + e
.getMessage());
303 } catch (NoSuchMethodException e
) {
305 logger
.error("NoSuchMethod: " + e
.getMessage());
308 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
319 //******* old *****************
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)){
327 // Integer year = Integer.valueOf(strYear.trim());
328 // if (year > 1750 && year < 2030){
329 // cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
331 // } catch (NumberFormatException e) {
332 // logger.debug("Not a Integer format in getCalendar()");