use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.
the class MacroScreenViewHandler method loadRenderers.
private ScreenStringRenderer loadRenderers(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context, Writer writer) throws TemplateException, IOException {
VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
ModelTheme modelTheme = visualTheme.getModelTheme();
String screenMacroLibraryPath = modelTheme.getScreenRendererLocation(getName());
String formMacroLibraryPath = modelTheme.getFormRendererLocation(getName());
String treeMacroLibraryPath = modelTheme.getTreeRendererLocation(getName());
String menuMacroLibraryPath = modelTheme.getMenuRendererLocation(getName());
ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType(getName()), screenMacroLibraryPath);
if (UtilValidate.isNotEmpty(formMacroLibraryPath)) {
FormStringRenderer formStringRenderer = new MacroFormRenderer(formMacroLibraryPath, request, response);
context.put("formStringRenderer", formStringRenderer);
}
if (UtilValidate.isNotEmpty(treeMacroLibraryPath)) {
TreeStringRenderer treeStringRenderer = new MacroTreeRenderer(treeMacroLibraryPath, writer);
context.put("treeStringRenderer", treeStringRenderer);
}
if (UtilValidate.isNotEmpty(menuMacroLibraryPath)) {
MenuStringRenderer menuStringRenderer = new MacroMenuRenderer(menuMacroLibraryPath, request, response);
context.put("menuStringRenderer", menuStringRenderer);
}
return screenStringRenderer;
}
use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.
the class MacroScreenViewHandler method render.
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
try {
Writer writer = response.getWriter();
VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
ModelTheme modelTheme = visualTheme.getModelTheme();
Delegator delegator = (Delegator) request.getAttribute("delegator");
// compress output if configured to do so
if (UtilValidate.isEmpty(encoding)) {
encoding = modelTheme.getEncoding(getName());
}
boolean compressOutput = "compressed".equals(encoding);
if (!compressOutput) {
compressOutput = "true".equals(modelTheme.getCompress(getName()));
}
if (!compressOutput && this.servletContext != null) {
compressOutput = "true".equals(this.servletContext.getAttribute("compressHTML"));
}
if (compressOutput) {
// StandardCompress defaults to a 2k buffer. That could be increased
// to speed up output.
writer = new StandardCompress().getWriter(writer, null);
}
MapStack<String> context = MapStack.create();
ScreenRenderer.populateContextForRequest(context, null, request, response, servletContext);
ScreenStringRenderer screenStringRenderer = loadRenderers(request, response, context, writer);
ScreenRenderer screens = new ScreenRenderer(writer, context, screenStringRenderer);
context.put("screens", screens);
context.put("simpleEncoder", UtilCodec.getEncoder(visualTheme.getModelTheme().getEncoder(getName())));
screenStringRenderer.renderScreenBegin(writer, context);
screens.render(page);
screenStringRenderer.renderScreenEnd(writer, context);
writer.flush();
} catch (TemplateException e) {
Debug.logError(e, "Error initializing screen renderer", module);
throw new ViewHandlerException(e.getMessage());
} catch (IOException e) {
throw new ViewHandlerException("Error in the response writer/output stream: " + e.toString(), e);
} catch (SAXException | ParserConfigurationException e) {
throw new ViewHandlerException("XML Error rendering page: " + e.toString(), e);
} catch (GeneralException e) {
throw new ViewHandlerException("Lower level error rendering page: " + e.toString(), e);
}
}
use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.
the class DataResourceWorker method renderDataResourceAsText.
public static void renderDataResourceAsText(LocalDispatcher dispatcher, Delegator delegator, String dataResourceId, Appendable out, Map<String, Object> templateContext, Locale locale, String targetMimeTypeId, boolean cache, List<GenericValue> webAnalytics) throws GeneralException, IOException {
if (delegator == null) {
delegator = dispatcher.getDelegator();
}
if (dataResourceId == null) {
throw new GeneralException("Cannot lookup data resource with for a null dataResourceId");
}
if (templateContext == null) {
templateContext = new HashMap<>();
}
if (UtilValidate.isEmpty(targetMimeTypeId)) {
targetMimeTypeId = "text/html";
}
if (locale == null) {
locale = Locale.getDefault();
}
// FIXME correctly propagate the theme, then fixes also the related FIXME below
VisualTheme visualTheme = ThemeFactory.getVisualThemeFromId("COMMON");
ModelTheme modelTheme = visualTheme.getModelTheme();
// if the target mimeTypeId is not a text type, throw an exception
if (!targetMimeTypeId.startsWith("text/")) {
throw new GeneralException("The desired mime-type is not a text type, cannot render as text: " + targetMimeTypeId);
}
// get the data resource object
GenericValue dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).cache(cache).queryOne();
if (dataResource == null) {
throw new GeneralException("No data resource object found for dataResourceId: [" + dataResourceId + "]");
}
// a data template attached to the data resource
String dataTemplateTypeId = dataResource.getString("dataTemplateTypeId");
// no template; or template is NONE; render the data
if (UtilValidate.isEmpty(dataTemplateTypeId) || "NONE".equals(dataTemplateTypeId)) {
DataResourceWorker.writeDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, out, cache);
} else {
// a template is defined; render the template first
templateContext.put("mimeTypeId", targetMimeTypeId);
// FTL template
if ("FTL".equals(dataTemplateTypeId)) {
try {
// get the template data for rendering
String templateText = getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache);
// if use web analytics.
if (UtilValidate.isNotEmpty(webAnalytics)) {
StringBuffer newTemplateText = new StringBuffer(templateText);
String webAnalyticsCode = "<script language=\"JavaScript\" type=\"text/javascript\">";
for (GenericValue webAnalytic : webAnalytics) {
StringWrapper wrapString = StringUtil.wrapString((String) webAnalytic.get("webAnalyticsCode"));
webAnalyticsCode += wrapString.toString();
}
webAnalyticsCode += "</script>";
newTemplateText.insert(templateText.lastIndexOf("</head>"), webAnalyticsCode);
templateText = newTemplateText.toString();
}
// render the FTL template
boolean useTemplateCache = cache && !UtilProperties.getPropertyAsBoolean("content", "disable.ftl.template.cache", false);
// Do not use dataResource.lastUpdatedStamp for dataResource template caching as it may use ftl file or electronicText
// If dataResource using ftl file use nowTimestamp to avoid freemarker caching
Timestamp lastUpdatedStamp = UtilDateTime.nowTimestamp();
// If dataResource is type of ELECTRONIC_TEXT then only use the lastUpdatedStamp of electronicText entity for freemarker caching
if ("ELECTRONIC_TEXT".equals(dataResource.getString("dataResourceTypeId"))) {
GenericValue electronicText = dataResource.getRelatedOne("ElectronicText", true);
if (electronicText != null) {
lastUpdatedStamp = electronicText.getTimestamp("lastUpdatedStamp");
}
}
FreeMarkerWorker.renderTemplateFromString("delegator:" + delegator.getDelegatorName() + ":DataResource:" + dataResourceId, templateText, templateContext, out, lastUpdatedStamp.getTime(), useTemplateCache);
} catch (TemplateException e) {
throw new GeneralException("Error rendering FTL template", e);
}
} else if ("XSLT".equals(dataTemplateTypeId)) {
File targetFileLocation = new File(System.getProperty("ofbiz.home") + "/runtime/tempfiles/docbook.css");
// This is related with the other FIXME above: we need to correctly propagate the theme.
String defaultVisualThemeId = EntityUtilProperties.getPropertyValue("general", "VISUAL_THEME", delegator);
visualTheme = ThemeFactory.getVisualThemeFromId(defaultVisualThemeId);
modelTheme = visualTheme.getModelTheme();
String docbookStylesheet = modelTheme.getProperty("VT_DOCBOOKSTYLESHEET").toString();
File sourceFileLocation = new File(System.getProperty("ofbiz.home") + "/themes" + docbookStylesheet.substring(1, docbookStylesheet.length() - 1));
UtilMisc.copyFile(sourceFileLocation, targetFileLocation);
// get the template data for rendering
String templateLocation = DataResourceWorker.getContentFile(dataResource.getString("dataResourceTypeId"), dataResource.getString("objectInfo"), (String) templateContext.get("contextRoot")).toString();
// render the XSLT template and file
String outDoc = null;
try {
outDoc = XslTransform.renderTemplate(templateLocation, (String) templateContext.get("docFile"));
} catch (TransformerException c) {
Debug.logError("XSL TransformerException: " + c.getMessage(), module);
}
out.append(outDoc);
// Screen Widget template
} else if ("SCREEN_COMBINED".equals(dataTemplateTypeId)) {
try {
MapStack<String> context = MapStack.create(templateContext);
context.put("locale", locale);
// prepare the map for preRenderedContent
String textData = (String) context.get("textData");
if (UtilValidate.isNotEmpty(textData)) {
Map<String, Object> prc = new HashMap<>();
String mapKey = (String) context.get("mapKey");
if (mapKey != null) {
prc.put(mapKey, mapKey);
}
// used for default screen defs
prc.put("body", textData);
context.put("preRenderedContent", prc);
}
// get the screen renderer; or create a new one
ScreenRenderer screens = (ScreenRenderer) context.get("screens");
if (screens == null) {
// TODO: replace "screen" to support dynamic rendering of different output
ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType("screen"), modelTheme.getScreenRendererLocation("screen"));
screens = new ScreenRenderer(out, context, screenStringRenderer);
screens.getContext().put("screens", screens);
}
// render the screen
ModelScreen modelScreen = null;
ScreenStringRenderer renderer = screens.getScreenStringRenderer();
String combinedName = dataResource.getString("objectInfo");
if ("URL_RESOURCE".equals(dataResource.getString("dataResourceTypeId")) && UtilValidate.isNotEmpty(combinedName) && combinedName.startsWith("component://")) {
modelScreen = ScreenFactory.getScreenFromLocation(combinedName);
} else {
// stored in a single file, long or short text
Document screenXml = UtilXml.readXmlDocument(getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache), true, true);
Map<String, ModelScreen> modelScreenMap = ScreenFactory.readScreenDocument(screenXml, "DataResourceId: " + dataResource.getString("dataResourceId"));
if (UtilValidate.isNotEmpty(modelScreenMap)) {
// get first entry, only one screen allowed per file
Map.Entry<String, ModelScreen> entry = modelScreenMap.entrySet().iterator().next();
modelScreen = entry.getValue();
}
}
if (UtilValidate.isNotEmpty(modelScreen)) {
modelScreen.renderScreenString(out, context, renderer);
} else {
throw new GeneralException("The dataResource file [" + dataResourceId + "] could not be found");
}
} catch (SAXException | ParserConfigurationException e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (TemplateException e) {
throw new GeneralException("Error creating Screen renderer", e);
}
} else if ("FORM_COMBINED".equals(dataTemplateTypeId)) {
try {
Map<String, Object> context = UtilGenerics.checkMap(templateContext.get("globalContext"));
context.put("locale", locale);
context.put("simpleEncoder", UtilCodec.getEncoder(modelTheme.getEncoder("screen")));
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
ModelForm modelForm = null;
ModelReader entityModelReader = delegator.getModelReader();
String formText = getDataResourceText(dataResource, targetMimeTypeId, locale, templateContext, delegator, cache);
Document formXml = UtilXml.readXmlDocument(formText, true, true);
Map<String, ModelForm> modelFormMap = FormFactory.readFormDocument(formXml, entityModelReader, dispatcher.getDispatchContext(), null);
if (UtilValidate.isNotEmpty(modelFormMap)) {
// get first entry, only one form allowed per file
Map.Entry<String, ModelForm> entry = modelFormMap.entrySet().iterator().next();
modelForm = entry.getValue();
}
String formrenderer = modelTheme.getFormRendererLocation("screen");
MacroFormRenderer renderer = new MacroFormRenderer(formrenderer, request, response);
FormRenderer formRenderer = new FormRenderer(modelForm, renderer);
formRenderer.render(out, context);
} catch (SAXException | ParserConfigurationException e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (TemplateException e) {
throw new GeneralException("Error creating Screen renderer", e);
} catch (Exception e) {
throw new GeneralException("Error rendering Screen template", e);
}
} else {
throw new GeneralException("The dataTemplateTypeId [" + dataTemplateTypeId + "] is not yet supported");
}
}
}
use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.
the class MacroScreenRenderer method renderLink.
public void renderLink(Appendable writer, Map<String, Object> context, ModelScreenWidget.ScreenLink link) throws IOException {
HttpServletResponse response = (HttpServletResponse) context.get("response");
HttpServletRequest request = (HttpServletRequest) context.get("request");
VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
ModelTheme modelTheme = visualTheme.getModelTheme();
String targetWindow = link.getTargetWindow(context);
String target = link.getTarget(context);
String uniqueItemName = link.getModelScreen().getName() + "_LF_" + UtilMisc.<String>addToBigDecimalInMap(context, "screenUniqueItemIndex", BigDecimal.ONE);
String linkType = WidgetWorker.determineAutoLinkType(link.getLinkType(), target, link.getUrlMode(), request);
String linkUrl = "";
String actionUrl = "";
StringBuilder parameters = new StringBuilder();
String width = link.getWidth();
if (UtilValidate.isEmpty(width)) {
width = String.valueOf(modelTheme.getLinkDefaultLayeredModalWidth());
}
String height = link.getHeight();
if (UtilValidate.isEmpty(height)) {
height = String.valueOf(modelTheme.getLinkDefaultLayeredModalHeight());
}
if ("hidden-form".equals(linkType) || "layered-modal".equals(linkType)) {
StringBuilder sb = new StringBuilder();
WidgetWorker.buildHyperlinkUrl(sb, target, link.getUrlMode(), null, link.getPrefix(context), link.getFullPath(), link.getSecure(), link.getEncode(), request, response, context);
actionUrl = sb.toString();
parameters.append("[");
for (Map.Entry<String, String> parameter : link.getParameterMap(context).entrySet()) {
if (parameters.length() > 1) {
parameters.append(",");
}
parameters.append("{'name':'");
parameters.append(parameter.getKey());
parameters.append("'");
parameters.append(",'value':'");
parameters.append(parameter.getValue());
parameters.append("'}");
}
parameters.append("]");
}
String id = link.getId(context);
String style = link.getStyle(context);
String name = link.getName(context);
String text = link.getText(context);
if (UtilValidate.isNotEmpty(target)) {
if (!"hidden-form".equals(linkType)) {
StringBuilder sb = new StringBuilder();
WidgetWorker.buildHyperlinkUrl(sb, target, link.getUrlMode(), link.getParameterMap(context), link.getPrefix(context), link.getFullPath(), link.getSecure(), link.getEncode(), request, response, context);
linkUrl = sb.toString();
}
}
String imgStr = "";
ModelScreenWidget.ScreenImage img = link.getImage();
if (img != null) {
StringWriter sw = new StringWriter();
renderImage(sw, context, img);
imgStr = sw.toString();
}
StringWriter sr = new StringWriter();
sr.append("<@renderLink ");
sr.append("parameterList=");
sr.append(parameters.length() == 0 ? "\"\"" : parameters.toString());
sr.append(" targetWindow=\"");
sr.append(targetWindow);
sr.append("\" target=\"");
sr.append(target);
sr.append("\" uniqueItemName=\"");
sr.append(uniqueItemName);
sr.append("\" linkType=\"");
sr.append(linkType);
sr.append("\" actionUrl=\"");
sr.append(actionUrl);
sr.append("\" id=\"");
sr.append(id);
sr.append("\" style=\"");
sr.append(style);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" width=\"");
sr.append(width);
sr.append("\" height=\"");
sr.append(height);
sr.append("\" linkUrl=\"");
sr.append(linkUrl);
sr.append("\" text=\"");
sr.append(text);
sr.append("\" imgStr=\"");
sr.append(imgStr.replaceAll("\"", "\\\\\""));
sr.append("\" />");
executeMacro(writer, sr.toString());
}
use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.
the class MacroScreenRenderer method renderScreenletBegin.
public void renderScreenletBegin(Appendable writer, Map<String, Object> context, boolean collapsed, ModelScreenWidget.Screenlet screenlet) throws IOException {
HttpServletRequest request = (HttpServletRequest) context.get("request");
HttpServletResponse response = (HttpServletResponse) context.get("response");
VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
ModelTheme modelTheme = visualTheme.getModelTheme();
boolean javaScriptEnabled = UtilHttp.isJavaScriptEnabled(request);
ModelScreenWidget.Menu tabMenu = screenlet.getTabMenu();
if (tabMenu != null) {
tabMenu.renderWidgetString(writer, context, this);
}
String title = screenlet.getTitle(context);
boolean collapsible = screenlet.collapsible();
ModelScreenWidget.Menu navMenu = screenlet.getNavigationMenu();
ModelScreenWidget.Form navForm = screenlet.getNavigationForm();
String expandToolTip = "";
String collapseToolTip = "";
String fullUrlString = "";
String menuString = "";
boolean showMore = false;
if (UtilValidate.isNotEmpty(title) || navMenu != null || navForm != null || collapsible) {
showMore = true;
if (collapsible) {
this.getNextElementId();
Map<String, Object> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
Map<String, Object> paramMap = UtilGenerics.checkMap(context.get("requestParameters"));
Map<String, Object> requestParameters = new HashMap<>(paramMap);
if (uiLabelMap != null) {
expandToolTip = (String) uiLabelMap.get("CommonExpand");
collapseToolTip = (String) uiLabelMap.get("CommonCollapse");
}
if (!javaScriptEnabled) {
requestParameters.put(screenlet.getPreferenceKey(context) + "_collapsed", collapsed ? "false" : "true");
String queryString = UtilHttp.urlEncodeArgs(requestParameters);
fullUrlString = request.getRequestURI() + "?" + queryString;
}
}
StringWriter sb = new StringWriter();
if (navMenu != null) {
MenuStringRenderer savedRenderer = (MenuStringRenderer) context.get("menuStringRenderer");
MenuStringRenderer renderer;
try {
renderer = new MacroMenuRenderer(modelTheme.getMenuRendererLocation("screen"), request, response);
context.put("menuStringRenderer", renderer);
navMenu.renderWidgetString(sb, context, this);
context.put("menuStringRenderer", savedRenderer);
} catch (TemplateException e) {
Debug.logError(e, module);
}
} else if (navForm != null) {
renderScreenletPaginateMenu(sb, context, navForm);
}
menuString = sb.toString();
}
Map<String, Object> parameters = new HashMap<>();
parameters.put("title", title);
parameters.put("collapsible", collapsible);
parameters.put("saveCollapsed", screenlet.saveCollapsed());
if (UtilValidate.isNotEmpty(screenlet.getId(context))) {
parameters.put("id", screenlet.getId(context));
parameters.put("collapsibleAreaId", screenlet.getId(context) + "_col");
} else {
parameters.put("id", "screenlet_" + screenLetsIdCounter);
parameters.put("collapsibleAreaId", "screenlet_" + screenLetsIdCounter + "_col");
screenLetsIdCounter++;
}
parameters.put("expandToolTip", expandToolTip);
parameters.put("collapseToolTip", collapseToolTip);
parameters.put("fullUrlString", fullUrlString);
parameters.put("padded", screenlet.padded());
parameters.put("menuString", menuString);
parameters.put("showMore", showMore);
parameters.put("collapsed", collapsed);
parameters.put("javaScriptEnabled", javaScriptEnabled);
executeMacro(writer, "renderScreenletBegin", parameters);
}
Aggregations