001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.actions.rules;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: import org.openharmonise.vfs.*;
025:
026: /**
027: * Rule that groups other rules and returns true in either an
028: * <code>AND</code> or <code>OR</code> mode.
029: *
030: * @author Matthew Large
031: * @version $Revision: 1.1 $
032: *
033: */
034: public class RuleGroup implements EnableRule {
035:
036: boolean m_bComparator = true;
037:
038: /**
039: * Rules in this rule group.
040: */
041: private ArrayList m_aEnableRules = new ArrayList();
042:
043: /**
044: * Group type.
045: */
046: private String m_sGroupingType = "AND";
047:
048: /**
049: * Group type identifier for OR groups.
050: */
051: public static String GROUPTYPE_OR = "OR";
052:
053: /**
054: * Group type identifier for AND groups.
055: */
056: public static String GROUPTYPE_AND = "AND";
057:
058: /**
059: *
060: */
061: public RuleGroup() {
062: super ();
063: }
064:
065: /* (non-Javadoc)
066: * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#isEnabled(com.simulacramedia.vfs.VirtualFile)
067: */
068: public boolean isEnabled(VirtualFile vfFile) {
069: boolean bEnabled = true;
070:
071: if (vfFile != null) {
072: if (this .m_sGroupingType.equals(RuleGroup.GROUPTYPE_AND)) {
073: Iterator itor = this .m_aEnableRules.iterator();
074: while (itor.hasNext()) {
075: if (!((EnableRule) itor.next()).isEnabled(vfFile)) {
076: bEnabled = false;
077: }
078: }
079: } else {
080: bEnabled = false;
081: Iterator itor = this .m_aEnableRules.iterator();
082: while (itor.hasNext()) {
083: if (((EnableRule) itor.next()).isEnabled(vfFile)) {
084: bEnabled = true;
085: }
086: }
087: }
088: }
089:
090: return this .m_bComparator == bEnabled;
091: }
092:
093: /**
094: * Adds a rule to this rule group.
095: *
096: * @param rule Rule to add
097: */
098: public void addEnableRule(EnableRule rule) {
099: this .m_aEnableRules.add(rule);
100: }
101:
102: /* (non-Javadoc)
103: * @see com.simulacramedia.contentmanager.actions.rules.EnableRule#setResultComparator(boolean)
104: */
105: public void setResultComparator(boolean bComparator) {
106: this .m_bComparator = bComparator;
107: }
108:
109: /**
110: * Sets the group type. This can be either <code>AND</code> or
111: * <code>OR</code>. An <code>AND</code> group will return true
112: * if all of the rules it contains return true. An <code>OR</code>
113: * group will return true if any of the rules it contains return true.
114: *
115: * @param sGroupType Group type
116: * @see RuleGroup#GROUPTYPE_AND
117: * @see RuleGroup#GROUPTYPE_OR
118: */
119: public void setGroupType(String sGroupType) {
120: if (sGroupType.equals(RuleGroup.GROUPTYPE_AND)
121: || sGroupType.equals(RuleGroup.GROUPTYPE_OR)) {
122: this.m_sGroupingType = sGroupType;
123: }
124: }
125:
126: }
|