KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date 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.commons.utils.PageList;
18 import org.exoplatform.services.cache.CacheService;
19 import org.exoplatform.services.cache.ExoCache;
20 import org.exoplatform.services.database.DBObjectPageList;
21 import org.exoplatform.services.database.HibernateService;
22 import org.exoplatform.services.database.ObjectQuery;
23 import org.exoplatform.services.database.XResources;
24 import org.exoplatform.services.organization.Query;
25 import org.exoplatform.services.organization.User;
26 import org.exoplatform.services.organization.UserEventListener;
27 import org.exoplatform.services.organization.impl.UserImpl;
28 /**
29  * Created by The eXo Platform SARL
30  * Author : Mestrallet Benjamin
31  * benjmestrallet@users.sourceforge.net
32  * Author : Tuan Nguyen
33  * tuan08@users.sourceforge.net
34  * Date: Aug 22, 2003
35  * Time: 4:51:21 PM
36  */

37 public class UserQueryHandler {
38   public static final String JavaDoc queryFindUserByName =
39     "from u in class org.exoplatform.services.organization.impl.UserImpl " +
40     "where u.userName = ?";
41   
42   private static final String JavaDoc queryFindUsersByGroupAndRole =
43     "select u " +
44     "from u in class org.exoplatform.services.organization.impl.UserImpl, " +
45     " m in class org.exoplatform.services.organization.impl.MembershipImpl, " +
46     " g in class org.exoplatform.services.organization.impl.GroupImpl " +
47     "where m.user = u " +
48     " and m.group = g " +
49     " and g.groupName = ? " +
50     " and m.role = ? ";
51
52   private HibernateService service_ ;
53   private ExoCache cache_ ;
54   private List JavaDoc listeners_ ;
55
56   public UserQueryHandler(HibernateService service,
57                           CacheService cservice) throws Exception JavaDoc {
58     service_ = service ;
59     cache_ = cservice.getCacheInstance(UserImpl.class.getName()) ;
60     cache_.setMaxSize(1000);
61     listeners_ = new ListenerStack(5) ;
62   }
63
64   final public List JavaDoc getUserEventListeners() {
65     return listeners_ ;
66   }
67
68   public void addUserEventListener(UserEventListener listener) {
69     listeners_.add(listener) ;
70   }
71   
72   public void createUser(User user) throws Exception JavaDoc {
73     Session session = service_.openSession();
74     Date JavaDoc date = new Date JavaDoc() ;
75     user.setLastLoginTime(date) ;
76     user.setCreatedDate(date) ;
77     preSave(user, true, session) ;
78     session.save(user, IdentifierUtil.generateUUID(user));
79     postSave(user, true, session) ;
80     UserProfileData upd = new UserProfileData(user.getUserName()) ;
81     session.save(upd);
82     session.flush() ;
83   }
84
85   public void saveUser(User user) throws Exception JavaDoc {
86     Session session = service_.openSession();
87     preSave(user, false, session) ;
88     session.update(user);
89     postSave(user, false, session) ;
90     session.flush() ;
91     cache_.put(user.getUserName(), user) ;
92   }
93   
94   void createUserEntry(User user, Session session) throws Exception JavaDoc {
95      session.save(user);
96   }
97
98   public User removeUser(String JavaDoc userName) throws Exception JavaDoc {
99     Session session = service_.openSession();
100     User foundUser = findUserByName(userName, session);
101     preDelete(foundUser , session) ;
102     session.delete(foundUser);
103     MembershipQueryHandler.removeMembershipEntriesOfUser(userName, session) ;
104     UserProfileQueryHandler.removeUserProfileEntry(userName, session) ;
105     postDelete(foundUser, session) ;
106     session.flush();
107     cache_.remove(userName) ;
108     return foundUser ;
109   }
110   
111   /*This method use with importer/exporter */
112   void removeUserEntry(User user, Session session) throws Exception JavaDoc {
113     session.delete(user);
114     cache_.remove(user.getUserName()) ;
115   }
116
117   public User findUserByName(String JavaDoc userName) throws Exception JavaDoc {
118     User user = (User)cache_.get(userName) ;
119     if(user != null) return user ;
120     Session session = service_.openSession();
121     user = findUserByName(userName, session) ;
122     if(user != null)cache_.put(userName, user) ;
123     return user;
124   }
125
126   public User findUserByName(String JavaDoc userName, Session session) throws Exception JavaDoc {
127     User user = (User) service_.findOne(session, queryFindUserByName, userName);
128     return user;
129   }
130
131   public PageList getUserPageList(int pageSize) throws Exception JavaDoc {
132     return new DBObjectPageList(service_ ,UserImpl.class) ;
133   }
134   
135   public PageList findUsers(Query q) throws Exception JavaDoc {
136     ObjectQuery oq = new ObjectQuery(UserImpl.class);
137     oq.addLIKE("userName", q.getUserName()) ;
138     oq.addLIKE("firstName", q.getFirstName() ) ;
139     oq.addLIKE("lastName", q.getLastName()) ;
140     oq.addLIKE("email", q.getEmail()) ;
141     oq.addGT("lastLoginTime", q.getFromLoginDate()) ;
142     oq.addLT("lastLoginTime", q.getToLoginDate()) ;
143     return new DBObjectPageList(service_, oq);
144   }
145
146   public PageList findUsersByGroup(String JavaDoc groupId) throws Exception JavaDoc {
147     String JavaDoc queryFindUsersInGroup =
148         "select u " +
149         "from u in class org.exoplatform.services.organization.impl.UserImpl, " +
150         " m in class org.exoplatform.services.organization.impl.MembershipImpl, " +
151         "where m.userName = u.userName " +
152         " and m.groupId = '" + groupId + "'" ;
153     String JavaDoc countUsersInGroup =
154       "select count(u) " +
155       "from u in class org.exoplatform.services.organization.impl.UserImpl, " +
156       " m in class org.exoplatform.services.organization.impl.MembershipImpl, " +
157       "where m.userName = u.userName " +
158       " and m.groupId = '" + groupId + "'" ;
159     return new DBObjectPageList(service_, 20,queryFindUsersInGroup, countUsersInGroup ) ;
160   }
161
162   public Collection JavaDoc findUsersByGroupAndRole(String JavaDoc groupName, String JavaDoc role) throws Exception JavaDoc {
163     Session session = service_.openSession();
164     Object JavaDoc[] args = new Object JavaDoc[] { groupName, role };
165     Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING };
166     List JavaDoc users = session.find( queryFindUsersByGroupAndRole, args, types );
167     return users;
168   }
169   
170   private void preSave(User user , 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       UserEventListener listener = (UserEventListener) listeners_.get(i) ;
175       listener.preSave(user, isNew, xresources) ;
176     }
177   }
178
179   private void postSave(User user , 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       UserEventListener listener = (UserEventListener) listeners_.get(i) ;
184       listener.postSave(user, isNew, xresources) ;
185     }
186   }
187
188   private void preDelete(User user , 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       UserEventListener listener = (UserEventListener) listeners_.get(i) ;
193       listener.preDelete(user, xresources) ;
194     }
195   }
196
197   private void postDelete(User user , 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       UserEventListener listener = (UserEventListener) listeners_.get(i) ;
202       listener.postDelete(user, xresources) ;
203     }
204   }
205 }
Popular Tags