001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.content.webapp.ftl;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.sql.Timestamp;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import org.ofbiz.base.util.Debug;
028: import org.ofbiz.base.util.GeneralException;
029: import org.ofbiz.base.util.UtilDateTime;
030: import org.ofbiz.base.util.UtilMisc;
031: import org.ofbiz.base.util.UtilValidate;
032: import org.ofbiz.base.util.template.FreeMarkerWorker;
033: import org.ofbiz.content.content.ContentWorker;
034: import org.ofbiz.entity.GenericDelegator;
035: import org.ofbiz.entity.GenericEntityException;
036: import org.ofbiz.entity.GenericValue;
037: import org.ofbiz.service.LocalDispatcher;
038:
039: import freemarker.core.Environment;
040: import freemarker.template.TemplateTransformModel;
041:
042: /**
043: * EditRenderSubContentTransform - Freemarker Transform for URLs (links)
044: *
045: * This is an interactive FreeMarker tranform that allows the user to modify the contents that are placed within it.
046: */
047: public class EditRenderSubContentTransform implements
048: TemplateTransformModel {
049:
050: public static final String module = EditRenderSubContentTransform.class
051: .getName();
052:
053: /**
054: * A wrapper for the FreeMarkerWorker version.
055: */
056: public static Object getWrappedObject(String varName,
057: Environment env) {
058: return FreeMarkerWorker.getWrappedObject(varName, env);
059: }
060:
061: public static String getArg(Map args, String key, Environment env) {
062: return FreeMarkerWorker.getArg(args, key, env);
063: }
064:
065: public static String getArg(Map args, String key, Map ctx) {
066: return FreeMarkerWorker.getArg(args, key, ctx);
067: }
068:
069: public Writer getWriter(final Writer out, Map args) {
070: final StringBuffer buf = new StringBuffer();
071: final Environment env = Environment.getCurrentEnvironment();
072: Map ctx = (Map) FreeMarkerWorker.getWrappedObject("context",
073: env);
074: final String editTemplate = getArg(args, "editTemplate", ctx);
075: final String wrapTemplateId = getArg(args, "wrapTemplateId",
076: ctx);
077: final String mapKey = getArg(args, "mapKey", ctx);
078: final String templateContentId = getArg(args,
079: "templateContentId", ctx);
080: final String subContentId = getArg(args, "subContentId", ctx);
081: String subDataResourceTypeIdTemp = getArg(args,
082: "subDataResourceTypeId", ctx);
083: final String contentId = getArg(args, "contentId", ctx);
084:
085: final Locale locale = (Locale) FreeMarkerWorker
086: .getWrappedObject("locale", env);
087: String mimeTypeIdTemp = getArg(args, "mimeTypeId", ctx);
088: final String rootDir = getArg(args, "rootDir", ctx);
089: final String webSiteId = getArg(args, "webSiteId", ctx);
090: final String https = getArg(args, "https", ctx);
091: final LocalDispatcher dispatcher = (LocalDispatcher) FreeMarkerWorker
092: .getWrappedObject("dispatcher", env);
093: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
094: .getWrappedObject("delegator", env);
095: final GenericValue userLogin = (GenericValue) FreeMarkerWorker
096: .getWrappedObject("userLogin", env);
097: GenericValue subContentDataResourceViewTemp = (GenericValue) FreeMarkerWorker
098: .getWrappedObject("subContentDataResourceView", env);
099: //final HttpServletRequest request = (HttpServletRequest)FreeMarkerWorker.getWrappedObject("request", env);
100:
101: ctx.put("mapKey", mapKey);
102: ctx.put("subDataResourceTypeIdTemp", subDataResourceTypeIdTemp);
103: ctx.put("contentId", contentId);
104: ctx.put("templateContentId", templateContentId);
105: ctx.put("locale", locale);
106:
107: // This transform does not need information about the subContent until the
108: // close action, but any embedded RenderDataResourceTransformation will need it
109: // and since it cannot be passed back up from that transform, the subContent view
110: // is gotten here and made available to underlying transforms to save overall
111: // processing time.
112: GenericValue parentContent = null;
113: //ctx.put("userLogin", userLogin);
114: List assocTypes = UtilMisc.toList("SUB_CONTENT");
115: Timestamp fromDate = UtilDateTime.nowTimestamp();
116: if (subContentDataResourceViewTemp == null) {
117: try {
118: subContentDataResourceViewTemp = ContentWorker
119: .getSubContent(delegator, contentId, mapKey,
120: subContentId, userLogin, assocTypes,
121: fromDate);
122: } catch (IOException e) {
123: Debug.logError(e, "Error getting sub-content", module);
124: throw new RuntimeException(e.getMessage());
125: }
126: }
127:
128: final GenericValue subContentDataResourceView = subContentDataResourceViewTemp;
129:
130: String dataResourceIdTemp = null;
131: String subContentIdSubTemp = null;
132: if (subContentDataResourceView != null
133: && subContentDataResourceView.get("contentId") != null) {
134:
135: dataResourceIdTemp = (String) subContentDataResourceView
136: .get("drDataResourceId");
137: subContentIdSubTemp = (String) subContentDataResourceView
138: .get("contentId");
139: if (UtilValidate.isEmpty(subDataResourceTypeIdTemp)) {
140: subDataResourceTypeIdTemp = (String) subContentDataResourceView
141: .get("drDataResourceTypeId");
142: }
143: if (UtilValidate.isEmpty(mimeTypeIdTemp)) {
144: mimeTypeIdTemp = (String) subContentDataResourceView
145: .get("mimeTypeId");
146: if (UtilValidate.isEmpty(mimeTypeIdTemp)
147: && UtilValidate.isNotEmpty(contentId)) { // will need these below
148: try {
149: parentContent = delegator.findByPrimaryKey(
150: "Content", UtilMisc.toMap("contentId",
151: contentId));
152: if (parentContent != null) {
153: mimeTypeIdTemp = (String) parentContent
154: .get("mimeTypeId");
155: }
156: } catch (GenericEntityException e) {
157: throw new RuntimeException(e.getMessage());
158: }
159: }
160:
161: }
162: ctx.put("subContentId", subContentIdSubTemp);
163: ctx.put("drDataResourceId", dataResourceIdTemp);
164: ctx.put("subContentDataResourceView",
165: subContentDataResourceView);
166: ctx.put("mimeTypeId", mimeTypeIdTemp);
167: //request.setAttribute("drDataResourceId", subContentDataResourceView.get("drDataResourceId"));
168: } else {
169: ctx.put("subContentId", null);
170: ctx.put("drDataResourceId", null);
171: ctx.put("subContentDataResourceView", null);
172: ctx.put("mimeTypeId", null);
173: //request.setAttribute("drDataResourceId", null);
174: }
175:
176: final String dataResourceId = dataResourceIdTemp;
177: final String subContentIdSub = subContentIdSubTemp;
178: //final GenericValue finalSubContentView = subContentDataResourceView;
179: //final GenericValue content = parentContent;
180: final Map templateContext = ctx;
181: final String mimeTypeId = mimeTypeIdTemp;
182: final String subDataResourceTypeId = subDataResourceTypeIdTemp;
183:
184: return new Writer(out) {
185:
186: public void write(char cbuf[], int off, int len) {
187: buf.append(cbuf, off, len);
188: }
189:
190: public void flush() throws IOException {
191: out.flush();
192: }
193:
194: public void close() throws IOException {
195: String wrappedFTL = buf.toString();
196: if (editTemplate != null
197: && editTemplate.equalsIgnoreCase("true")) {
198: if (UtilValidate.isNotEmpty(wrapTemplateId)) {
199: templateContext.put("wrappedFTL", wrappedFTL);
200: //ServletContext servletContext = (ServletContext)request.getSession().getServletContext();
201: //String rootDir = servletContext.getRealPath("/");
202: templateContext.put("webSiteId", webSiteId);
203: templateContext.put("https", https);
204: templateContext.put("rootDir", rootDir);
205:
206: Map templateRoot = FreeMarkerWorker
207: .createEnvironmentMap(env);
208:
209: templateRoot.put("wrapDataResourceId",
210: dataResourceId);
211: templateRoot.put("wrapDataResourceTypeId",
212: subDataResourceTypeId);
213: templateRoot.put("wrapContentIdTo", contentId);
214: templateRoot.put("wrapSubContentId",
215: subContentIdSub);
216: templateRoot.put("wrapMimeTypeId", mimeTypeId);
217: templateRoot.put("wrapMapKey", mapKey);
218: templateRoot.put("context", templateContext);
219:
220: try {
221: ContentWorker.renderContentAsText(
222: dispatcher, delegator,
223: wrapTemplateId, out, templateRoot,
224: locale, mimeTypeId, false);
225: } catch (IOException e) {
226: Debug.logError(e, "Error rendering content"
227: + e.getMessage(), module);
228: throw new IOException(
229: "Error rendering content"
230: + e.toString());
231: } catch (GeneralException e2) {
232: Debug.logError(e2,
233: "Error rendering content"
234: + e2.getMessage(), module);
235: throw new IOException(
236: "Error rendering content"
237: + e2.toString());
238: }
239:
240: Map ctx = (Map) FreeMarkerWorker
241: .getWrappedObject("context", env);
242: templateContext.put("contentId", contentId);
243: templateContext.put("locale", locale);
244: templateContext.put("mapKey", null);
245: templateContext.put("subContentId", null);
246: templateContext.put("templateContentId", null);
247: templateContext.put("subDataResourceTypeId",
248: null);
249: templateContext.put("mimeTypeId", null);
250: templateContext.put("wrappedFTL", null);
251: }
252: } else {
253: out.write(wrappedFTL);
254: }
255: }
256: };
257: }
258: }
|