1 24 package org.riotfamily.riot.hibernate.dao; 25 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 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 48 public class HqlCollectionDao extends HibernateSupport 49 implements SortableDao, ParentChildDao, CutAndPasteEnabledDao { 50 51 private Log log = LogFactory.getLog(HqlCollectionDao.class); 52 53 private Class entityClass; 54 55 private boolean polymorph = true; 56 57 private String where; 58 59 private Class parentClass; 60 61 private String parentProperty; 62 63 private String collectionProperty; 64 65 public Class getEntityClass() { 66 return entityClass; 67 } 68 69 public void setEntityClass(Class itemClass) { 70 this.entityClass = itemClass; 71 } 72 73 public void setPolymorph(boolean polymorph) { 74 this.polymorph = polymorph; 75 } 76 77 public void setWhere(String string) { 78 where = string; 79 } 80 81 public void setParentClass(Class parentClass) { 82 this.parentClass = parentClass; 83 } 84 85 public void setCollectionProperty(String property) { 86 this.collectionProperty = property; 87 } 88 89 public void setParentProperty(String parentProperty) { 90 this.parentProperty = parentProperty; 91 } 92 93 public String getObjectId(Object entity) { 94 return HibernateUtils.getIdAsString(getSessionFactory(), entity); 95 } 96 97 public Object getParent(Object entity) { 98 if (parentProperty != null) { 99 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, collectionProperty); 129 } 130 131 protected void buildQueryString(StringBuffer 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 list(Object parent, ListParams params) { 147 return listInternal(parent, params); 148 } 149 150 protected List listInternal(Object parent, ListParams params) { 151 StringBuffer hql = new StringBuffer ("select this "); 152 buildQueryString(hql, params); 153 154 Collection 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 parent, ListParams params) { 171 StringBuffer hql = new StringBuffer ("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 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 size = (Number ) query.uniqueResult(); 188 return size.intValue(); 189 } 190 191 protected String getOrderBy(ListParams params) { 192 StringBuffer sb = new StringBuffer (); 193 if (params.hasOrder()) { 194 sb.append(" order by"); 195 Iterator 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 load(String 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 entity) { 216 getSession().update(entity); 217 } 218 219 public void addChild(Object entity, Object 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 entity, Object 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 |