/**
 * 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.*;
import olstore.session.helper.ItemHelperLocal;
import olstore.session.helper.ItemHelperLocalHome;
import olstore.dto.ItemValue;
import olstore.form.CreateItemForm;
import olstore.framework.EJBHomeFactory;

import org.apache.struts.action.*;
import org.apache.commons.beanutils.BeanUtils;


public class ItemSaveAction extends DemoBaseAction {

    /**
     * Acts as a relay for all item related tasks with a default action
     * 
     */
    public ActionForward execute ( ActionMapping mapping,
                                   ActionForm form,
                                   HttpServletRequest request,
                                   HttpServletResponse response
                                 ) throws Exception {

        try { 

            CreateItemForm createForm = (CreateItemForm)form;
            ItemValue itemVal = new ItemValue();
            BeanUtils.copyProperties ( itemVal , createForm );
            
            EJBHomeFactory factory = EJBHomeFactory.getInstance();
            ItemHelperLocalHome itemHelperHome = (ItemHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.ITEM_HELPER );
            ItemHelperLocal itemHelper = itemHelperHome.create();

            itemHelper.saveItem ( itemVal );

            ActionMessages messages = new ActionMessages();
            ActionMessage msg = new ActionMessage("item.save.success", 
                                                  itemVal.getName() );
            messages.add("success", msg);
            saveMessages(request, messages);

            return mapping.findForward ( "updateItem" );
        } catch ( Exception e ) {
            //Logging code here
        	e.printStackTrace();
            ActionErrors errors = new ActionErrors();
            errors.add("error", new ActionError("errors.item.save", e.getMessage() ));
            saveErrors(request, errors);
            // Return to same page 
            return (new ActionForward(mapping.getInput()));
        }

    }
        
}
