View Javadoc

1   package org.kuali.student.common.ui.client.configurable.mvc.multiplicity;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.kuali.student.common.ui.client.configurable.mvc.sections.GroupSection;
8   
9   public class SwapCompositeCondition {
10      private String conditionId;
11      private CompositeConditionOperator op;
12      private List<SwapCondition> childrenConditions;
13      private List<SwapCompositeCondition> childrenCompositeConditions;
14      public SwapCompositeCondition(CompositeConditionOperator op) {
15          setOp(op);
16      }
17      public CompositeConditionOperator getOp() {
18          return op;
19      }
20      public void setOp(CompositeConditionOperator op) {
21          this.op = op;
22      }
23      public List<SwapCondition> getChildrenConditions() {
24          if (childrenConditions == null) {
25              childrenConditions = new ArrayList<SwapCondition>();
26          }
27          return childrenConditions;
28      }
29      public void setChildrenConditions(List<SwapCondition> childrenConditions) {
30          this.childrenConditions = childrenConditions;
31      }
32      public List<SwapCompositeCondition> getChildrenCompositeConditions() {
33          return childrenCompositeConditions;
34      }
35      public void setChildrenCompositeConditions(List<SwapCompositeCondition> childrenCompositeConditions) {
36          this.childrenCompositeConditions = childrenCompositeConditions;
37      }
38      public boolean evaluate(GroupSection section, Map<String, String> helperFieldKeys) {
39          boolean result = false;
40          boolean allSubCompositeConditionsMet = true;
41          boolean allSubConditionsMet = true;
42          if (childrenCompositeConditions != null && !childrenCompositeConditions.isEmpty()) {
43              for (SwapCompositeCondition childCompositeCondition : childrenCompositeConditions) {
44                  boolean subCompositeConditionMet = childCompositeCondition.evaluate(section, helperFieldKeys);
45                  if (op == CompositeConditionOperator.OR) {
46                      // as soon as I find one sub condition that meets criteria the entire condition is met for OR
47                      if (subCompositeConditionMet) {
48                          allSubCompositeConditionsMet = true;
49                          break;
50                      }
51                  } else if (op == CompositeConditionOperator.AND) {
52                      // the entire condition is false once I find at least one sub condition that fails the criteria
53                      if (!subCompositeConditionMet) {
54                          allSubCompositeConditionsMet = false;
55                          break;
56                      }
57                  }
58              }
59          }
60          if (childrenConditions != null && !childrenConditions.isEmpty()) {
61              for (SwapCondition childCondition : childrenConditions) {
62                  boolean subConditionMet = childCondition.evaluate(section, helperFieldKeys);
63                  if (op == CompositeConditionOperator.OR) {
64                      // as soon as I find one sub condition that meets criteria the entire condition is met for OR
65                      if (subConditionMet) {
66                          allSubConditionsMet = true;
67                          break;
68                      }
69                  } else if (op == CompositeConditionOperator.AND) {
70                      // the entire condition is false once I find at least one sub condition that fails the criteria
71                      if (!subConditionMet) {
72                          allSubConditionsMet = false;
73                          break;
74                      }
75                  }
76              }
77          }
78          if (op == CompositeConditionOperator.OR) {
79              result = allSubCompositeConditionsMet || allSubConditionsMet;
80          } else if (op == CompositeConditionOperator.AND) {
81              result = allSubCompositeConditionsMet && allSubConditionsMet;
82          }
83          return result;
84      }
85      public String getConditionId() {
86          return conditionId;
87      }
88      public void setConditionId(String conditionId) {
89          this.conditionId = conditionId;
90      }
91      
92  }