KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > hibernate > GroupQueryHandler


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.hibernate;
6
7
8 import java.util.Collection JavaDoc;
9 import java.util.List JavaDoc;
10
11 import net.sf.hibernate.Hibernate;
12 import net.sf.hibernate.Session;
13 import net.sf.hibernate.type.Type;
14
15 import org.exoplatform.commons.exception.UniqueObjectException;
16 import org.exoplatform.commons.utils.ListenerStack;
17 import org.exoplatform.services.database.HibernateService;
18 import org.exoplatform.services.database.XResources;
19 import org.exoplatform.services.organization.Group;
20 import org.exoplatform.services.organization.GroupEventListener;
21 import org.exoplatform.services.organization.impl.GroupImpl;
22 /**
23  * Created by The eXo Platform SARL
24  * Author : Mestrallet Benjamin
25  * benjmestrallet@users.sourceforge.net
26  * Author : Tuan Nguyen
27  * tuan08@users.sourceforge.net
28  * Date: Aug 22, 2003
29  * Time: 4:51:21 PM
30  */

31 public class GroupQueryHandler {
32   public static final String JavaDoc queryFindGroupByName =
33     "from g in class org.exoplatform.services.organization.impl.GroupImpl " +
34     "where g.groupName = ? ";
35   
36   public static final String JavaDoc queryFindGroupById =
37     "from g in class org.exoplatform.services.organization.impl.GroupImpl " +
38     "where g.id = ? ";
39   
40   public static final String JavaDoc queryFindGroupByParent =
41     "from g in class org.exoplatform.services.organization.impl.GroupImpl " +
42     "where g.parentId = ? ";
43
44   private static final String JavaDoc queryFindRootGroups =
45     "from g in class org.exoplatform.services.organization.impl.GroupImpl " +
46     "where g.parentId is null";
47   private static final String JavaDoc queryFindGroupsOfUser =
48     "select g " +
49     "from g in class org.exoplatform.services.organization.impl.GroupImpl, " +
50     " m in class org.exoplatform.services.organization.impl.MembershipImpl, " +
51     "where m.groupId = g.id " +
52     " and m.userName = ?" ;
53   
54   private static final String JavaDoc queryFindGroupByMembership =
55     "select g " +
56     "from m in class org.exoplatform.services.organization.impl.MembershipImpl, " +
57     " g in class org.exoplatform.services.organization.impl.GroupImpl " +
58     "where m.groupId = g.id " +
59     " and m.userName = ? " +
60     " and m.membershipType = ? ";
61   
62   private HibernateService service_ ;
63   private List JavaDoc listeners_ ;
64
65   public GroupQueryHandler(HibernateService service) {
66     service_ = service ;
67     listeners_ = new ListenerStack(5) ;
68   }
69
70   public void addGroupEventListener(GroupEventListener listener) {
71     listeners_.add(listener) ;
72   }
73
74   public void createGroup(Group group) throws Exception JavaDoc {
75     Session session = service_.openSession();
76     String JavaDoc groupId = "/" + group.getGroupName() ;
77     Object JavaDoc o = session.get(GroupImpl.class, groupId) ;
78     if(o != null) {
79       Object JavaDoc[] args = {group.getGroupName()} ;
80       throw new UniqueObjectException("OrganizationService.unique-group-exception", args) ;
81     }
82     preSave(group, true, session) ;
83     session.save(group, "/" + group.getGroupName());
84     postSave(group, true, session) ;
85     session.flush() ;
86   }
87
88   public void addChild(Group parent, Group child) throws Exception JavaDoc {
89     Session session = service_.openSession();
90     Group parentGroup = (Group) session.get(GroupImpl.class, parent.getId()) ;
91     String JavaDoc groupId = parentGroup.getId() + "/" + child.getGroupName() ;
92     Object JavaDoc o = session.get(GroupImpl.class, groupId) ;
93     if(o != null) {
94       Object JavaDoc[] args = {child.getGroupName()} ;
95       throw new UniqueObjectException("OrganizationService.unique-group-exception", args) ;
96     }
97     GroupImpl childImpl = (GroupImpl) child ;
98     preSave(child, true, session) ;
99     childImpl.setParentId(parentGroup.getId()) ;
100     session.save(childImpl, groupId);
101     postSave(child, true, session) ;
102     session.flush() ;
103   }
104
105   public void saveGroup(Group group) throws Exception JavaDoc {
106     Session session = service_.openSession();
107     preSave(group, false, session) ;
108     session.update(group);
109     postSave(group, false, session) ;
110     session.flush();
111   }
112
113   public Group removeGroup(Group group) throws Exception JavaDoc {
114     Session session = service_.openSession();
115     preDelete(group, session) ;
116     session.delete(group);
117     session.delete(queryFindGroupByParent, group.getId(), Hibernate.STRING );
118     MembershipQueryHandler.removeMembershipEntriesOfGroup(group, session) ;
119     postDelete(group, session) ;
120     session.flush();
121     return group ;
122   }
123   
124   static void removeGroupEntry(String JavaDoc groupName, Session session) throws Exception JavaDoc {
125     session.delete(queryFindGroupByName, groupName, Hibernate.STRING);
126   }
127
128   public Collection JavaDoc findGroupByMembership(String JavaDoc userName, String JavaDoc membershipType) throws Exception JavaDoc {
129     Session session = service_.openSession();
130     Object JavaDoc[] args = new Object JavaDoc[] { userName, membershipType };
131     Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING };
132     List JavaDoc groups = session.find(queryFindGroupByMembership, args, types );
133     return groups ;
134   }
135
136   public Group findGroupByName(String JavaDoc groupName) throws Exception JavaDoc {
137     Session session = service_.openSession();
138     Group group =(Group)service_.findOne(session,queryFindGroupByName, groupName);
139     return group;
140   }
141   
142   public Group findGroupById(String JavaDoc groupId) throws Exception JavaDoc {
143     Session session = service_.openSession();
144     Group group =(Group)service_.findOne(session,queryFindGroupById, groupId);
145     return group;
146   }
147
148   public Collection JavaDoc findGroups(Group parent) throws Exception JavaDoc {
149     Session session = service_.openSession();
150     if (parent == null) {
151         return session.find( queryFindRootGroups );
152     }
153     return session.find( queryFindGroupByParent, parent.getId(), Hibernate.STRING );
154   }
155   
156   public Collection JavaDoc findGroupsOfUser(String JavaDoc user) throws Exception JavaDoc {
157     Session session = service_.openSession();
158     return session.find( queryFindGroupsOfUser, user, Hibernate.STRING );
159   }
160
161   private void preSave(Group group , boolean isNew , Session session) throws Exception JavaDoc {
162     XResources xresources = new XResources() ;
163     xresources.addResource(Session.class, session) ;
164     for (int i = 0 ; i < listeners_.size(); i++) {
165       GroupEventListener listener = (GroupEventListener) listeners_.get(i) ;
166       listener.preSave(group, isNew, xresources) ;
167     }
168   }
169
170   private void postSave(Group group , boolean isNew , Session session) throws Exception JavaDoc {
171     XResources xresources = new XResources() ;
172     xresources.addResource(Session.class, session) ;
173     for (int i = 0 ; i < listeners_.size(); i++) {
174       GroupEventListener listener = (GroupEventListener) listeners_.get(i) ;
175       listener.postSave(group, isNew, xresources) ;
176     }
177   }
178
179   private void preDelete(Group group , Session session) throws Exception JavaDoc {
180     XResources xresources = new XResources() ;
181     xresources.addResource(Session.class, session) ;
182     for (int i = 0 ; i < listeners_.size(); i++) {
183       GroupEventListener listener = (GroupEventListener) listeners_.get(i) ;
184       listener.preDelete(group, xresources) ;
185     }
186   }
187
188   private void postDelete(Group group , Session session) throws Exception JavaDoc {
189     XResources xresources = new XResources() ;
190     xresources.addResource(Session.class, session) ;
191     for (int i = 0 ; i < listeners_.size(); i++) {
192       GroupEventListener listener = (GroupEventListener) listeners_.get(i) ;
193       listener.postDelete(group, xresources) ;
194     }
195   }
196 }
Popular Tags