KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.ofbiz.content.webapp.ftl;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.sql.Timestamp JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.ofbiz.base.util.Debug;
27 import org.ofbiz.base.util.GeneralException;
28 import org.ofbiz.base.util.UtilDateTime;
29 import org.ofbiz.base.util.UtilMisc;
30 import org.ofbiz.base.util.UtilValidate;
31 import org.ofbiz.base.util.template.FreeMarkerWorker;
32 import org.ofbiz.content.content.ContentServicesComplex;
33 import org.ofbiz.content.content.ContentWorker;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericEntityException;
36 import org.ofbiz.entity.GenericValue;
37 import org.ofbiz.webapp.ftl.LoopWriter;
38
39 import freemarker.core.Environment;
40 import freemarker.template.TemplateModelException;
41 import freemarker.template.TemplateTransformModel;
42 import freemarker.template.TransformControl;
43
44 /**
45  * LoopSubContentTransform - Freemarker Transform for URLs (links)
46  *
47  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
48  * @version $Rev: 5462 $
49  * @since 3.0
50  */

51 public class LoopSubContentTransform implements TemplateTransformModel {
52
53     public static final String JavaDoc module = LoopSubContentTransform.class.getName();
54
55     public static final String JavaDoc [] saveKeyNames = {"contentId", "subContentId", "mimeType", "subContentDataResourceView", "wrapTemplateId", "contentTemplateId"};
56     public static final String JavaDoc [] removeKeyNames = {"wrapTemplateId", "entityList", "entityIndex", "textData", "dataResourceId","drDataResourceId", "subContentIdSub", "parentContent", "wrappedFTL"};
57
58     /**
59      * A wrapper for the FreeMarkerWorker version.
60      */

61     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
62         return FreeMarkerWorker.getWrappedObject(varName, env);
63     }
64
65     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
66         return FreeMarkerWorker.getArg(args, key, env);
67     }
68
69     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
70         return FreeMarkerWorker.getArg(args, key, ctx);
71     }
72
73     public static boolean prepCtx(GenericDelegator delegator, Map JavaDoc ctx) {
74
75         //String contentId = (String)ctx.get("contentId");
76
//String mimeTypeId = (String)ctx.get("mimeTypeId");
77
List JavaDoc lst = (List JavaDoc) ctx.get("entityList");
78         Integer JavaDoc idx = (Integer JavaDoc) ctx.get("entityIndex");
79         if (idx == null)
80             idx = new Integer JavaDoc(0);
81         int i = idx.intValue();
82         if (i >= lst.size()) {
83             return false;
84         }
85         GenericValue subContentDataResourceView = (GenericValue) lst.get(i);
86         ctx.put("subContentDataResourceView", subContentDataResourceView);
87         GenericValue electronicText = null;
88         try {
89             electronicText = subContentDataResourceView.getRelatedOne("ElectronicText");
90         } catch (GenericEntityException e) {
91             throw new RuntimeException JavaDoc(e.getMessage());
92         }
93
94         String JavaDoc dataResourceId = (String JavaDoc) subContentDataResourceView.get("drDataResourceId");
95         String JavaDoc subContentIdSub = (String JavaDoc) subContentDataResourceView.get("contentId");
96         // This order is taken so that the dataResourceType can be overridden in the transform arguments.
97
String JavaDoc subDataResourceTypeId = (String JavaDoc)ctx.get("subDataResourceTypeId");
98         if (UtilValidate.isEmpty(subDataResourceTypeId)) {
99             subDataResourceTypeId = (String JavaDoc) subContentDataResourceView.get("drDataResourceTypeId");
100             // TODO: If this value is still empty then it is probably necessary to get a value from
101
// the parent context. But it will already have one and it is the same context that is
102
// being passed.
103
}
104         // This order is taken so that the mimeType can be overridden in the transform arguments.
105
String JavaDoc mimeTypeId = (String JavaDoc)ctx.get("mimeTypeId");
106         if (UtilValidate.isEmpty(mimeTypeId)) {
107             mimeTypeId = (String JavaDoc) subContentDataResourceView.get("mimeTypeId");
108             String JavaDoc parentContentId = (String JavaDoc)ctx.get("contentId");
109             if (UtilValidate.isEmpty(mimeTypeId) && UtilValidate.isNotEmpty(parentContentId)) { // will need these below
110
try {
111                     GenericValue parentContent = delegator.findByPrimaryKey("Content", UtilMisc.toMap("contentId", parentContentId));
112                     if (parentContent != null) {
113                         mimeTypeId = (String JavaDoc) parentContent.get("mimeTypeId");
114                         ctx.put("parentContent", parentContent);
115                     }
116                 } catch (GenericEntityException e) {
117                     throw new RuntimeException JavaDoc(e.getMessage());
118                 }
119             }
120
121         }
122
123         // This is what the FM template will see.
124
ctx.put("subContentDataResourceView", subContentDataResourceView);
125         if (electronicText != null)
126             ctx.put("textData", electronicText.get("textData"));
127         else
128             ctx.put("textData", null);
129         ctx.put("entityIndex", new Integer JavaDoc(i + 1));
130         ctx.put("subContentId", subContentIdSub);
131         ctx.put("drDataResourceId", dataResourceId);
132         ctx.put("mimeTypeId", mimeTypeId);
133         ctx.put("dataResourceId", dataResourceId);
134         ctx.put("subContentIdSub", subContentIdSub);
135         ctx.put("subDataResourceTypeId", subDataResourceTypeId);
136         return true;
137     }
138
139     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
140         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
141         final Environment env = Environment.getCurrentEnvironment();
142         final Map JavaDoc templateCtx = (Map JavaDoc) FreeMarkerWorker.getWrappedObject("context", env);
143         //FreeMarkerWorker.convertContext(templateCtx);
144
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
145         //templateCtx.put("buf", buf);
146
final Map JavaDoc savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames);
147         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
148         String JavaDoc contentAssocTypeId = (String JavaDoc)templateCtx.get("contentAssocTypeId");
149         if (UtilValidate.isEmpty(contentAssocTypeId)) {
150             contentAssocTypeId = "SUB_CONTENT";
151             templateCtx.put("contentAssocTypeId ", contentAssocTypeId );
152         }
153         List JavaDoc assocTypes = UtilMisc.toList(contentAssocTypeId);
154         templateCtx.put("assocTypes", assocTypes);
155         Locale JavaDoc locale = (Locale JavaDoc) templateCtx.get("locale");
156         if (locale == null) {
157             locale = Locale.getDefault();
158             templateCtx.put("locale", locale);
159         }
160
161 /*
162         final String editTemplate = getArg(args, "editTemplate", ctx);
163         final String wrapTemplateId = getArg(args, "wrapTemplateId", ctx);
164         final String mapKey = getArg(args, "mapKey", ctx);
165         final String templateContentId = getArg(args, "templateContentId", ctx);
166         final String subDataResourceTypeId = getArg(args, "subDataResourceTypeId", ctx);
167         final String contentId = getArg(args, "contentId", ctx);
168         final String rootDir = getArg(args, "rootDir", ctx);
169         final String webSiteId = getArg(args, "webSiteId", ctx);
170         final String https = getArg(args, "https", ctx);
171         final String viewSize = getArg(args, "viewSize", ctx);
172         final String viewIndex = getArg(args, "viewIndex", ctx);
173         final String listSize = getArg(args, "listSize", ctx);
174         final String highIndex = getArg(args, "highIndex", ctx);
175         final String lowIndex = getArg(args, "lowIndex", ctx);
176         final String queryString = getArg(args, "queryString", ctx);
177         final Locale locale = (Locale) FreeMarkerWorker.getWrappedObject("locale", env);
178         final String mimeTypeId = getArg(args, "mimeTypeId", ctx);
179         //final LocalDispatcher dispatcher = (LocalDispatcher) FreeMarkerWorker.getWrappedObject("dispatcher", env);
180         final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
181         //final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
182
183         ctx.put("mapKey", mapKey);
184         ctx.put("contentId", contentId);
185         ctx.put("templateContentId", templateContentId);
186         ctx.put("locale", locale);
187
188         //ctx.put("userLogin", userLogin);
189         String contentAssocTypeId = getArg(args, "contentAssocTypeId", ctx);
190         if (UtilValidate.isEmpty(contentAssocTypeId))
191             contentAssocTypeId = "SUB_CONTENT";
192         List assocTypes = UtilMisc.toList(contentAssocTypeId);
193         ctx.put("assocTypes", assocTypes);
194 */

195         String JavaDoc fromDateStr = (String JavaDoc)templateCtx.get("fromDateStr");
196         Timestamp JavaDoc fromDate = null;
197         if (UtilValidate.isNotEmpty(fromDateStr)) {
198             fromDate = UtilDateTime.toTimestamp(fromDateStr);
199         }
200         if (fromDate == null)
201             fromDate = UtilDateTime.nowTimestamp();
202         String JavaDoc thisContentId = (String JavaDoc)templateCtx.get("contentId");
203         if (UtilValidate.isEmpty(thisContentId)) {
204             thisContentId = (String JavaDoc)templateCtx.get("subContentId");
205         }
206         String JavaDoc thisMapKey = (String JavaDoc)templateCtx.get("mapKey");
207         //GenericValue subContentDataResourceView = null;
208
Map JavaDoc results =
209             ContentServicesComplex.getAssocAndContentAndDataResourceMethod(delegator, thisContentId, thisMapKey, "From", fromDate, null, null, null, assocTypes, null);
210         List JavaDoc entityList = (List JavaDoc) results.get("entityList");
211         templateCtx.put("entityList", entityList);
212
213         return new LoopWriter(out) {
214
215             public void write(char cbuf[], int off, int len) {
216                 buf.append(cbuf, off, len);
217                 //StringBuffer ctxBuf = (StringBuffer) templateCtx.get("buf");
218
//ctxBuf.append(cbuf, off, len);
219
}
220
221             public void flush() throws IOException JavaDoc {
222                 out.flush();
223             }
224
225             public int onStart() throws TemplateModelException, IOException JavaDoc {
226                 templateCtx.put("entityIndex", new Integer JavaDoc(0));
227                 boolean inProgress = prepCtx(delegator, templateCtx);
228                 if (inProgress) {
229                     return TransformControl.EVALUATE_BODY;
230                 } else {
231                     return TransformControl.SKIP_BODY;
232                 }
233             }
234
235             public int afterBody() throws TemplateModelException, IOException JavaDoc {
236                 Integer JavaDoc idx = (Integer JavaDoc) templateCtx.get("entityIndex");
237                 int i = idx.intValue();
238                 boolean inProgress = prepCtx(delegator, templateCtx);
239                 //out.write(buf.toString());
240
//buf.setLength(0);
241
if (inProgress)
242                     return TransformControl.REPEAT_EVALUATION;
243                 else
244                     return TransformControl.END_EVALUATION;
245             }
246
247             public void close() throws IOException JavaDoc {
248
249                 String JavaDoc wrappedFTL = buf.toString();
250                 String JavaDoc encloseWrappedText = (String JavaDoc)templateCtx.get("encloseWrappedText");
251                 if (UtilValidate.isEmpty(encloseWrappedText) || encloseWrappedText.equalsIgnoreCase("false")) {
252                     out.write(wrappedFTL);
253                     wrappedFTL = ""; // So it won't get written again below.
254
}
255                 String JavaDoc wrapTemplateId = (String JavaDoc)templateCtx.get("wrapTemplateId");
256                 if (UtilValidate.isNotEmpty(wrapTemplateId)) {
257                     templateCtx.put("wrappedFTL", wrappedFTL);
258                     
259                     Map JavaDoc templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
260                     
261 /*
262                     templateRoot.put("viewSize", viewSize);
263                     templateRoot.put("viewIndex", viewIndex);
264                     templateRoot.put("listSize", listSize);
265                     templateRoot.put("highIndex", highIndex);
266                     templateRoot.put("lowIndex", lowIndex);
267                     templateRoot.put("queryString", queryString);
268                     templateRoot.put("wrapMapKey", mapKey);
269 */

270                     templateRoot.put("wrapDataResourceTypeId", templateCtx.get("subDataResourceTypeId"));
271                     templateRoot.put("wrapContentIdTo", templateCtx.get("contentId"));
272                     templateRoot.put("wrapMimeTypeId", templateCtx.get("mimeTypeId"));
273                     templateRoot.put("context", templateCtx);
274                     
275
276                     Locale JavaDoc locale = (Locale JavaDoc) templateCtx.get("locale");
277                     if (locale == null)
278                         locale = Locale.getDefault();
279                     String JavaDoc mimeTypeId = (String JavaDoc) templateCtx.get("mimeTypeId");
280                     try {
281                         ContentWorker.renderContentAsText(delegator, wrapTemplateId, out, templateRoot, null, locale, mimeTypeId);
282                     } catch (GeneralException e) {
283                         Debug.logError(e, "Error rendering content", module);
284                         throw new IOException JavaDoc("Error rendering content" + e.toString());
285                     }
286                 } else {
287                     if (UtilValidate.isNotEmpty(wrappedFTL))
288                         out.write(wrappedFTL);
289                 }
290                     FreeMarkerWorker.removeValues(templateCtx, removeKeyNames);
291                     FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
292             }
293         };
294     }
295 }
296
Popular Tags