KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > dao > support > CustomDaoAdpater


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

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