KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > dao > HqlCollectionDao


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.hibernate.dao;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.hibernate.Query;
33 import org.riotfamily.common.beans.PropertyUtils;
34 import org.riotfamily.riot.dao.CutAndPasteEnabledDao;
35 import org.riotfamily.riot.dao.ListParams;
36 import org.riotfamily.riot.dao.Order;
37 import org.riotfamily.riot.dao.ParentChildDao;
38 import org.riotfamily.riot.dao.SortableDao;
39 import org.riotfamily.riot.hibernate.support.HibernateSupport;
40 import org.riotfamily.riot.hibernate.support.HibernateUtils;
41 import org.springframework.util.Assert;
42
43
44 /**
45  * RiotDao implementation that loads a bean and returns one of the
46  * bean's properties as (filtered) collection.
47  */

48 public class HqlCollectionDao extends HibernateSupport
49         implements SortableDao, ParentChildDao, CutAndPasteEnabledDao {
50
51     private Log log = LogFactory.getLog(HqlCollectionDao.class);
52     
53     private Class JavaDoc entityClass;
54     
55     private boolean polymorph = true;
56         
57     private String JavaDoc where;
58     
59     private Class JavaDoc parentClass;
60     
61     private String JavaDoc parentProperty;
62     
63     private String JavaDoc collectionProperty;
64     
65     public Class JavaDoc getEntityClass() {
66         return entityClass;
67     }
68      
69     public void setEntityClass(Class JavaDoc itemClass) {
70         this.entityClass = itemClass;
71     }
72     
73     public void setPolymorph(boolean polymorph) {
74         this.polymorph = polymorph;
75     }
76         
77     public void setWhere(String JavaDoc string) {
78         where = string;
79     }
80     
81     public void setParentClass(Class JavaDoc parentClass) {
82         this.parentClass = parentClass;
83     }
84     
85     public void setCollectionProperty(String JavaDoc property) {
86         this.collectionProperty = property;
87     }
88     
89     public void setParentProperty(String JavaDoc parentProperty) {
90         this.parentProperty = parentProperty;
91     }
92     
93     public String JavaDoc getObjectId(Object JavaDoc entity) {
94         return HibernateUtils.getIdAsString(getSessionFactory(), entity);
95     }
96
97     public Object JavaDoc getParent(Object JavaDoc entity) {
98         if (parentProperty != null) {
99             return PropertyUtils.getProperty(entity, parentProperty);
100         }
101         StringBuffer JavaDoc hql = new StringBuffer JavaDoc();
102         hql.append("select parent from ").append(parentClass.getName());
103         hql.append(" parent join parent.").append(collectionProperty);
104         hql.append(" child where child = :child");
105         
106         Query query = createQuery(hql.toString());
107         query.setMaxResults(1);
108         query.setParameter("child", entity);
109         return query.uniqueResult();
110     }
111     
112     public void save(Object JavaDoc entity, Object JavaDoc parent) {
113         if (parentProperty != null) {
114             PropertyUtils.setProperty(entity, parentProperty, parent);
115         }
116         getCollection(parent).add(entity);
117         getSession().save(entity);
118         getSession().update(parent);
119     }
120     
121     public void delete(Object JavaDoc entity, Object JavaDoc parent) {
122         getCollection(parent).remove(entity);
123         getSession().delete(entity);
124         getSession().update(parent);
125     }
126        
127     protected Collection JavaDoc getCollection(Object JavaDoc parent) {
128         return (Collection JavaDoc) PropertyUtils.getProperty(parent, collectionProperty);
129     }
130     
131     protected void buildQueryString(StringBuffer JavaDoc hql, ListParams params) {
132         boolean hasWhere = false;
133         if (!polymorph) {
134             hql.append(" where this.class = ");
135             hql.append(entityClass.getName());
136             hasWhere = true;
137         }
138         if (where != null) {
139             hql.append(hasWhere ? " and " : " where ");
140             hasWhere = true;
141             hql.append(where);
142         }
143         hql.append(getOrderBy(params));
144     }
145
146     public final Collection JavaDoc list(Object JavaDoc parent, ListParams params) {
147         return listInternal(parent, params);
148     }
149     
150     protected List JavaDoc listInternal(Object JavaDoc parent, ListParams params) {
151         StringBuffer JavaDoc hql = new StringBuffer JavaDoc("select this ");
152         buildQueryString(hql, params);
153         
154         Collection JavaDoc c = getCollection(parent);
155         Query query = getSession().createFilter(c, hql.toString());
156         
157         if (params.getPageSize() > 0) {
158             query.setFirstResult(params.getOffset());
159             query.setMaxResults(params.getPageSize());
160         }
161         if (params.getFilter() != null) {
162             query.setProperties(params.getFilter());
163         }
164         if (log.isDebugEnabled()) {
165             log.debug("HQL query: " + query.getQueryString());
166         }
167         return query.list();
168     }
169
170     public int getListSize(Object JavaDoc parent, ListParams params) {
171         StringBuffer JavaDoc hql = new StringBuffer JavaDoc("select count(*) ");
172         if (!polymorph) {
173             hql.append(" where this.class = ");
174             hql.append(entityClass.getName());
175         }
176         if (where != null) {
177             hql.append(polymorph ? " where " : " and ");
178             hql.append(where);
179         }
180
181         Collection JavaDoc c = getCollection(parent);
182         Query query = getSession().createFilter(c, hql.toString());
183
184         if (params.getFilter() != null) {
185             query.setProperties(params.getFilter());
186         }
187         Number JavaDoc size = (Number JavaDoc) query.uniqueResult();
188         return size.intValue();
189     }
190     
191     protected String JavaDoc getOrderBy(ListParams params) {
192         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
193         if (params.hasOrder()) {
194             sb.append(" order by");
195             Iterator JavaDoc it = params.getOrder().iterator();
196             while (it.hasNext()) {
197                 Order order = (Order) it.next();
198                 sb.append(" this.");
199                 sb.append(order.getProperty());
200                 sb.append(' ');
201                 sb.append(order.isAscending() ? "asc" : "desc");
202                 if (it.hasNext()) {
203                     sb.append(',');
204                 }
205             }
206         }
207         return sb.toString();
208     }
209         
210     public Object JavaDoc load(String JavaDoc objectId) {
211         Assert.notNull(objectId, "A non-null id must be passed to load()");
212         return HibernateUtils.get(getSession(), entityClass, objectId);
213     }
214     
215     public void update(Object JavaDoc entity) {
216         getSession().update(entity);
217     }
218     
219     public void addChild(Object JavaDoc entity, Object JavaDoc parent) {
220         getCollection(parent).add(entity);
221         if (parentProperty != null) {
222             PropertyUtils.setProperty(entity, parentProperty, parent);
223         }
224         getSession().update(parent);
225         if (parentProperty != null) {
226             getSession().update(entity);
227         }
228     }
229     
230     public void removeChild(Object JavaDoc entity, Object JavaDoc parent) {
231         getCollection(parent).remove(entity);
232         if (parentProperty != null) {
233             PropertyUtils.setProperty(entity, parentProperty, null);
234         }
235         getSession().update(parent);
236         if (parentProperty != null) {
237             getSession().update(entity);
238         }
239     }
240
241 }
242
Popular Tags