1 5 package org.exoplatform.services.organization.hibernate; 6 7 import java.util.Collection ; 8 import java.util.List ; 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 29 public class UserProfileQueryHandler { 30 static private UserProfile NOT_FOUND = new UserProfileImpl() ; 31 private static final String 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 listeners_ ; 38 39 public UserProfileQueryHandler(HibernateService service, 40 CacheService cservice) throws Exception { 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 { 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 { 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 userName) throws Exception { 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 userName) throws Exception { 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 userName, Session session) throws Exception { 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 userName, Session session) throws Exception { 101 session.delete(queryFindUserProfileByName, userName, Hibernate.STRING ); 102 } 103 104 public Collection findUserProfiles() throws Exception { 105 return null ; 106 } 107 } | Popular Tags |