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.product.config;
019:
020: import java.io.IOException;
021: import java.io.StringWriter;
022: import java.io.Writer;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import javax.servlet.http.HttpServletRequest;
029:
030: import org.ofbiz.base.util.Debug;
031: import org.ofbiz.base.util.GeneralException;
032: import org.ofbiz.base.util.UtilHttp;
033: import org.ofbiz.base.util.UtilMisc;
034: import org.ofbiz.base.util.UtilValidate;
035: import org.ofbiz.content.content.ContentWorker;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.entity.model.ModelEntity;
039: import org.ofbiz.entity.model.ModelUtil;
040: import org.ofbiz.entity.util.EntityUtil;
041: import org.ofbiz.service.LocalDispatcher;
042:
043: /**
044: * Product Config Item Content Worker: gets product content to display
045: */
046: public class ProductConfigItemContentWrapper implements
047: java.io.Serializable {
048:
049: public static final String module = ProductConfigItemContentWrapper.class
050: .getName();
051:
052: protected LocalDispatcher dispatcher;
053: protected GenericValue productConfigItem;
054: protected Locale locale;
055: protected String mimeTypeId;
056:
057: public static ProductConfigItemContentWrapper makeProductConfigItemContentWrapper(
058: GenericValue productConfigItem, HttpServletRequest request) {
059: return new ProductConfigItemContentWrapper(productConfigItem,
060: request);
061: }
062:
063: public ProductConfigItemContentWrapper(LocalDispatcher dispatcher,
064: GenericValue productConfigItem, Locale locale,
065: String mimeTypeId) {
066: this .dispatcher = dispatcher;
067: this .productConfigItem = productConfigItem;
068: this .locale = locale;
069: this .mimeTypeId = mimeTypeId;
070: }
071:
072: public ProductConfigItemContentWrapper(
073: GenericValue productConfigItem, HttpServletRequest request) {
074: this .dispatcher = (LocalDispatcher) request
075: .getAttribute("dispatcher");
076: this .productConfigItem = productConfigItem;
077: this .locale = UtilHttp.getLocale(request);
078: this .mimeTypeId = "text/html";
079: }
080:
081: public String get(String confItemContentTypeId) {
082: return getProductConfigItemContentAsText(productConfigItem,
083: confItemContentTypeId, locale, mimeTypeId,
084: productConfigItem.getDelegator(), dispatcher);
085: }
086:
087: public static String getProductConfigItemContentAsText(
088: GenericValue productConfigItem,
089: String confItemContentTypeId, HttpServletRequest request) {
090: LocalDispatcher dispatcher = (LocalDispatcher) request
091: .getAttribute("dispatcher");
092: return getProductConfigItemContentAsText(productConfigItem,
093: confItemContentTypeId, UtilHttp.getLocale(request),
094: "text/html", productConfigItem.getDelegator(),
095: dispatcher);
096: }
097:
098: public static String getProductConfigItemContentAsText(
099: GenericValue productConfigItem,
100: String confItemContentTypeId, Locale locale,
101: LocalDispatcher dispatcher) {
102: return getProductConfigItemContentAsText(productConfigItem,
103: confItemContentTypeId, locale, null, null, dispatcher);
104: }
105:
106: public static String getProductConfigItemContentAsText(
107: GenericValue productConfigItem,
108: String confItemContentTypeId, Locale locale,
109: String mimeTypeId, GenericDelegator delegator,
110: LocalDispatcher dispatcher) {
111: String candidateFieldName = ModelUtil
112: .dbNameToVarName(confItemContentTypeId);
113: try {
114: Writer outWriter = new StringWriter();
115: getProductConfigItemContentAsText(null, productConfigItem,
116: confItemContentTypeId, locale, mimeTypeId,
117: delegator, dispatcher, outWriter);
118: String outString = outWriter.toString();
119: if (outString.length() > 0) {
120: return outString;
121: } else {
122: return null;
123: }
124: } catch (GeneralException e) {
125: Debug
126: .logError(
127: e,
128: "Error rendering ProdConfItemContent, inserting empty String",
129: module);
130: return productConfigItem.getString(candidateFieldName);
131: } catch (IOException e) {
132: Debug
133: .logError(
134: e,
135: "Error rendering ProdConfItemContent, inserting empty String",
136: module);
137: return productConfigItem.getString(candidateFieldName);
138: }
139: }
140:
141: public static void getProductConfigItemContentAsText(
142: String configItemId, GenericValue productConfigItem,
143: String confItemContentTypeId, Locale locale,
144: String mimeTypeId, GenericDelegator delegator,
145: LocalDispatcher dispatcher, Writer outWriter)
146: throws GeneralException, IOException {
147: if (configItemId == null && productConfigItem != null) {
148: configItemId = productConfigItem.getString("configItemId");
149: }
150:
151: if (delegator == null && productConfigItem != null) {
152: delegator = productConfigItem.getDelegator();
153: }
154:
155: if (UtilValidate.isEmpty(mimeTypeId)) {
156: mimeTypeId = "text/html";
157: }
158:
159: String candidateFieldName = ModelUtil
160: .dbNameToVarName(confItemContentTypeId);
161: //Debug.logInfo("candidateFieldName=" + candidateFieldName, module);
162: ModelEntity productConfigItemModel = delegator
163: .getModelEntity("ProductConfigItem");
164: if (productConfigItemModel.isField(candidateFieldName)) {
165: if (productConfigItem == null) {
166: productConfigItem = delegator.findByPrimaryKeyCache(
167: "ProductConfigItem", UtilMisc.toMap(
168: "configItemId", configItemId));
169: }
170: if (productConfigItem != null) {
171: String candidateValue = productConfigItem
172: .getString(candidateFieldName);
173: if (UtilValidate.isNotEmpty(candidateValue)) {
174: outWriter.write(candidateValue);
175: return;
176: }
177: }
178: }
179:
180: List productConfigItemContentList = delegator.findByAndCache(
181: "ProdConfItemContent", UtilMisc.toMap("configItemId",
182: configItemId, "confItemContentTypeId",
183: confItemContentTypeId), UtilMisc
184: .toList("-fromDate"));
185: productConfigItemContentList = EntityUtil
186: .filterByDate(productConfigItemContentList);
187: GenericValue productConfigItemContent = EntityUtil
188: .getFirst(productConfigItemContentList);
189: if (productConfigItemContent != null) {
190: // when rendering the product config item content, always include the ProductConfigItem and ProdConfItemContent records that this comes from
191: Map inContext = new HashMap();
192: inContext.put("productConfigItem", productConfigItem);
193: inContext.put("productConfigItemContent",
194: productConfigItemContent);
195: ContentWorker.renderContentAsText(dispatcher, delegator,
196: productConfigItemContent.getString("contentId"),
197: outWriter, inContext, locale, mimeTypeId, false);
198: }
199: }
200: }
|