1 40 package org.dspace.app.webui.servlet.admin; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 45 import javax.servlet.ServletException ; 46 import javax.servlet.http.HttpServletRequest ; 47 import javax.servlet.http.HttpServletResponse ; 48 49 import java.util.HashSet ; 50 import java.util.Iterator ; 51 import java.util.Set ; 52 53 import org.dspace.app.webui.servlet.DSpaceServlet; 54 import org.dspace.app.webui.util.JSPManager; 55 import org.dspace.app.webui.util.UIUtil; 56 import org.dspace.authorize.AuthorizeException; 57 import org.dspace.authorize.AuthorizeManager; 58 import org.dspace.core.Constants; 59 import org.dspace.core.Context; 60 import org.dspace.eperson.EPerson; 61 import org.dspace.eperson.Group; 62 63 69 public class GroupEditServlet extends DSpaceServlet 70 { 71 protected void doDSGet(Context c, HttpServletRequest request, 72 HttpServletResponse response) throws ServletException , IOException , 73 SQLException , AuthorizeException 74 { 75 doDSPost(c, request, response); 76 } 77 78 protected void doDSPost(Context c, HttpServletRequest request, 79 HttpServletResponse response) throws ServletException , IOException , 80 SQLException , AuthorizeException 81 { 82 int groupID = UIUtil.getIntParameter(request, "group_id"); 84 Group group = null; 85 86 if (groupID >= 0) 87 { 88 group = Group.find(c, groupID); 89 } 90 91 if (group != null) 93 { 94 AuthorizeManager.authorizeAction(c, group, Constants.ADD); 96 97 boolean submit_edit = (request.getParameter("submit_edit") != null); 98 boolean submit_group_update = (request 99 .getParameter("submit_group_update") != null); 100 boolean submit_group_delete = (request 101 .getParameter("submit_group_delete") != null); 102 103 if (submit_edit && !submit_group_update && !submit_group_delete) 106 { 107 request.setAttribute("group", group); 108 request.setAttribute("members", group.getMembers()); 109 request.setAttribute("membergroups", group.getMemberGroups()); 110 111 JSPManager.showJSP(request, response, "/tools/group-edit.jsp"); 112 } 113 else if (submit_group_update) 115 { 116 String newName = request.getParameter("group_name"); 118 119 if (!newName.equals(group.getName())) 120 { 121 group.setName(newName); 122 group.update(); 123 } 124 125 int[] eperson_ids = UIUtil.getIntParameters(request, 126 "eperson_id"); 127 int[] group_ids = UIUtil.getIntParameters(request, "group_ids"); 128 129 EPerson[] members = group.getMembers(); 131 Group[] membergroups = group.getMemberGroups(); 132 133 if (eperson_ids != null) 134 { 135 Set memberSet = new HashSet (); 138 Set epersonIDSet = new HashSet (); 139 140 for (int x = 0; x < members.length; x++) 142 { 143 Integer epersonID = new Integer (members[x].getID()); 144 memberSet.add(epersonID); 145 } 146 147 for (int x = 0; x < eperson_ids.length; x++) 149 { 150 epersonIDSet.add(new Integer (eperson_ids[x])); 151 } 152 153 Iterator i = epersonIDSet.iterator(); 156 157 while (i.hasNext()) 158 { 159 Integer currentID = (Integer ) i.next(); 160 161 if (!memberSet.contains(currentID)) 162 { 163 group.addMember(EPerson.find(c, currentID 164 .intValue())); 165 } 166 } 167 168 for (int x = 0; x < members.length; x++) 170 { 171 EPerson e = members[x]; 172 173 if (!epersonIDSet.contains(new Integer (e.getID()))) 174 { 175 group.removeMember(e); 176 } 177 } 178 } 179 else 180 { 181 183 for (int y = 0; y < members.length; y++) 184 { 185 group.removeMember(members[y]); 186 } 187 } 188 189 if (group_ids != null) 190 { 191 Set memberSet = new HashSet (); 194 Set groupIDSet = new HashSet (); 195 196 for (int x = 0; x < membergroups.length; x++) 198 { 199 Integer myID = new Integer (membergroups[x].getID()); 200 memberSet.add(myID); 201 } 202 203 for (int x = 0; x < group_ids.length; x++) 205 { 206 groupIDSet.add(new Integer (group_ids[x])); 207 } 208 209 Iterator i = groupIDSet.iterator(); 212 213 while (i.hasNext()) 214 { 215 Integer currentID = (Integer ) i.next(); 216 217 if (!memberSet.contains(currentID)) 218 { 219 group 220 .addMember(Group.find(c, currentID 221 .intValue())); 222 } 223 } 224 225 for (int x = 0; x < membergroups.length; x++) 227 { 228 Group g = membergroups[x]; 229 230 if (!groupIDSet.contains(new Integer (g.getID()))) 231 { 232 group.removeMember(g); 233 } 234 } 235 236 } 237 else 238 { 239 for (int y = 0; y < membergroups.length; y++) 241 { 242 group.removeMember(membergroups[y]); 243 } 244 } 245 246 group.update(); 247 248 request.setAttribute("group", group); 249 request.setAttribute("members", group.getMembers()); 250 request.setAttribute("membergroups", group.getMemberGroups()); 251 252 JSPManager.showJSP(request, response, "/tools/group-edit.jsp"); 253 c.complete(); 254 } 255 else if (submit_group_delete) 256 { 257 AuthorizeManager.authorizeAction(c, group, Constants.WRITE); 259 260 group.delete(); 262 263 showMainPage(c, request, response); 264 } 265 else 266 { 267 request.setAttribute("group", group); 269 request.setAttribute("members", group.getMembers()); 270 request.setAttribute("membergroups", group.getMemberGroups()); 271 272 JSPManager.showJSP(request, response, "/tools/group-edit.jsp"); 273 } 274 } 275 else 276 { 278 String button = UIUtil.getSubmitButton(request, "submit"); 281 282 if (button.equals("submit_add")) 283 { 284 group = Group.create(c); 285 286 group.setName("new group" + group.getID()); 287 group.update(); 288 289 request.setAttribute("group", group); 290 request.setAttribute("members", group.getMembers()); 291 request.setAttribute("membergroups", group.getMemberGroups()); 292 293 JSPManager.showJSP(request, response, "/tools/group-edit.jsp"); 294 c.complete(); 295 } 296 else 297 { 298 showMainPage(c, request, response); 300 } 301 } 302 } 303 304 private void showMainPage(Context c, HttpServletRequest request, 305 HttpServletResponse response) throws ServletException , IOException , 306 SQLException , AuthorizeException 307 { 308 Group[] groups = Group.findAll(c, Group.NAME); 309 310 request.setAttribute("groups", groups); 313 314 JSPManager.showJSP(request, response, "/tools/group-list.jsp"); 315 c.complete(); 316 } 317 } 318 | Popular Tags |