KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > util > WEBFormViewUtil


1 package net.sourceforge.formview.util;
2
3 import java.util.Collection JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import javax.servlet.ServletContext JavaDoc;
9 import javax.servlet.ServletRequest JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11
12 import org.apache.struts.action.ActionErrors;
13 import org.apache.struts.action.ActionMessages;
14
15 import net.sourceforge.formview.FieldView;
16 import net.sourceforge.formview.FormView;
17 import net.sourceforge.formview.FormViewConstants;
18 import net.sourceforge.formview.FormViewResources;
19 import net.sourceforge.formview.permission.IPermissionsAdapter;
20 import net.sourceforge.formview.permission.RolesPermissionsAdapter;
21
22 /**
23  * Description : Utils for using FormView with WEB Application.
24  *
25  * @version 1.0.0
26  * @author <a HREF="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
27  */

28 public class WEBFormViewUtil {
29
30     public static boolean contextPathInitialized = false;
31     
32     /**
33      * Save FormViewResources into Servlet Context.
34      * @param context
35      * @param resources
36      */

37     public static void saveFormViewResources(ServletContext JavaDoc context,FormViewResources resources) {
38         context.setAttribute(FormViewConstants.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY , resources);
39     }
40     
41     /**
42      * Return FormViewResources initialized
43      * and stored into Servlet Context.
44      *
45      * @param context
46      * @return
47      */

48     protected static FormViewResources getFormViewResources(ServletContext JavaDoc context) {
49         FormViewResources resources = (FormViewResources)context.getAttribute(FormViewConstants.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY);
50         if (resources == null) {
51             throw new IllegalArgumentException JavaDoc("Impossible to get FormViewResources into servlet context. Check if you have register FormViewResources into WEB Servlet Context with key \"net.sourceforge.formview.SERVLETCONTEXT_FORMVIEW_RESSOURCES_KEY\" (If you are in Struts check if you have intiliaze Plugin FormViewPlugin correctly).");
52         }
53         return resources;
54     }
55     
56     public static void saveState(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc state) {
57         saveState(request, context, state, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY);
58     }
59     
60     /**
61      * Save State into request.
62      * @param request
63      * @param context
64      * @param state
65      */

66     public static void saveState(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc state, String JavaDoc subFormId) {
67         if (state != null && state.length() > 0) {
68             // Save state into request
69
request.setAttribute(getREQUEST_FORMSTATE_KEY(subFormId), state);
70         }
71         initializeContextPath(request, context);
72      }
73     
74     
75     public static void saveFormView(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc formName) {
76         saveFormView(request, context, formName, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY);
77     }
78     
79     /**
80      * Compute FormView with name <b>formName</b> with state (stored into request) and roles
81      * and save FormView into request.
82      * @param request
83      * @param context
84      * @param formName
85      */

86     public static void saveFormView(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc formName, String JavaDoc subFormId) {
87         FormView formView = getFormViewSaved(request, subFormId);
88         if (formView == null) {
89             FormViewResources resources = getFormViewResources(context);
90             // get form state stored into request
91
String JavaDoc formState = (String JavaDoc)request.getAttribute(getREQUEST_FORMSTATE_KEY(subFormId));
92             // Get formView called formName
93
if (formName != null && formName.length() > 0) {
94                 formView = resources.getFormView(formName, formState);
95                 if (formView != null) {
96                     // Save into Map of FormView
97
Map JavaDoc formViewSavedMap = getFormViewSavedMap(request);
98                     if (formViewSavedMap == null) {
99                         formViewSavedMap = new HashMap JavaDoc();
100                         request.setAttribute(FormViewConstants.REQUEST_FORMVIEW_KEY, formViewSavedMap);
101                     }
102                     // Save formView into Map
103
formViewSavedMap.put(subFormId, formView);
104                 }
105             }
106         }
107         initializeContextPath(request, context);
108         
109     }
110     
111     public static FormView getFormViewSaved(ServletRequest JavaDoc request) {
112         return getFormViewSaved(request, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY);
113     }
114
115     public static FormView getFormViewSaved(ServletRequest JavaDoc request, String JavaDoc subFormId) {
116         Map JavaDoc formViewSavedMap = getFormViewSavedMap(request);
117         if (formViewSavedMap != null)
118             return (FormView)formViewSavedMap.get(subFormId);
119         return null;
120     }
121     
122     public static Map JavaDoc getFormViewSavedMap(ServletRequest JavaDoc request) {
123         return (Map JavaDoc)request.getAttribute(FormViewConstants.REQUEST_FORMVIEW_KEY);
124     }
125     
126     public static void saveFieldView(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc formName, FieldView fieldView) {
127         saveFieldView(request, context, formName, fieldView, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY);
128     }
129             
130     
131     public static void saveFieldView(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc formName, FieldView fieldView, String JavaDoc subFormId) {
132         FormView formView = getFormViewSaved(request, subFormId);
133         if (formView != null) {
134             formView.mergeFieldView(fieldView);
135         }
136     }
137
138     public static void savePermissionAdapter(ServletRequest JavaDoc request, IPermissionsAdapter permissionsAdapter) {
139         // Save permission adapter into request
140
request.setAttribute(FormViewConstants.REQUEST_FORMPERMISSION_KEY, permissionsAdapter);
141     }
142     
143     public static String JavaDoc processHtmlContent(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc htmlContent)
144     {
145         return processHtmlContent(request, context, htmlContent, FormViewConstants.MAP_FORMVIEW_DEFAULT_KEY);
146     }
147             
148     
149     public static String JavaDoc processHtmlContent(ServletRequest JavaDoc request, ServletContext JavaDoc context, String JavaDoc htmlContent, String JavaDoc subFormId) {
150         FormView form = getFormViewSaved(request, subFormId);
151         String JavaDoc state = (String JavaDoc)request.getAttribute(getREQUEST_FORMSTATE_KEY(subFormId));
152
153         // Permission Adapater (by default
154
IPermissionsAdapter permissionsAdapter = (IPermissionsAdapter)request.getAttribute(FormViewConstants.REQUEST_FORMPERMISSION_KEY);
155         if (permissionsAdapter == null) {
156             if (request instanceof HttpServletRequest JavaDoc)
157                 permissionsAdapter = new RolesPermissionsAdapter((HttpServletRequest JavaDoc)request);
158         }
159         FormViewResources resources = getFormViewResources(context);
160         return resources.processHtmlContent(form, state, permissionsAdapter, htmlContent);
161     }
162     
163     /**
164      * Add context value
165      * @param context
166      * @param key
167      * @param value
168      */

169     public static void addContextValue(ServletContext JavaDoc context, String JavaDoc key, String JavaDoc value) {
170         FormViewResources resources = getFormViewResources(context);
171         resources.addContextValue(key, value);
172     }
173     
174     private static void initializeContextPath(ServletRequest JavaDoc request, ServletContext JavaDoc context) {
175         if (!contextPathInitialized) {
176             if (request instanceof HttpServletRequest JavaDoc) {
177                 HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc)request;
178                 // Add ContextPath (path of Web App) into Map of context values
179
addContextValue(context, "contextPath", httpRequest.getContextPath());
180             }
181         }
182         contextPathInitialized = true;
183     }
184     
185     /*public static String getREQUEST_FORMVIEW_KEY(String subFormId) {
186         if (subFormId != null && subFormId.length() > 0) {
187             return FormViewConstants.REQUEST_FORMVIEW_KEY + "_" + subFormId;
188         }
189         return FormViewConstants.REQUEST_FORMVIEW_KEY;
190     }*/

191
192     public static String JavaDoc getREQUEST_FORMSTATE_KEY(String JavaDoc subFormId) {
193         if (subFormId != null && subFormId.length() > 0) {
194             return FormViewConstants.REQUEST_FORMSTATE_KEY + "_" + subFormId;
195         }
196         return FormViewConstants.REQUEST_FORMSTATE_KEY;
197     }
198     
199     public static void mergeFormViewWithStrutsActionErrors(ServletRequest JavaDoc request, ServletContext JavaDoc context) {
200         ActionMessages errors = (ActionMessages)request.getAttribute("org.apache.struts.action.ERROR");
201         if (errors != null && !errors.isEmpty()) {
202             Map JavaDoc formViewSavedMap = getFormViewSavedMap(request);
203             if (formViewSavedMap != null) {
204                 // Get error behaviour
205
FormViewResources resources = getFormViewResources(context);
206                 String JavaDoc errorBehaviour = resources.getErrorBehaviour();
207                 
208                 // Loop for Form View saved
209
Collection JavaDoc formViewList = formViewSavedMap.values();
210                 for (Iterator JavaDoc iter = formViewList.iterator(); iter.hasNext();) {
211                     FormView formView = (FormView) iter.next();
212                     mergeFormViewWithStrutsActionErrors(request, formView, errorBehaviour, errors);
213                 }
214             }
215         }
216     }
217     
218     public static void mergeFormViewWithStrutsActionErrors(ServletRequest JavaDoc request, FormView formView, String JavaDoc errorBehaviour, ActionMessages errors) {
219         // Loop for Struts ActionMessages
220
if (formView != null && errors != null) {
221 // There are one error => get first error in order to save into request
222
// the first field name with error
223
boolean isFirstFieldNameWithErrorInitialized = false;
224             for (Iterator JavaDoc iter = errors.properties(); iter.hasNext();) {
225                 String JavaDoc currentFieldName = (String JavaDoc) iter.next();
226                 if (currentFieldName != null &&
227                 !currentFieldName.equals(ActionMessages.GLOBAL_MESSAGE) &&
228                 !currentFieldName.equals(ActionErrors.GLOBAL_ERROR) &&
229                 !currentFieldName.equals("")) {
230                     // Action error is a error associated with field
231
FieldView fieldViewWithError = formView.getField(currentFieldName);
232                     if (fieldViewWithError == null) {
233                         fieldViewWithError = new FieldView();
234                         fieldViewWithError.setProperty(currentFieldName);
235                         formView.addField(fieldViewWithError);
236                     }
237                     fieldViewWithError.setBehaviour(errorBehaviour);
238                     
239                     if (!isFirstFieldNameWithErrorInitialized) {
240                         isFirstFieldNameWithErrorInitialized = true;
241                         saveFirstFieldWithError(currentFieldName, new Integer JavaDoc(0), request);
242                     }
243                 }
244             }
245         }
246     }
247     
248     public static void setMergeFormViewWithStrutsActionErrors(ServletRequest JavaDoc request, boolean merge) {
249         request.setAttribute(FormViewConstants.MERGE_WITH_STRUTS_ACTIONERRORS , new Boolean JavaDoc(merge));
250     }
251     
252     public static boolean isMergeFormViewWithStrutsActionErrors(ServletRequest JavaDoc request) {
253         Boolean JavaDoc merge = (Boolean JavaDoc)request.getAttribute(FormViewConstants.MERGE_WITH_STRUTS_ACTIONERRORS);
254         if (merge != null)
255             return merge.booleanValue();
256         return true;
257     }
258
259     public static void saveFirstFieldWithError(String JavaDoc fieldNameWithError, HttpServletRequest JavaDoc request) {
260         saveFirstFieldWithError(fieldNameWithError, new Integer JavaDoc(0), request);
261     }
262     
263     public static void saveFirstFieldWithError(String JavaDoc fieldNameWithError, Integer JavaDoc fieldIndexWithError, ServletRequest JavaDoc request) {
264         // there are errors associated with field
265
if (request.getAttribute("firstFieldNameWithError") == null) {
266             request.setAttribute("firstFieldNameWithError", fieldNameWithError);
267             request.setAttribute("firstFieldIndexWithError", fieldIndexWithError);
268         }
269     }
270 }
271
Popular Tags