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 import java.util.Map ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.hibernate.Query; 34 import org.riotfamily.common.beans.PropertyUtils; 35 import org.riotfamily.riot.dao.ListParams; 36 import org.riotfamily.riot.dao.Order; 37 import org.riotfamily.riot.dao.RiotDao; 38 import org.riotfamily.riot.dao.SortableDao; 39 import org.riotfamily.riot.dao.SwappableItemDao; 40 import org.riotfamily.riot.hibernate.support.HibernateSupport; 41 import org.riotfamily.riot.hibernate.support.HibernateUtils; 42 import org.springframework.util.Assert; 43 44 47 public class HqlDao extends HibernateSupport implements RiotDao, 48 SortableDao, SwappableItemDao { 49 50 private static final Log log = LogFactory.getLog(HqlDao.class); 51 52 private Class entityClass; 53 54 private boolean polymorph = true; 55 56 private String where; 57 58 private String positionProperty; 59 60 63 public Class getEntityClass() { 64 return entityClass; 65 } 66 67 70 public void setEntityClass(Class itemClass) { 71 this.entityClass = itemClass; 72 } 73 74 77 public boolean isPolymorph() { 78 return polymorph; 79 } 80 81 84 public void setPolymorph(boolean polymorph) { 85 this.polymorph = polymorph; 86 } 87 88 91 public String getWhere() { 92 return where; 93 } 94 95 98 public void setWhere(String where) { 99 this.where = where; 100 } 101 102 105 public void setIdProperty(String idProperty) { 106 } 107 108 public void setPositionProperty(String positionProperty) { 109 this.positionProperty = positionProperty; 110 } 111 112 public String getObjectId(Object item) { 113 return HibernateUtils.getIdAsString(getSessionFactory(), item); 114 } 115 116 119 public Collection list(Object parent, ListParams params) { 120 return listInternal(parent, params); 121 } 122 123 protected List listInternal(Object parent, ListParams params) { 124 Query query = createQuery(buildHql(parent, params)); 125 setQueryParameters(query, parent, params); 126 if (params.getPageSize() > 0) { 127 query.setFirstResult(params.getOffset()); 128 query.setMaxResults(params.getPageSize()); 129 } 130 return query.list(); 131 } 132 133 136 public int getListSize(Object parent, ListParams params) { 137 Query query = createQuery(buildCountHql(parent, params)); 138 setQueryParameters(query, parent, params); 139 Number size = (Number ) query.uniqueResult(); 140 if (size == null) { 141 return 0; 142 } 143 return size.intValue(); 144 } 145 146 protected void setQueryParameters(Query query, Object parent, 147 ListParams params) { 148 149 if (params.getFilter() != null) { 150 if (params.getFilter() instanceof Map ) { 151 Map filterMap = (Map ) params.getFilter(); 152 query.setProperties(filterMap); 153 } 154 else { 155 query.setProperties(params.getFilter()); 156 } 157 158 HibernateUtils.setCollectionValueParams(query, 159 params.getFilteredProperties(), params.getFilter()); 160 } 161 if (params.getSearch() != null) { 162 query.setParameter("search", params.getSearch() 163 .toLowerCase().replace('*', '%') + "%"); 164 } 165 } 166 167 170 protected String buildCountHql(Object parent, ListParams params) { 171 StringBuffer hql = new StringBuffer (); 172 hql.append("select count(this) from "); 173 hql.append(entityClass.getName()); 174 hql.append(" as this"); 175 HibernateUtils.appendHql(hql, "where", getWhereClause(parent, params)); 176 log.info(hql); 177 return hql.toString(); 178 } 179 180 183 protected String buildHql(Object parent, ListParams params) { 184 StringBuffer hql = new StringBuffer (); 185 hql.append("select this from "); 186 hql.append(entityClass.getName()); 187 hql.append(" as this"); 188 HibernateUtils.appendHql(hql, "where", getWhereClause(parent, params)); 189 HibernateUtils.appendHql(hql, "order by", getOrderBy(params)); 190 log.info(hql); 191 return hql.toString(); 192 } 193 194 protected String getWhereClause(Object parent, ListParams params) { 195 StringBuffer sb = new StringBuffer (); 196 HibernateUtils.appendHql(sb, null, where); 197 198 if (params.getFilter() != null) { 199 String filter = HibernateUtils.getExampleWhereClause( 200 params.getFilter(), "this", 201 params.getFilteredProperties()); 202 203 HibernateUtils.appendHql(sb, "and", filter); 204 } 205 206 if (params.getSearch() != null) { 207 String search = HibernateUtils.getSearchWhereClause("this", 208 params.getSearchProperties(), "search"); 209 210 HibernateUtils.appendHql(sb, "and", search); 211 } 212 213 if (!polymorph) { 214 HibernateUtils.appendHql(sb, "and", "(this.class = ") 215 .append(entityClass.getName()).append(')'); 216 } 217 218 return sb.toString(); 219 } 220 221 protected String getOrderBy(ListParams params) { 222 StringBuffer sb = new StringBuffer (); 223 if (params.hasOrder()) { 224 Iterator it = params.getOrder().iterator(); 225 while (it.hasNext()) { 226 Order order = (Order) it.next(); 227 if (!order.isCaseSensitive()) { 228 sb.append(" lower("); 229 } 230 sb.append(" this."); 231 sb.append(order.getProperty()); 232 if (!order.isCaseSensitive()) { 233 sb.append(" ) "); 234 } 235 sb.append(' '); 236 sb.append(order.isAscending() ? "asc" : "desc"); 237 if (it.hasNext()) { 238 sb.append(','); 239 } 240 } 241 } 242 return sb.toString(); 243 } 244 245 public void save(Object entity, Object parent) { 246 getSession().save(entity); 247 } 248 249 public void delete(Object entity, Object parent) { 250 getSession().delete(entity); 251 } 252 253 public Object load(String objectId) { 254 Assert.notNull(objectId, "A non-null id must be passed to load()"); 255 return HibernateUtils.get(getSession(), entityClass, objectId); 256 } 257 258 public void update(Object entity) { 259 getSession().update(entity); 260 } 261 262 public void swapEntity(Object item, Object parent, ListParams params, 263 int swapWith) { 264 265 Assert.notNull(positionProperty, "A positionProperty must be specified."); 266 267 List items = listInternal(parent, params); 268 Object nextItem = items.get(swapWith); 269 270 Object pos1 = PropertyUtils.getProperty(item, positionProperty); 271 Object pos2 = PropertyUtils.getProperty(nextItem, positionProperty); 272 273 PropertyUtils.setProperty(item, positionProperty, pos2); 274 PropertyUtils.setProperty(nextItem, positionProperty, pos1); 275 276 getSession().update(item); 277 getSession().update(nextItem); 278 } 279 280 } | Popular Tags |