VendorHelper.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: VendorHelper.java,v 1.2 2007/11/01 22:53:15 hannes Exp $
 *
 * Description: 
 *
 */

package app.web.helper;

import java.util.*;
import toolbox.services.*;
import freemarker.template.*;
import app.services.*;

/**
 * Template helper to retrieve vendor information.
 *
 @author  Hannes Holtzhausen
 */

public class VendorHelper implements TemplateMethodModel
{
  /**
   * Default constructor
   */

  public VendorHelper()
  {
  }


  /**
   * Retrieve the vendor information.
   *
   @param  args  List containing arguments. This implementation supports
   *               the following arguments:
   *               <ul>
   *                 <li>id | all: Indicates whether a single vendor 
   *                     should be returned by id or all vendors.</li>
   *                 <li>&lt;id&gt;: If the first argument is "id", then this
   *                     argument must contain the vendor identifier.
   *                 </li>
   *               </ul>
   *
   @return  List containing vendor information.
   *
   @throws TemplateModelException
   */

  public Object exec(List argsthrows TemplateModelException
  {
    ProductService service = null;

    try
    {
      ServiceRegistry registry =
                             ServiceRegistryFactory.getRegistry("beansdemo");
      service = 
        (ProductService)registry.getService(registry.getProperty("default_svc"),
                                            ProductService.class);

      String mode = (String)args.get(0);

      if(mode.equals("all"))
      {
        return service.getVendors();
      }
      else
      {
        Integer id = new Integer((String)args.get(1));
        return service.getVendor(id);
      }
    }
    catch (ProductException pe)
    {
      throw new TemplateModelException(pe);
    }
    catch (ServiceException se)
    {
      throw new TemplateModelException(se);
    }
  }
}