| 1 package org.appfuse.dao.ibatis; 2 3 import java.io.Serializable ; 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.Method ; 6 import java.util.List ; 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 20 public class BaseDaoiBATIS extends SqlMapClientDaoSupport implements Dao { 21 protected final Log log = LogFactory.getLog(getClass()); 22 23 public List getObjects(Class clazz) { 24 return getSqlMapClientTemplate().queryForList(getSelectQuery(ClassUtils.getShortName(clazz)), null); 25 } 26 27 public Object getObject(Class clazz, Serializable primaryKey) { 28 Object 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 object) { 36 String className = ClassUtils.getShortName(object.getClass()); 37 Object primaryKey = getPrimaryKeyValue(object); 38 String keyId = null; 39 40 if (primaryKey != null) { 42 keyId = primaryKey.toString(); 43 } 44 45 if (StringUtils.isBlank(keyId)) { 47 prepareObjectForSaveOrUpdate(object); 48 primaryKey = getSqlMapClientTemplate().insert(getInsertQuery(className), object); 49 50 if (primaryKey != null) { 52 keyId = primaryKey.toString(); 53 } 54 } else { 55 prepareObjectForSaveOrUpdate(object); 56 getSqlMapClientTemplate().update(getUpdateQuery(className), object); 57 } 58 59 if (getPrimaryKeyValue(object) == null) { 61 throw new ObjectRetrievalFailureException(className, object); 62 } 63 } 64 65 public void removeObject(Class clazz, Serializable primaryKey) { 66 getSqlMapClientTemplate().update(getDeleteQuery(ClassUtils.getShortName(clazz)), primaryKey); 67 } 68 69 private String getPrimaryKeyFieldName(Object o) { 70 Field fieldlist[] = o.getClass().getDeclaredFields(); 71 String fieldName = null; 72 for (int i = 0; i < fieldlist.length; i++) { 73 Field 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 getPrimaryKeyValue(Object o) { 83 String fieldName = getPrimaryKeyFieldName(o); 85 String getterMethod = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); 86 87 try { 88 Method getMethod = o.getClass().getMethod(getterMethod, null); 89 return getMethod.invoke(o, null); 90 } catch (Exception 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 o) { 98 try { 99 Field fieldlist[] = o.getClass().getDeclaredFields(); 100 for (int i = 0; i < fieldlist.length; i++) { 101 Field fld = fieldlist[i]; 102 String fieldName = fld.getName(); 103 if (fieldName.equals("version")) { 104 Method setMethod = o.getClass().getMethod("setVersion", new Class []{Integer .class}); 105 Object value = o.getClass().getMethod("getVersion", null).invoke(o, null); 106 if (value == null) { 107 setMethod.invoke(o, new Object []{new Integer (1)}); 108 } else { 109 setMethod.invoke(o, new Object []{new Integer (((Integer ) value).intValue()+1)}); 110 } 111 } 112 } 113 } catch (Exception e) { 114 e.printStackTrace(); 115 log.error("Could not prepare '" + ClassUtils.getShortName(o.getClass()) + "' for insert/update"); 116 } 117 } 118 119 122 public String getSelectQuery(String className) { 123 return "get" + className + "s"; 124 } 125 126 129 public String getFindQuery(String className) { 130 return "get" + className; 131 } 132 133 136 public String getInsertQuery(String className) { 137 return "add" + className; 138 } 139 140 143 public String getUpdateQuery(String className) { 144 return "update" + className; 145 } 146 147 150 public String getDeleteQuery(String className) { 151 return "delete" + className; 152 } 153 } 154 | Popular Tags |