001: /**
002: * Copyright 2006 Webmedia Group Ltd.
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: **/package org.araneaframework.uilib.form.constraint;
016:
017: import java.util.Collection;
018: import java.util.HashSet;
019: import java.util.Set;
020: import org.araneaframework.Environment;
021: import org.araneaframework.framework.LocalizationContext;
022: import org.araneaframework.uilib.ConfigurationContext;
023: import org.araneaframework.uilib.form.Constraint;
024:
025: /**
026: * Base class for constraints. A {@link org.araneaframework.uilib.form.Constraint}
027: * operates on the form elements or forms providing means to constrain their content.
028: *
029: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
030: */
031: public abstract class BaseConstraint implements java.io.Serializable,
032: Constraint {
033: private Environment environment;
034: protected String customErrorMessage;
035: private Set errors;
036:
037: //*********************************************************************
038: //* PUBLIC METHODS
039: //*********************************************************************
040:
041: public boolean validate() throws Exception {
042: clearErrors();
043:
044: validateConstraint();
045:
046: //Putting custom message only
047: if (errors != null && !errors.isEmpty()
048: && customErrorMessage != null) {
049: clearErrors();
050: addError(customErrorMessage);
051: }
052:
053: return isValid();
054: }
055:
056: /**
057: * Returns whether the constraint is satisfied/valid. Constraint is valid
058: * when no validation errors were produced.
059: */
060: public boolean isValid() {
061: //XXX: should it throw NotValidatedYetException if called before validation
062: return errors == null || errors.size() == 0;
063: }
064:
065: public Set getErrors() {
066: if (errors == null)
067: errors = new HashSet();
068: return errors;
069: }
070:
071: public void clearErrors() {
072: errors = null;
073: }
074:
075: public void setCustomErrorMessage(String customErrorMessage) {
076: this .customErrorMessage = customErrorMessage;
077: }
078:
079: public void setEnvironment(Environment environment) {
080: // allow setting of constraint environment only once
081: if (this .environment == null)
082: this .environment = environment;
083: }
084:
085: public Environment getEnvironment() {
086: return environment;
087: }
088:
089: //*********************************************************************
090: //* PROTECTED METHODS
091: //*********************************************************************
092:
093: protected void addError(String error) {
094: getErrors().add(error);
095: }
096:
097: protected void addErrors(Collection errorList) {
098: getErrors().addAll(errorList);
099: }
100:
101: protected ConfigurationContext getConfiguration() {
102: return (ConfigurationContext) getEnvironment().getEntry(
103: ConfigurationContext.class);
104: }
105:
106: protected String t(String key) {
107: LocalizationContext locCtx = (LocalizationContext) getEnvironment()
108: .getEntry(LocalizationContext.class);
109: return locCtx.localize(key);
110: }
111:
112: //*********************************************************************
113: //* ABSTRACT IMPLEMENTATION METHODS
114: //*********************************************************************
115:
116: /**
117: * This method should validate the constraint conditions adding error messages
118: * and add messages about unsatisfied conditions.
119: */
120: protected abstract void validateConstraint() throws Exception;
121: }
|