View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.control.action;
16  
17  import org.figure8.join.control.form.UserForm;
18  import org.figure8.join.core.DuplicateEntityException;
19  import org.figure8.join.businessobjects.security.User;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.struts.action.ActionForm;
24  import org.apache.struts.action.ActionForward;
25  import org.apache.struts.action.ActionMapping;
26  
27  import javax.servlet.http.HttpSession;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  /**
31   * Struts action used for creating User within Join application (this is necessary
32   * because this action has a different input form from all other User related actions
33   * contained into {@link UserActions}).
34   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
35   * @version $Revision: 1.1 $
36   *
37   * @struts.action path="/createUser" name="userForm"
38   *                scope="request" parameter="op" validate="true"
39   *                input="/pages/mainpage.jsp?body=/jsp/user/user.jsp"
40   * @struts.action-forward name="Form" path="/jsp/user/user.jsp"
41   * @struts.action-forward name="Permissions" path="/action/permission?op=loadPermissions"
42   */
43  public class CreateUserAction extends UserActions{
44  
45     // Static -------------------------------------------------------------------
46  
47     /** Get a commons logger. */
48     private static final Log log = LogUtil.getLog(CreateUserAction.class);
49  
50     /** Operation code for creating a specific User */
51     public static final String CREATE_OP = "create";
52  
53     /**
54      * The request scope attribute under which is stored the User that
55      * has raised a DuplicateEntityException during save of another one.
56      */
57     public static final String DUPLICATE_USER_KEY = "duplicate";
58  
59  
60     // Constructors -------------------------------------------------------------
61  
62     /** Creates a new instance of CreateUserAction. */
63     public CreateUserAction(){
64     }
65  
66  
67     // Implementation of JoinAction ---------------------------------------------
68  
69     /**
70      *
71      * @param operation String representing the operation to invoke on Action
72      * @param mapping Mapping between forwards name and path for this action
73      * @param form The form object containing request parameters
74      * @param request The servlet container request wrapper
75      * @param response The servlet container response wrapper
76      * @return A forward to the next view to render and display
77      */
78     public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
79              throws Exception{
80        // Trace operation to execute.
81        log.debug("doExecute() called for '" + operation + "' operation");
82  
83        if (CREATE_OP.equals(operation) && form instanceof UserForm){
84           // Get the user form.
85           UserForm uForm = (UserForm)form;
86           log.info("Creation of a new user with login: " + uForm.getLogin());
87           User user = new User(uForm.getLogin(), uForm.getPassword(), uForm.getLastname(), uForm.getFirstname());
88  
89           // Save user using manager.
90           try {userManager.saveUser(user);}
91           catch (DuplicateEntityException dee){
92              // Store exception within request.
93              request.setAttribute(DUPLICATE_USER_KEY, dee.getOriginalEntity());
94              return mapping.findForward("Form");
95           }
96  
97           // Store user within session.
98           HttpSession session = request.getSession();
99           session.setAttribute(UserActions.USER_KEY, user);
100 
101          // Forward.
102          return mapping.findForward("Permissions");
103       }
104       else{
105          // Default : empty user form page.
106          request.getSession().removeAttribute(USER_KEY);
107          return mapping.findForward("Form");
108       }
109    }
110 }