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.JoinAction;
18  import org.figure8.join.control.form.UserForm;
19  import org.figure8.join.businessobjects.security.User;
20  import org.figure8.join.businessfacades.security.UserManager;
21  import org.figure8.join.businessfacades.artifact.ArtifactManager;
22  import org.figure8.join.businessfacades.artifact.AssemblyManager;
23  import org.figure8.join.util.LogUtil;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.beanutils.PropertyUtils;
27  import org.apache.struts.action.ActionForm;
28  import org.apache.struts.action.ActionForward;
29  import org.apache.struts.action.ActionMapping;
30  
31  import javax.servlet.http.HttpSession;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import java.util.List;
36  /**
37   * Struts action used for managing Users within Join application
38   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
39   * @version $Revision: 1.2 $
40   *
41   * @struts.action path="/user" name="userForm"
42   *                scope="request" parameter="op" validate="true"
43   *                input="/pages/mainpage.jsp?body=/jsp/user/useraccount.jsp"
44   * @struts.action-forward name="Users" path="/jsp/user/users.jsp"
45   * @struts.action-forward name="Account" path="/jsp/user/useraccount.jsp"
46   * @struts.action-forward name="Details" path="/jsp/user/userdetails.jsp"
47   */
48  public class UserActions extends JoinAction{
49  
50     // Static -------------------------------------------------------------------
51  
52     /** Get a commons logger. */
53     private static final Log log = LogUtil.getLog(UserActions.class);
54  
55     /** Operation code for loading a specific User */
56     public static final String LOAD_OP = "load";
57     /** Operation code for saving a specific User */
58     public static final String SAVE_OP = "save";
59     /** Operation code for searching users using lastname */
60     public static final String SEARCH_OP = "search";
61     /** Operation code for getting details on a specific User */
62     public static final String DETAILS_OP = "details";
63  
64     /**
65      * The session scope attribute under which the User object currently
66      * selected or created by our logged-in User is stored.
67      */
68     public static final String USER_KEY = "user";
69     /**
70      * The request scope attribute under which the retrieved Users
71      * list is stored.
72      */
73     public static final String USERS_KEY = "users";
74  
75  
76     // Attributes ---------------------------------------------------------------
77  
78     /** A Join User manager instance */
79     protected UserManager userManager = null;
80     /** The artifact manager implementation to use. */
81     protected ArtifactManager artifactManager = null;
82     /** The assembly manager implementation to use. */
83     protected AssemblyManager assemblyManager = null;
84  
85  
86     // Constructors -------------------------------------------------------------
87  
88     /** Creates a new instance of UserActions. */
89     public UserActions(){
90     }
91  
92  
93     // Public -------------------------------------------------------------------
94  
95     /** @param manager <code>UserManager</code> implementation instance */
96     public void setUserManager(UserManager manager){
97        this.userManager = manager;
98     }
99     /** @param manager The ArtifactManager implementation to use */
100    public void setArtifactManager(ArtifactManager manager){
101       this.artifactManager = manager;
102    }
103    /** @param manager The AssemblyManager implementation to use */
104    public void setAssemblyManager(AssemblyManager manager){
105       this.assemblyManager = manager;
106    }
107 
108 
109    // Implementation of JoinAction ---------------------------------------------
110 
111    /**
112     *
113     * @param operation String representing the operation to invoke on Action
114     * @param mapping Mapping between forwards name and path for this action
115     * @param form The form object containing request parameters
116     * @param request The servlet container request wrapper
117     * @param response The servlet container response wrapper
118     * @return A forward to the next view to render and display
119     */
120    public ActionForward doExecute(String operation, ActionMapping mapping, ActionForm form,
121                                   HttpServletRequest request, HttpServletResponse response) throws Exception{
122       // Trace operation to execute.
123       log.debug("doExecute() called for '" + operation + "' operation");
124 
125       if (LOAD_OP.equals(operation)){
126          return loadUser(mapping, form, request, response);
127       }
128       else if (SAVE_OP.equals(operation)){
129          return saveUser(mapping, form, request, response);
130       }
131       else if (SEARCH_OP.equals(operation)){
132          return searchUsers(mapping, form, request, response);
133       }
134       else if (DETAILS_OP.equals(operation)){
135          return getUserDetails(mapping, form, request, response);
136       }
137       else{
138          // Default : empty user management page.
139          request.getSession().removeAttribute(USER_KEY);
140          return mapping.findForward("Users");
141       }
142    }
143 
144 
145    // Protected ----------------------------------------------------------------
146 
147    /**
148     * Load a specified user from datastore and fill form with it.
149     * @return A forward to the next view to render and display
150     */
151    protected ActionForward loadUser(ActionMapping mapping, ActionForm form,
152                                     HttpServletRequest request, HttpServletResponse response) throws Exception{
153       if (form instanceof UserForm){
154          // Get the request user from manager or session.
155          User user = null;
156          UserForm uForm = (UserForm)form;
157 
158          if (uForm.getLogin() == null && isLoggedIn(request))
159             user = getUserContainer(request).getView().getUser();
160          else
161             user = userManager.getUser(uForm.getLogin());
162 
163          // Store user within session.
164          HttpSession session = request.getSession();
165          session.setAttribute(USER_KEY, user);
166          // Copy form into the user properties.
167          PropertyUtils.copyProperties(uForm, user);
168 
169          // Forward.
170          return mapping.findForward("Account");
171       }
172       // This should not happen...
173       return null;
174    }
175 
176    /**
177     * Save an updated and existing user from datastore. User should have been loaded
178     * before and the present into the {@link USER_KEY} session attribute.
179     * @return A forward to the next view to render and display
180     */
181    protected ActionForward saveUser(ActionMapping mapping, ActionForm form,
182                                     HttpServletRequest request, HttpServletResponse response) throws Exception{
183       if (form instanceof UserForm){
184          // Get the user form.
185          UserForm uForm = (UserForm)form;
186 
187          // Retrieve also User from session.
188          HttpSession session = request.getSession();
189          User user = (User)session.getAttribute(USER_KEY);
190 
191          // Populate user with modified form before saving.
192          if (user != null){
193             log.info("Update of existing User having login: " + uForm.getLogin());
194             // We have to backup id to not lose it when copying properties.
195             // This is due to the public setId(long) of User object...
196             long backupId = user.getId();
197             PropertyUtils.copyProperties(user, uForm);
198             user.setId(backupId);
199             // Save it.
200             userManager.saveUser(user);
201          }
202 
203          // Forward.
204          return mapping.findForward("Account");
205       }
206       // This should not happen...
207       return null;
208    }
209 
210    /**
211     * Retrieve a list of Users using the lastname criterion. This lastname information
212     * is retrieved : from User present in session under {@link USER_KEY} attribute if
213     * present of from the UserForm supplied with request. The research is done using
214     * the <code>getUsers()</code> method of {@link UserManager}.
215     * @return A forward to the next view to render and display
216     */
217    protected ActionForward searchUsers(ActionMapping mapping, ActionForm form,
218                                        HttpServletRequest request, HttpServletResponse response) throws Exception{
219       if (form instanceof UserForm){
220          String criterion = null;
221 
222          // Look if User is defined in session.
223          HttpSession session = request.getSession();
224          User user = (User)session.getAttribute(USER_KEY);
225 
226          if (user != null)
227             criterion = user.getLastname();
228          else
229             criterion = ((UserForm)form).getLastname();
230 
231          // Retrieve list and put into request attribute.
232          List users = userManager.getUsers(criterion);
233          request.setAttribute(USERS_KEY, users);
234 
235          // Forward after having cleaned the session.
236          request.getSession().removeAttribute(USER_KEY);
237          return mapping.findForward("Users");
238       }
239       // This should not happen...
240       return null;
241    }
242 
243    /**
244     * Get a user details such has its informations and artifacts it has delivered, build, etc...
245     * @return A forward to the next view to render and display
246     */
247    protected ActionForward getUserDetails(ActionMapping mapping, ActionForm form,
248                                           HttpServletRequest request, HttpServletResponse response) throws Exception{
249       if (form instanceof UserForm){
250          // Get the user form.
251          UserForm uForm = (UserForm)form;
252 
253          request.setAttribute(USER_KEY, userManager.getUser(uForm.getLogin()));
254          // TODO : complete interfaces of manager for having following methods.
255          // request.setAttribute(DeliverableActions.DELIVERABLES_KEY, artifactManager.getDeliverablesSuppliedByUser(uForm.getLogin())));
256          // request.setAttribute(AssemblyActions.ASSEMBLIES_KEY, assemblyManager.getAssembliesComposedByUser(uForm.getLogin())));
257          // request.setAttribute(BuildActions.BUILDS_KEY, assemblyManager.getBuildsComposedByUser(uForm.getLogin()));
258 
259          return mapping.findForward("Details");
260       }
261       // This should not happen...
262       return null;
263    }
264 }