KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
36  * ParentChildDao implementation based on Hibernate.
37  */

38 public class HqlParentChildDao extends HqlDao implements ParentChildDao,
39         CutAndPasteEnabledDao {
40
41     private String JavaDoc parentProperty;
42     
43     public String JavaDoc getParentProperty() {
44         return parentProperty;
45     }
46     
47     public void setParentProperty(String JavaDoc parentProperty) {
48         this.parentProperty = parentProperty;
49     }
50     
51     public Object JavaDoc getParent(Object JavaDoc entity) {
52         return PropertyUtils.getProperty(entity, parentProperty);
53     }
54     
55     public void save(Object JavaDoc entity, Object JavaDoc parent) {
56         PropertyUtils.setProperty(entity, parentProperty, parent);
57         getSession().save(entity);
58     }
59     
60     public void delete(Object JavaDoc entity, Object JavaDoc parent) {
61         PropertyUtils.setProperty(entity, parentProperty, null);
62         getSession().delete(entity);
63     }
64     
65     protected void setQueryParameters(Query query, Object JavaDoc 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 JavaDoc getWhereClause(Object JavaDoc parent, ListParams params) {
75         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc entity, Object JavaDoc parent) {
91         PropertyUtils.setProperty(entity, parentProperty, parent);
92         update(entity);
93     }
94     
95     public void removeChild(Object JavaDoc entity, Object JavaDoc parent) {
96         PropertyUtils.setProperty(entity, parentProperty, null);
97         update(entity);
98     }
99
100 }
Popular Tags