TimePeriod -> jodaTime and ImportWrapper
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ImportHelper.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.io.common;
5
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;
11
12 import org.apache.log4j.Logger;
13 import org.jdom.Element;
14 import org.jdom.Namespace;
15 import org.joda.time.DateTime;
16
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;
26 /**
27 * @author a.mueller
28 *
29 */
30 public class ImportHelper {
31 private static final Logger logger = Logger.getLogger(ImportHelper.class);
32
33 public static final boolean OVERWRITE = true;
34 public static final boolean NO_OVERWRITE = false;
35
36 public static final boolean OBLIGATORY = true;
37 public static final boolean FACULTATIVE = false;
38
39
40
41 public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, long sourceId, String namespace){
42 return setOriginalSource(idEntity, sourceReference, String.valueOf(sourceId), namespace);
43 }
44
45 /**
46 * Adds an original source object to the identifiable entity.
47 * @param idEntity
48 * @param sourceReference
49 * @param sourceId
50 * @return
51 */
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);
58 return true;
59 }
60
61
62 public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
63 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);
64 }
65
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);
68 }
69
70 public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
71 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE);
72 }
73
74 public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull){
75 Object strValue;
76 try {
77 strValue = rs.getObject(dbAttrName);
78 return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
79 }catch (SQLException e) {
80 logger.error("SQLException: " + e);
81 return false;
82 }
83
84 }
85
86
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);
89 }
90
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);
93 }
94
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);
97 }
98
99
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);
102 }
103
104 public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
105 Object strValue;
106 strValue = getXmlInputValue(root, xmlElementName, namespace);
107 return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
108 }
109
110 public static boolean addValue(Object sourceValue, CdmBase cdmBase, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
111 String methodName;
112 // Object strValue;
113 try {
114 if (overwriteNull == NO_OVERWRITE && sourceValue == null ){
115 if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
116 return true;
117 }
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");
122 }
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");
127 }
128 methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
129 }else{
130 logger.error("Class not supported: " + clazz.toString());
131 return false;
132 }
133 Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
134 cdmMethod.invoke(cdmBase, sourceValue);
135 return true;
136 } catch (NullPointerException e) {
137 logger.error("NullPointerException: " + e.getMessage());
138 return false;
139 } catch (IllegalArgumentException e) {
140 logger.error("IllegalArgumentException: " + e.getMessage());
141 return false;
142 } catch (IllegalAccessException e) {
143 logger.error("IllegalAccessException: " + e.getMessage());
144 return false;
145 } catch (InvocationTargetException e) {
146 logger.error("InvocationTargetException: " + e.getMessage());
147 return false;
148 }catch (SecurityException e) {
149 logger.error("SecurityException: " + e.getMessage());
150 return false;
151 } catch (NoSuchMethodException e) {
152 if (obligat){
153 logger.error("NoSuchMethod: " + e.getMessage());
154 return false;
155 }else{
156 if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
157 return true;
158 }
159 }
160 }
161
162 public static boolean addAnnotationFromResultSet(ResultSet rs, String attributeName, AnnotatableEntity cdmBase, Language language){
163 try {
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);
169
170 }
171 return true;
172 } catch (SQLException e) {
173 logger.warn(e);
174 e.printStackTrace();
175 return false;
176 }
177 }
178
179
180
181 public static Object getXmlInputValue(Element root, String xmlElementName, Namespace namespace){
182 Object result = null;
183 Element child = root.getChild(xmlElementName, namespace);
184 if (child != null){
185 result = child.getText().trim();
186 }
187 return result;
188 }
189
190
191 public static TimePeriod getDatePublished(String refYear){
192 //FIXME until now only quick and dirty and wrong
193 if (refYear == null){
194 return null;
195 }
196 String[] years = refYear.split("-");
197 DateTime dtStart = null;
198 DateTime dtEnd = null;
199
200 if (years.length > 2 || years.length <= 0){
201 logger.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX getDatePublished");
202 }else {
203 dtStart = parseSingleDate(years[0]);
204
205 if (years.length >= 2){
206 dtEnd = parseSingleDate(years[1]);
207 }
208 }
209 TimePeriod result = TimePeriod.NewInstance(dtStart, dtEnd);
210 return result;
211 }
212
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)){
217 try {
218 Integer year = Integer.valueOf(singleDateString.trim());
219 if (year > 1750 && year < 2050){
220 dt = dt.withYear(year);
221 }
222 } catch (NumberFormatException e) {
223 logger.debug("Not a Integer format in getCalendar()");
224 }
225 }
226 return dt;
227
228 }
229
230
231 //******* old *****************
232
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)){
238 // try {
239 // Integer year = Integer.valueOf(strYear.trim());
240 // if (year > 1750 && year < 2030){
241 // cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
242 // }
243 // } catch (NumberFormatException e) {
244 // logger.debug("Not a Integer format in getCalendar()");
245 // }
246 // }
247 // return cal;
248 // }
249
250
251 }