/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */
package olstore.action;


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

import olstore.dto.AddressValue;
import olstore.dto.UserValue;
import olstore.form.DemoDynaBaseForm;
import olstore.framework.EJBHomeFactory;

import org.apache.commons.beanutils.BeanUtils;
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.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import olstore.session.helper.UserHelperLocal;
import olstore.session.helper.UserHelperLocalHome;


public class UserUpdateAction extends DemoBaseAction {
    
    /**
     * Action to update a users profile.
     */
    public ActionForward execute ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        
        try {
            
            DemoDynaBaseForm modUserForm=(DemoDynaBaseForm) form;
            UserValue userValue=new UserValue ();
            AddressValue addressValue=new AddressValue();
            
            //copy properties from the form to the DTOs
            BeanUtils.copyProperties (addressValue, modUserForm);
            BeanUtils.copyProperties (userValue, modUserForm);
            
            String username=request.getRemoteUser();
            EJBHomeFactory factory=EJBHomeFactory.getInstance ();
            UserHelperLocalHome userHelperHome=(UserHelperLocalHome) factory.getLocalHome (EJBHomeFactory.USER_HELPER);
            UserHelperLocal userHelper=userHelperHome.create();
            
            //save the information to the entity beans
            userHelper.SaveUser (userValue, addressValue, username);
            
            //to load the page the next time
            modUserForm.set ("submitType","new");
            
            //reload the saved info back into the form.
            UserModAction modAction=new UserModAction();
            modAction.loadUser(mapping, form, request, response);
            
            //create a message that the user's info was updated
            ActionMessages messages = new ActionMessages();
            ActionMessage msg = new ActionMessage("user.save.success", username);
            messages.add("success", msg);
            saveMessages(request, messages);
            
            //return back to the same page
            return (new ActionForward(mapping.getInput()));
            
        } catch ( Exception e ) {
            //Logging code here
            ActionErrors errors = new ActionErrors();
            errors.add("error", new ActionError("errors.type.save", e.getMessage() ));
            saveErrors(request, errors);
            // Return to same page
            return (new ActionForward(mapping.getInput()));
        }
        
    }
}
