001: /*
002: * Copyright 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;
017:
018: import java.util.List;
019: import java.util.ArrayList;
020: import java.util.Collections;
021:
022: /**
023: * When a validation failure occurs, eventually an ObjectValidationException is
024: * thrown to the caller. This is an unchecked exception that should be
025: * handled by the caller. The exception contains the list of ValidationFailures
026: * that represent the validations that have failed. Ultimately, the failures
027: * should be used to notify the end user (or system).
028: *
029: * @author Shellman, Dan
030: */
031: public class ObjectValidationException extends RuntimeException {
032: protected List failures = new ArrayList();
033: protected List warnings = new ArrayList();
034: protected ValidationException validationException;
035:
036: /**
037: * Default constructor with no pre-defined validation failures.
038: */
039: public ObjectValidationException() {
040: } //end ObjectValidationException()
041:
042: /**
043: * Constructed from an existing ValidationException.
044: *
045: * @param e An existing ValidationException.
046: */
047: public ObjectValidationException(ValidationException e) {
048: failures.addAll(e.getFailures());
049: warnings.addAll(e.getWarnings());
050: validationException = e;
051: } //end ObjectValidationException()
052:
053: /**
054: * Constructor with pre-defined validation failures.
055: *
056: * @param validationFailures The List of ValidationFailures for this exception.
057: */
058: public ObjectValidationException(List validationFailures) {
059: failures.addAll(validationFailures);
060: } //end ObjectValidationException()
061:
062: /**
063: * Constructor with pre-defined validation failures and warnings. Each
064: * List must contain ValidationFailure objects.
065: *
066: * @param validationFailures The List of failures for this exception.
067: * @param validationWarnings The List of warnings for this exception.
068: */
069: public ObjectValidationException(List validationFailures,
070: List validationWarnings) {
071: failures.addAll(validationFailures);
072: warnings.addAll(validationWarnings);
073: } //end ObjectValidationException()
074:
075: /**
076: * Add a failure to this exception. This failure will be appended
077: * to the end of the internal list of failures.
078: *
079: * @param failure The ValidationFailure to add to this exception.
080: */
081: public void addFailure(ValidationFailure failure) {
082: failures.add(failure);
083: } //and addFailure()
084:
085: /**
086: * Add a warning to this exception. This warning will be appended
087: * to the end of the internal list of warnings.
088: *
089: * @param warning The warning to add to this exception.
090: */
091: public void addWarning(ValidationFailure warning) {
092: warnings.add(warning);
093: } //and addWarning()
094:
095: /**
096: * Retrieves the List of ValidationFailures associated with this exception
097: * that represent the failures.
098: *
099: * @return Returns a List of ValidationFailures.
100: */
101: public List getFailures() {
102: return Collections.unmodifiableList(failures);
103: } //end getFailures()
104:
105: /**
106: * Retrieves the List of ValidationFailures associated with this exception
107: * that represent the warnings.
108: *
109: * @return Returns a List of warnings.
110: */
111: public List getWarnings() {
112: return Collections.unmodifiableList(warnings);
113: } //end getWarnings()
114:
115: /**
116: * Retrieves the list of failure messages (List of String objects).
117: *
118: * @return Returns the List of failure messages (as Strings).
119: */
120: public List getFailureMessages() {
121: List list = new ArrayList();
122:
123: for (int i = 0; i < failures.size(); i++) {
124: list
125: .add(((ValidationFailure) failures.get(i))
126: .getMessage());
127: }
128:
129: return list;
130: } //end getFailureMessages()
131:
132: /**
133: * Retrieves the list of warning messages (List of String objects).
134: *
135: * @return Returns the List of warning messages (as Strings).
136: */
137: public List getWarningMessages() {
138: List list = new ArrayList();
139:
140: for (int i = 0; i < warnings.size(); i++) {
141: list
142: .add(((ValidationFailure) warnings.get(i))
143: .getMessage());
144: }
145:
146: return list;
147: } //end getWarningMessages()
148:
149: /**
150: * Provides access to the root ValidationException that was used
151: * to construct this exception. This is interesting in order to get
152: * access to the appropriate stack trace information.
153: *
154: * @return Returns the root ValidationException that generated this exception.
155: */
156: public ValidationException getRootException() {
157: return validationException;
158: } //end getRootException()
159: } //end ObjectValidationException
|