001: /*
002: * Copyright 2006 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.impl;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020:
021: import org.iscreen.Validator;
022:
023: /**
024: * This class represents the JavaBean that's used to evaluate OGNL
025: * expressions. Failure messages and documentation may reference
026: * this JavaBean to reference its properties. Note that certain
027: * properties are certain lifecycles (meaning, they are valid at
028: * certain times). For example, the failure property is only
029: * available during runtime, after a validation has been performed
030: * (and a failure has been discovered). Therefore, it's not "valid"
031: * to reference it within documentation. Note the lifecycles of
032: * each property on the getters/setters.
033: *
034: * @author Shellman, Dan
035: */
036: public class ContextBean {
037: protected Object bean;
038: protected String label;
039: protected Object failure;
040: protected Validator validator;
041: protected Collection fields = new ArrayList();
042: protected Integer index;
043:
044: /**
045: * Default constructor.
046: */
047: public ContextBean() {
048: } //end ContextBean()
049:
050: /**
051: * The JavaBean being validated is set during runtime, prior to
052: * any validators being called (prior to validation).
053: *
054: * @param theBean The JavaBean being validated.
055: */
056: public void setBean(Object theBean) {
057: bean = theBean;
058: } //end setBean()
059:
060: /**
061: * The JavaBean being validated can be retrieved by failure
062: * messages, since the bean is set prior to validation (prior
063: * to any validators being called).
064: *
065: * @return Returns the JavaBean being validated.
066: */
067: public Object getBean() {
068: return bean;
069: } //end getBean()
070:
071: /**
072: * The label is set during runtime, prior to any validators
073: * being called (prior to validation). In fact, the label is
074: * set during configuration of the ValidationService.
075: *
076: * @param theLabel The label associated with a particular validation.
077: */
078: public void setLabel(String theLabel) {
079: label = theLabel;
080: } //end setLabel()
081:
082: /**
083: * The label is available during runtime, prior to any validators
084: * being called (prior to validation). In fact, the label is
085: * set during configuration of the ValidationService.
086: *
087: * @return Returns the label associated with a particular validation.
088: */
089: public String getLabel() {
090: return label;
091: } //end getLabel()
092:
093: /**
094: * The Validator is set during runtime, prior to any
095: * validators being called (prior to validation). It has the
096: * same lifecycle as the label.
097: *
098: * @param theValidator The validator being used.
099: */
100: public void setValidator(Validator theValidator) {
101: validator = theValidator;
102: } //end setValidator()
103:
104: /**
105: * The Validator is available during runtime, prior to
106: * any validators being called (prior to validation). It has
107: * the same lifecycle as the label.
108: *
109: * @return Returns the Validator that is currently being called.
110: */
111: public Validator getValidator() {
112: return validator;
113: } //end getValidator()
114:
115: /**
116: * The failure object, which represents an object associated with
117: * a particular failure (and failure message), is set during
118: * validation, when a validator reports the failure.
119: *
120: * @param theFailure The failure object set by a Validator during
121: * validation (associated with a failure message).
122: */
123: public void setFailure(Object theFailure) {
124: failure = theFailure;
125: } //end setFailure()
126:
127: /**
128: * The failure object, which represents an object associated with
129: * a particular failure (and failure message), is available to
130: * failure messages, as it's set prior to them being evaluated.
131: *
132: * @return Return the failure object set by a Validator during
133: * validation (associated with a failure message).
134: */
135: public Object getFailure() {
136: return failure;
137: } //end getFailure()
138:
139: /**
140: * The fields, which are a collection of OGNL 'getter' expressions meant
141: * to map from the JavaBean (or object) being validated to the validation
142: * bean used by the Validator, are set prior to validation.
143: *
144: * @param theFields The Collection of fields (or OGNL 'getter' expressions).
145: */
146: public void setFields(Collection theFields) {
147: fields.clear();
148: fields.addAll(theFields);
149: } //end setFields()
150:
151: /**
152: * Returns the fields, which are OGNL 'getter' expressions. These are
153: * set prior to validation.
154: *
155: * @return Returns the fields (OGNL 'getter' expressions) being validated.
156: */
157: public Collection getFields() {
158: Collection col = new ArrayList();
159:
160: col.addAll(fields);
161:
162: return col;
163: } //end getFields()
164:
165: /**
166: * Sets the index of this OGNL root. This index is used when a JavaBean/Object
167: * has a property that is some form of array/Collection that is being
168: * validated across. The index represents the particular object within
169: * the array/Collection that is being validated. This property is set
170: * prior to validation of the indexed property (but not necessarily before
171: * the parent is validated).
172: *
173: * @param theIndex The index of the JavaBean property being validated.
174: */
175: public void setIndex(int theIndex) {
176: index = new Integer(theIndex);
177: } //end setIndex()
178:
179: /**
180: * Gets the index of this OGNL root. This index is used when a JavaBean/Object
181: * has a property that is some form of array/Collection that is being
182: * validated across. The index represents the particular object within
183: * the array/Collection that is being validated. If a JavaBean/Object is
184: * NOT being used in this way (such as the parent JavaBean), then this
185: * method will return null (note that null is NOT the same as zero, since
186: * zero is the first index in an array/Collection, where null means that
187: * there is no array/Collection being validated).
188: *
189: * @return Returns the index of the JavaBean property being validated.
190: */
191: public Integer getIndex() {
192: return index;
193: } //end getIndex()
194: } //end ContextBean
|