/* Copyright (c) 2003, Oracle Corporation. All rights reserved. */

package oracle.portal.sample.v2.devguide.struts;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This is the handler for updating a customer store with values submitted in
 * a form.
 * <p>
 * This code includes a check for duplicate submissions using a synchroniser
 * token. When the form from which values are submitted is generated, the 
 * Struts framework generates a key or session which it saves in the user
 * session and inserts in the form. The first time a form is submitted the
 * two tokens match indicating that the submission is valid. The token is
 * then removed from the session so that subsequent resubmissions may be 
 * detectd as duplicates.
 */
public class UpdateCustomerStoreAction extends Action 
{
    /**
     * This action adds the bean associated with this action to a 
     * <code>CustomerStore</code> object which is persisted as a session
     * attribute.
     * <p>
     * If there is no current <code>CustomerStore</code> object, one is 
     * created. Persisting data as a session attribute is used to keep the
     * sample code simple, it should not be used in a real-world application.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) 
        throws IOException, ServletException
    {
        // Check for duplicate form submission. This is set in the
        // AddCustomerAction handler class.
        if (!isTokenValid(request, true))
        {
            return mapping.findForward("duplicateSubmission");
        }

        // For the sake of this sample customer details are stored as a class 
        // stored in the session. In a real-world application they should be
        // persisted properly.
        CustomerStore store = (CustomerStore)request.
                             getSession().getAttribute("SAMPLE_CUSTOMER_STORE");
        if (store == null)
        {
            // No customer store object, make one now and use that. Store it 
            // in the session.
            store = new CustomerStore();
            request.getSession().setAttribute("SAMPLE_CUSTOMER_STORE", store);
        }
   
        store.addCustomer((CustomerDetailsActionForm)form); 
        return mapping.findForward("success");
    }
}
