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
|
import java.util.List;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.jdom.Element;
|
15
|
import org.jdom.Namespace;
|
16
|
import org.joda.time.DateTime;
|
17
|
|
18
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
19
|
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
|
20
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
23
|
import eu.etaxonomy.cdm.model.common.Language;
|
24
|
import eu.etaxonomy.cdm.model.common.OriginalSource;
|
25
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
26
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
27
|
/**
|
28
|
* @author a.mueller
|
29
|
*
|
30
|
*/
|
31
|
public class ImportHelper {
|
32
|
private static final Logger logger = Logger.getLogger(ImportHelper.class);
|
33
|
|
34
|
public static final boolean OVERWRITE = true;
|
35
|
public static final boolean NO_OVERWRITE = false;
|
36
|
|
37
|
public static final boolean OBLIGATORY = true;
|
38
|
public static final boolean FACULTATIVE = false;
|
39
|
|
40
|
|
41
|
|
42
|
public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, long sourceId, String namespace){
|
43
|
return setOriginalSource(idEntity, sourceReference, String.valueOf(sourceId), namespace);
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Adds an original source object to the identifiable entity.
|
48
|
* @param idEntity
|
49
|
* @param sourceReference
|
50
|
* @param sourceId
|
51
|
* @return
|
52
|
*/
|
53
|
public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase sourceReference, String sourceId, String namespace){
|
54
|
OriginalSource originalSource = new OriginalSource();
|
55
|
originalSource.setIdInSource(sourceId);
|
56
|
originalSource.setCitation(sourceReference);
|
57
|
originalSource.setIdNamespace(namespace);
|
58
|
idEntity.addSource(originalSource);
|
59
|
return true;
|
60
|
}
|
61
|
|
62
|
|
63
|
public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
|
64
|
return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);
|
65
|
}
|
66
|
|
67
|
public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean overwriteNull){
|
68
|
return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, overwriteNull);
|
69
|
}
|
70
|
|
71
|
public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
|
72
|
return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE);
|
73
|
}
|
74
|
|
75
|
public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull){
|
76
|
Object strValue;
|
77
|
try {
|
78
|
strValue = rs.getObject(dbAttrName);
|
79
|
return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
|
80
|
}catch (SQLException e) {
|
81
|
logger.error("SQLException: " + e);
|
82
|
return false;
|
83
|
}
|
84
|
|
85
|
}
|
86
|
|
87
|
|
88
|
public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
|
89
|
return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, OVERWRITE);
|
90
|
}
|
91
|
|
92
|
public static boolean addXmlStringValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, boolean overwriteNull){
|
93
|
return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, String.class, overwriteNull);
|
94
|
}
|
95
|
|
96
|
public static boolean addXmlBooleanValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName){
|
97
|
return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, boolean.class, OVERWRITE);
|
98
|
}
|
99
|
|
100
|
|
101
|
public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull){
|
102
|
return addXmlValue(root, cdmBase, xmlElementName, namespace, cdmAttrName, clazz, overwriteNull, OBLIGATORY);
|
103
|
}
|
104
|
|
105
|
public static boolean addXmlValue(Element root, CdmBase cdmBase, String xmlElementName, Namespace namespace, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
|
106
|
Object strValue;
|
107
|
strValue = getXmlInputValue(root, xmlElementName, namespace);
|
108
|
return addValue(strValue, cdmBase, cdmAttrName, clazz, overwriteNull, obligat);
|
109
|
}
|
110
|
|
111
|
public static boolean addValue(Object sourceValue, CdmBase cdmBase, String cdmAttrName, Class clazz, boolean overwriteNull, boolean obligat){
|
112
|
String methodName;
|
113
|
// Object strValue;
|
114
|
try {
|
115
|
if (overwriteNull == NO_OVERWRITE && sourceValue == null ){
|
116
|
if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
|
117
|
return true;
|
118
|
}
|
119
|
if (logger.isDebugEnabled()) { logger.debug("addValue: " + sourceValue);}
|
120
|
if (clazz == boolean.class || clazz == Boolean.class){
|
121
|
if (cdmAttrName == null || cdmAttrName.length() < 1 ){
|
122
|
throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
|
123
|
}
|
124
|
methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
|
125
|
}else if(clazz == String.class) {
|
126
|
if (cdmAttrName == null || cdmAttrName.length() < 1 ){
|
127
|
throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
|
128
|
}
|
129
|
methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
|
130
|
}else{
|
131
|
logger.error("Class not supported: " + clazz.toString());
|
132
|
return false;
|
133
|
}
|
134
|
Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
|
135
|
cdmMethod.invoke(cdmBase, sourceValue);
|
136
|
return true;
|
137
|
} catch (NullPointerException e) {
|
138
|
logger.error("NullPointerException: " + e.getMessage());
|
139
|
return false;
|
140
|
} catch (IllegalArgumentException e) {
|
141
|
logger.error("IllegalArgumentException: " + e.getMessage());
|
142
|
return false;
|
143
|
} catch (IllegalAccessException e) {
|
144
|
logger.error("IllegalAccessException: " + e.getMessage());
|
145
|
return false;
|
146
|
} catch (InvocationTargetException e) {
|
147
|
logger.error("InvocationTargetException: " + e.getMessage());
|
148
|
return false;
|
149
|
}catch (SecurityException e) {
|
150
|
logger.error("SecurityException: " + e.getMessage());
|
151
|
return false;
|
152
|
} catch (NoSuchMethodException e) {
|
153
|
if (obligat){
|
154
|
logger.error("NoSuchMethod: " + e.getMessage());
|
155
|
return false;
|
156
|
}else{
|
157
|
if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
|
158
|
return true;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
}
|
163
|
|
164
|
private static boolean valuesAreNull(List<Object> values){
|
165
|
for (Object sourceValue : values.toArray()){
|
166
|
if (sourceValue != null){
|
167
|
return false;
|
168
|
}
|
169
|
}
|
170
|
return true;
|
171
|
}
|
172
|
|
173
|
public static boolean addMultipleValues(List<Object> sourceValues, CdmBase cdmBase, String cdmAttrName, List<Class> classes, boolean overwriteNull, boolean obligat){
|
174
|
String methodName;
|
175
|
// Object strValue;
|
176
|
try {
|
177
|
|
178
|
if (overwriteNull == NO_OVERWRITE && valuesAreNull(sourceValues)){
|
179
|
if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
|
180
|
return true;
|
181
|
}
|
182
|
if (logger.isDebugEnabled()) { logger.debug("addValues: " + sourceValues.toString());}
|
183
|
|
184
|
|
185
|
if (cdmAttrName == null || cdmAttrName.length() < 1 ){
|
186
|
throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
|
187
|
}
|
188
|
methodName = "add" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
|
189
|
|
190
|
Class[] classArray = classes.toArray(new Class[0]);
|
191
|
Method cdmMethod = cdmBase.getClass().getMethod(methodName, classArray);
|
192
|
cdmMethod.invoke(cdmBase, sourceValues.toArray());
|
193
|
return true;
|
194
|
} catch (NullPointerException e) {
|
195
|
logger.error("NullPointerException: " + e.getMessage());
|
196
|
return false;
|
197
|
} catch (IllegalArgumentException e) {
|
198
|
logger.error("IllegalArgumentException: " + e.getMessage());
|
199
|
return false;
|
200
|
} catch (IllegalAccessException e) {
|
201
|
logger.error("IllegalAccessException: " + e.getMessage());
|
202
|
return false;
|
203
|
} catch (InvocationTargetException e) {
|
204
|
logger.error("InvocationTargetException: " + e.getMessage());
|
205
|
return false;
|
206
|
}catch (SecurityException e) {
|
207
|
logger.error("SecurityException: " + e.getMessage());
|
208
|
return false;
|
209
|
} catch (NoSuchMethodException e) {
|
210
|
if (obligat){
|
211
|
logger.error("NoSuchMethod: " + e.getMessage());
|
212
|
return false;
|
213
|
}else{
|
214
|
if (logger.isDebugEnabled()){ logger.debug("NoSuchMethod: " + e.getMessage());}
|
215
|
return true;
|
216
|
}
|
217
|
}
|
218
|
|
219
|
}
|
220
|
|
221
|
public static boolean addAnnotationFromResultSet(ResultSet rs, String attributeName, AnnotatableEntity cdmBase, Language language){
|
222
|
try {
|
223
|
String value = rs.getString(attributeName);
|
224
|
if (CdmUtils.Nz(value).equals("")){
|
225
|
String strAnnotation = attributeName + ": " + value;
|
226
|
Annotation annoatation = Annotation.NewInstance(strAnnotation, language);
|
227
|
cdmBase.addAnnotation(annoatation);
|
228
|
|
229
|
}
|
230
|
return true;
|
231
|
} catch (SQLException e) {
|
232
|
logger.warn(e);
|
233
|
e.printStackTrace();
|
234
|
return false;
|
235
|
}
|
236
|
}
|
237
|
|
238
|
|
239
|
|
240
|
public static Object getXmlInputValue(Element root, String xmlElementName, Namespace namespace){
|
241
|
Object result = null;
|
242
|
Element child = root.getChild(xmlElementName, namespace);
|
243
|
if (child != null){
|
244
|
result = child.getText().trim();
|
245
|
}
|
246
|
return result;
|
247
|
}
|
248
|
|
249
|
|
250
|
public static TimePeriod getDatePublished(String refYear){
|
251
|
//FIXME until now only quick and dirty and wrong
|
252
|
if (refYear == null){
|
253
|
return null;
|
254
|
}
|
255
|
String[] years = refYear.split("-");
|
256
|
DateTime dtStart = null;
|
257
|
DateTime dtEnd = null;
|
258
|
|
259
|
if (years.length > 2 || years.length <= 0){
|
260
|
logger.warn("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX getDatePublished");
|
261
|
}else {
|
262
|
dtStart = parseSingleDate(years[0]);
|
263
|
|
264
|
if (years.length >= 2){
|
265
|
dtEnd = parseSingleDate(years[1]);
|
266
|
}
|
267
|
}
|
268
|
TimePeriod result = TimePeriod.NewInstance(dtStart, dtEnd);
|
269
|
return result;
|
270
|
}
|
271
|
|
272
|
private static DateTime parseSingleDate(String singleDateString){
|
273
|
//FIXME until now only quick and dirty and wrong
|
274
|
DateTime dt = new DateTime(9999, 12, 30, 0,0,0,0);
|
275
|
if (CdmUtils.isNumeric(singleDateString)){
|
276
|
try {
|
277
|
Integer year = Integer.valueOf(singleDateString.trim());
|
278
|
if (year > 1750 && year < 2050){
|
279
|
dt = dt.withYear(year);
|
280
|
}
|
281
|
} catch (NumberFormatException e) {
|
282
|
logger.debug("Not a Integer format in getCalendar()");
|
283
|
}
|
284
|
}
|
285
|
return dt;
|
286
|
|
287
|
}
|
288
|
|
289
|
|
290
|
//******* old *****************
|
291
|
|
292
|
// private static Calendar getCalendar(String strYear){
|
293
|
// //FIXME until now only quick and dirty and wrong
|
294
|
// Calendar cal = Calendar.getInstance();
|
295
|
// cal.set(9999, Calendar.DECEMBER, 30, 0, 0, 0);
|
296
|
// if (CdmUtils.isNumeric(strYear)){
|
297
|
// try {
|
298
|
// Integer year = Integer.valueOf(strYear.trim());
|
299
|
// if (year > 1750 && year < 2030){
|
300
|
// cal.set(year, Calendar.JANUARY, 1, 0, 0, 0);
|
301
|
// }
|
302
|
// } catch (NumberFormatException e) {
|
303
|
// logger.debug("Not a Integer format in getCalendar()");
|
304
|
// }
|
305
|
// }
|
306
|
// return cal;
|
307
|
// }
|
308
|
|
309
|
|
310
|
}
|