1 24 package org.riotfamily.riot.hibernate.dao; 25 26 import org.hibernate.Query; 27 import org.riotfamily.common.beans.PropertyUtils; 28 import org.riotfamily.riot.dao.CutAndPasteEnabledDao; 29 import org.riotfamily.riot.dao.ListParams; 30 import org.riotfamily.riot.dao.ParentChildDao; 31 import org.riotfamily.riot.hibernate.support.HibernateUtils; 32 33 34 35 38 public class HqlParentChildDao extends HqlDao implements ParentChildDao, 39 CutAndPasteEnabledDao { 40 41 private String parentProperty; 42 43 public String getParentProperty() { 44 return parentProperty; 45 } 46 47 public void setParentProperty(String parentProperty) { 48 this.parentProperty = parentProperty; 49 } 50 51 public Object getParent(Object entity) { 52 return PropertyUtils.getProperty(entity, parentProperty); 53 } 54 55 public void save(Object entity, Object parent) { 56 PropertyUtils.setProperty(entity, parentProperty, parent); 57 getSession().save(entity); 58 } 59 60 public void delete(Object entity, Object parent) { 61 PropertyUtils.setProperty(entity, parentProperty, null); 62 getSession().delete(entity); 63 } 64 65 protected void setQueryParameters(Query query, Object parent, 66 ListParams params) { 67 68 super.setQueryParameters(query, parent, params); 69 if (parent != null) { 70 query.setParameter("parent", parent); 71 } 72 } 73 74 protected String getWhereClause(Object parent, ListParams params) { 75 StringBuffer sb = new StringBuffer (); 76 if (parentProperty != null) { 77 sb.append("this."); 78 sb.append(parentProperty); 79 if (parent == null) { 80 sb.append(" is null"); 81 } 82 else { 83 sb.append(" = :parent "); 84 } 85 } 86 HibernateUtils.appendHql(sb, "and", super.getWhereClause(parent, params)); 87 return sb.toString(); 88 } 89 90 public void addChild(Object entity, Object parent) { 91 PropertyUtils.setProperty(entity, parentProperty, parent); 92 update(entity); 93 } 94 95 public void removeChild(Object entity, Object parent) { 96 PropertyUtils.setProperty(entity, parentProperty, null); 97 update(entity); 98 } 99 100 } | Popular Tags |