KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > menu > MenuWrapTransform


1 /*
2  * $Id: MenuWrapTransform.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.menu;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.GeneralException;
38 import org.ofbiz.base.util.UtilValidate;
39 import org.ofbiz.base.util.template.FreeMarkerWorker;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericValue;
42 import org.ofbiz.webapp.ftl.LoopWriter;
43 import org.ofbiz.widget.WidgetContentWorker;
44 import org.ofbiz.widget.html.HtmlMenuWrapper;
45
46 import freemarker.core.Environment;
47 import freemarker.template.TemplateModelException;
48 import freemarker.template.TemplateTransformModel;
49 import freemarker.template.TransformControl;
50
51 //import com.clarkware.profiler.Profiler;
52
/**
53  * MenuWrapTransform - Freemarker Transform for URLs (links)
54  *
55  * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
56  *
57  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
58  * @version $Rev: 5462 $
59  * @since 3.0
60  */

61 public class MenuWrapTransform implements TemplateTransformModel {
62
63     public static final String JavaDoc module = MenuWrapTransform.class.getName();
64     public static final String JavaDoc [] upSaveKeyNames = {"globalNodeTrail"};
65     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "subDataResourceTypeId", "mimeTypeId", "whenMap", "locale", "wrapTemplateId", "encloseWrapText", "nullThruDatesOnly", "renderOnStart", "renderOnClose", "menuDefFile", "menuName", "associatedContentId", "wrapperClassName"};
66     
67     /**
68      * A wrapper for the FreeMarkerWorker version.
69      */

70     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
71         return FreeMarkerWorker.getWrappedObject(varName, env);
72     }
73
74     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
75         return FreeMarkerWorker.getArg(args, key, env);
76     }
77
78     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
79         return FreeMarkerWorker.getArg(args, key, ctx);
80     }
81
82     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
83         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
84         final Environment env = Environment.getCurrentEnvironment();
85         final Map JavaDoc templateCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
86         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
87         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
88         final HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) FreeMarkerWorker.getWrappedObject("response", env);
89         final HttpSession JavaDoc session = (HttpSession JavaDoc) FreeMarkerWorker.getWrappedObject("session", env);
90         FreeMarkerWorker.getSiteParameters(request, templateCtx);
91         final Map JavaDoc savedValuesUp = new HashMap JavaDoc();
92         FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp);
93         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
94         //final String menuDefFile = (String)templateCtx.get("menuDefFile");
95
//final String menuName = (String)templateCtx.get("menuName");
96
//final String associatedContentId = (String)templateCtx.get("associatedContentId");
97
final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
98         List JavaDoc trail = (List JavaDoc)templateCtx.get("globalNodeTrail");
99         String JavaDoc contentAssocPredicateId = (String JavaDoc)templateCtx.get("contentAssocPredicateId");
100         String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateCtx.get("nullThruDatesOnly");
101         Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
102         GenericValue val = null;
103         try {
104             if (WidgetContentWorker.contentWorker != null) {
105                 val = WidgetContentWorker.contentWorker.getCurrentContentExt(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
106             } else {
107                 Debug.logError("Not rendering content, not ContentWorker found.", module);
108             }
109         } catch(GeneralException e) {
110             throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
111         }
112         final GenericValue view = val;
113
114         String JavaDoc dataResourceId = null;
115         try {
116             dataResourceId = (String JavaDoc) view.get("drDataResourceId");
117         } catch (Exception JavaDoc e) {
118             dataResourceId = (String JavaDoc) view.get("dataResourceId");
119         }
120         String JavaDoc subContentIdSub = (String JavaDoc) view.get("contentId");
121         // This order is taken so that the dataResourceType can be overridden in the transform arguments.
122
String JavaDoc subDataResourceTypeId = (String JavaDoc)templateCtx.get("subDataResourceTypeId");
123         if (UtilValidate.isEmpty(subDataResourceTypeId)) {
124             try {
125                 subDataResourceTypeId = (String JavaDoc) view.get("drDataResourceTypeId");
126             } catch (Exception JavaDoc e) {
127                 // view may be "Content"
128
}
129             // TODO: If this value is still empty then it is probably necessary to get a value from
130
// the parent context. But it will already have one and it is the same context that is
131
// being passed.
132
}
133         // This order is taken so that the mimeType can be overridden in the transform arguments.
134
String JavaDoc mimeTypeId = null;
135         if (WidgetContentWorker.contentWorker != null) {
136             mimeTypeId = WidgetContentWorker.contentWorker.getMimeTypeIdExt(delegator, view, templateCtx);
137         } else {
138             Debug.logError("Not rendering content, not ContentWorker found.", module);
139         }
140         templateCtx.put("drDataResourceId", dataResourceId);
141         templateCtx.put("mimeTypeId", mimeTypeId);
142         templateCtx.put("dataResourceId", dataResourceId);
143         templateCtx.put("subContentIdSub", subContentIdSub);
144         templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
145         final Map JavaDoc savedValues = new HashMap JavaDoc();
146         FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
147
148         return new LoopWriter(out) {
149
150             public int onStart() throws TemplateModelException, IOException JavaDoc {
151                 String JavaDoc renderOnStart = (String JavaDoc)templateCtx.get("renderOnStart");
152                 if (renderOnStart != null && renderOnStart.equalsIgnoreCase("true")) {
153                     renderMenu();
154                 }
155                 return TransformControl.EVALUATE_BODY;
156             }
157
158             public void write(char cbuf[], int off, int len) {
159                 buf.append(cbuf, off, len);
160             }
161
162             public void flush() throws IOException JavaDoc {
163                 out.flush();
164             }
165
166             public void close() throws IOException JavaDoc {
167                 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
168                 String JavaDoc wrappedContent = buf.toString();
169                 out.write(wrappedContent);
170                 String JavaDoc renderOnClose = (String JavaDoc)templateCtx.get("renderOnClose");
171                 if (renderOnClose == null || !renderOnClose.equalsIgnoreCase("false")) {
172                     renderMenu();
173                 }
174                 FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env);
175             }
176
177             public void renderMenu() throws IOException JavaDoc {
178            
179                 ModelMenu modelMenu = null;
180                 String JavaDoc menuDefFile = (String JavaDoc)templateCtx.get("menuDefFile");
181                 String JavaDoc menuName = (String JavaDoc)templateCtx.get("menuName");
182                 String JavaDoc menuWrapperClassName = (String JavaDoc)templateCtx.get("menuWrapperClassName");
183                 HtmlMenuWrapper menuWrapper = HtmlMenuWrapper.getMenuWrapper(request, response, session, menuDefFile, menuName, menuWrapperClassName);
184                 String JavaDoc associatedContentId = (String JavaDoc)templateCtx.get("associatedContentId");
185                 menuWrapper.putInContext("defaultAssociatedContentId", associatedContentId);
186                 menuWrapper.putInContext("currentValue", view);
187
188                 if (menuWrapper == null) {
189                     throw new IOException JavaDoc("HtmlMenuWrapper with def file:" + menuDefFile + " menuName:" + menuName + " and HtmlMenuWrapper class:" + menuWrapperClassName + " could not be instantiated.");
190                 }
191                 String JavaDoc menuStr = menuWrapper.renderMenuString();
192                 out.write(menuStr);
193                 return;
194             }
195                 
196         };
197     }
198 }
199
Popular Tags