KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > webui > servlet > admin > GroupEditServlet


1 /*
2  * GroupEditServlet.java
3  *
4  * Version: $Revision: 1.16 $
5  *
6  * Date: $Date: 2006/07/05 19:55:31 $
7  *
8  * Copyright (c) 2002-2005, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.webui.servlet.admin;
41
42 import java.io.IOException JavaDoc;
43 import java.sql.SQLException JavaDoc;
44
45 import javax.servlet.ServletException JavaDoc;
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47 import javax.servlet.http.HttpServletResponse JavaDoc;
48
49 import java.util.HashSet JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Set JavaDoc;
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 /**
64  * Servlet for editing groups
65  *
66  * @author dstuve
67  * @version $Revision: 1.16 $
68  */

69 public class GroupEditServlet extends DSpaceServlet
70 {
71     protected void doDSGet(Context c, HttpServletRequest JavaDoc request,
72             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
73             SQLException JavaDoc, AuthorizeException
74     {
75         doDSPost(c, request, response);
76     }
77
78     protected void doDSPost(Context c, HttpServletRequest JavaDoc request,
79             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
80             SQLException JavaDoc, AuthorizeException
81     {
82         // Find out if there's a group parameter
83
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         // group is set
92
if (group != null)
93         {
94             // is this user authorized to edit this group?
95
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             // just chosen a group to edit - get group and pass it to
104
// group-edit.jsp
105
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             // update the members of the group
114
else if (submit_group_update)
115             {
116                 // first off, did we change the group name?
117
String JavaDoc 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                 // now get members, and add new ones and remove missing ones
130
EPerson[] members = group.getMembers();
131                 Group[] membergroups = group.getMemberGroups();
132
133                 if (eperson_ids != null)
134                 {
135                     // some epeople were listed, now make group's epeople match
136
// given epeople
137
Set JavaDoc memberSet = new HashSet JavaDoc();
138                     Set JavaDoc epersonIDSet = new HashSet JavaDoc();
139
140                     // add all members to a set
141
for (int x = 0; x < members.length; x++)
142                     {
143                         Integer JavaDoc epersonID = new Integer JavaDoc(members[x].getID());
144                         memberSet.add(epersonID);
145                     }
146
147                     // now all eperson_ids are put in a set
148
for (int x = 0; x < eperson_ids.length; x++)
149                     {
150                         epersonIDSet.add(new Integer JavaDoc(eperson_ids[x]));
151                     }
152
153                     // process eperson_ids, adding those to group not already
154
// members
155
Iterator JavaDoc i = epersonIDSet.iterator();
156
157                     while (i.hasNext())
158                     {
159                         Integer JavaDoc currentID = (Integer JavaDoc) i.next();
160
161                         if (!memberSet.contains(currentID))
162                         {
163                             group.addMember(EPerson.find(c, currentID
164                                     .intValue()));
165                         }
166                     }
167
168                     // process members, removing any that aren't in eperson_ids
169
for (int x = 0; x < members.length; x++)
170                     {
171                         EPerson e = members[x];
172
173                         if (!epersonIDSet.contains(new Integer JavaDoc(e.getID())))
174                         {
175                             group.removeMember(e);
176                         }
177                     }
178                 }
179                 else
180                 {
181                     // no members found (ids == null), remove them all!
182

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                     // some groups were listed, now make group's member groups
192
// match given group IDs
193
Set JavaDoc memberSet = new HashSet JavaDoc();
194                     Set JavaDoc groupIDSet = new HashSet JavaDoc();
195
196                     // add all members to a set
197
for (int x = 0; x < membergroups.length; x++)
198                     {
199                         Integer JavaDoc myID = new Integer JavaDoc(membergroups[x].getID());
200                         memberSet.add(myID);
201                     }
202
203                     // now all eperson_ids are put in a set
204
for (int x = 0; x < group_ids.length; x++)
205                     {
206                         groupIDSet.add(new Integer JavaDoc(group_ids[x]));
207                     }
208
209                     // process group_ids, adding those to group not already
210
// members
211
Iterator JavaDoc i = groupIDSet.iterator();
212
213                     while (i.hasNext())
214                     {
215                         Integer JavaDoc currentID = (Integer JavaDoc) i.next();
216
217                         if (!memberSet.contains(currentID))
218                         {
219                             group
220                                     .addMember(Group.find(c, currentID
221                                             .intValue()));
222                         }
223                     }
224
225                     // process members, removing any that aren't in eperson_ids
226
for (int x = 0; x < membergroups.length; x++)
227                     {
228                         Group g = membergroups[x];
229
230                         if (!groupIDSet.contains(new Integer JavaDoc(g.getID())))
231                         {
232                             group.removeMember(g);
233                         }
234                     }
235
236                 }
237                 else
238                 {
239                     // no members found (ids == null), remove them all!
240
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                 // phony authorize, only admins can do this
258
AuthorizeManager.authorizeAction(c, group, Constants.WRITE);
259
260                 // delete group, return to group-list.jsp
261
group.delete();
262
263                 showMainPage(c, request, response);
264             }
265             else
266             {
267                 // unknown action, show edit page
268
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         // no group set
277
{
278             // want to add a group - create a blank one, and pass to
279
// group_edit.jsp
280
String JavaDoc 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                 // show the main page (select groups)
299
showMainPage(c, request, response);
300             }
301         }
302     }
303
304     private void showMainPage(Context c, HttpServletRequest JavaDoc request,
305             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
306             SQLException JavaDoc, AuthorizeException
307     {
308         Group[] groups = Group.findAll(c, Group.NAME);
309
310         // if( groups == null ) { System.out.println("groups are null"); }
311
// else System.out.println("# of groups: " + groups.length);
312
request.setAttribute("groups", groups);
313
314         JSPManager.showJSP(request, response, "/tools/group-list.jsp");
315         c.complete();
316     }
317 }
318
Popular Tags