1 5 package org.exoplatform.services.organization.hibernate; 6 7 8 import java.util.Collection ; 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.exception.UniqueObjectException; 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.GroupEventListener; 21 import org.exoplatform.services.organization.impl.GroupImpl; 22 31 public class GroupQueryHandler { 32 public static final String queryFindGroupByName = 33 "from g in class org.exoplatform.services.organization.impl.GroupImpl " + 34 "where g.groupName = ? "; 35 36 public static final String queryFindGroupById = 37 "from g in class org.exoplatform.services.organization.impl.GroupImpl " + 38 "where g.id = ? "; 39 40 public static final String queryFindGroupByParent = 41 "from g in class org.exoplatform.services.organization.impl.GroupImpl " + 42 "where g.parentId = ? "; 43 44 private static final String queryFindRootGroups = 45 "from g in class org.exoplatform.services.organization.impl.GroupImpl " + 46 "where g.parentId is null"; 47 private static final String queryFindGroupsOfUser = 48 "select g " + 49 "from g in class org.exoplatform.services.organization.impl.GroupImpl, " + 50 " m in class org.exoplatform.services.organization.impl.MembershipImpl, " + 51 "where m.groupId = g.id " + 52 " and m.userName = ?" ; 53 54 private static final String queryFindGroupByMembership = 55 "select g " + 56 "from m in class org.exoplatform.services.organization.impl.MembershipImpl, " + 57 " g in class org.exoplatform.services.organization.impl.GroupImpl " + 58 "where m.groupId = g.id " + 59 " and m.userName = ? " + 60 " and m.membershipType = ? "; 61 62 private HibernateService service_ ; 63 private List listeners_ ; 64 65 public GroupQueryHandler(HibernateService service) { 66 service_ = service ; 67 listeners_ = new ListenerStack(5) ; 68 } 69 70 public void addGroupEventListener(GroupEventListener listener) { 71 listeners_.add(listener) ; 72 } 73 74 public void createGroup(Group group) throws Exception { 75 Session session = service_.openSession(); 76 String groupId = "/" + group.getGroupName() ; 77 Object o = session.get(GroupImpl.class, groupId) ; 78 if(o != null) { 79 Object [] args = {group.getGroupName()} ; 80 throw new UniqueObjectException("OrganizationService.unique-group-exception", args) ; 81 } 82 preSave(group, true, session) ; 83 session.save(group, "/" + group.getGroupName()); 84 postSave(group, true, session) ; 85 session.flush() ; 86 } 87 88 public void addChild(Group parent, Group child) throws Exception { 89 Session session = service_.openSession(); 90 Group parentGroup = (Group) session.get(GroupImpl.class, parent.getId()) ; 91 String groupId = parentGroup.getId() + "/" + child.getGroupName() ; 92 Object o = session.get(GroupImpl.class, groupId) ; 93 if(o != null) { 94 Object [] args = {child.getGroupName()} ; 95 throw new UniqueObjectException("OrganizationService.unique-group-exception", args) ; 96 } 97 GroupImpl childImpl = (GroupImpl) child ; 98 preSave(child, true, session) ; 99 childImpl.setParentId(parentGroup.getId()) ; 100 session.save(childImpl, groupId); 101 postSave(child, true, session) ; 102 session.flush() ; 103 } 104 105 public void saveGroup(Group group) throws Exception { 106 Session session = service_.openSession(); 107 preSave(group, false, session) ; 108 session.update(group); 109 postSave(group, false, session) ; 110 session.flush(); 111 } 112 113 public Group removeGroup(Group group) throws Exception { 114 Session session = service_.openSession(); 115 preDelete(group, session) ; 116 session.delete(group); 117 session.delete(queryFindGroupByParent, group.getId(), Hibernate.STRING ); 118 MembershipQueryHandler.removeMembershipEntriesOfGroup(group, session) ; 119 postDelete(group, session) ; 120 session.flush(); 121 return group ; 122 } 123 124 static void removeGroupEntry(String groupName, Session session) throws Exception { 125 session.delete(queryFindGroupByName, groupName, Hibernate.STRING); 126 } 127 128 public Collection findGroupByMembership(String userName, String membershipType) throws Exception { 129 Session session = service_.openSession(); 130 Object [] args = new Object [] { userName, membershipType }; 131 Type[] types = new Type[] { Hibernate.STRING, Hibernate.STRING }; 132 List groups = session.find(queryFindGroupByMembership, args, types ); 133 return groups ; 134 } 135 136 public Group findGroupByName(String groupName) throws Exception { 137 Session session = service_.openSession(); 138 Group group =(Group)service_.findOne(session,queryFindGroupByName, groupName); 139 return group; 140 } 141 142 public Group findGroupById(String groupId) throws Exception { 143 Session session = service_.openSession(); 144 Group group =(Group)service_.findOne(session,queryFindGroupById, groupId); 145 return group; 146 } 147 148 public Collection findGroups(Group parent) throws Exception { 149 Session session = service_.openSession(); 150 if (parent == null) { 151 return session.find( queryFindRootGroups ); 152 } 153 return session.find( queryFindGroupByParent, parent.getId(), Hibernate.STRING ); 154 } 155 156 public Collection findGroupsOfUser(String user) throws Exception { 157 Session session = service_.openSession(); 158 return session.find( queryFindGroupsOfUser, user, Hibernate.STRING ); 159 } 160 161 private void preSave(Group group , boolean isNew , Session session) throws Exception { 162 XResources xresources = new XResources() ; 163 xresources.addResource(Session.class, session) ; 164 for (int i = 0 ; i < listeners_.size(); i++) { 165 GroupEventListener listener = (GroupEventListener) listeners_.get(i) ; 166 listener.preSave(group, isNew, xresources) ; 167 } 168 } 169 170 private void postSave(Group group , boolean isNew , Session session) throws Exception { 171 XResources xresources = new XResources() ; 172 xresources.addResource(Session.class, session) ; 173 for (int i = 0 ; i < listeners_.size(); i++) { 174 GroupEventListener listener = (GroupEventListener) listeners_.get(i) ; 175 listener.postSave(group, isNew, xresources) ; 176 } 177 } 178 179 private void preDelete(Group group , 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 GroupEventListener listener = (GroupEventListener) listeners_.get(i) ; 184 listener.preDelete(group, xresources) ; 185 } 186 } 187 188 private void postDelete(Group group , 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 GroupEventListener listener = (GroupEventListener) listeners_.get(i) ; 193 listener.postDelete(group, xresources) ; 194 } 195 } 196 } | Popular Tags |