001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.hibernate.dao;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.hibernate.Query;
033: import org.riotfamily.common.beans.PropertyUtils;
034: import org.riotfamily.riot.dao.CutAndPasteEnabledDao;
035: import org.riotfamily.riot.dao.ListParams;
036: import org.riotfamily.riot.dao.Order;
037: import org.riotfamily.riot.dao.ParentChildDao;
038: import org.riotfamily.riot.dao.SortableDao;
039: import org.riotfamily.riot.hibernate.support.HibernateSupport;
040: import org.riotfamily.riot.hibernate.support.HibernateUtils;
041: import org.springframework.util.Assert;
042:
043: /**
044: * RiotDao implementation that loads a bean and returns one of the
045: * bean's properties as (filtered) collection.
046: */
047: public class HqlCollectionDao extends HibernateSupport implements
048: SortableDao, ParentChildDao, CutAndPasteEnabledDao {
049:
050: private Log log = LogFactory.getLog(HqlCollectionDao.class);
051:
052: private Class entityClass;
053:
054: private boolean polymorph = true;
055:
056: private String where;
057:
058: private Class parentClass;
059:
060: private String parentProperty;
061:
062: private String collectionProperty;
063:
064: public Class getEntityClass() {
065: return entityClass;
066: }
067:
068: public void setEntityClass(Class itemClass) {
069: this .entityClass = itemClass;
070: }
071:
072: public void setPolymorph(boolean polymorph) {
073: this .polymorph = polymorph;
074: }
075:
076: public void setWhere(String string) {
077: where = string;
078: }
079:
080: public void setParentClass(Class parentClass) {
081: this .parentClass = parentClass;
082: }
083:
084: public void setCollectionProperty(String property) {
085: this .collectionProperty = property;
086: }
087:
088: public void setParentProperty(String parentProperty) {
089: this .parentProperty = parentProperty;
090: }
091:
092: public String getObjectId(Object entity) {
093: return HibernateUtils
094: .getIdAsString(getSessionFactory(), entity);
095: }
096:
097: public Object getParent(Object entity) {
098: if (parentProperty != null) {
099: return PropertyUtils.getProperty(entity, parentProperty);
100: }
101: StringBuffer hql = new StringBuffer();
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 entity, Object 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 entity, Object parent) {
122: getCollection(parent).remove(entity);
123: getSession().delete(entity);
124: getSession().update(parent);
125: }
126:
127: protected Collection getCollection(Object parent) {
128: return (Collection) PropertyUtils.getProperty(parent,
129: collectionProperty);
130: }
131:
132: protected void buildQueryString(StringBuffer hql, ListParams params) {
133: boolean hasWhere = false;
134: if (!polymorph) {
135: hql.append(" where this.class = ");
136: hql.append(entityClass.getName());
137: hasWhere = true;
138: }
139: if (where != null) {
140: hql.append(hasWhere ? " and " : " where ");
141: hasWhere = true;
142: hql.append(where);
143: }
144: hql.append(getOrderBy(params));
145: }
146:
147: public Collection list(Object parent, ListParams params) {
148: return listInternal(parent, params);
149: }
150:
151: protected List listInternal(Object parent, ListParams params) {
152: StringBuffer hql = new StringBuffer("select this ");
153: buildQueryString(hql, params);
154:
155: Collection c = getCollection(parent);
156: Query query = getSession().createFilter(c, hql.toString());
157:
158: if (params.getPageSize() > 0) {
159: query.setFirstResult(params.getOffset());
160: query.setMaxResults(params.getPageSize());
161: }
162: if (params.getFilter() != null) {
163: query.setProperties(params.getFilter());
164: }
165: if (log.isDebugEnabled()) {
166: log.debug("HQL query: " + query.getQueryString());
167: }
168: return query.list();
169: }
170:
171: public int getListSize(Object parent, ListParams params) {
172: StringBuffer hql = new StringBuffer("select count(*) ");
173: if (!polymorph) {
174: hql.append(" where this.class = ");
175: hql.append(entityClass.getName());
176: }
177: if (where != null) {
178: hql.append(polymorph ? " where " : " and ");
179: hql.append(where);
180: }
181:
182: Collection c = getCollection(parent);
183: Query query = getSession().createFilter(c, hql.toString());
184:
185: if (params.getFilter() != null) {
186: query.setProperties(params.getFilter());
187: }
188: Number size = (Number) query.uniqueResult();
189: return size.intValue();
190: }
191:
192: protected String getOrderBy(ListParams params) {
193: StringBuffer sb = new StringBuffer();
194: if (params.hasOrder()) {
195: sb.append(" order by");
196: Iterator it = params.getOrder().iterator();
197: while (it.hasNext()) {
198: Order order = (Order) it.next();
199: sb.append(" this.");
200: sb.append(order.getProperty());
201: sb.append(' ');
202: sb.append(order.isAscending() ? "asc" : "desc");
203: if (it.hasNext()) {
204: sb.append(',');
205: }
206: }
207: }
208: return sb.toString();
209: }
210:
211: public Object load(String objectId) {
212: Assert.notNull(objectId,
213: "A non-null id must be passed to load()");
214: return HibernateUtils.get(getSession(), entityClass, objectId);
215: }
216:
217: public void update(Object entity) {
218: getSession().update(entity);
219: }
220:
221: public void addChild(Object entity, Object parent) {
222: getCollection(parent).add(entity);
223: if (parentProperty != null) {
224: PropertyUtils.setProperty(entity, parentProperty, parent);
225: }
226: getSession().update(parent);
227: if (parentProperty != null) {
228: getSession().update(entity);
229: }
230: }
231:
232: public void removeChild(Object entity, Object parent) {
233: getCollection(parent).remove(entity);
234: if (parentProperty != null) {
235: PropertyUtils.setProperty(entity, parentProperty, null);
236: }
237: getSession().update(parent);
238: if (parentProperty != null) {
239: getSession().update(entity);
240: }
241: }
242:
243: }
|