KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Collection JavaDoc;
8 import java.util.Iterator 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.utils.IdentifierUtil;
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.Membership;
21 import org.exoplatform.services.organization.MembershipEventListener;
22 import org.exoplatform.services.organization.User;
23 import org.exoplatform.services.organization.impl.MembershipImpl;
24 /**
25  * Created by The eXo Platform SARL
26  * Author : Mestrallet Benjamin
27  * benjmestrallet@users.sourceforge.net
28  * Author : Tuan Nguyen
29  * tuan08@users.sourceforge.net
30  * Date: Aug 22, 2003
31  * Time: 4:51:21 PM
32  */

33 public class MembershipQueryHandler {
34
35   private static final String JavaDoc queryFindMembershipByUserGroupAndType =
36     "from m in class org.exoplatform.services.organization.impl.MembershipImpl " +
37     "where m.userName = ? " +
38     " and m.groupId = ? " +
39     " and m.type = ? " ;
40   
41   private static final String JavaDoc queryFindMembershipsByUserAndGroup =
42     "from m in class org.exoplatform.services.organization.impl.MembershipImpl " +
43     "where m.userName = ? " +
44     " and m.groupId = ? " ;
45  
46   private static final String JavaDoc queryFindMembershipsByGroup =
47     "from m in class org.exoplatform.services.organization.impl.MembershipImpl " +
48     "where m.groupId = ? " ;
49   
50   private static final String JavaDoc queryFindMembership =
51     "from m in class org.exoplatform.services.organization.impl.MembershipImpl " +
52     "where m.id = ? " ;
53   
54   private static final String JavaDoc queryFindMembershipsByUser =
55     "from m in class org.exoplatform.services.organization.impl.MembershipImpl " +
56     "where m.userName = ? " ;
57
58   private HibernateService service_ ;
59   private List JavaDoc listeners_ ;
60
61   public MembershipQueryHandler(HibernateService service) {
62     service_ = service ;
63     listeners_ = new ListenerStack(5) ;
64   }
65
66   public void addMembershipEventListener(MembershipEventListener listener) {
67     listeners_.add(listener) ;
68   }
69
70   public void createMembership(Membership m) throws Exception JavaDoc {
71     Session session = service_.openSession();
72     preSave(m, true, session) ;
73     session.save(m, IdentifierUtil.generateUUID(m));
74     postSave(m, true, session) ;
75     session.flush() ;
76   }
77   
78   public void createMembershipEntries(Collection JavaDoc c, Session session) throws Exception JavaDoc {
79     Iterator JavaDoc i = c.iterator() ;
80     while(i.hasNext()) {
81         Membership impl = (Membership) i.next() ;
82         session.save(impl, impl.getId());
83     }
84   }
85
86   public void linkMembership(String JavaDoc userName, Group g, Membership m) throws Exception JavaDoc {
87     Session session = service_.openSession();
88     MembershipImpl impl = (MembershipImpl) m;
89     User user =(User)service_.findExactOne(session,UserQueryHandler.queryFindUserByName, userName);
90     impl.setUserName(user.getUserName());
91     impl.setGroupId(g.getId());
92     if(impl.getId() == null) {
93         String JavaDoc id = IdentifierUtil.generateUUID(m) ;
94         preSave(impl, true, session) ;
95         session.save(impl, id);
96       postSave(impl, true,session) ;
97     } else {
98         preSave(m, false, session) ;
99         session.update(m);
100         postSave(m, false,session) ;
101     }
102     session.flush();
103   }
104
105   public void saveMembership(Membership m) throws Exception JavaDoc {
106     Session session = service_.openSession();
107     preSave(m, false, session) ;
108     session.update(m);
109     postSave(m, false,session) ;
110     session.flush();
111   }
112
113   public Membership removeMembership(String JavaDoc id) throws Exception JavaDoc {
114     Session session = service_.openSession();
115     Membership m =(Membership) service_.findOne(session,queryFindMembership, id);
116     if(m != null) {
117         preDelete(m, session) ;
118         session.delete(m);
119         postDelete(m, session) ;
120         session.flush();
121     }
122     return m;
123   }
124
125   public Membership findMembershipByUserGroupAndType(String JavaDoc userName,
126                                                      String JavaDoc groupId,
127                                                      String JavaDoc type) throws Exception JavaDoc {
128     Session session = service_.openSession();
129     Object JavaDoc[] args = new Object JavaDoc[] { userName, groupId , type};
130     Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING, Hibernate.STRING };
131     List JavaDoc memberships = session.find( queryFindMembershipByUserGroupAndType, args, types );
132     if(memberships.size() == 0) {
133       return null ;
134     } else if(memberships.size() == 1) {
135       return (Membership) memberships.get(0) ;
136     } else {
137       throw new Exception JavaDoc("Expect 0 or 1 membership but found" + memberships.size()) ;
138     }
139   }
140
141   public Collection JavaDoc findMembershipsByUserAndGroup(String JavaDoc userName, String JavaDoc groupId) throws Exception JavaDoc {
142     Session session = service_.openSession();
143     Object JavaDoc[] args = new Object JavaDoc[] { userName, groupId };
144     Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING };
145     List JavaDoc memberships = session.find(queryFindMembershipsByUserAndGroup, args, types );
146     return memberships;
147   }
148
149   public Collection JavaDoc findMembershipsByUser(String JavaDoc userName) throws Exception JavaDoc {
150     Session session = service_.openSession();
151     List JavaDoc memberships = session.find(queryFindMembershipsByUser, userName, Hibernate.STRING );
152     return memberships;
153   }
154   
155   static void removeMembershipEntriesOfUser(String JavaDoc userName, Session session) throws Exception JavaDoc {
156     session.delete(queryFindMembershipsByUser, userName, Hibernate.STRING );
157   }
158   
159   static void removeMembershipEntriesOfGroup(Group group, Session session) throws Exception JavaDoc {
160     session.delete(queryFindMembershipsByGroup, group.getId(), Hibernate.STRING );
161   }
162   
163   Collection JavaDoc findMembershipsByUser(String JavaDoc userName, Session session) throws Exception JavaDoc {
164     return session.find( queryFindMembershipsByUser, userName, Hibernate.STRING );
165   }
166
167   public Collection JavaDoc findMembershipsByGroup(Group group) throws Exception JavaDoc {
168     Session session = service_.openSession();
169     List JavaDoc memberships = session.find( queryFindMembershipsByGroup, group.getId(), Hibernate.STRING );
170     return memberships;
171   }
172
173   public Membership findMembership(String JavaDoc id) throws Exception JavaDoc {
174     Session session = service_.openSession();
175     Membership membership = (Membership) session.find(queryFindMembership, id, Hibernate.STRING ) ;
176     return membership;
177   }
178
179   private void preSave(Membership membership , boolean isNew , 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       MembershipEventListener listener = (MembershipEventListener) listeners_.get(i) ;
184       listener.preSave(membership, isNew, xresources) ;
185     }
186   }
187
188   private void postSave(Membership membership , boolean isNew , 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       MembershipEventListener listener = (MembershipEventListener) listeners_.get(i) ;
193       listener.postSave(membership, isNew, xresources) ;
194     }
195   }
196
197   private void preDelete(Membership membership , Session session) throws Exception JavaDoc {
198     XResources xresources = new XResources() ;
199     xresources.addResource(Session.class, session) ;
200     for (int i = 0 ; i < listeners_.size(); i++) {
201       MembershipEventListener listener = (MembershipEventListener) listeners_.get(i) ;
202       listener.preDelete(membership, xresources) ;
203     }
204   }
205
206   private void postDelete(Membership membership , Session session) throws Exception JavaDoc {
207     XResources xresources = new XResources() ;
208     xresources.addResource(Session.class, session) ;
209     for (int i = 0 ; i < listeners_.size(); i++) {
210       MembershipEventListener listener = (MembershipEventListener) listeners_.get(i) ;
211       listener.postDelete(membership, xresources) ;
212     }
213   }
214 }
Popular Tags