(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / ImportHelper.java
1 /**
2 *
3 */
4 package eu.etaxonomy.cdm.io.berlinModel;
5
6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10
11 import org.apache.log4j.Logger;
12
13 import eu.etaxonomy.cdm.model.common.CdmBase;
14 import eu.etaxonomy.cdm.model.name.BotanicalName;
15 /**
16 * @author a.mueller
17 *
18 */
19 public class ImportHelper {
20 private static final Logger logger = Logger.getLogger(ImportHelper.class);
21
22
23 public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
24 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class);
25 }
26
27
28 public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
29 return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class);
30 }
31
32 public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz){
33 try {
34 String methodName;
35 Object strValue = rs.getObject(dbAttrName);
36 if (logger.isDebugEnabled()) { logger.debug("addValue: " + strValue);}
37 if (clazz == boolean.class || clazz == Boolean.class){
38 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
39 throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
40 }
41 methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
42 }else if(clazz == String.class) {
43 if (cdmAttrName == null || cdmAttrName.length() < 1 ){
44 throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
45 }
46 methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
47 }else{
48 logger.error("Class not supported: " + clazz.toString());
49 return false;
50 }
51 Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
52 cdmMethod.invoke(cdmBase, strValue);
53 return true;
54 } catch (IllegalArgumentException e) {
55 logger.error("IllegalArgumentException: " + e.getMessage());
56 return false;
57 } catch (IllegalAccessException e) {
58 logger.error("IllegalAccessException: " + e.getMessage());
59 return false;
60 } catch (InvocationTargetException e) {
61 logger.error("InvocationTargetException: " + e.getMessage());
62 return false;
63 }catch (SecurityException e) {
64 logger.error("SecurityException: " + e.getMessage());
65 return false;
66 } catch (NoSuchMethodException e) {
67 logger.error("NoSuchMethod: " + e.getMessage());
68 return false;
69 }catch (SQLException e) {
70 logger.error("SQLException: " + e);
71 return false;
72 }
73
74 }
75
76 }