/**
 * 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.Vector;

import javax.servlet.http.*;


import olstore.dto.PropertyValue;
import olstore.form.CreateTypeForm;

import org.apache.struts.action.*;


public class TypeCreateAction 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 {
        
        //Throw an error if it doesn't equal of any of these?
        try {
            String action = ((CreateTypeForm)form).getSubmitType();
            if ( action == null || action.equals("") )  {
                CreateNewType ( mapping, form, request, response );
            } else if ( action.equals ( "updateProperties" ) ) {
                updateProperties ( mapping, form, request, response );
            } else if ( action.equals ( "saveType" ) ) {
                return mapping.findForward ( "saveType" );
            }
            
            return mapping.findForward ( "updateType" );
            
        } catch ( Exception e ) {
            //Logging code here
            ActionErrors errors = new ActionErrors();
            errors.add("error", new ActionError("errors.type.load", e.getMessage() ));
            saveErrors(request, errors);
            // Return to same page 
            return (new ActionForward(mapping.getInput()));
        }
        
    }
    
    
    public void CreateNewType ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        
        // Add a few empty Properties as defaults
        Vector props = new Vector();
        for ( int i = 0; i <5 ; i++ ) {
            props.add ( new PropertyValue() );
        }
        ((CreateTypeForm)form).setProperties ( props );
    }
    
    
    /**
     * This method updates the number of properties 
     * for display. The only validation that should take place in this
     * particular action is that the total number of fields is a valid
     * integer
     */
    public ActionForward updateProperties ( ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        try {
            Vector propValueArr = ((CreateTypeForm)form).getProperties();
            String numPropsStr =  ((CreateTypeForm)form).getNumProps();
            int numProps = Integer.parseInt ( numPropsStr );
            
            // if the number is smaller than the present length, then truncate
            // the existing array
            if (  numProps < propValueArr.size() ) {
                for ( int i = propValueArr.size() -1 ; i > numProps - 1 ; i-- ) {
                    propValueArr.remove ( i );
                }
            }
            
            // If the number is larger, then grow the ArrayList
            if ( numProps > propValueArr.size() ) {
                for ( int i = propValueArr.size() ; i < numProps ; i++ ) {
                    propValueArr.add ( new PropertyValue() );
                }
            }
            
            ((CreateTypeForm)form).setProperties ( propValueArr ) ;
            return mapping.findForward ( "updateType" );
        }
        
        catch (NumberFormatException n){
            ActionErrors errors = new ActionErrors();
            errors.add ("error", new ActionError("errors.not.int", n.getMessage() ));
            saveErrors(request,errors);
            return (new ActionForward(mapping.getInput()));
        }
        
        
        catch (Exception e){
            //Logging code here
            ActionErrors errors = new ActionErrors();
            errors.add("error", new ActionError("errors.item.load", e.getMessage() ));
            saveErrors(request, errors);
            // Return to same page
            return (new ActionForward(mapping.getInput()));
        }
        
        
        
    }
    
}
