1 24 package org.riotfamily.riot.dao.support; 25 26 import java.lang.reflect.Method ; 27 import java.util.Collection ; 28 29 import org.riotfamily.common.beans.PropertyUtils; 30 import org.riotfamily.riot.dao.RiotDao; 31 import org.riotfamily.riot.dao.ListParams; 32 import org.springframework.beans.factory.InitializingBean; 33 import org.springframework.util.Assert; 34 import org.springframework.util.ReflectionUtils; 35 36 public class CustomDaoAdpater implements RiotDao, InitializingBean { 37 38 public static final String DEFAULT_ID_PROPERTY = "id"; 39 40 private Object customDao; 41 42 private Class itemClass; 43 44 private String idProperty = DEFAULT_ID_PROPERTY; 45 46 private String listMethodName; 47 48 private String loadMethodName; 49 50 private String saveMethodName; 51 52 private String updateMethodName; 53 54 private String deleteMethodName; 55 56 57 private Class idClass; 58 59 private Method listMethod; 60 61 private Method loadMethod; 62 63 private Method saveMethod; 64 65 private Method updateMethod; 66 67 private Method deleteMethod; 68 69 70 public CustomDaoAdpater(Object customDao, Class itemClass) { 71 this.customDao = customDao; 72 this.itemClass = itemClass; 73 } 74 75 public void setIdProperty(String idProperty) { 76 this.idProperty = idProperty; 77 } 78 79 public void setListMethod(String listMethodName) { 80 this.listMethodName = listMethodName; 81 } 82 83 public void setLoadMethod(String loadMethodName) { 84 this.loadMethodName = loadMethodName; 85 } 86 87 public void setSaveMethod(String saveMethodName) { 88 this.saveMethodName = saveMethodName; 89 } 90 91 public void setUpdateMethod(String updateMethodName) { 92 this.updateMethodName = updateMethodName; 93 } 94 95 public void setDeleteMethod(String deleteMethodName) { 96 this.deleteMethodName = deleteMethodName; 97 } 98 99 public void afterPropertiesSet() throws Exception { 100 idClass = PropertyUtils.getPropertyType(itemClass, idProperty); 101 Class daoClass = customDao.getClass(); 102 103 Assert.notNull(listMethodName, "A listMethod must be specified."); 104 listMethod = daoClass.getMethod(listMethodName, null); 105 106 if (loadMethodName != null) { 107 loadMethod = daoClass.getMethod(loadMethodName, new Class [] { idClass }); 108 } 109 if (saveMethodName != null) { 110 saveMethod = daoClass.getMethod(saveMethodName, new Class [] { itemClass }); 111 } 112 113 if (updateMethodName != null) { 114 updateMethod = daoClass.getMethod(updateMethodName, new Class [] { itemClass }); 115 } 116 117 if (deleteMethodName != null) { 118 deleteMethod = daoClass.getMethod(deleteMethodName, new Class [] { itemClass }); 119 } 120 } 121 122 public Class getEntityClass() { 123 return itemClass; 124 } 125 126 public Collection list(Object parent, ListParams params) { 127 return (Collection ) ReflectionUtils.invokeMethod(listMethod, customDao, null); 128 } 129 130 public String getObjectId(Object item) { 131 if (idProperty == null) { 132 return null; 133 } 134 return PropertyUtils.getPropertyAsString(item, idProperty); 135 } 136 137 public int getListSize(Object parent, ListParams params) { 138 return -1; 139 } 140 141 public Object load(String objectId) { 142 Assert.notNull(loadMethod, "A loadMethod must be specified."); 143 Object id = PropertyUtils.convert(objectId, idClass); 144 return ReflectionUtils.invokeMethod(loadMethod, customDao, new Object [] { id }); 145 } 146 147 public void save(Object item, Object parent) { 148 Assert.notNull(saveMethod, "A saveMethod must be specified."); 149 ReflectionUtils.invokeMethod(saveMethod, customDao, new Object [] { item }); 150 } 151 152 public void update(Object item) { 153 Assert.notNull(saveMethod, "An updateMethod must be specified."); 154 ReflectionUtils.invokeMethod(updateMethod, customDao, new Object [] { item }); 155 } 156 157 public void delete(Object item, Object parent) { 158 Assert.notNull(saveMethod, "A deleteMethod must be specified."); 159 ReflectionUtils.invokeMethod(deleteMethod, customDao, new Object [] { item }); 160 } 161 162 } 163 | Popular Tags |