Package org.nemesis.forum.webapp.admin.action

Source Code of org.nemesis.forum.webapp.admin.action.EditMyProfileAction

package org.nemesis.forum.webapp.admin.action;

import java.util.Enumeration;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.nemesis.forum.ForumFactory;
import org.nemesis.forum.ProfileManager;
import org.nemesis.forum.User;
import org.nemesis.forum.exception.UnauthorizedException;
import org.nemesis.forum.webapp.admin.bean.UserBean;

/**
* @author dlaurent
*
* 20 f�vr. 2003
*
*/
public class EditMyProfileAction extends BaseAction {

  static protected Log log = LogFactory.getLog(EditMyProfileAction.class);

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    //check logon
    checkUser(request);
   
    ActionErrors errors = new ActionErrors();
    //user need cancel
    if (isCancelled(request)) {
      return (mapping.findForward("cancel"));
    }

    try {

      ForumFactory forumFactory = ForumFactory.getInstance(getAuthToken(request));
      ProfileManager manager = forumFactory.getProfileManager();
      User user = manager.getUser(getAuthToken(request).getUserID());
      try {

        //retrieve the user
        User u = manager.getUser(getUser(request));

        //first, populate
        if (request.getParameter("userBean.userName") == null) {
          UserBean ub = new UserBean();
          ub.setId(u.getID());
          ub.setEmail(u.getEmail());
          ub.setEmailVisible(u.isEmailVisible());
          ub.setName(u.getName());
          ub.setNameVisible(u.isNameVisible());
          ub.setUserName(u.getUsername());
          String key=null;
          for(Enumeration e= u.propertyNames();e.hasMoreElements();){
            key=(String)e.nextElement();
            ub.setPropertie(key, u.getProperty(key));
          }

          PropertyUtils.setProperty(form, "userBean", ub);
          return mapping.findForward("view");
        }

        //validate
        errors = form.validate(mapping,request);
        if (!errors.isEmpty()) {
          saveErrors(request, errors);
          return mapping.findForward("view");
        }
        //save
        UserBean ub = (UserBean) PropertyUtils.getProperty(form, "userBean");
       
        u.setEmail(ub.getEmail());
        u.setEmailVisible(ub.isEmailVisible());
        u.setName(ub.getName());
        u.setNameVisible(ub.isNameVisible());
       
        //need change password
        if(ub.isChangePassword()){
          u.setPassword(ub.getPassword());         
        }
        //properties
        String temp = null;
        for (Iterator it = ub.getProperties().keySet().iterator(); it.hasNext();) {
          temp = (String) it.next();
          u.setProperty(temp, (String) ub.getProperties().get(temp));
        }
       
       
      }catch( NumberFormatException aee ) {
          return mapping.findForward("cancel");
      } catch (UnauthorizedException aee) {
        errors.add("general", new ActionError("addUser.unauthorized"));
        return mapping.findForward("cancel");
      }

    } catch (Exception e) {
      String eid = this.getClass().getName() + "_" + System.currentTimeMillis();
      log.error("eid:" + eid + "\nsessionID" + request.getSession().getId(), e);
      errors.add("general", new ActionError("error.general", "erreur id:" + eid));
    }

   

    return mapping.findForward("success");
  }

}
TOP

Related Classes of org.nemesis.forum.webapp.admin.action.EditMyProfileAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.