/** Filename: AbstractRequestHandler.java
* The AbstractRequestHandler is derived from HttpServlet and it implements
* the service method. This object will handle the incoming request, run the
* RuleEngine on the incoming data and then send a message to its derived self
* indicating either success or failure of the action.
*
*/
public class AbstractRequestHandler extends HttpServlet {
// Request Handler as defined by HttpServlet. Notice that we have made this
// final since we dont want any of the derived classes to actually implement a
// service() method. Instead, control will be passed on to them using the two
// methods defined below.
public final void Service (ServletRequest request, ServletResponse response)
{
// Get the unique identifier for the screen.
String ScreenID = getParameter("screenid");
// Construct a hash table containing the key value pairs of the UI fields
// and the user entered values. This has to be passed to the RulesEngine
// With the right screen ID.
HashMap UIFields;
RuleEngine REngine = RuleEngine.getInstance();
REngine.Validate(ScreenID, UIFields);
}
// This method will be called by the service() method implementation of this
// class if the rule engine returns success for all validations.
protected abstract void ValidationSuccess(ServletRequest request,
ServletResponse response);
// This method is called by the service() method if the RuleEngine returns a
// validation failure for one of the rules.
protected abstract void ValidationFailed(servletRequest request,
ServletResponse response, Rule ruleFailed);
}
/** Filename: Rule.java
* This is the abstract interface that represents a validation Rule. This
* interface will be used extensively by the RuleEngine to handle UI
* validations
*/
public interface Rule {
public boolean checkValid();
}
/** Filename: RuleEngine.java
* The RuleEngine will handle all the rule validations. The sequence of
* validation is defined by the RuleSequence that the engine obtains from the
* RuleStore.
*/
public class RuleEngine {
/** Construct a RuleEngine. This class would typically be a singleton
* since there is no reason to have more than one RuleEngine
*/
public RuleEngine()
{
}
/** The actual validate method that is the entry point for all the users of
* the class. This method takes the screen ID for which validation has to
* be done and validates the data that has been passed in. The return
* value represents the results of the validation that can be used for
* error reporting if there is an validation error.
*/
public ValidationResult validate(String ScreenId, HashMap UIData)
{
//Assume validation is successful before starting on validation
ValidationResult ValResult = new ValidationResult(true);
//Get the RuleStore singleton object.
RuleStore TheStore = RuleStore.getInstance();
//Get the Rule sequences for the screen.
RuleSequence TheSequence = TheStore.getRules(ScreenId);
//For a given field find out the sequence of rules to be applied
Rule RuleInSeq[] = TheSequence.getRules(UIData.key());
//Apply the rules
for(int i =0;i<RuleInSeq.length;i++)
{
if (false == RuleInSeq[i].checkValid(UIData.value()))
{
//If a rule failed, set the result object with the necessary
//information
ValResult.setStatus(false, RuleInSeq[i]);
break;
}
}
return ValResult;
}
}