use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.
the class LoopSubContentTransform method getWriter.
@Override
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final StringBuilder buf = new StringBuilder();
final Environment env = Environment.getCurrentEnvironment();
final Map<String, Object> templateCtx = FreeMarkerWorker.getWrappedObject("context", env);
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final Map<String, Object> savedValues = FreeMarkerWorker.saveValues(templateCtx, saveKeyNames);
FreeMarkerWorker.overrideWithArgs(templateCtx, args);
String contentAssocTypeId = (String) templateCtx.get("contentAssocTypeId");
if (UtilValidate.isEmpty(contentAssocTypeId)) {
contentAssocTypeId = "SUB_CONTENT";
templateCtx.put("contentAssocTypeId ", contentAssocTypeId);
}
List<String> assocTypes = UtilMisc.toList(contentAssocTypeId);
templateCtx.put("assocTypes", assocTypes);
Locale locale = (Locale) templateCtx.get("locale");
if (locale == null) {
locale = Locale.getDefault();
templateCtx.put("locale", locale);
}
String fromDateStr = (String) templateCtx.get("fromDateStr");
Timestamp fromDate = null;
if (UtilValidate.isNotEmpty(fromDateStr)) {
fromDate = UtilDateTime.toTimestamp(fromDateStr);
}
if (fromDate == null) {
fromDate = UtilDateTime.nowTimestamp();
}
String thisContentId = (String) templateCtx.get("contentId");
// DEJ20080730 Should always use contentId, not subContentId since we're searching for that and it is confusing
String thisMapKey = (String) templateCtx.get("mapKey");
Map<String, Object> results = ContentServicesComplex.getAssocAndContentAndDataResourceMethod(delegator, thisContentId, thisMapKey, null, fromDate, null, null, null, assocTypes, null);
List<GenericValue> entityList = UtilGenerics.checkList(results.get("entityList"));
templateCtx.put("entityList", entityList);
return new LoopWriter(out) {
@Override
public void write(char[] cbuf, int off, int len) {
buf.append(cbuf, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public int onStart() throws TemplateModelException, IOException {
templateCtx.put("entityIndex", Integer.valueOf(0));
boolean inProgress = prepCtx(delegator, templateCtx);
if (inProgress) {
return TransformControl.EVALUATE_BODY;
}
return TransformControl.SKIP_BODY;
}
@Override
public int afterBody() throws TemplateModelException, IOException {
boolean inProgress = prepCtx(delegator, templateCtx);
if (inProgress) {
return TransformControl.REPEAT_EVALUATION;
}
return TransformControl.END_EVALUATION;
}
@Override
public void close() throws IOException {
String wrappedFTL = buf.toString();
String encloseWrappedText = (String) templateCtx.get("encloseWrappedText");
if (UtilValidate.isEmpty(encloseWrappedText) || "false".equalsIgnoreCase(encloseWrappedText)) {
out.write(wrappedFTL);
// So it won't get written again below.
wrappedFTL = "";
}
String wrapTemplateId = (String) templateCtx.get("wrapTemplateId");
if (UtilValidate.isNotEmpty(wrapTemplateId)) {
templateCtx.put("wrappedFTL", wrappedFTL);
Map<String, Object> templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
templateRoot.put("wrapDataResourceTypeId", templateCtx.get("subDataResourceTypeId"));
templateRoot.put("wrapContentIdTo", templateCtx.get("contentId"));
templateRoot.put("wrapMimeTypeId", templateCtx.get("mimeTypeId"));
templateRoot.put("context", templateCtx);
Locale locale = (Locale) templateCtx.get("locale");
if (locale == null) {
locale = Locale.getDefault();
}
String mimeTypeId = (String) templateCtx.get("mimeTypeId");
try {
ContentWorker.renderContentAsText(dispatcher, wrapTemplateId, out, templateRoot, locale, mimeTypeId, null, null, true);
} catch (GeneralException e) {
Debug.logError(e, "Error rendering content", module);
throw new IOException("Error rendering content" + e.toString());
}
} else {
if (UtilValidate.isNotEmpty(wrappedFTL)) {
out.write(wrappedFTL);
}
}
FreeMarkerWorker.removeValues(templateCtx, removeKeyNames);
FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
}
};
}
use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.
the class RenderSubContentCacheTransform method getWriter.
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final Environment env = Environment.getCurrentEnvironment();
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
final HttpServletResponse response = FreeMarkerWorker.getWrappedObject("response", env);
final Map<String, Object> templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
FreeMarkerWorker.getSiteParameters(request, templateRoot);
final Map<String, Object> savedValuesUp = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
FreeMarkerWorker.overrideWithArgs(templateRoot, args);
final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
List<Map<String, ? extends Object>> trail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String contentAssocPredicateId = (String) templateRoot.get("contentAssocPredicateId");
String strNullThruDatesOnly = (String) templateRoot.get("nullThruDatesOnly");
Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
String thisSubContentId = (String) templateRoot.get("subContentId");
final boolean directAssocMode = UtilValidate.isNotEmpty(thisSubContentId) ? true : false;
GenericValue val = null;
try {
val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
} catch (GeneralException e) {
throw new RuntimeException("Error getting current content. " + e.toString());
}
final GenericValue view = val;
String dataResourceId = null;
String subContentIdSub = null;
if (view != null) {
try {
dataResourceId = (String) view.get("drDataResourceId");
} catch (IllegalArgumentException e) {
dataResourceId = (String) view.get("dataResourceId");
}
subContentIdSub = (String) view.get("contentId");
}
// This order is taken so that the dataResourceType can be overridden in the transform arguments.
String subDataResourceTypeId = (String) templateRoot.get("subDataResourceTypeId");
if (UtilValidate.isEmpty(subDataResourceTypeId) && view != null) {
try {
subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
} catch (IllegalArgumentException e) {
// view may be "Content"
}
// TODO: If this value is still empty then it is probably necessary to get a value from
// the parent context. But it will already have one and it is the same context that is
// being passed.
}
String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateRoot);
templateRoot.put("drDataResourceId", dataResourceId);
templateRoot.put("mimeTypeId", mimeTypeId);
templateRoot.put("dataResourceId", dataResourceId);
templateRoot.put("subContentId", subContentIdSub);
templateRoot.put("subDataResourceTypeId", subDataResourceTypeId);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
try {
renderSubContent();
FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}
public void renderSubContent() throws IOException {
List<Map<String, ? extends Object>> passedGlobalNodeTrail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String editRequestName = (String) templateRoot.get("editRequestName");
GenericValue thisView = null;
if (view != null) {
thisView = view;
} else if (passedGlobalNodeTrail.size() > 0) {
Map<String, ? extends Object> map = UtilGenerics.checkMap(passedGlobalNodeTrail.get(passedGlobalNodeTrail.size() - 1));
if (Debug.infoOn()) {
Debug.logInfo("in Render(3), map ." + map, module);
}
if (map != null) {
thisView = (GenericValue) map.get("value");
}
}
String mimeTypeId = (String) templateRoot.get("mimeTypeId");
Locale locale = (Locale) templateRoot.get("locale");
if (locale == null)
locale = Locale.getDefault();
if (UtilValidate.isNotEmpty(editRequestName)) {
String editStyle = getEditStyle();
openEditWrap(out, editStyle);
}
if (thisView != null) {
String contentId = thisView.getString("contentId");
if (contentId != null) {
try {
ContentWorker.renderContentAsText(dispatcher, contentId, out, templateRoot, locale, mimeTypeId, null, null, true);
} catch (GeneralException e) {
Debug.logError(e, "Error rendering content", module);
throw new IOException("Error rendering thisView:" + thisView + " msg:" + e.toString());
}
}
}
if (UtilValidate.isNotEmpty(editRequestName)) {
closeEditWrap(out, editRequestName);
}
}
public void openEditWrap(Writer out, String editStyle) throws IOException {
String divStr = "<div class=\"" + editStyle + "\">";
out.write(divStr);
}
public void closeEditWrap(Writer out, String editRequestName) throws IOException {
String fullRequest = editRequestName;
String contentId = null;
String contentIdTo = null;
String contentAssocTypeId = null;
String mapKey = null;
String fromDate = null;
if (!directAssocMode) {
contentIdTo = (String) templateRoot.get("contentId");
contentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
mapKey = (String) templateRoot.get("mapKey");
fromDate = (String) templateRoot.get("fromDate");
if (Debug.infoOn())
Debug.logInfo("in Render(0), view ." + view, module);
if (view != null) {
ModelEntity modelEntity = view.getModelEntity();
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("caContentId") != null)
contentId = view.getString("caContentId");
if (UtilValidate.isEmpty(contentId) && modelEntity.getField("contentId") != null)
contentId = view.getString("contentId");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("caContentIdTo") != null)
contentIdTo = view.getString("caContentIdTo");
if (UtilValidate.isEmpty(contentIdTo) && modelEntity.getField("contentIdTo") != null)
contentIdTo = view.getString("contentIdTo");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("caContentAssocTypeId") != null)
contentAssocTypeId = view.getString("caContentAssocTypeId");
if (UtilValidate.isEmpty(contentAssocTypeId) && modelEntity.getField("contentAssocTypeId") != null)
contentAssocTypeId = view.getString("contentAssocTypeId");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("caMapKey") != null)
mapKey = view.getString("caMapKey");
if (UtilValidate.isEmpty(mapKey) && modelEntity.getField("mapKey") != null)
mapKey = view.getString("mapKey");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("caFromDate") != null)
fromDate = view.getString("caFromDate");
if (UtilValidate.isEmpty(fromDate) && modelEntity.getField("fromDate") != null)
fromDate = view.getString("fromDate");
}
} else {
contentId = (String) templateRoot.get("subContentId");
}
if (Debug.infoOn())
Debug.logInfo("in Render(0), contentIdTo ." + contentIdTo, module);
String delim = "?";
if (UtilValidate.isNotEmpty(contentId)) {
fullRequest += delim + "contentId=" + contentId;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentIdTo)) {
fullRequest += delim + "contentIdTo=" + contentIdTo;
delim = "&";
}
if (UtilValidate.isNotEmpty(contentAssocTypeId)) {
fullRequest += delim + "contentAssocTypeId=" + contentAssocTypeId;
delim = "&";
}
if (UtilValidate.isNotEmpty(mapKey)) {
fullRequest += delim + "mapKey=" + mapKey;
delim = "&";
}
if (UtilValidate.isNotEmpty(fromDate)) {
fullRequest += delim + "fromDate=" + fromDate;
delim = "&";
}
if (Debug.infoOn())
Debug.logInfo("in Render(2), contentIdTo ." + contentIdTo, module);
out.write("<a href=\"");
ServletContext servletContext = request.getSession().getServletContext();
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
out.append(rh.makeLink(request, response, "/" + fullRequest, false, false, true));
out.write("\">Edit</a>");
out.write("</div>");
}
public String getEditStyle() {
String editStyle = (String) templateRoot.get("editStyle");
if (UtilValidate.isEmpty(editStyle)) {
editStyle = UtilProperties.getPropertyValue("content", "defaultEditStyle");
}
if (UtilValidate.isEmpty(editStyle)) {
editStyle = "buttontext";
}
return editStyle;
}
};
}
use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.
the class TraverseSubContentCacheTransform method getWriter.
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final StringBuilder buf = new StringBuilder();
final Environment env = Environment.getCurrentEnvironment();
final Map<String, Object> templateRoot = FreeMarkerWorker.createEnvironmentMap(env);
final Map<String, Object> savedValuesUp = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateRoot, upSaveKeyNames, savedValuesUp);
final Map<String, Object> savedValues = new HashMap<String, Object>();
FreeMarkerWorker.overrideWithArgs(templateRoot, args);
String startContentAssocTypeId = (String) templateRoot.get("contentAssocTypeId");
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
FreeMarkerWorker.getSiteParameters(request, templateRoot);
final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
List<Map<String, ? extends Object>> globalNodeTrail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
String strNullThruDatesOnly = (String) templateRoot.get("nullThruDatesOnly");
String contentAssocPredicateId = (String) templateRoot.get("contentAssocPredicateId");
Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
try {
// getCurrentContent puts the "current" node on the end of globalNodeTrail.
// It may have already been there, but getCurrentContent will compare its contentId
// to values in templateRoot.
ContentWorker.getCurrentContent(delegator, globalNodeTrail, userLogin, templateRoot, nullThruDatesOnly, contentAssocPredicateId);
} catch (GeneralException e) {
throw new RuntimeException("Error getting current content. " + e.toString());
}
final Map<String, Object> traverseContext = new HashMap<String, Object>();
traverseContext.put("delegator", delegator);
Map<String, Object> whenMap = new HashMap<String, Object>();
whenMap.put("followWhen", templateRoot.get("followWhen"));
whenMap.put("pickWhen", templateRoot.get("pickWhen"));
whenMap.put("returnBeforePickWhen", templateRoot.get("returnBeforePickWhen"));
whenMap.put("returnAfterPickWhen", templateRoot.get("returnAfterPickWhen"));
traverseContext.put("whenMap", whenMap);
env.setVariable("whenMap", FreeMarkerWorker.autoWrap(whenMap, env));
String fromDateStr = (String) templateRoot.get("fromDateStr");
String thruDateStr = (String) templateRoot.get("thruDateStr");
Timestamp fromDate = null;
if (UtilValidate.isNotEmpty(fromDateStr)) {
fromDate = UtilDateTime.toTimestamp(fromDateStr);
}
traverseContext.put("fromDate", fromDate);
Timestamp thruDate = null;
if (UtilValidate.isNotEmpty(thruDateStr)) {
thruDate = UtilDateTime.toTimestamp(thruDateStr);
}
traverseContext.put("thruDate", thruDate);
traverseContext.put("contentAssocTypeId", startContentAssocTypeId);
String direction = (String) templateRoot.get("direction");
if (UtilValidate.isEmpty(direction)) {
direction = "From";
}
traverseContext.put("direction", direction);
return new LoopWriter(out) {
@Override
public void write(char[] cbuf, int off, int len) {
buf.append(cbuf, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public int onStart() throws TemplateModelException, IOException {
List<Map<String, ? extends Object>> nodeTrail = null;
Map<String, Object> node = null;
List<Map<String, ? extends Object>> globalNodeTrail = UtilGenerics.checkList(templateRoot.get("globalNodeTrail"));
if (globalNodeTrail.size() > 0) {
int sz = globalNodeTrail.size();
nodeTrail = new LinkedList<Map<String, ? extends Object>>();
node = UtilGenerics.checkMap(globalNodeTrail.get(sz - 1));
Boolean checkedObj = (Boolean) node.get("checked");
Map<String, Object> whenMap = UtilGenerics.checkMap(templateRoot.get("whenMap"));
if (checkedObj == null || !checkedObj.booleanValue()) {
ContentWorker.checkConditions(delegator, node, null, whenMap);
}
} else {
throw new IOException("Empty node trail entries");
}
Boolean isReturnBeforePickBool = (Boolean) node.get("isReturnBeforePick");
if (isReturnBeforePickBool != null && isReturnBeforePickBool.booleanValue()) {
return TransformControl.SKIP_BODY;
}
ContentWorker.selectKids(node, traverseContext);
nodeTrail.add(node);
traverseContext.put("nodeTrail", nodeTrail);
Boolean isPickBool = (Boolean) node.get("isPick");
Boolean isFollowBool = (Boolean) node.get("isFollow");
boolean isPick = true;
if ((isPickBool == null || !isPickBool.booleanValue()) && (isFollowBool != null && isFollowBool.booleanValue())) {
isPick = ContentWorker.traverseSubContent(traverseContext);
}
if (isPick) {
populateContext(traverseContext, templateRoot);
FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues);
return TransformControl.EVALUATE_BODY;
} else {
return TransformControl.SKIP_BODY;
}
}
@Override
public int afterBody() throws TemplateModelException, IOException {
FreeMarkerWorker.reloadValues(templateRoot, savedValues, env);
boolean inProgress = ContentWorker.traverseSubContent(traverseContext);
if (inProgress) {
populateContext(traverseContext, templateRoot);
FreeMarkerWorker.saveContextValues(templateRoot, saveKeyNames, savedValues);
return TransformControl.REPEAT_EVALUATION;
} else {
return TransformControl.END_EVALUATION;
}
}
@Override
public void close() throws IOException {
FreeMarkerWorker.reloadValues(templateRoot, savedValuesUp, env);
String wrappedContent = buf.toString();
out.write(wrappedContent);
}
public void populateContext(Map<String, Object> traverseContext, Map<String, Object> templateContext) {
List<Map<String, ? extends Object>> nodeTrail = UtilGenerics.checkList(traverseContext.get("nodeTrail"));
int sz = nodeTrail.size();
Map<String, ? extends Object> node = nodeTrail.get(sz - 1);
GenericValue content = (GenericValue) node.get("value");
String contentId = (String) node.get("contentId");
String contentAssocTypeId = (String) node.get("contentAssocTypeId");
envWrap("contentAssocTypeId", contentAssocTypeId);
envWrap("contentId", contentId);
envWrap("content", content);
String mapKey = (String) node.get("mapKey");
envWrap("mapKey", mapKey);
envWrap("subContentDataResourceView", null);
List<Map<String, ? extends Object>> globalNodeTrail = UtilGenerics.checkList(templateContext.get("nodeTrail"));
String contentIdEnd = null;
String contentIdStart = null;
if (globalNodeTrail != null) {
Map<String, ? extends Object> ndEnd = globalNodeTrail.get(globalNodeTrail.size() - 1);
contentIdEnd = (String) ndEnd.get("contentId");
Map<String, ? extends Object> ndStart = nodeTrail.get(0);
contentIdStart = (String) ndStart.get("contentId");
} else {
globalNodeTrail = new LinkedList<Map<String, ? extends Object>>();
contentIdStart = "";
}
boolean bIdEnd = UtilValidate.isNotEmpty(contentIdEnd);
boolean bIdStart = UtilValidate.isNotEmpty(contentIdStart);
boolean bEquals = contentIdStart.equals(contentIdEnd);
if (bIdEnd && bIdStart && bEquals) {
List<Map<String, ? extends Object>> subList = nodeTrail.subList(1, sz);
globalNodeTrail.addAll(subList);
} else {
globalNodeTrail.addAll(nodeTrail);
}
int indentSz = globalNodeTrail.size();
envWrap("indent", Integer.valueOf(indentSz));
String trailCsv = ContentWorker.nodeTrailToCsv(globalNodeTrail);
envWrap("nodeTrailCsv", trailCsv);
envWrap("globalNodeTrail", globalNodeTrail);
}
public void envWrap(String varName, Object obj) {
templateRoot.put(varName, obj);
env.setVariable(varName, FreeMarkerWorker.autoWrap(obj, env));
}
};
}
use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.
the class WrapSubContentCacheTransform method getWriter.
@SuppressWarnings("unchecked")
public Writer getWriter(final Writer out, Map args) {
final StringBuilder buf = new StringBuilder();
final Environment env = Environment.getCurrentEnvironment();
Map<String, Object> envContext = FreeMarkerWorker.getWrappedObject("context", env);
final Map<String, Object> templateCtx;
if (envContext == null) {
templateCtx = FreeMarkerWorker.createEnvironmentMap(env);
} else {
templateCtx = envContext;
}
final LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
final Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
final HttpServletRequest request = FreeMarkerWorker.getWrappedObject("request", env);
FreeMarkerWorker.getSiteParameters(request, templateCtx);
final Map<String, Object> savedValuesUp = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateCtx, upSaveKeyNames, savedValuesUp);
FreeMarkerWorker.overrideWithArgs(templateCtx, args);
final String wrapTemplateId = (String) templateCtx.get("wrapTemplateId");
final GenericValue userLogin = FreeMarkerWorker.getWrappedObject("userLogin", env);
List<Map<String, ? extends Object>> trail = UtilGenerics.checkList(templateCtx.get("globalNodeTrail"));
String contentAssocPredicateId = (String) templateCtx.get("contentAssocPredicateId");
String strNullThruDatesOnly = (String) templateCtx.get("nullThruDatesOnly");
Boolean nullThruDatesOnly = (strNullThruDatesOnly != null && "true".equalsIgnoreCase(strNullThruDatesOnly)) ? Boolean.TRUE : Boolean.FALSE;
GenericValue val = null;
try {
val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
} catch (GeneralException e) {
throw new RuntimeException("Error getting current content. " + e.toString());
}
final GenericValue view = val;
String dataResourceId = null;
try {
dataResourceId = (String) view.get("drDataResourceId");
} catch (IllegalArgumentException e) {
dataResourceId = (String) view.get("dataResourceId");
}
String subContentIdSub = (String) view.get("contentId");
// This order is taken so that the dataResourceType can be overridden in the transform arguments.
String subDataResourceTypeId = (String) templateCtx.get("subDataResourceTypeId");
if (UtilValidate.isEmpty(subDataResourceTypeId)) {
try {
subDataResourceTypeId = (String) view.get("drDataResourceTypeId");
} catch (IllegalArgumentException e) {
// view may be "Content"
}
// TODO: If this value is still empty then it is probably necessary to get a value from
// the parent context. But it will already have one and it is the same context that is
// being passed.
}
final Map<String, Object> savedValues = new HashMap<String, Object>();
FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
// This order is taken so that the mimeType can be overridden in the transform arguments.
String mimeTypeId = ContentWorker.getMimeTypeId(delegator, view, templateCtx);
templateCtx.put("drDataResourceId", dataResourceId);
templateCtx.put("mimeTypeId", mimeTypeId);
templateCtx.put("dataResourceId", dataResourceId);
templateCtx.put("subContentIdSub", subContentIdSub);
templateCtx.put("subDataResourceTypeId", subDataResourceTypeId);
// Not something to pass on
templateCtx.put("wrapTemplateId", null);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) {
buf.append(cbuf, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
String wrappedContent = buf.toString();
if (UtilValidate.isNotEmpty(wrapTemplateId)) {
templateCtx.put("wrappedContent", wrappedContent);
Map<String, Object> templateRoot = null;
Map<String, Object> templateRootTemplate = UtilGenerics.checkMap(templateCtx.get("templateRootTemplate"));
if (templateRootTemplate == null) {
Map<String, Object> templateRootTmp = FreeMarkerWorker.createEnvironmentMap(env);
templateRoot = UtilMisc.makeMapWritable(templateRootTmp);
templateCtx.put("templateRootTemplate", templateRootTmp);
} else {
templateRoot = UtilMisc.makeMapWritable(templateRootTemplate);
}
templateRoot.put("context", templateCtx);
String mimeTypeId = (String) templateCtx.get("mimeTypeId");
Locale locale = (Locale) templateCtx.get("locale");
if (locale == null) {
locale = Locale.getDefault();
}
try {
ContentWorker.renderContentAsText(dispatcher, wrapTemplateId, out, templateRoot, locale, mimeTypeId, null, null, true);
} catch (IOException e) {
Debug.logError(e, "Error rendering content" + e.getMessage(), module);
throw new IOException("Error rendering content" + e.toString());
} catch (GeneralException e2) {
Debug.logError(e2, "Error rendering content" + e2.getMessage(), module);
throw new IOException("Error rendering content" + e2.toString());
}
FreeMarkerWorker.reloadValues(templateCtx, savedValuesUp, env);
}
}
};
}
use of org.apache.ofbiz.base.util.GeneralException in project ofbiz-framework by apache.
the class ShoppingCartHelper method addToCartBulkRequirements.
/**
* Adds a set of requirements to the cart.
*/
public Map<String, Object> addToCartBulkRequirements(String catalogId, Map<String, ? extends Object> context) {
String itemGroupNumber = (String) context.get("itemGroupNumber");
// check if we are using per row submit
boolean useRowSubmit = (!context.containsKey("_useRowSubmit")) ? false : "Y".equalsIgnoreCase((String) context.get("_useRowSubmit"));
// The number of multi form rows is retrieved
int rowCount = UtilHttp.getMultiFormRowCount(context);
// assume that the facility is the same for all requirements
String facilityId = (String) context.get("facilityId_o_0");
if (UtilValidate.isNotEmpty(facilityId)) {
cart.setFacilityId(facilityId);
}
// now loop throw the rows and prepare/invoke the service for each
for (int i = 0; i < rowCount; i++) {
String productId = null;
String quantStr = null;
String requirementId = null;
String thisSuffix = UtilHttp.getMultiRowDelimiter() + i;
boolean rowSelected = (!context.containsKey("_rowSubmit" + thisSuffix)) ? false : "Y".equalsIgnoreCase((String) context.get("_rowSubmit" + thisSuffix));
// make sure we are to process this row
if (useRowSubmit && !rowSelected) {
continue;
}
// build the context
if (context.containsKey("productId" + thisSuffix)) {
productId = (String) context.get("productId" + thisSuffix);
quantStr = (String) context.get("quantity" + thisSuffix);
requirementId = (String) context.get("requirementId" + thisSuffix);
GenericValue requirement = null;
try {
requirement = EntityQuery.use(delegator).from("Requirement").where("requirementId", requirementId).queryOne();
} catch (GenericEntityException gee) {
Debug.logError(gee, module);
}
if (requirement == null) {
return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "OrderRequirementDoesNotExists", UtilMisc.toMap("requirementId", requirementId), cart.getLocale()));
}
if (UtilValidate.isNotEmpty(quantStr)) {
BigDecimal quantity;
try {
quantity = (BigDecimal) ObjectType.simpleTypeConvert(quantStr, "BigDecimal", null, cart.getLocale());
} catch (GeneralException ge) {
quantity = BigDecimal.ZERO;
}
if (quantity.compareTo(BigDecimal.ZERO) > 0) {
Iterator<ShoppingCartItem> items = this.cart.iterator();
boolean requirementAlreadyInCart = false;
while (items.hasNext() && !requirementAlreadyInCart) {
ShoppingCartItem sci = items.next();
if (sci.getRequirementId() != null && sci.getRequirementId().equals(requirementId)) {
requirementAlreadyInCart = true;
continue;
}
}
if (requirementAlreadyInCart) {
if (Debug.warningOn()) {
Debug.logWarning(UtilProperties.getMessage(resource_error, "OrderTheRequirementIsAlreadyInTheCartNotAdding", UtilMisc.toMap("requirementId", requirementId), cart.getLocale()), module);
}
continue;
}
try {
if (Debug.verboseOn()) {
Debug.logVerbose("Bulk Adding to cart requirement [" + quantity + "] of [" + productId + "]", module);
}
int index = this.cart.addOrIncreaseItem(productId, null, quantity, null, null, null, requirement.getTimestamp("requiredByDate"), null, null, null, catalogId, null, null, itemGroupNumber, null, dispatcher);
ShoppingCartItem sci = this.cart.items().get(index);
sci.setRequirementId(requirementId);
} catch (CartItemModifyException | ItemNotFoundException e) {
return ServiceUtil.returnError(e.getMessage());
}
}
}
}
}
// Indicate there were no non critical errors
return ServiceUtil.returnSuccess();
}
Aggregations