package hmi.util.validation;

import org.apache.struts.action.*;
import java.util.*;

/**
 * This object is used for Struts 1.0 compatibility.
 *
 * Struts 1.0 has deprecated the widely-used ErrorMessages object in favor of
 * ActionErrors.  To avoid massive code rewrites, this object re-implements
 * the ErrorMessages object by deriving from ActionErrors and implementing the
 * old ErrorMessages interface in terms of the underlying base class.
 *
 * Those of you into GOF may recognize the Facade design pattern.
 *
 * @author:  Richard Robbins
 */

public class ErrorMessages extends ActionErrors {

  /**
   * Implementation of Struts V0.5 "ErrorMessages.getSize" method
   */
  public int getSize() {
    return size();
  }

/**
 * Implementation of Struts V0.5 "ErrorMessages.addError" method.
 */
  public void addError( String errorKey) {
    add(GLOBAL_ERROR,  new ActionError( errorKey));
  }

/**
 * Implementation of Struts V0.5 "ErrorMessages.getErrors" method.
 */
  public String[] getErrors() {
    Iterator i = get();
    ArrayList allKeys = new ArrayList();

    while (i.hasNext()) {
      allKeys.add(((ActionError)i.next()).getKey());
    }

    return (String [])allKeys.toArray();
  }

/**
 * Implementation of Struts V0.5 "ErrorMessages.getError" method.
 */
  public String getError( int whichOne) {
    Iterator i = get();
    int curError = 0;
    String theKey;

    while (i.hasNext()) {
      if (curError++ == whichOne)
        return (((ActionError)i.next()).getKey());
    }
    return "not found";
  }
}