KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > html > HtmlMenuWrapper


1 /*
2  * $Id: HtmlMenuWrapper.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.html;
25
26 import java.io.IOException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33 import javax.xml.parsers.ParserConfigurationException JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.base.util.UtilHttp;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.widget.menu.MenuFactory;
39 import org.ofbiz.widget.menu.MenuStringRenderer;
40 import org.ofbiz.widget.menu.ModelMenu;
41 import org.ofbiz.entity.GenericValue;
42
43 import org.xml.sax.SAXException JavaDoc;
44
45
46 /**
47  * Widget Library - HTML Menu Wrapper class - makes it easy to do the setup and render of a menu
48  *
49  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
50  * @version $Rev: 5462 $
51  * @since 3.0
52  */

53 public class HtmlMenuWrapper {
54     
55     public static final String JavaDoc module = HtmlMenuWrapper.class.getName();
56     
57     protected String JavaDoc resourceName;
58     protected String JavaDoc menuName;
59     protected HttpServletRequest JavaDoc request;
60     protected HttpServletResponse JavaDoc response;
61     protected ModelMenu modelMenu;
62     protected MenuStringRenderer renderer;
63     protected Map JavaDoc context;
64
65     protected HtmlMenuWrapper() {}
66
67     public HtmlMenuWrapper(String JavaDoc resourceName, String JavaDoc menuName, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
68             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
69         init(resourceName, menuName, request, response);
70     }
71
72     public void init(String JavaDoc resourceName, String JavaDoc menuName, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
73             throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc {
74         this.resourceName = resourceName;
75         this.menuName = menuName;
76         this.request = request;
77         this.response = response;
78         
79         this.modelMenu = MenuFactory.getMenuFromWebappContext(resourceName, menuName, request);
80
81         this.renderer = getMenuRenderer();
82         
83         this.context = new HashMap JavaDoc();
84         Map JavaDoc parameterMap = UtilHttp.getParameterMap(request);
85         context.put("parameters", parameterMap);
86
87         HttpSession JavaDoc session = request.getSession();
88         GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
89         context.put("userLogin", userLogin);
90         
91         //make sure the locale is in the context
92
context.put("locale", UtilHttp.getLocale(request));
93         
94         // if there was an error message, this is an error
95
if (UtilValidate.isNotEmpty((String JavaDoc) request.getAttribute("_ERROR_MESSAGE_"))) {
96             context.put("isError", Boolean.TRUE);
97         } else {
98             context.put("isError", Boolean.FALSE);
99         }
100         
101         // if a parameter was passed saying this is an error, it is an error
102
if ("true".equals((String JavaDoc) parameterMap.get("isError"))) {
103             context.put("isError", Boolean.TRUE);
104         }
105     }
106
107     public MenuStringRenderer getMenuRenderer() {
108         return new HtmlMenuRenderer(request, response);
109     }
110     
111     public String JavaDoc renderMenuString() {
112         HttpServletRequest JavaDoc req = ((HtmlMenuRenderer)renderer).request;
113         ServletContext JavaDoc ctx = (ServletContext JavaDoc) req.getAttribute("servletContext");
114         if (ctx == null) {
115             if (Debug.infoOn()) Debug.logInfo("in renderMenuString, ctx is null(0)" , "");
116         }
117
118         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
119         modelMenu.renderMenuString(buffer, context, renderer);
120
121         HttpServletRequest JavaDoc req2 = ((HtmlMenuRenderer)renderer).request;
122         ServletContext JavaDoc ctx2 = (ServletContext JavaDoc) req2.getAttribute("servletContext");
123         if (ctx2 == null) {
124             if (Debug.infoOn()) Debug.logInfo("in renderMenuString, ctx is null(2)" , "");
125         }
126
127         return buffer.toString();
128     }
129
130     /**
131      * Tells the menu library whether this is a response to an error or not.
132      * Defaults on initialization according to the presense of an errorMessage
133      * in the request or if an isError parameter was passed to the page with
134      * the value "true". If true then the prefilled values will come from the
135      * parameters Map instead of the value Map.
136      */

137     public void setIsError(boolean isError) {
138         this.context.put("isError", new Boolean JavaDoc(isError));
139     }
140     
141     public boolean getIsError() {
142         Boolean JavaDoc isErrorBoolean = (Boolean JavaDoc) this.context.get("isError");
143         if (isErrorBoolean == null) {
144             return false;
145         } else {
146             return isErrorBoolean.booleanValue();
147         }
148     }
149     
150     public void setMenuOverrideName(String JavaDoc menuName) {
151         this.context.put("menuName", menuName);
152     }
153     
154     public void putInContext(String JavaDoc name, Object JavaDoc value) {
155         this.context.put(name, value);
156     }
157     
158     public void putInContext(String JavaDoc menuItemName, String JavaDoc valueName, Object JavaDoc value) {
159         Map JavaDoc valueMap = (Map JavaDoc)context.get(menuItemName);
160         if (valueMap == null) {
161             valueMap = new HashMap JavaDoc();
162             context.put(menuItemName, valueMap);
163         }
164         valueMap.put(valueName, value);
165     }
166     
167     public Object JavaDoc getFromContext(String JavaDoc name) {
168         return this.context.get(name);
169     }
170     
171     public Object JavaDoc getFromContext(String JavaDoc menuItemName, String JavaDoc valueName) {
172         Map JavaDoc valueMap = (Map JavaDoc)context.get(menuItemName);
173         if (valueMap == null) {
174             valueMap = new HashMap JavaDoc();
175             context.put(menuItemName, valueMap);
176         }
177         return valueMap.get(valueName);
178     }
179     
180     public ModelMenu getModelMenu() {
181         return modelMenu;
182     }
183
184     public MenuStringRenderer getRenderer() {
185         return renderer;
186     }
187
188     public void setRenderer(MenuStringRenderer renderer) {
189         this.renderer = renderer;
190     }
191
192     public void setRequest(HttpServletRequest JavaDoc request) {
193         this.request = request;
194         ((HtmlMenuRenderer)renderer).setRequest( request );
195     }
196
197     public void setResponse(HttpServletResponse JavaDoc response) {
198         this.response = response;
199         ((HtmlMenuRenderer)renderer).setResponse( response );
200     }
201
202     public HttpServletRequest JavaDoc getRequest() {
203         return ((HtmlMenuRenderer)renderer).request;
204     }
205
206     public HttpServletResponse JavaDoc getResponse() {
207         return ((HtmlMenuRenderer)renderer).response;
208     }
209
210     public static HtmlMenuWrapper getMenuWrapper(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, HttpSession JavaDoc session, String JavaDoc menuDefFile, String JavaDoc menuName, String JavaDoc menuWrapperClassName ) {
211         
212         HtmlMenuWrapper menuWrapper = null;
213         
214         String JavaDoc menuSig = menuDefFile + "__" + menuName;
215         if (session != null) {
216              menuWrapper = (HtmlMenuWrapper)session.getAttribute(menuSig);
217         }
218
219         if (menuWrapper == null) {
220             try {
221                 Class JavaDoc cls = Class.forName("org.ofbiz.widget.html." + menuWrapperClassName);
222                 menuWrapper = (HtmlMenuWrapper)cls.newInstance();
223                 menuWrapper.init(menuDefFile, menuName, request, response);
224             } catch(InstantiationException JavaDoc e) {
225                 throw new RuntimeException JavaDoc(e.getMessage());
226             } catch(IllegalAccessException JavaDoc e2) {
227                 throw new RuntimeException JavaDoc(e2.getMessage());
228             } catch(ClassNotFoundException JavaDoc e3) {
229                 throw new RuntimeException JavaDoc("Class not found:" + e3.getMessage());
230             } catch(IOException JavaDoc e4) {
231                 throw new RuntimeException JavaDoc(e4.getMessage());
232             } catch(SAXException JavaDoc e5) {
233                 throw new RuntimeException JavaDoc(e5.getMessage());
234             } catch(ParserConfigurationException JavaDoc e6) {
235                 throw new RuntimeException JavaDoc(e6.getMessage());
236             }
237         } else {
238             menuWrapper.setRequest(request);
239             menuWrapper.setResponse(response);
240             Map JavaDoc parameterMap = UtilHttp.getParameterMap(request);
241             menuWrapper.setParameters( parameterMap);
242
243             GenericValue userLogin = (GenericValue)session.getAttribute("userLogin");
244             menuWrapper.putInContext("userLogin", userLogin);
245         
246         }
247
248         if (session != null) {
249             session.setAttribute(menuSig, menuWrapper);
250         }
251         return menuWrapper;
252     }
253
254     public void setParameters(Map JavaDoc paramMap) {
255         context.put("parameters", paramMap);
256     }
257
258 }
259
Popular Tags