/**
 * 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 java.util.ArrayList;
//import java.util.Collection;
//import java.util.Iterator;
//import java.util.Vector;

import javax.servlet.http.*;
//import javax.naming.Context;
//import javax.naming.InitialContext;

//import olstore.dto.PropertyValue;
//import olstore.dto.PictureValue;
import olstore.session.helper.TypeHelperLocal;
import olstore.session.helper.TypeHelperLocalHome;
//import olstore.entity.TypeLocal;
//import olstore.entity.TypeLocalHome;
//import olstore.entity.PropertyLocal;

import olstore.dto.TypeValue;
import olstore.form.CreateTypeForm;
import olstore.framework.EJBHomeFactory;

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


public class TypeSaveAction extends DemoBaseAction {
    
    public ActionForward execute ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        
        try { 
            
            CreateTypeForm createForm = (CreateTypeForm)form;
            TypeValue typeVal = new TypeValue();
            BeanUtils.copyProperties ( typeVal , createForm );
            
            EJBHomeFactory factory = EJBHomeFactory.getInstance();
            TypeHelperLocalHome typeHelperHome = (TypeHelperLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE_HELPER );
            TypeHelperLocal typeHelper = typeHelperHome.create();
            
            typeHelper.saveType ( typeVal );
            
            ActionMessages messages = new ActionMessages();
            ActionMessage msg = new ActionMessage("type.save.success", 
                    typeVal.getName() );
            messages.add("success", msg);
            saveMessages(request, messages);
            
            return mapping.findForward ( "updateType" );
        } 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()));
        }
        
    }
    
}
