/*
 * Created on 2004-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.startup;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.RuleSetBase;
import org.xml.sax.Attributes;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class WorkflowRuleSet extends RuleSetBase
{

	// ----------------------------------------------------- Instance Variables

	/**
	 * The matching pattern prefix to use for recognizing our elements.
	 */
	protected String prefix = null;

	// ------------------------------------------------------------ Constructor

	/**
	 * Construct an instance of this <code>RuleSet</code> with the default
	 * matching pattern prefix.
	 */
	public WorkflowRuleSet()
	{

		this("");

	}

	/**
	 * Construct an instance of this <code>RuleSet</code> with the specified
	 * matching pattern prefix.
	 *
	 * @param prefix Prefix for matching pattern rules (including the
	 *  trailing slash character)
	 */
	public WorkflowRuleSet(String prefix)
	{

		super();
		this.namespaceURI = null;
		this.prefix = prefix;

	}

	// --------------------------------------------------------- Public Methods

	/**
	 * <p>Add the set of Rule instances defined in this RuleSet to the
	 * specified <code>Digester</code> instance, associating them with
	 * our namespace URI (if any).  This method should only be called
	 * by a Digester instance.</p>
	 *
	 * @param digester Digester instance to which the new Rule instances
	 *  should be added.
	 */
	public void addRuleInstances(Digester digester)
	{
		digester.addCallMethod(prefix + "workflow-app/display-name","setDisplayName", 0);
																	 
		digester.addCallMethod(prefix + "workflow-app/init-param", "addInitParameter", 2);
		digester.addCallParam(prefix + "workflow-app/init-param/param-name", 0);
		digester.addCallParam(prefix + "workflow-app/init-param/param-value", 1);

		digester.addCallMethod(prefix + "workflow-app/factory-entry", "setFactory", 2);
		digester.addCallParam(prefix + "workflow-app/factory-entry/entry-name", 0);
		digester.addCallParam(prefix + "workflow-app/factory-entry/entry-value", 1);
		
		digester.addCallMethod(prefix + "workflow-app/conext-config/backgroundProcessorDelay","setBackgroundProcessorDelay", 1,new Class[] { Integer.TYPE });
		digester.addCallParam(prefix + "workflow-app/conext-config/backgroundProcessorDelay", 0);

		digester.addCallMethod(prefix + "workflow-app/conext-config/autoDeploy","setAutoDeploy", 1,new Class[] { Boolean.TYPE });
		digester.addCallParam(prefix + "workflow-app/conext-config/autoDeploy", 0);
		
		digester.addCallMethod(prefix + "workflow-app/conext-config/reloadable","setReloadable", 1,new Class[] { Boolean.TYPE });
		digester.addCallParam(prefix + "workflow-app/conext-config/reloadable", 0);
																			 
		digester.addObjectCreate(prefix + "workflow-app/store-entry","com.zosatapo.commons.store.Store");
		digester.addCallMethod(prefix + "workflow-app/store-entry/entry-type", "setType", 0);
		digester.addCallMethod(prefix + "workflow-app/store-entry/entry-user", "setUserName", 0);
		digester.addCallMethod(prefix + "workflow-app/store-entry/entry-password", "setPassword", 0);
		digester.addCallMethod(prefix + "workflow-app/store-entry/entry-url", "setURL", 0);
		digester.addCallMethod(prefix + "workflow-app/store-entry/entry-driver","setDriverClass",0);

		digester.addRule(prefix + "workflow-app/store-entry/entry-key", new SetStoreRule());
	}
}
class SetStoreRule extends Rule
{
	/**
	 * The storeKey text collected from this element.
	 */
	protected String storeKey = null;

	/**
	 * Process the start of this element.
	 *
	 * @param attributes The attribute list for this element
	 */
	public void begin(Attributes attributes) throws Exception
	{
	}

	/**
	 * Process the body text of this element.
	 *
	 * @param bodyText The body text of this element
	 */
	public void body(String bodyText) throws Exception
	{
		this.storeKey = bodyText.trim();
	}

	/**
	 * Process the end of this element.
	 */
	public void end() throws Exception
	{
		Object store = digester.peek(0);
		Object config = digester.peek(1);
		
		//Call the specified method
		Class paramTypes[] = new Class[2];
		paramTypes[0]=java.lang.String.class;
		paramTypes[1]=store.getClass();
		MethodUtils.invokeMethod(config, "addStore",new Object[]{ storeKey,store }, paramTypes);
		
	}
}
