KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > webapp > ftl > EditRenderSubContentTransform


1 /*
2  * $Id: EditRenderSubContentTransform.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.content.webapp.ftl;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.sql.Timestamp JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.GeneralException;
35 import org.ofbiz.base.util.UtilDateTime;
36 import org.ofbiz.base.util.UtilMisc;
37 import org.ofbiz.base.util.UtilValidate;
38 import org.ofbiz.base.util.template.FreeMarkerWorker;
39 import org.ofbiz.content.content.ContentWorker;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43
44 import freemarker.core.Environment;
45 import freemarker.template.TemplateTransformModel;
46
47 /**
48  * EditRenderSubContentTransform - Freemarker Transform for URLs (links)
49  *
50  * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
51  *
52  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
53  * @version $Rev: 5462 $
54  * @since 3.0
55  */

56 public class EditRenderSubContentTransform implements TemplateTransformModel {
57
58     public static final String JavaDoc module = EditRenderSubContentTransform.class.getName();
59     
60     /**
61      * A wrapper for the FreeMarkerWorker version.
62      */

63     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
64         return FreeMarkerWorker.getWrappedObject(varName, env);
65     }
66
67     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
68         return FreeMarkerWorker.getArg(args, key, env);
69     }
70
71     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
72         return FreeMarkerWorker.getArg(args, key, ctx);
73     }
74
75     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
76         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
77         final Environment env = Environment.getCurrentEnvironment();
78         Map JavaDoc ctx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
79         final String JavaDoc editTemplate = getArg(args, "editTemplate", ctx);
80         final String JavaDoc wrapTemplateId = getArg(args, "wrapTemplateId", ctx);
81         final String JavaDoc mapKey = getArg(args, "mapKey", ctx);
82         final String JavaDoc templateContentId = getArg(args, "templateContentId", ctx);
83         final String JavaDoc subContentId = getArg(args, "subContentId", ctx);
84         String JavaDoc subDataResourceTypeIdTemp = getArg(args, "subDataResourceTypeId", ctx);
85         final String JavaDoc contentId = getArg(args, "contentId", ctx);
86
87         
88         final Locale JavaDoc locale = (Locale JavaDoc) FreeMarkerWorker.getWrappedObject("locale", env);
89         String JavaDoc mimeTypeIdTemp = getArg(args, "mimeTypeId", ctx);
90         final String JavaDoc rootDir = getArg(args, "rootDir", ctx);
91         final String JavaDoc webSiteId = getArg(args, "webSiteId", ctx);
92         final String JavaDoc https = getArg(args, "https", ctx);
93         //final LocalDispatcher dispatcher = (LocalDispatcher) FreeMarkerWorker.getWrappedObject("dispatcher", env);
94
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
95         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
96         GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker.getWrappedObject("subContentDataResourceView", env);
97         //final HttpServletRequest request = (HttpServletRequest)FreeMarkerWorker.getWrappedObject("request", env);
98

99         ctx.put("mapKey", mapKey);
100         ctx.put("subDataResourceTypeIdTemp", subDataResourceTypeIdTemp);
101         ctx.put("contentId", contentId);
102         ctx.put("templateContentId", templateContentId);
103         ctx.put("locale", locale);
104
105         // This transform does not need information about the subContent until the
106
// close action, but any embedded RenderDataResourceTransformation will need it
107
// and since it cannot be passed back up from that transform, the subContent view
108
// is gotten here and made available to underlying transforms to save overall
109
// processing time.
110
GenericValue parentContent = null;
111         //ctx.put("userLogin", userLogin);
112
List JavaDoc assocTypes = UtilMisc.toList("SUB_CONTENT");
113         Timestamp JavaDoc fromDate = UtilDateTime.nowTimestamp();
114         if (subContentDataResourceViewTemp == null) {
115             try {
116                 subContentDataResourceViewTemp = ContentWorker.getSubContent(delegator, contentId, mapKey, subContentId, userLogin, assocTypes, fromDate);
117             } catch (IOException JavaDoc e) {
118                 Debug.logError(e, "Error getting sub-content", module);
119                 throw new RuntimeException JavaDoc(e.getMessage());
120             }
121         }
122
123         final GenericValue subContentDataResourceView = subContentDataResourceViewTemp;
124
125         String JavaDoc dataResourceIdTemp = null;
126         String JavaDoc subContentIdSubTemp = null;
127         if (subContentDataResourceView != null && subContentDataResourceView.get("contentId") != null) {
128
129
130             dataResourceIdTemp = (String JavaDoc) subContentDataResourceView.get("drDataResourceId");
131             subContentIdSubTemp = (String JavaDoc) subContentDataResourceView.get("contentId");
132             if (UtilValidate.isEmpty(subDataResourceTypeIdTemp)) {
133                 subDataResourceTypeIdTemp = (String JavaDoc) subContentDataResourceView.get("drDataResourceTypeId");
134             }
135             if (UtilValidate.isEmpty(mimeTypeIdTemp)) {
136                 mimeTypeIdTemp = (String JavaDoc) subContentDataResourceView.get("mimeTypeId");
137                 if (UtilValidate.isEmpty(mimeTypeIdTemp) && UtilValidate.isNotEmpty(contentId)) { // will need these below
138
try {
139                         parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", contentId));
140                         if (parentContent != null) {
141                             mimeTypeIdTemp = (String JavaDoc) parentContent.get("mimeTypeId");
142                         }
143                     } catch (GenericEntityException e) {
144                         throw new RuntimeException JavaDoc(e.getMessage());
145                     }
146                 }
147
148             }
149             ctx.put("subContentId", subContentIdSubTemp);
150             ctx.put("drDataResourceId", dataResourceIdTemp);
151             ctx.put("subContentDataResourceView", subContentDataResourceView);
152             ctx.put("mimeTypeId", mimeTypeIdTemp);
153             //request.setAttribute("drDataResourceId", subContentDataResourceView.get("drDataResourceId"));
154
} else {
155             ctx.put("subContentId", null);
156             ctx.put("drDataResourceId", null);
157             ctx.put("subContentDataResourceView", null);
158             ctx.put("mimeTypeId", null);
159             //request.setAttribute("drDataResourceId", null);
160
}
161
162         final String JavaDoc dataResourceId = dataResourceIdTemp;
163         final String JavaDoc subContentIdSub = subContentIdSubTemp;
164         //final GenericValue finalSubContentView = subContentDataResourceView;
165
//final GenericValue content = parentContent;
166
final Map JavaDoc templateContext = ctx;
167         final String JavaDoc mimeTypeId = mimeTypeIdTemp;
168         final String JavaDoc subDataResourceTypeId = subDataResourceTypeIdTemp;
169
170         return new Writer JavaDoc(out) {
171
172             public void write(char cbuf[], int off, int len) {
173                 buf.append(cbuf, off, len);
174             }
175
176             public void flush() throws IOException JavaDoc {
177                 out.flush();
178             }
179
180             public void close() throws IOException JavaDoc {
181                 String JavaDoc wrappedFTL = buf.toString();
182                 if (editTemplate != null && editTemplate.equalsIgnoreCase("true")) {
183                     if (UtilValidate.isNotEmpty(wrapTemplateId)) {
184                         templateContext.put("wrappedFTL", wrappedFTL);
185                         //ServletContext servletContext = (ServletContext)request.getSession().getServletContext();
186
//String rootDir = servletContext.getRealPath("/");
187
templateContext.put("webSiteId", webSiteId);
188                         templateContext.put("https", https);
189                         templateContext.put("rootDir", rootDir);
190                         
191                         Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
192                         
193                         templateRoot.put("wrapDataResourceId", dataResourceId);
194                         templateRoot.put("wrapDataResourceTypeId", subDataResourceTypeId);
195                         templateRoot.put("wrapContentIdTo", contentId);
196                         templateRoot.put("wrapSubContentId", subContentIdSub);
197                         templateRoot.put("wrapMimeTypeId", mimeTypeId);
198                         templateRoot.put("wrapMapKey", mapKey);
199                         templateRoot.put("context", templateContext);
200                         
201                         try {
202                             ContentWorker.renderContentAsText(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId);
203                         } catch (IOException JavaDoc e) {
204                             Debug.logError(e, "Error rendering content" + e.getMessage(), module);
205                             throw new IOException JavaDoc("Error rendering content" + e.toString());
206                         } catch (GeneralException e2) {
207                             Debug.logError(e2, "Error rendering content" + e2.getMessage(), module);
208                             throw new IOException JavaDoc("Error rendering content" + e2.toString());
209                         }
210                         
211                         Map JavaDoc ctx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
212                         templateContext.put("contentId", contentId);
213                         templateContext.put("locale", locale);
214                         templateContext.put("mapKey", null);
215                         templateContext.put("subContentId", null);
216                         templateContext.put("templateContentId", null);
217                         templateContext.put("subDataResourceTypeId", null);
218                         templateContext.put("mimeTypeId", null);
219                         templateContext.put("wrappedFTL", null);
220                     }
221                 } else {
222                     out.write(wrappedFTL);
223                 }
224             }
225         };
226     }
227 }
228
Popular Tags