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.List;
019: import java.util.Locale;
020: import org.iscreen.DocumentationIterator;
021: import org.iscreen.ObjectValidationException;
022:
023: import org.iscreen.ValidationException;
024: import org.iscreen.ValidationService;
025:
026: /**
027: * This is the base class implementation of the ValidationService interface.
028: *
029: * @author Shellman, Dan
030: */
031: public abstract class BaseValidationService implements
032: ValidationService {
033: protected String id;
034: protected Locale defaultLocale;
035:
036: /**
037: * Constructor taking unique id. The unique id is required. In addition,
038: * the default locale is provided.
039: *
040: * @param uniqueId The unique id for this service.
041: * @param locale The default locale for this service.
042: */
043: public BaseValidationService(String uniqueId, Locale locale) {
044: id = uniqueId;
045: defaultLocale = locale;
046: } //end BaseValidationService()
047:
048: public void validate(Object obj) throws ValidationException {
049: validate(obj, defaultLocale);
050: } //end validate()
051:
052: public void validate(Object obj, Locale locale)
053: throws ValidationException {
054: ContextBean contextBean;
055: DefaultValidatorContext context;
056: ValidatorWrapper wrapper;
057: List failures;
058:
059: //Basic algorithm:
060: //Loop through validators and perform the following:
061: // -Initialize an ContextBean and ValidatorContext
062: // -Call the validator wrapper's validate() method with context and bean
063: // -Store any validation failures
064: //Once done looping through validators, if there are more than zero
065: //validation failures, create a ValidationException, stuff the validation
066: //failures in there, and throw it. Otherwise, return.
067: contextBean = constructContextBean(obj);
068: context = constructContext(contextBean, locale);
069:
070: resetValidatorIteration();
071: wrapper = getNextValidator();
072: while (wrapper != null) {
073: boolean continueFlag;
074:
075: context.setValidator(wrapper);
076: continueFlag = wrapper.validate(context, contextBean, obj);
077: if (continueFlag == false) {
078: break;
079: }
080:
081: wrapper = getNextValidator();
082: }
083:
084: //Finally, check to see if there were any failures.
085: if (context.getCount() > 0) {
086: throw new ValidationException(context.getFailures(),
087: context.getWarnings());
088: }
089: } //end validate()
090:
091: public void validateObject(Object obj, Locale locale) {
092: try {
093: validate(obj, locale);
094: } catch (ValidationException e) {
095: throw new ObjectValidationException(e);
096: }
097: } //end validateObject()
098:
099: public void validateObject(Object obj) {
100: validateObject(obj, defaultLocale);
101: } //end validateObject()
102:
103: public String getServiceName() {
104: return id;
105: } //end getServiceName()
106:
107: /**
108: * Retrieves an iterator of String values representing the documentation
109: * configured for this validation service (the underlying validation set).
110: *
111: * @return Returns an iterator of Strings representing documentation.
112: */
113: public DocumentationIterator getDocumentation() {
114: DocumentationIterator docIt = new DocumentationIterator();
115: ValidatorWrapper wrapper;
116:
117: resetValidatorIteration();
118: wrapper = getNextValidator();
119: while (wrapper != null) {
120: docIt.addIterator(wrapper.getDoc());
121:
122: wrapper = getNextValidator();
123: }
124:
125: return docIt;
126: } //end getDocumentation()
127:
128: // ***
129: // Protected methods
130: // ***
131:
132: /**
133: * Retrieves the "next" Validator in the set of Validators that this
134: * validation service is configured to use. If there are no more
135: * Validators, then this method should return null. It should return
136: * the first Validator after a call to resetValidatorIteration().
137: *
138: * @return Returns the next ValidatorWrapper in the set of validators.
139: */
140: protected abstract ValidatorWrapper getNextValidator();
141:
142: /**
143: * Resets the iteration of Validators using the getNextValidator() method.
144: */
145: protected abstract void resetValidatorIteration();
146:
147: /**
148: * This can be over-written by sub-classes, but it's not really necessary,
149: * since this service requires a particular implementation of the
150: * ValidatorContext.
151: *
152: *
153: * @param root The ContextBean that the context needs.
154: * @param locale The locale to use in generating error messages.
155: * @return Returns a constructed and configured DefaultValidatorContext.
156: */
157: protected DefaultValidatorContext constructContext(
158: ContextBean root, Locale locale) {
159: return new DefaultValidatorContext(root, locale);
160: } //end constructContext()
161:
162: /**
163: * This can be over-written by sub-classes, but it's not really necessary,
164: * since this service requires an OGNL root object.
165: *
166: *
167: * @return Returns an instance of ContextBean fully configured.
168: */
169: protected ContextBean constructContextBean(Object objToValidate) {
170: ContextBean root;
171:
172: root = new ContextBean();
173: root.setBean(objToValidate);
174:
175: return root;
176: } //end constructContextBean()
177: } //end BaseValidationService
|