Index: lams_central/src/java/org/lamsfoundation/lams/web/ProfileSaveAction.java =================================================================== RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/ProfileSaveAction.java,v diff -u -r1.10 -r1.11 --- lams_central/src/java/org/lamsfoundation/lams/web/ProfileSaveAction.java 20 May 2008 06:03:24 -0000 1.10 +++ lams_central/src/java/org/lamsfoundation/lams/web/ProfileSaveAction.java 23 Jan 2009 01:36:33 -0000 1.11 @@ -50,87 +50,87 @@ /** * @author jliew - * + * */ /** - * @struts.action path="/saveprofile" - * name="UserForm" - * input=".editprofile" - * scope="request" - * validate="false" + * @struts.action path="/saveprofile" name="UserForm" input=".editprofile" + * scope="request" validate="false" * - * @struts.action-forward name="profile" path="/index.do?state=active&tab=profile" redirect="true" - * @struts.action-forward name="editprofile" path="/index.do?state=active&tab=editprofile" + * @struts.action-forward name="profile" + * path="/index.do?state=active&tab=profile" + * redirect="true" + * @struts.action-forward name="editprofile" + * path="/index.do?state=active&tab=editprofile" */ public class ProfileSaveAction extends Action { - - private static Logger log = Logger.getLogger(ProfileSaveAction.class); - private static IUserManagementService service; - public ActionForward execute(ActionMapping mapping, - ActionForm form, - HttpServletRequest request, - HttpServletResponse response) throws Exception { - - if(isCancelled(request)){ - return mapping.findForward("profile"); - } - - ActionMessages errors = new ActionMessages(); - User requestor = (User)getService().getUserByLogin(request.getRemoteUser()); - DynaActionForm userForm = (DynaActionForm)form; - - // check requestor is same as user being edited - log.debug("requestor: "+requestor.getLogin()+", form login: "+userForm.get("login")); - if(!requestor.getLogin().equals(userForm.get("login"))){ - errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.authorisation")); - saveErrors(request,errors); - return (mapping.getInputForward()); - } - - // (dyna)form validation - if ((userForm.get("firstName") == null) || (userForm.getString("firstName").trim().length() == 0)) { - errors.add("firstName", new ActionMessage("error.firstname.required")); - } - if ((userForm.get("lastName") == null) || (userForm.getString("lastName").trim().length() == 0)) { - errors.add("lastName", new ActionMessage("error.lastname.required")); - } - if ((userForm.get("email") == null) || (userForm.getString("email").trim().length() == 0)) { - errors.add("email", new ActionMessage("error.email.required")); - } else { - Pattern p = Pattern.compile(".+@.+\\.[a-z]+"); - Matcher m = p.matcher(userForm.getString("email")); - if (!m.matches()) { - errors.add("email", new ActionMessage("error.valid.email.required")); - } - } - - if (!errors.isEmpty()) { - saveErrors(request, errors); - return (mapping.findForward("editprofile")); - } - - BeanUtils.copyProperties(requestor,userForm); - SupportedLocale locale = (SupportedLocale) getService().findById(SupportedLocale.class, (Integer)userForm.get("localeId")); - requestor.setLocale(locale); - getService().save(requestor); - log.debug("profile edited: "+requestor); - - // replace UserDTO in the shared session - HttpSession ss = SessionManager.getSession(); - ss.removeAttribute(AttributeNames.USER); - ss.setAttribute(AttributeNames.USER, requestor.getUserDTO()); - - return mapping.findForward("profile"); + private static Logger log = Logger.getLogger(ProfileSaveAction.class); + private static IUserManagementService service; + + public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws Exception { + + if (isCancelled(request)) { + return mapping.findForward("profile"); } - - private IUserManagementService getService(){ - if(service==null){ - WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet().getServletContext()); - service = (IUserManagementService) ctx.getBean("userManagementService"); - } - return service; + + ActionMessages errors = new ActionMessages(); + User requestor = (User) getService().getUserByLogin(request.getRemoteUser()); + DynaActionForm userForm = (DynaActionForm) form; + + // check requestor is same as user being edited + log.debug("requestor: " + requestor.getLogin() + ", form login: " + userForm.get("login")); + if (!requestor.getLogin().equals(userForm.get("login"))) { + errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.authorisation")); + saveErrors(request, errors); + return (mapping.getInputForward()); } + // (dyna)form validation + if ((userForm.get("firstName") == null) || (userForm.getString("firstName").trim().length() == 0)) { + errors.add("firstName", new ActionMessage("error.firstname.required")); + } + if ((userForm.get("lastName") == null) || (userForm.getString("lastName").trim().length() == 0)) { + errors.add("lastName", new ActionMessage("error.lastname.required")); + } + if ((userForm.get("email") == null) || (userForm.getString("email").trim().length() == 0)) { + errors.add("email", new ActionMessage("error.email.required")); + } else { + Pattern p = Pattern.compile(".+@.+\\.[a-z]+"); + Matcher m = p.matcher(userForm.getString("email")); + if (!m.matches()) { + errors.add("email", new ActionMessage("error.valid.email.required")); + } + } + + if (!errors.isEmpty()) { + saveErrors(request, errors); + return (mapping.findForward("editprofile")); + } + + BeanUtils.copyProperties(requestor, userForm); + SupportedLocale locale = (SupportedLocale) getService().findById(SupportedLocale.class, + (Integer) userForm.get("localeId")); + requestor.setLocale(locale); + getService().save(requestor); + log.debug("profile edited: " + requestor); + + // replace UserDTO in the shared session + HttpSession ss = SessionManager.getSession(); + ss.removeAttribute(AttributeNames.USER); + ss.setAttribute(AttributeNames.USER, requestor.getUserDTO()); + + return mapping.findForward("profile"); + } + + private IUserManagementService getService() { + if (service == null) { + WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() + .getServletContext()); + service = (IUserManagementService) ctx.getBean("userManagementService"); + } + return service; + } + } \ No newline at end of file