View Javadoc

1   /*
2    * $Id: NestedValidationActivationRule.java,v 1.2 2004/02/27 19:53:47 eelco12 Exp $
3    * $Revision: 1.2 $
4    * $Date: 2004/02/27 19:53:47 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  
32  package nl.openedge.baritus.validation.impl;
33  
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  import nl.openedge.baritus.FormBeanContext;
39  import nl.openedge.baritus.validation.ValidationActivationRule;
40  
41  import org.infohazard.maverick.flow.ControllerContext;
42  
43  /***
44   * You can use a list of validators while registering just one by using
45   * NestedValidationActionRule.
46   * 
47   * @author Eelco Hillenius
48   */
49  public class NestedValidationActivationRule implements ValidationActivationRule
50  {
51  
52  	private List rules = null;
53  
54  	/***
55  	 * allows validation if all nested rules return true.
56  	 * @param cctx controller context
57  	 * @param formBeanContext form bean context
58  	 * @return boolean if all nested rules return true, false if one of them did not
59  	 * 
60  	 * @see nl.openedge.baritus.validation.ValidationActivationRule#allowValidation(org.infohazard.maverick.flow.ControllerContext, nl.openedge.baritus.FormBeanContext)
61  	 */
62  	public boolean allowValidation(
63  		ControllerContext cctx,
64  		FormBeanContext formBeanContext)
65  	{
66  		boolean allow = true;
67  		
68  		if(rules != null)
69  		{
70  			for(Iterator i = rules.iterator(); i.hasNext(); )
71  			{
72  				ValidationActivationRule nested = (ValidationActivationRule)i.next();
73  				allow = nested.allowValidation(cctx, formBeanContext);
74  				
75  				if(!allow) break;
76  			}
77  		}
78  		
79  		return allow;
80  	}
81  	
82  	/***
83  	 * add a validation activation rule to the end of the list.
84  	 * @param rule validation activation rule to be added
85  	 */
86  	public void addRule(ValidationActivationRule rule)
87  	{
88  		if(rules == null)
89  		{
90  			rules = new ArrayList();
91  		}
92  		rules.add(rule);
93  	}
94  
95  	/***
96  	 * add a validation activation rule to the list at the provided position.
97  	 * 
98  	 * @param index the position in the list at which the provided rule should
99  	 * 	be inserted
100 	 * @param rule validation activation rule to be added
101 	 */
102 	public void addRule(int index, ValidationActivationRule rule)
103 	{
104 		if(rules == null)
105 		{
106 			rules = new ArrayList();
107 		}
108 		rules.add(index, rule);
109 	}
110 	
111 	/***
112 	 * add list of rules to current list of rules.
113 	 * 
114 	 * @param rules list of rules to be added
115 	 */
116 	public void addAllRules(List rules)
117 	{
118 		if(rules == null)
119 		{
120 			rules = new ArrayList();
121 		}
122 		rules.addAll(rules);
123 	}
124 
125 
126 	/***
127 	 * remove a validation activation rule from list.
128 	 * @param rule validation activation rule to be removed
129 	 */
130 	public void removeRule(ValidationActivationRule rule)
131 	{
132 		if(rules != null)
133 		{
134 			rules.remove(rule);
135 		}
136 	}
137 	
138 	/***
139 	 * remove a validation activation rule at the provided position
140 	 * @param index the position at which the rule should be removed
141 	 */
142 	public void removeRule(int index)
143 	{
144 		if(rules != null)
145 		{
146 			rules.remove(index);
147 		}
148 	}
149 	
150 	/***
151 	 * clear the list of rules.
152 	 */
153 	public void clear()
154 	{
155 		if(rules != null)
156 		{
157 			rules.clear();
158 			rules = null;	
159 		}
160 	}
161 
162 
163 	/***
164 	 * get list of validation activation rules.
165 	 * @return List validation activation rules
166 	 */
167 	public List getRules()
168 	{
169 		return rules;
170 	}
171 
172 	/***
173 	 * set list of validation activation rules.
174 	 * @param list validation activation rules
175 	 */
176 	public void setRules(List list)
177 	{
178 		rules = list;
179 	}
180 
181 }