1 5 package org.exoplatform.services.organization.hibernate; 6 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 import java.util.List ; 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 33 public class MembershipQueryHandler { 34 35 private static final String 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 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 queryFindMembershipsByGroup = 47 "from m in class org.exoplatform.services.organization.impl.MembershipImpl " + 48 "where m.groupId = ? " ; 49 50 private static final String queryFindMembership = 51 "from m in class org.exoplatform.services.organization.impl.MembershipImpl " + 52 "where m.id = ? " ; 53 54 private static final String queryFindMembershipsByUser = 55 "from m in class org.exoplatform.services.organization.impl.MembershipImpl " + 56 "where m.userName = ? " ; 57 58 private HibernateService service_ ; 59 private List 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 { 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 c, Session session) throws Exception { 79 Iterator 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 userName, Group g, Membership m) throws Exception { 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 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 { 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 id) throws Exception { 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 userName, 126 String groupId, 127 String type) throws Exception { 128 Session session = service_.openSession(); 129 Object [] args = new Object [] { userName, groupId , type}; 130 Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING, Hibernate.STRING }; 131 List 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 ("Expect 0 or 1 membership but found" + memberships.size()) ; 138 } 139 } 140 141 public Collection findMembershipsByUserAndGroup(String userName, String groupId) throws Exception { 142 Session session = service_.openSession(); 143 Object [] args = new Object [] { userName, groupId }; 144 Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING }; 145 List memberships = session.find(queryFindMembershipsByUserAndGroup, args, types ); 146 return memberships; 147 } 148 149 public Collection findMembershipsByUser(String userName) throws Exception { 150 Session session = service_.openSession(); 151 List memberships = session.find(queryFindMembershipsByUser, userName, Hibernate.STRING ); 152 return memberships; 153 } 154 155 static void removeMembershipEntriesOfUser(String userName, Session session) throws Exception { 156 session.delete(queryFindMembershipsByUser, userName, Hibernate.STRING ); 157 } 158 159 static void removeMembershipEntriesOfGroup(Group group, Session session) throws Exception { 160 session.delete(queryFindMembershipsByGroup, group.getId(), Hibernate.STRING ); 161 } 162 163 Collection findMembershipsByUser(String userName, Session session) throws Exception { 164 return session.find( queryFindMembershipsByUser, userName, Hibernate.STRING ); 165 } 166 167 public Collection findMembershipsByGroup(Group group) throws Exception { 168 Session session = service_.openSession(); 169 List memberships = session.find( queryFindMembershipsByGroup, group.getId(), Hibernate.STRING ); 170 return memberships; 171 } 172 173 public Membership findMembership(String id) throws Exception { 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 { 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 { 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 { 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 { 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 |