4 package eu
.etaxonomy
.cdm
.io
.common
;
6 import java
.lang
.reflect
.InvocationTargetException
;
7 import java
.lang
.reflect
.Method
;
8 import java
.sql
.ResultSet
;
9 import java
.sql
.SQLException
;
10 import java
.util
.Calendar
;
12 import org
.apache
.log4j
.Logger
;
13 import org
.jdom
.Element
;
14 import org
.jdom
.Namespace
;
15 import org
.joda
.time
.DateTime
;
17 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
18 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
19 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
22 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
23 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSource
;
24 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
30 public class ImportHelper
{
31 private static final Logger logger
= Logger
.getLogger(ImportHelper
.class);
33 public static final boolean OVERWRITE
= true;
34 public static final boolean NO_OVERWRITE
= false;
36 public static final boolean OBLIGATORY
= true;
37 public static final boolean FACULTATIVE
= false;
41 public static boolean setOriginalSource(IdentifiableEntity idEntity
, ReferenceBase sourceReference
, long sourceId
, String namespace
){
42 return setOriginalSource(idEntity
, sourceReference
, String
.valueOf(sourceId
), namespace
);
46 * Adds an original source object to the identifiable entity.
48 * @param sourceReference
52 public static boolean setOriginalSource(IdentifiableEntity idEntity
, ReferenceBase sourceReference
, String sourceId
, String namespace
){
53 OriginalSource originalSource
= new OriginalSource();
54 originalSource
.setIdInSource(sourceId
);
55 originalSource
.setCitation(sourceReference
);
56 originalSource
.setIdNamespace(namespace
);
57 idEntity
.addSource(originalSource
);
62 public static boolean addStringValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
){
63 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, String
.class, OVERWRITE
);
66 public static boolean addStringValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
, boolean overwriteNull
){
67 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, String
.class, overwriteNull
);
70 public static boolean addBooleanValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
){
71 return addValue(rs
, cdmBase
, dbAttrName
, cdmAttrName
, boolean.class, OVERWRITE
);
74 public static boolean addValue(ResultSet rs
, CdmBase cdmBase
, String dbAttrName
, String cdmAttrName
, Class clazz
, boolean overwriteNull
){
77 strValue
= rs
.getObject(dbAttrName
);
78 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, OBLIGATORY
);
79 }catch (SQLException e
) {
80 logger
.error("SQLException: " + e
);
87 public static boolean addXmlStringValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
){
88 return addXmlValue(root
, cdmBase
, xmlElementName
, namespace
, cdmAttrName
, String
.class, OVERWRITE
);
91 public static boolean addXmlStringValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
, boolean overwriteNull
){
92 return addXmlValue(root
, cdmBase
, xmlElementName
, namespace
, cdmAttrName
, String
.class, overwriteNull
);
95 public static boolean addXmlBooleanValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
){
96 return addXmlValue(root
, cdmBase
, xmlElementName
, namespace
, cdmAttrName
, boolean.class, OVERWRITE
);
100 public static boolean addXmlValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
, Class clazz
, boolean overwriteNull
){
101 return addXmlValue(root
, cdmBase
, xmlElementName
, namespace
, cdmAttrName
, clazz
, overwriteNull
, OBLIGATORY
);
104 public static boolean addXmlValue(Element root
, CdmBase cdmBase
, String xmlElementName
, Namespace namespace
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean obligat
){
106 strValue
= getXmlInputValue(root
, xmlElementName
, namespace
);
107 return addValue(strValue
, cdmBase
, cdmAttrName
, clazz
, overwriteNull
, obligat
);
110 public static boolean addValue(Object sourceValue
, CdmBase cdmBase
, String cdmAttrName
, Class clazz
, boolean overwriteNull
, boolean obligat
){
114 if (overwriteNull
== NO_OVERWRITE
&& sourceValue
== null ){
115 if (logger
.isDebugEnabled()) { logger
.debug("no overwrite for NULL-value");}
118 if (logger
.isDebugEnabled()) { logger
.debug("addValue: " + sourceValue
);}
119 if (clazz
== boolean.class || clazz
== Boolean
.class){
120 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
121 throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
123 methodName
= "set" + cdmAttrName
.substring(2, 3).toUpperCase() + cdmAttrName
.substring(3) ;
124 }else if(clazz
== String
.class) {
125 if (cdmAttrName
== null || cdmAttrName
.length() < 1 ){
126 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
128 methodName
= "set" + cdmAttrName
.substring(0, 1).toUpperCase() + cdmAttrName
.substring(1) ;
130 logger
.error("Class not supported: " + clazz
.toString());
133 Method cdmMethod
= cdmBase
.getClass().getMethod(methodName
, clazz
);
134 cdmMethod
.invoke(cdmBase
, sourceValue
);
136 } catch (NullPointerException e
) {
137 logger
.error("NullPointerException: " + e
.getMessage());
139 } catch (IllegalArgumentException e
) {
140 logger
.error("IllegalArgumentException: " + e
.getMessage());
142 } catch (IllegalAccessException e
) {
143 logger
.error("IllegalAccessException: " + e
.getMessage());
145 } catch (InvocationTargetException e
) {
146 logger
.error("InvocationTargetException: " + e
.getMessage());
148 }catch (SecurityException e
) {
149 logger
.error("SecurityException: " + e
.getMessage());
151 } catch (NoSuchMethodException e
) {
153 logger
.error("NoSuchMethod: " + e
.getMessage());
156 if (logger
.isDebugEnabled()){ logger
.debug("NoSuchMethod: " + e
.getMessage());}
162 public static boolean addAnnotationFromResultSet(ResultSet rs
, String attributeName
, AnnotatableEntity cdmBase
, Language language
){
164 String value
= rs
.getString(attributeName
);
165 if (CdmUtils
.Nz(value
).equals("")){
166 String strAnnotation
= attributeName
+ ": " + value
;
167 Annotation annoatation
= Annotation
.NewInstance(strAnnotation
, language
);
168 cdmBase
.addAnnotation(annoatation
);
172 } catch (SQLException e
) {
181 public static Object
getXmlInputValue(Element root
, String xmlElementName
, Namespace namespace
){
182 Object result
= null;
183 Element child
= root
.getChild(xmlElementName
, namespace
);
185 result
= child
.getText().trim();
191 public static TimePeriod
getDatePublished(String refYear
){
192 //FIXME until now only quick and dirty and wrong
193 if (refYear
== null){
196 String
[] years
= refYear
.split("-");
197 DateTime dtStart
= null;
198 DateTime dtEnd
= null;
200 if (years
.length
> 2 || years
.length
<= 0){
201 logger
.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX getDatePublished");
203 dtStart
= parseSingleDate(years
[0]);
205 if (years
.length
>= 2){
206 dtEnd
= parseSingleDate(years
[1]);
209 TimePeriod result
= TimePeriod
.NewInstance(dtStart
, dtEnd
);
213 private static DateTime
parseSingleDate(String singleDateString
){
214 //FIXME until now only quick and dirty and wrong
215 DateTime dt
= new DateTime(9999, 12, 30, 0,0,0,0);
216 if (CdmUtils
.isNumeric(singleDateString
)){
218 Integer year
= Integer
.valueOf(singleDateString
.trim());
219 if (year
> 1750 && year
< 2050){
220 dt
= dt
.withYear(year
);
222 } catch (NumberFormatException e
) {
223 logger
.debug("Not a Integer format in getCalendar()");
231 //******* old *****************
233 // private static Calendar getCalendar(String strYear){
234 // //FIXME until now only quick and dirty and wrong
235 // Calendar cal = Calendar.getInstance();
236 // cal.set(9999, Calendar.DECEMBER, 30, 0, 0, 0);
237 // if (CdmUtils.isNumeric(strYear)){
239 // Integer year = Integer.valueOf(strYear.trim());
240 // if (year > 1750 && year < 2030){
241 // cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
243 // } catch (NumberFormatException e) {
244 // logger.debug("Not a Integer format in getCalendar()");