KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > persistence > hibernate > core > GroupDAOHibernate


1 /*
2  * Copyright 2005 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.persistence.hibernate.core;
17
18 import com.blandware.atleap.persistence.core.GroupDAO;
19 import com.blandware.atleap.model.core.*;
20 import com.blandware.atleap.common.util.PartialCollection;
21 import com.blandware.atleap.common.util.QueryInfo;
22
23 import java.util.List JavaDoc;
24 import java.util.ArrayList JavaDoc;
25
26 /**
27  * <p>This class interacts with Spring's HibernateTemplate to save/delete and
28  * retrieve Group objects.
29  * </p>
30  * <p><a HREF="GroupDAOHibernate.java.htm"><i>View source</i></a></p>
31  *
32  * @author Roman Puchkovskiy <a HREF="mailto:roman.puchkovskiy@blandware.com">
33  * &lt;roman.puchkovskiy@blandware.com&gt;</a>
34  * @version $Revision: 1.3 $ $Date: 2006/03/12 08:46:04 $
35  */

36 public class GroupDAOHibernate extends BaseDAOHibernate implements GroupDAO {
37
38     /**
39      * @see com.blandware.atleap.persistence.core.GroupDAO#createGroup(com.blandware.atleap.model.core.Group)
40      */

41     public void createGroup(Group group) {
42         getHibernateTemplate().save(group);
43     }
44
45     /**
46      * @see com.blandware.atleap.persistence.core.GroupDAO#retrieveGroup(String)
47      */

48     public Group retrieveGroup(String JavaDoc groupname) {
49         return (Group) getHibernateTemplate().get(Group.class, groupname);
50     }
51
52     /**
53      * @see com.blandware.atleap.persistence.core.GroupDAO#updateGroup(com.blandware.atleap.model.core.Group)
54      */

55     public void updateGroup(Group group) {
56         getHibernateTemplate().update(group);
57     }
58
59     /**
60      * @see com.blandware.atleap.persistence.core.GroupDAO#deleteGroup(com.blandware.atleap.model.core.Group)
61      */

62     public void deleteGroup(Group group) {
63         List JavaDoc roles = new ArrayList JavaDoc(group.getRoles());
64         List JavaDoc users = new ArrayList JavaDoc(group.getUsers());
65
66         for ( int i = 0; i < users.size(); i++ ) {
67             User user = (User) users.get(i);
68             for ( int j = 0; j < roles.size(); j++ ) {
69                 Role role = (Role) roles.get(j);
70                 user.removeRole(role, group);
71             }
72         }
73
74         getHibernateTemplate().delete(group);
75
76         // break relations between roles and deleted group
77
for ( int i = 0; i < roles.size(); i++ ) {
78             Role role = (Role) roles.get(i);
79             group.removeRole(role);
80         }
81
82         // break relations between users and deleted group
83
for ( int i = 0; i < users.size(); i++ ) {
84             User user = (User) users.get(i);
85             group.removeUser(user);
86         }
87
88     }
89
90     /**
91      * @see com.blandware.atleap.persistence.core.GroupDAO#listGroups(com.blandware.atleap.common.util.QueryInfo)
92      */

93     public PartialCollection listGroups(QueryInfo queryInfo) {
94         String JavaDoc whereClause = new String JavaDoc();
95         String JavaDoc orderByClause = new String JavaDoc();
96         Boolean JavaDoc fixed = null;
97         if ( queryInfo != null ) {
98             if ( queryInfo.getQueryParameters() != null ) {
99                 fixed = (Boolean JavaDoc) queryInfo.getQueryParameters().get("fixed");
100             }
101             whereClause = queryInfo.getWhereClause();
102             orderByClause = queryInfo.getOrderByClause();
103             if ( whereClause != null && whereClause.length() != 0 ) {
104                 whereClause = " where " + whereClause;
105             } else {
106                 whereClause = new String JavaDoc();
107             }
108             if ( orderByClause != null && orderByClause.length() != 0 ) {
109                 orderByClause = " order by " + orderByClause;
110             } else {
111                 orderByClause = new String JavaDoc();
112             }
113         }
114
115         List JavaDoc list = null;
116         Integer JavaDoc total = null;
117
118         if ( fixed != null ) {
119             if ( whereClause.length() != 0 ) {
120                 whereClause += " and ";
121             } else {
122                 whereClause += " where ";
123             }
124             whereClause += "fixed = " + (fixed.booleanValue() ? "'T'" : "'F'");
125         }
126         String JavaDoc hqlPart = "from Group g left outer join g.roles as r" + whereClause;
127         if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
128             String JavaDoc hqlForTotal = "select count(distinct g.name) " + hqlPart;
129             total = (Integer JavaDoc) findUniqueResult(hqlForTotal);
130             if ( total == null ) {
131                 total = new Integer JavaDoc(0);
132             }
133         }
134         // If we don't have any info about the total number of results yet or
135
// we know that there's something that will be found, then fetch data
136
if ( total == null || total.intValue() > 0 ) {
137             String JavaDoc hql = "select distinct g " + hqlPart + orderByClause;
138             list = executeFind(hql, queryInfo);
139             if ( total == null ) {
140                 total = new Integer JavaDoc(list.size());
141             }
142         } else {
143             list = new ArrayList JavaDoc();
144         }
145         return new PartialCollection(list, total);
146     }
147
148     /**
149      * @see com.blandware.atleap.persistence.core.GroupDAO#hasDuplicates(com.blandware.atleap.model.core.Group)
150      */

151     public boolean hasDuplicates(Group group) {
152
153         ArrayList JavaDoc args = new ArrayList JavaDoc();
154         args.add(group.getTitle());
155         args.add(group.getName());
156
157         String JavaDoc hql = "select count(g.name) from Group g where g.title = ? and g.name != ?";
158
159         int count = ((Integer JavaDoc) findUniqueResult(hql, args.toArray())).intValue();
160
161         return count > 0;
162     }
163
164     // ~ Finders ================================================================
165

166     /**
167      * @see com.blandware.atleap.persistence.core.GroupDAO#findGroupByTitle(java.lang.String)
168      */

169     public Group findGroupByTitle(String JavaDoc title) {
170         return (Group) findUniqueResult("from Group g where g.title = ?", new Object JavaDoc[]{title});
171     }
172
173 }
174
Popular Tags