KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
9
10 import net.sf.hibernate.Hibernate;
11 import net.sf.hibernate.Session;
12
13 import org.exoplatform.commons.utils.ListenerStack;
14 import org.exoplatform.services.cache.CacheService;
15 import org.exoplatform.services.cache.ExoCache;
16 import org.exoplatform.services.database.HibernateService;
17 import org.exoplatform.services.organization.UserProfile;
18 import org.exoplatform.services.organization.UserProfileEventListener;
19 import org.exoplatform.services.organization.impl.UserProfileImpl;
20 /**
21  * Created by The eXo Platform SARL
22  * Author : Mestrallet Benjamin
23  * benjmestrallet@users.sourceforge.net
24  * Author : Tuan Nguyen
25  * tuan08@users.sourceforge.net
26  * Date: Aug 22, 2003
27  * Time: 4:51:21 PM
28  */

29 public class UserProfileQueryHandler {
30   static private UserProfile NOT_FOUND = new UserProfileImpl() ;
31   private static final String JavaDoc queryFindUserProfileByName =
32     "from u in class org.exoplatform.services.organization.hibernate.UserProfileData " +
33     "where u.userName = ?";
34   
35   private HibernateService service_ ;
36   private ExoCache cache_ ;
37   private List JavaDoc listeners_ ;
38
39   public UserProfileQueryHandler(HibernateService service,
40                                  CacheService cservice) throws Exception JavaDoc {
41     service_ = service ;
42     cache_ = cservice.getCacheInstance(getClass().getName()) ;
43     cache_.setMaxSize(1000);
44     listeners_ = new ListenerStack(5) ;
45   }
46
47   public void addUserProfileEventListener(UserProfileEventListener listener) {
48     listeners_.add(listener) ;
49   }
50   
51   void createUserProfileEntry(UserProfile up, Session session) throws Exception JavaDoc {
52     UserProfileData upd = new UserProfileData() ;
53     upd.setUserProfile(up) ;
54     session.save(upd);
55     session.flush();
56     cache_.remove(up.getUserName()) ;
57   }
58   
59   public void saveUserProfile(UserProfile profile) throws Exception JavaDoc {
60     Session session = service_.openSession();
61     UserProfileData upd =
62       (UserProfileData)service_.findOne(session, queryFindUserProfileByName, profile.getUserName());
63     upd.setUserProfile(profile) ;
64     session.update(upd);
65     session.flush();
66     cache_.put(profile.getUserName(), profile) ;
67   }
68
69   public UserProfile removeUserProfile(String JavaDoc userName) throws Exception JavaDoc {
70     Session session = service_.openSession();
71     UserProfileData upd =(UserProfileData)service_.findExactOne(session,queryFindUserProfileByName, userName);
72     session.delete(upd);
73     session.flush();
74     cache_.remove(userName) ;
75     return upd.getUserProfile() ;
76   }
77
78   public UserProfile findUserProfileByName(String JavaDoc userName) throws Exception JavaDoc {
79     UserProfile up = (UserProfile)cache_.get(userName) ;
80     if(up != null) {
81       if(NOT_FOUND == up ) return null ;
82       return up ;
83     }
84     Session session = service_.openSession();
85     up = findUserProfileByName(userName, session) ;
86     if(up != null) cache_.put(userName, up) ;
87     else cache_.put(userName, NOT_FOUND) ;
88     return up ;
89   }
90   
91   public UserProfile findUserProfileByName(String JavaDoc userName, Session session) throws Exception JavaDoc {
92     UserProfileData upd =
93         (UserProfileData)service_.findOne(session, queryFindUserProfileByName, userName);
94     if (upd != null) {
95       return upd.getUserProfile() ;
96     }
97     return null ;
98   }
99
100   static void removeUserProfileEntry(String JavaDoc userName, Session session) throws Exception JavaDoc {
101     session.delete(queryFindUserProfileByName, userName, Hibernate.STRING );
102   }
103   
104   public Collection JavaDoc findUserProfiles() throws Exception JavaDoc {
105     return null ;
106   }
107 }
Popular Tags