KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > util > RequestContext


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.util;
8
9
10 import java.io.Serializable JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14
15 import com.inversoft.error.BasicError;
16 import com.inversoft.error.ErrorList;
17 import com.inversoft.error.PropertyError;
18
19
20 /**
21  * <p>
22  * This class is a facade on top of the HttpServletRequest
23  * that provides an interface for handling ErrorMessages.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  * @since 2.0
28  * @version 2.0
29  */

30 public class RequestContext implements Serializable JavaDoc {
31
32     /**
33      * The key that the {@link ErrorList} is stored under in the HttpServletRequest
34      */

35     public static final String JavaDoc ERROR_LIST_KEY = "inversoftErrors";
36
37     /**
38      * The error list
39      */

40     private ErrorList errors;
41
42     /**
43      * The flag if the request is valid. Set by the DefaultModelParser during
44      * MVC execution. Can also be changed by hand
45      */

46     private boolean valid;
47
48
49     /**
50      * Constructs a new <code>RequestContext</code>.
51      */

52     public RequestContext(HttpServletRequest JavaDoc request) {
53         errors = (ErrorList) request.getAttribute(ERROR_LIST_KEY);
54         if (errors == null) {
55             errors = new ErrorList();
56             request.setAttribute(ERROR_LIST_KEY, errors);
57         }
58     }
59
60
61     /**
62      * Adds a new {@link com.inversoft.error.BasicError BasicError} to the
63      * request context.
64      *
65      * @param error The BasicError to add to the request context
66      */

67     public void addError(BasicError error) {
68         errors.addError(error);
69     }
70
71     /**
72      * Adds a new {@link com.inversoft.error.PropertyError PropertyError} to the
73      * request context. PropertyError's are generally used in conjunction with
74      * Model components of the MVC that are invalid. However, PropertyError's may
75      * be used for anything.
76      *
77      * @param error The PropertyError to add to the request context
78      */

79     public void addError(PropertyError error) {
80         errors.addError(error);
81     }
82
83     /**
84      * Returns whether or not the RequestContext contains any errors at all.
85      *
86      * @return True if the RequestContext contains errors, false otherwise
87      */

88     public boolean hasErrors() {
89         return !errors.isEmpty();
90     }
91
92     /**
93      * Returns whether or not the RequestContext contains any {@link
94      * com.inversoft.error.PropertyError PropertyError} objects
95      *
96      * @return True if an error exists, false otherwise
97      */

98     public boolean hasPropertyErrors() {
99         return errors.hasPropertyErrors();
100     }
101
102     /**
103      * Returns whether or not the RequestContext contains any {@link
104      * com.inversoft.error.PropertyError PropertyError} for the given property
105      * name
106      *
107      * @param propertyName The propertyName to check if an error exists for
108      * @return True if an error exists, false otherwise
109      */

110     public boolean hasPropertyErrors(String JavaDoc propertyName) {
111         return errors.hasPropertyErrors(propertyName);
112     }
113
114     /**
115      * Returns a list of all the errors including the {@link
116      * com.inversoft.error.BasicError BasicError} and {@link
117      * com.inversoft.error.PropertyError PropertyError} objects stored in the
118      * RequestContext. This is returned in a list and modifications to this list
119      * DO NOT affect the state of the RequestContext.
120      *
121      * @return A List containing all the Errors. If there are no errors,
122      * an empty List is returned, never null
123      */

124     public List JavaDoc getAllErrors() {
125         return errors.getAllErrors();
126     }
127
128     /**
129      * Returns a list of all the {@link com.inversoft.error.BasicError BasicError}
130      * objects stored in the RequestContext. This is returned in a list and
131      * modifications to this list DO NOT affect the state of the RequestContext.
132      *
133      * @return A List containing all the BasicErrors. If there are no errors,
134      * an empty List is returned, never null
135      */

136     public List JavaDoc getBasicErrors() {
137         return errors.getBasicErrors();
138     }
139
140     /**
141      * Returns a list of all the {@link com.inversoft.error.PropertyError
142      * PropertyError} objects stored in the RequestContext. This is returned in
143      * a list and modifications to this list DO NOT affect the state of the
144      * RequestContext.
145      *
146      * @return A List containing all the PropertyErrors. If there are no errors,
147      * an empty List is returned, never null
148      */

149     public List JavaDoc getPropertyErrors() {
150         return errors.getPropertyErrors();
151     }
152
153     /**
154      * Returns the {@link com.inversoft.error.PropertyError PropertyError} object
155      * that relates to the property given.
156      *
157      * @param property The name of the property whose errors to retrieve
158      * @return The PropertyError for the given property or null if one does not
159      * exist
160      */

161     public List JavaDoc getPropertyErrors(String JavaDoc property) {
162         return errors.getPropertyErrors(property);
163     }
164
165     /**
166      * Returns whether or not the current request is valid. This is normally used
167      * to indicate that the model objects were all successfully validated.
168      *
169      * @return True if the request is valid, false otherwise
170      */

171     public boolean isValid() {
172         return valid;
173     }
174
175     /**
176      * Updates whether or not the request was valid or not. This is normally used
177      * to indicate that the model objects were all successfully validated.
178      *
179      * @param valid True if the request is valid, false otherwise
180      */

181     public void setValid(boolean valid) {
182         this.valid = valid;
183     }
184
185     /**
186      * Clears out all the errors from the RequestContext.
187      */

188     public void clear() {
189         errors.clear();
190     }
191 }
Popular Tags