All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jharks.workflow.rules.QueuePeekRule Maven / Gradle / Ivy

package org.jharks.workflow.rules;

import java.util.ArrayList;
import java.util.List;

import org.jharks.workflow.Rule;
import org.jharks.workflow.RuleCondition;
import org.jharks.workflow.SessionHistory;
import org.jharks.workflow.Stackable;
import org.jharks.workflow.WorkFlowConstants;


/**
 * The rule will examine the queue on the SessionHistory
 * and return true if an item is in queue and meets any configured 
 * conditions.  If no conditions are set the rule will apply as long
 * as the queue is not empty.
 * 
 * @author Michael J. Rizzo
 */
public class QueuePeekRule extends Rule implements WorkFlowConstants {

    /** Conditions available on rule instance. */
    private ArrayList conditions;
    
    /** Default public constructor that initializes conditions list. */
    public QueuePeekRule() {
        conditions = new ArrayList();
    }
    
    /**
     * Checks rule properties against supplied input to see if 
     * rule must be applied.
     * @see org.jharks.workflow.Rule#checkRule(java.lang.Object)
     */
    public boolean checkRule(Object input)  {
        
        if (input instanceof SessionHistory) {
          SessionHistory sh = (SessionHistory) input;
          
          if (sh.isStackEmpty())
              return false;
          
          if (conditions.size() == 0) {
             Stackable item = (Stackable) sh.peek(); 
             if (item != null && item.queueSize() > 0)
             return true; 
          }
          
          for (int i=0; i < conditions.size(); i++) {
            RuleCondition rc = (RuleCondition) conditions.get(i);
            if (! rc.checkCondition(input)) {
                return false;
            } 
          }
          
          Stackable item = sh.peek();
          if (item != null && item.queueSize() > 0)
            return true;
          
          return false;
    
        } else {
            throw new ClassCastException("Expecting SessionHistory Object.");
        }
    }

    /**
     * Add RuleCondition instance to Rule. 
     * @see org.jharks.workflow.Rule#addRuleCondition(
     *      org.jharks.workflow.RuleCondition)
     */
    public void addRuleCondition(RuleCondition rc) {
        conditions.add(rc);
    }
    
    /**
     * Retrieve conditions of the Rule instance.
     * @return the conditions.
     */
    public List getRuleConditions() {
        return conditions;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy