/*
 * Created on Sep 29, 2005
 * Copyright (c) 2005 OpenRules, Inc. 
 */
package com.openrules.forms.gui.jsp;

import java.util.HashMap;
import com.openrules.forms.gui.impl.TableLayout;

/**
 * An instance of Dialog is automatically created for every user's session
 * inside JspFormsSession constructor. This instance is available from an
 * JspFormsSession using getDialog() getter. A user can use standard put/get
 * methods of a HashMap to add/receive business objects to the dialog - see
 * example in index.jsp for HelloOpenRulesWeb sample. By default, the dialog 
 * object is passed to the "html" and/or "body" methods as the first parameter.
 * 
 * Dialog allows a user to control the current state of user interaction. 
 * Initially it defines only the initial dialog step "Init" with a hard-coded
 * ID "Initial Step". A user can use processing flow rules to define the next
 * step and an associated TableLayout to be displayed next. The initial action
 * has a hard-codded ID "Init". All other actions and steps/layouts should be
 * defined by a user. The attribute "errors" can be used to check the number
 * of errors during a request processing. For example, if the were errors
 * inside rules that validate a user's input, the proper rules can use
 * dialog.addError() method to increase number of errors and then if 
 * dialog.getErrors()>0 leave the next step the same as current.
 *
 */
public class Dialog extends HashMap {

	String current;
	String next;
	TableLayout nextLayout;
	int errors;
	String status;
	String lastAction;
	
	static final String INITIAL_STEP = "InitialStep";
	static final String INITIAL_ACTION = "Init";
	
	public Dialog() {
		current = INITIAL_STEP;
		next = "?";
		errors = 0;
		status = "";
		lastAction = INITIAL_ACTION;
	}
	
	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}
	
	public int getErrors() {
		return errors;
	}
	
	public void setErrors(int n) {
		errors = n;
	}
	
	public void addError() {
		errors += 1;
	}

	public String getLastAction() {
		return lastAction;
	}

	public void setLastAction(String lastAction) {
		this.lastAction = lastAction;
	}

	public String getNext() {
		return next;
	}

	public void setNext(String next) {
		this.next = next;
	}
	
	public TableLayout getNextLayout() {
		return nextLayout;
	}
	
	public void setNextLayout(TableLayout layout) {
		this.nextLayout = layout;
	}
	
	/**
	 * @return Returns the status.
	 */
	public String getStatus() {
		return status;
	}
	/**
	 * @param status The status to set.
	 */
	public void setStatus(String status) {
		this.status = status;
	}
	public static void main(String[] args) {
	}
	
	public boolean isCurrent(String step) {
		return current.equals(step);
	}
	
	public boolean isNext(String step) {
		return next.equals(step);
	}
	
	public boolean isInitial() {
		return isCurrent(INITIAL_STEP);
	}
	
	public boolean isAction(String action) {
		return lastAction.equals(action);
	}
	
}