Project

General

Profile

Download (3.68 KB) Statistics
| Branch: | Tag: | Revision:
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.common.IdentifiableEntity;
15
import eu.etaxonomy.cdm.model.common.OriginalSource;
16
import eu.etaxonomy.cdm.model.name.BotanicalName;
17
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
18
/**
19
 * @author a.mueller
20
 *
21
 */
22
public class ImportHelper {
23
	private static final Logger logger = Logger.getLogger(ImportHelper.class);
24
	
25
	public static final boolean OVERWRITE = true;
26
	public static final boolean  NO_OVERWRITE = false;
27
	
28
	
29
	public static boolean setOriginalSource(IdentifiableEntity idEntity, ReferenceBase berlinModelRef, int berlinModelId){
30
		OriginalSource originalSource = new OriginalSource();
31
		originalSource.setIdInSource(String.valueOf(berlinModelId));
32
		originalSource.setCitation(berlinModelRef);
33
		idEntity.addSource(originalSource);
34
		return true;
35
	}
36
	
37
	
38
	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
39
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, OVERWRITE);
40
	}
41
	
42
	public static boolean addStringValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, boolean overwriteNull){
43
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, String.class, overwriteNull);
44
	}
45
		
46
	public static boolean addBooleanValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName){
47
		return addValue(rs, cdmBase, dbAttrName, cdmAttrName, boolean.class, OVERWRITE);
48
	}
49

    
50
	public static boolean addValue(ResultSet rs, CdmBase cdmBase, String dbAttrName, String cdmAttrName, Class clazz, boolean overwriteNull){
51
		String methodName;
52
		Object strValue;
53
		try {
54
			strValue = rs.getObject(dbAttrName);
55
			if (overwriteNull == NO_OVERWRITE && strValue == null ){
56
				if (logger.isDebugEnabled()) { logger.debug("no overwrite for NULL-value");}
57
				return true;
58
			}
59
			if (logger.isDebugEnabled()) { logger.debug("addValue: " + strValue);}
60
			if (clazz == boolean.class || clazz == Boolean.class){
61
				if (cdmAttrName == null || cdmAttrName.length() < 1 ){
62
					throw new IllegalArgumentException("boolean CdmAttributeName should have atleast 3 characters");
63
				}
64
				methodName = "set" + cdmAttrName.substring(2, 3).toUpperCase() + cdmAttrName.substring(3) ;
65
			}else if(clazz == String.class) {
66
				if (cdmAttrName == null || cdmAttrName.length() < 1 ){
67
					throw new IllegalArgumentException("CdmAttributeName should have atleast 1 character");
68
				}
69
				methodName = "set" + cdmAttrName.substring(0, 1).toUpperCase() + cdmAttrName.substring(1) ;
70
			}else{
71
				logger.error("Class not supported: " + clazz.toString());
72
				return false;
73
			}
74
			Method cdmMethod = cdmBase.getClass().getMethod(methodName, clazz);
75
			cdmMethod.invoke(cdmBase, strValue);
76
			return true;
77
		} catch (IllegalArgumentException e) {
78
			logger.error("IllegalArgumentException: " + e.getMessage());
79
			return false;
80
		} catch (IllegalAccessException e) {
81
			logger.error("IllegalAccessException: " + e.getMessage());
82
			return false;
83
		} catch (InvocationTargetException e) {
84
			logger.error("InvocationTargetException: " + e.getMessage());
85
			return false;
86
		}catch (SecurityException e) {
87
			logger.error("SecurityException: " + e.getMessage());
88
			return false;
89
		} catch (NoSuchMethodException e) {
90
			logger.error("NoSuchMethod: " + e.getMessage());
91
			return false;
92
		}catch (SQLException e) {
93
			logger.error("SQLException: " +  e);
94
			return false;
95
		}
96

    
97
	}	
98

    
99
}
(12-12/13)