package com.wisdom.contentment.om.tools;

// JDK classes
import java.util.*;
import java.math.*;

// Turbine classes
import org.apache.turbine.om.*;
import org.apache.turbine.util.*;
import org.apache.turbine.util.db.*;
import org.apache.turbine.services.pull.*;
import org.apache.turbine.services.cache.*;

// Object Model classes
import com.wisdom.contentment.om.*;


/** 
 * This class was autogenerated by Torque on: [Wed Sep 19 12:45:07 CEST 2001]
 * <p>
 * It is the abstract superclass of com.wisdom.contentment.om.tools.DefaultQueryTool,
 * which has also been autogenerated. Use that class as a Turbine
 * Pull Service Tool by compiling it and adding it to the Pull Service
 * section of the TurbineResources.properties file like this:
 * <p>
 * <pre>
 * tool.request.query=com.wisdom.contentment.om.tools.DefaultQueryTool
 * </pre>
 */
public abstract class BaseDefaultQueryTool implements ApplicationTool {

  protected RunData data;

  /**
   * Initialize the DefaultQueryTool object.
   *
   * @param data This is null, RunData or User depending upon specified tool scope.
   */
  public void init(Object runData) {
    data = (RunData) runData;
  }

  /**
   * This lets the tool know that it should be
   * refreshed. The tool can perform whatever actions
   * are necessary to refresh itself. This is necessary
   * for sane development where you probably want the
   * tools to refresh themselves on every request.
   */
  public void refresh() {
  }

    
  /**
   * Get all Categories
   */
  public List getAllCategories() throws Exception {
    return getAllCategories(new Criteria());
  }
  
  /**
   * Get all Categories
   */
  protected List getAllCategories(Criteria c) throws Exception {
    return CategoryPeer.doSelect(c);
  }

  /**
   * Get Category by primary key
   */
  protected Category getCategory(ObjectKey id) {
    Category ref = null;

    try {ref = CategoryPeer.retrieveByPK(id);} 
    catch (Exception e) {}

    return ref;
  }

  /**
   * Get Category by id stored in the context.
   */
  public Category getCategoryFromContextInfo() throws Exception {
    return getCategory(new NumberKey(data.getParameters().getString("id", "")));
  }

    
  /**
   * Get all Items
   */
  public List getAllItems() throws Exception {
    return getAllItems(new Criteria());
  }
  
  /**
   * Get all Items
   */
  protected List getAllItems(Criteria c) throws Exception {
    return ItemPeer.doSelect(c);
  }

  /**
   * Get Item by primary key
   */
  protected Item getItem( NumberKey id, NumberKey categoryId) {
    Item ref = null;

    try {ref = ItemPeer.retrieveByPK( id, categoryId);} 
    catch (Exception e) {}

    return ref;
  }

  /**
   * Get Item by id stored in the context.
   */
  public Item getItemFromContextInfo() throws Exception {
    ParameterParser parameters = data.getParameters();

    NumberKey key1 = new NumberKey(parameters.getString("id", ""));
    NumberKey key2 = new NumberKey(parameters.getString("categoryId", ""));
    
    return getItem( key1, key2);
  }
  
  
  
}
