KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > view > jsp > error > ErrorHelper


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.mvc.view.jsp.error;
8
9
10 import javax.servlet.jsp.PageContext JavaDoc;
11
12 import com.inversoft.verge.mvc.model.ModelResolution;
13 import com.inversoft.verge.mvc.view.jsp.model.ModelResolutionRegistry;
14 import com.inversoft.verge.util.RequestContext;
15
16
17 /**
18  * <p>
19  * This is a toolkit class that provides methods to help
20  * handle error messages.
21  * </p>
22  *
23  * @author Brian Pontarelli
24  * @since 2.0
25  * @version 2.0
26  */

27 public class ErrorHelper {
28
29     /**
30      * Static class
31      */

32     private ErrorHelper() {
33         // Empty
34
}
35
36
37     /**
38      * This is a hybrid method. This first checks the given property to see if
39      * the request context contains PropertyErrors for that property. If none are
40      * found, this tries to determine, using the PageContext, if the property
41      * String is a model definition, which contains a page variable name rather
42      * then the name that the bean is stored under in the scope (request, session,
43      * application). It does this by converting the first name (up to the first
44      * . character) from the page name to the scope name. This is done by locating
45      * the MetaData for the bean and retrieving the id from the meta data.
46      *
47      * @param property The property name to check
48      * @param context The RequestContext to check for PropertyErrors
49      * @param pageContext To use if the property name returns no results
50      * @return True if the RequestContext contains a PropertyError, false
51      * otherwise
52      */

53     public static boolean hasPropertyErrors(String JavaDoc property, RequestContext context,
54             PageContext JavaDoc pageContext) {
55         return (findPropertyErrors(property, context, pageContext) != null);
56     }
57
58     /**
59      * This is a hybrid method. This first checks the given property to see if
60      * the request context contains PropertyErrors for that property. If none are
61      * found, this tries to determine, using the PageContext, if the property
62      * String is a model definition, which contains a page variable name rather
63      * then the name that the bean is stored under in the scope (request, session,
64      * application). It does this by converting the first name (up to the first
65      * . character) from the page name to the scope name. This is done by locating
66      * the MetaData for the bean and retrieving the id from the meta data.
67      *
68      * @param property The property name to check
69      * @param context The RequestContext to check for PropertyErrors
70      * @param pageContext To use if the property name returns no results
71      * @return The property name the property errors were found for or null if
72      * none were found
73      */

74     public static String JavaDoc findPropertyErrors(String JavaDoc property, RequestContext context,
75             PageContext JavaDoc pageContext) {
76
77         if (!context.hasPropertyErrors(property)) {
78
79             // Try looking up the meta data for the property and converting
80
// to the scope ID of the bean and retrying
81
int index = property.indexOf(".");
82             if (index == -1) {
83                 return null;
84             }
85
86             ModelResolution mr = ModelResolutionRegistry.lookup(
87                 property.substring(0, index), pageContext);
88             if (mr == null) {
89                 return null;
90             }
91
92             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
93             buf.append(mr.getMetaData().getID()).append(".");
94             buf.append(property.substring(index + 1));
95             property = buf.toString();
96             if (!context.hasPropertyErrors(property)) {
97                 return null;
98             }
99         }
100
101         return property;
102     }
103 }
Popular Tags