001: /*
002: * Copyright 2006-2007 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.Collections;
020: import java.util.List;
021: import java.util.Locale;
022:
023: import org.iscreen.FailureMessage;
024: import org.iscreen.ValidationFailure;
025:
026: /**
027: * This is the default implementation of the ValidatorContext that's
028: * passed to Validators during validation.
029: *
030: * @author Shellman, Dan
031: */
032: public class DefaultValidatorContext implements
033: InternalValidatorContext {
034: protected List failures = new ArrayList();
035: protected List warnings = new ArrayList();
036: protected ContextBean contextBean;
037: protected Locale locale;
038: protected ValidatorWrapper validator;
039:
040: /**
041: * Default constructor.
042: */
043: public DefaultValidatorContext(ContextBean theRoot, Locale theLocale) {
044: contextBean = theRoot;
045: locale = theLocale;
046: } //end DefaultValidatorContext()
047:
048: public void reportFailure(FailureMessage messageObject) {
049: reportFailure(messageObject, null);
050: } //end reportFailure()
051:
052: public void reportFailure(FailureMessage messageObject,
053: Object failureObject) {
054: ValidationFailure failure;
055:
056: failure = new ValidationFailure();
057:
058: contextBean.setFailure(failureObject);
059: failure.setMessage(messageObject
060: .getMessage(contextBean, locale));
061: failure.setLabel(contextBean.getLabel());
062: failure.setFields(contextBean.getFields());
063: failure.setIndex(contextBean.getIndex());
064: if (validator != null) {
065: failure.setName(validator.getName());
066: }
067:
068: failures.add(failure);
069:
070: contextBean.setFailure(null);
071: } //end reportFailure()
072:
073: public void reportWarning(FailureMessage messageObject) {
074: reportFailure(messageObject, null);
075: } //end reportWarning()
076:
077: public void reportWarning(FailureMessage messageObject,
078: Object failureObject) {
079: ValidationFailure failure;
080:
081: failure = new ValidationFailure();
082:
083: contextBean.setFailure(failureObject);
084: failure.setMessage(messageObject
085: .getMessage(contextBean, locale));
086: failure.setLabel(contextBean.getLabel());
087: failure.setFields(contextBean.getFields());
088: failure.setIndex(contextBean.getIndex());
089: if (validator != null) {
090: failure.setName(validator.getName());
091: }
092:
093: warnings.add(failure);
094:
095: contextBean.setFailure(null);
096: } //end reportWarning()
097:
098: public Locale getLocale() {
099: return locale;
100: } //end getLocale()
101:
102: /**
103: * Retrieves a List of the current failures within this context. This
104: * List contains ValidationFailure objects.
105: *
106: * @return Returns a List of current failures.
107: */
108: public List getFailures() {
109: return Collections.unmodifiableList(failures);
110: } //end getFailures()
111:
112: /**
113: * Retrieves a List of the current warnings within this context. This
114: * List contains ValidationFailure objects.
115: *
116: * @return Returns a List of current warnings.
117: */
118: public List getWarnings() {
119: return Collections.unmodifiableList(warnings);
120: } //end getWarnings()
121:
122: /**
123: * Adds "raw" failures (which is a List of ValidationFailure objects)
124: * to the existing set of failures.
125: *
126: * @param failureList The List of ValidationFailures to add to this context.
127: */
128: public void addRawFailures(List failureList) {
129: failures.addAll(failureList);
130: } //end addRawFailures()
131:
132: /**
133: * Adds "raw" warnings (which is a List of ValidationFailure objects)
134: * to the existing set of warnings.
135: *
136: * @param warningList The List of ValidationFailures to add to this context.
137: */
138: public void addRawWarnings(List warningList) {
139: warnings.addAll(warningList);
140: } //end addRawWarnings()
141:
142: /**
143: * Retrieves the number of current failures in this context.
144: *
145: * @return Returns the number of failures.
146: */
147: public int getFailureCount() {
148: return failures.size();
149: } //end getFailureCount()
150:
151: /**
152: * Retrieves the number of current warnings in this context.
153: *
154: * @return Returns the number of warnings.
155: */
156: public int getWarningCount() {
157: return warnings.size();
158: } //end getWarningCount()
159:
160: /**
161: * Calculates the total failure and warning counts. Basically, returns
162: * the addition of getFailureCount() and getWarningCount() calls.
163: *
164: * @return Returns the total failure and warning counts.
165: */
166: public int getCount() {
167: return failures.size() + warnings.size();
168: } //end getCount()
169:
170: /**
171: * Sets the ValidatorWrapper that the context needs to use.
172: *
173: * @param wrapper The ValidatorWrapper to use.
174: */
175: public void setValidator(ValidatorWrapper wrapper) {
176: validator = wrapper;
177: } //end setValidator()
178: } //end DefaultValidatorContext
|