KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * EPersonAdminServlet.java
3  *
4  * Version: $Revision: 1.20 $
5  *
6  * Date: $Date: 2006/09/12 13:17:10 $
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 org.apache.log4j.Logger;
50 import org.dspace.app.webui.servlet.DSpaceServlet;
51 import org.dspace.app.webui.util.JSPManager;
52 import org.dspace.app.webui.util.UIUtil;
53 import org.dspace.authorize.AuthorizeException;
54 import org.dspace.core.Context;
55 import org.dspace.eperson.EPerson;
56 import org.dspace.eperson.Group;
57 import org.dspace.eperson.EPersonDeletionException;
58
59 /**
60  * Servlet for editing and creating e-people
61  *
62  * @author David Stuve
63  * @version $Revision: 1.20 $
64  */

65 public class EPersonAdminServlet extends DSpaceServlet
66 {
67     /** Logger */
68     private static Logger log = Logger.getLogger(EPersonAdminServlet.class);
69
70     protected void doDSGet(Context context, HttpServletRequest JavaDoc request,
71             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
72             SQLException JavaDoc, AuthorizeException
73     {
74         showMain(context, request, response);
75     }
76
77     protected void doDSPost(Context context, HttpServletRequest JavaDoc request,
78             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
79             SQLException JavaDoc, AuthorizeException
80     {
81         String JavaDoc button = UIUtil.getSubmitButton(request, "submit");
82
83         if (button.equals("submit_add"))
84         {
85             // add an EPerson, then jump user to edit page
86
EPerson e = EPerson.create(context);
87
88             // create clever name and do update before continuing
89
e.setEmail("newuser" + e.getID());
90             e.update();
91
92             request.setAttribute("eperson", e);
93
94             JSPManager.showJSP(request, response,
95                     "/dspace-admin/eperson-edit.jsp");
96
97             context.complete();
98         }
99         else if (button.equals("submit_edit"))
100         {
101             // edit an eperson
102
EPerson e = EPerson.find(context, UIUtil.getIntParameter(request,
103                     "eperson_id"));
104             
105             // Check the EPerson exists
106
if (e == null)
107             {
108                 request.setAttribute("no_eperson_selected", new Boolean JavaDoc(true));
109                 showMain(context, request, response);
110             }
111             else
112             {
113                 // what groups is this person a member of?
114
Group[] groupMemberships = Group.allMemberGroups(context, e);
115     
116                 request.setAttribute("eperson", e);
117                 request.setAttribute("group.memberships", groupMemberships);
118     
119                 JSPManager.showJSP(request, response,
120                         "/dspace-admin/eperson-edit.jsp");
121     
122                 context.complete();
123             }
124         }
125         else if (button.equals("submit_save"))
126         {
127             // Update the metadata for an e-person
128
EPerson e = EPerson.find(context, UIUtil.getIntParameter(request,
129                     "eperson_id"));
130
131             // see if the user changed the email - if so, make sure
132
// the new email is unique
133
String JavaDoc oldEmail = e.getEmail();
134             String JavaDoc newEmail = request.getParameter("email").trim();
135             String JavaDoc netid = request.getParameter("netid");
136
137             if (!newEmail.equals(oldEmail))
138             {
139                 // change to email, now see if it's unique
140
if (EPerson.findByEmail(context, newEmail) == null)
141                 {
142                     // it's unique - proceed!
143
e.setEmail(newEmail);
144
145                     e
146                             .setFirstName(request.getParameter("firstname")
147                                     .equals("") ? null : request
148                                     .getParameter("firstname"));
149
150                     e
151                             .setLastName(request.getParameter("lastname")
152                                     .equals("") ? null : request
153                                     .getParameter("lastname"));
154
155                     if (netid != null)
156                     {
157                         e.setNetid(netid.equals("") ? null : netid);
158                     }
159                     else
160                     {
161                         e.setNetid(null);
162                     }
163
164                     // FIXME: More data-driven?
165
e.setMetadata("phone", request.getParameter("phone")
166                             .equals("") ? null : request.getParameter("phone"));
167
168                     e.setCanLogIn((request.getParameter("can_log_in") != null)
169                             && request.getParameter("can_log_in")
170                                     .equals("true"));
171
172                     e.setRequireCertificate((request
173                             .getParameter("require_certificate") != null)
174                             && request.getParameter("require_certificate")
175                                     .equals("true"));
176
177                     e.update();
178
179                     showMain(context, request, response);
180                     context.complete();
181                 }
182                 else
183                 {
184                     // not unique - send error message & let try again
185
request.setAttribute("eperson", e);
186                     request.setAttribute("email_exists", new Boolean JavaDoc(true));
187
188                     JSPManager.showJSP(request, response,
189                             "/dspace-admin/eperson-edit.jsp");
190
191                     context.complete();
192                 }
193             }
194             else
195             {
196                 // no change to email
197
if (netid != null)
198                 {
199                     e.setNetid(netid.equals("") ? null : netid);
200                 }
201                 else
202                 {
203                     e.setNetid(null);
204                 }
205
206                 e
207                         .setFirstName(request.getParameter("firstname").equals(
208                                 "") ? null : request.getParameter("firstname"));
209
210                 e
211                         .setLastName(request.getParameter("lastname")
212                                 .equals("") ? null : request
213                                 .getParameter("lastname"));
214
215                 // FIXME: More data-driven?
216
e.setMetadata("phone",
217                         request.getParameter("phone").equals("") ? null
218                                 : request.getParameter("phone"));
219
220                 e.setCanLogIn((request.getParameter("can_log_in") != null)
221                         && request.getParameter("can_log_in").equals("true"));
222
223                 e.setRequireCertificate((request
224                         .getParameter("require_certificate") != null)
225                         && request.getParameter("require_certificate").equals(
226                                 "true"));
227
228                 e.update();
229
230                 showMain(context, request, response);
231                 context.complete();
232             }
233         }
234         else if (button.equals("submit_delete"))
235         {
236             // Start delete process - go through verification step
237
EPerson e = EPerson.find(context, UIUtil.getIntParameter(request,
238                     "eperson_id"));
239             
240             // Check the EPerson exists
241
if (e == null)
242             {
243                 request.setAttribute("no_eperson_selected", new Boolean JavaDoc(true));
244                 showMain(context, request, response);
245             }
246             else
247             {
248                 request.setAttribute("eperson", e);
249     
250                 JSPManager.showJSP(request, response,
251                         "/dspace-admin/eperson-confirm-delete.jsp");
252             }
253         }
254         else if (button.equals("submit_confirm_delete"))
255         {
256             // User confirms deletion of type
257
EPerson e = EPerson.find(context, UIUtil.getIntParameter(request,
258                     "eperson_id"));
259
260             try
261             {
262                 e.delete();
263             }
264             catch (EPersonDeletionException ex)
265             {
266                 request.setAttribute("eperson", e);
267                 request.setAttribute("tableList", ex.getTables());
268                 JSPManager.showJSP(request, response,
269                         "/dspace-admin/eperson-deletion-error.jsp");
270             }
271
272             showMain(context, request, response);
273             context.complete();
274         }
275         else
276         {
277             // Cancel etc. pressed - show list again
278
showMain(context, request, response);
279         }
280     }
281
282     private void showMain(Context c, HttpServletRequest JavaDoc request,
283             HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
284             SQLException JavaDoc, AuthorizeException
285     {
286         JSPManager.showJSP(request, response, "/dspace-admin/eperson-main.jsp");
287     }
288 }
289
Popular Tags