KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > dao > ibatis > BaseDaoiBATIS


1 package org.appfuse.dao.ibatis;
2
3 import java.io.Serializable JavaDoc;
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.appfuse.dao.Dao;
12 import org.springframework.orm.ObjectRetrievalFailureException;
13 import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
14 import org.springframework.util.ClassUtils;
15
16 /**
17  * @author Bobby Diaz
18  * @version 1.0
19  */

20 public class BaseDaoiBATIS extends SqlMapClientDaoSupport implements Dao {
21     protected final Log log = LogFactory.getLog(getClass());
22
23     public List JavaDoc getObjects(Class JavaDoc clazz) {
24         return getSqlMapClientTemplate().queryForList(getSelectQuery(ClassUtils.getShortName(clazz)), null);
25     }
26
27     public Object JavaDoc getObject(Class JavaDoc clazz, Serializable JavaDoc primaryKey) {
28         Object JavaDoc object = getSqlMapClientTemplate().queryForObject(getFindQuery(ClassUtils.getShortName(clazz)), primaryKey);
29         if (object == null) {
30             throw new ObjectRetrievalFailureException(ClassUtils.getShortName(clazz), primaryKey);
31         }
32         return object;
33     }
34
35     public void saveObject(final Object JavaDoc object) {
36         String JavaDoc className = ClassUtils.getShortName(object.getClass());
37         Object JavaDoc primaryKey = getPrimaryKeyValue(object);
38         String JavaDoc keyId = null;
39
40         // check for null id
41
if (primaryKey != null) {
42             keyId = primaryKey.toString();
43         }
44
45         // check for new record
46
if (StringUtils.isBlank(keyId)) {
47             prepareObjectForSaveOrUpdate(object);
48             primaryKey = getSqlMapClientTemplate().insert(getInsertQuery(className), object);
49
50             // check for null id
51
if (primaryKey != null) {
52                 keyId = primaryKey.toString();
53             }
54         } else {
55             prepareObjectForSaveOrUpdate(object);
56             getSqlMapClientTemplate().update(getUpdateQuery(className), object);
57         }
58
59         // check for null id
60
if (getPrimaryKeyValue(object) == null) {
61             throw new ObjectRetrievalFailureException(className, object);
62         }
63     }
64
65     public void removeObject(Class JavaDoc clazz, Serializable JavaDoc primaryKey) {
66         getSqlMapClientTemplate().update(getDeleteQuery(ClassUtils.getShortName(clazz)), primaryKey);
67     }
68     
69     private String JavaDoc getPrimaryKeyFieldName(Object JavaDoc o) {
70         Field JavaDoc fieldlist[] = o.getClass().getDeclaredFields();
71         String JavaDoc fieldName = null;
72         for (int i = 0; i < fieldlist.length; i++) {
73             Field JavaDoc fld = fieldlist[i];
74             if (fld.getName().equals("id") || fld.getName().indexOf("Id") > -1 || fld.getName().equals("version")) {
75                 fieldName = fld.getName();
76                 break;
77             }
78         }
79         return fieldName;
80     }
81
82     protected Object JavaDoc getPrimaryKeyValue(Object JavaDoc o) {
83         // Use reflection to find the first property that has the name "id" or "Id"
84
String JavaDoc fieldName = getPrimaryKeyFieldName(o);
85         String JavaDoc getterMethod = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
86         
87         try {
88             Method JavaDoc getMethod = o.getClass().getMethod(getterMethod, null);
89             return getMethod.invoke(o, null);
90         } catch (Exception JavaDoc e) {
91             e.printStackTrace();
92             log.error("Could not invoke method '" + getterMethod + "' on " + ClassUtils.getShortName(o.getClass()));
93         }
94         return null;
95     }
96     
97     protected void prepareObjectForSaveOrUpdate(Object JavaDoc o) {
98         try {
99             Field JavaDoc fieldlist[] = o.getClass().getDeclaredFields();
100             for (int i = 0; i < fieldlist.length; i++) {
101                 Field JavaDoc fld = fieldlist[i];
102                 String JavaDoc fieldName = fld.getName();
103                 if (fieldName.equals("version")) {
104                     Method JavaDoc setMethod = o.getClass().getMethod("setVersion", new Class JavaDoc[]{Integer JavaDoc.class});
105                     Object JavaDoc value = o.getClass().getMethod("getVersion", null).invoke(o, null);
106                     if (value == null) {
107                         setMethod.invoke(o, new Object JavaDoc[]{new Integer JavaDoc(1)});
108                     } else {
109                         setMethod.invoke(o, new Object JavaDoc[]{new Integer JavaDoc(((Integer JavaDoc) value).intValue()+1)});
110                     }
111                 }
112             }
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace();
115             log.error("Could not prepare '" + ClassUtils.getShortName(o.getClass()) + "' for insert/update");
116         }
117     }
118
119     /**
120      * @return Returns the select query name.
121      */

122     public String JavaDoc getSelectQuery(String JavaDoc className) {
123         return "get" + className + "s";
124     }
125
126     /**
127      * @return Returns the find query name.
128      */

129     public String JavaDoc getFindQuery(String JavaDoc className) {
130         return "get" + className;
131     }
132
133     /**
134      * @return Returns the insert query name.
135      */

136     public String JavaDoc getInsertQuery(String JavaDoc className) {
137         return "add" + className;
138     }
139
140     /**
141      * @return Returns the update query name.
142      */

143     public String JavaDoc getUpdateQuery(String JavaDoc className) {
144         return "update" + className;
145     }
146
147     /**
148      * @return Returns the delete query name.
149      */

150     public String JavaDoc getDeleteQuery(String JavaDoc className) {
151         return "delete" + className;
152     }
153 }
154
Popular Tags