VendorFormAction.java
/*
 *  Copyright 2006 - 2012 Hannes Holtzhausen
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.

 */

/*
 * $Id: VendorFormAction.java,v 1.2 2007/11/01 22:53:15 hannes Exp $
 *
 * Description: 
 *
 */

package app.web;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import toolbox.web.*;
import toolbox.services.*;
import toolbox.web.validation.*;
import app.beans.*;
import app.services.*;

/**
 * Handles all vendor form requests.
 *
 @author Hannes Holtzhausen
 */

public class VendorFormAction extends BaseWebAction implements WebAction
{
  /**
   * Default constructor
   */

  public VendorFormAction()
  {
  }


  /**
   * Receive requests to add or update vendor information and attempts
   * to perform the operation.
   *
   @param  request   HTTP request.
   @param  response  HTTP response.
   @param  context   Web application context. 
   *
   @throws  ServletException 
   @throws  IOException 
   */

  public void execute(HttpServletRequest request,
                      HttpServletResponse response,
                      ServletContext contextthrows ServletException,
                                                     IOException
  {
    ProductService service = null;

    try
    {
      ServiceRegistry registry = 
                               ServiceRegistryFactory.getRegistry("beansdemo");

      service = 
       (ProductService)registry.getService(registry.getProperty("default_svc"),
                                           ProductService.class);

      String msg = null;
      String action = request.getParameter("vendor_action");

      Map map = getValidator().validate("vendor",request);
      Vendor vendor = Vendor.create(map);

      if(action.equals("add"))
      {
        service.addVendor(vendor);
        msg = getResourceString("generic.addmessage",
                                new Object[] { vendor.getName() }
                                request.getLocale());
      }
      else
      {
        service.updateVendor(vendor);
        msg = getResourceString("generic.updatemessage",
                                new Object[] { vendor.getName() }
                                request.getLocale());
      }

      request.setAttribute("message",msg);
      dispatch(request,response,context,getSuccessTemplate());
    }
    catch (ValidationException ve)
    {
      request.setAttribute("errors",ve.getMessages());
      dispatch(request,response,context,getFailureTemplate());
    }
    catch (ServiceException ae)
    {
      request.setAttribute("message",ae.getMessage());
      dispatch(request,response,context,getFailureTemplate());
    }
    catch (Exception e)
    {
      request.setAttribute("message",e.getMessage());
      dispatch(request,response,context,getFailureTemplate());
    }
  }
}