}
public static void renderDataResourceAsText(Delegator delegator, String dataResourceId, Appendable out,
Map<String, Object> templateContext, Locale locale, String targetMimeTypeId, boolean cache) throws GeneralException, IOException {
if (dataResourceId == null) {
throw new GeneralException("Cannot lookup data resource with for a null dataResourceId");
}
if (templateContext == null) {
templateContext = FastMap.newInstance();
}
if (UtilValidate.isEmpty(targetMimeTypeId)) {
targetMimeTypeId = "text/html";
}
if (locale == null) {
locale = Locale.getDefault();
}
// check for a cached template
if (cache) {
String disableCache = UtilProperties.getPropertyValue("content", "disable.ftl.template.cache");
if (disableCache == null || !disableCache.equalsIgnoreCase("true")) {
try {
Template cachedTemplate = FreeMarkerWorker.getTemplate("DataResource:" + dataResourceId);
if (cachedTemplate != null) {
String subContentId = (String) templateContext.get("subContentId");
if (UtilValidate.isNotEmpty(subContentId)) {
templateContext.put("contentId", subContentId);
templateContext.put("subContentId", null);
templateContext.put("globalNodeTrail", null); // Force getCurrentContent to query for subContent
}
FreeMarkerWorker.renderTemplate(cachedTemplate, templateContext, out);
}
} catch (TemplateException e) {
Debug.logError("Error rendering FTL template. " + e.getMessage(), module);
throw new GeneralException("Error rendering FTL template", e);
}
return;
}
}
// 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 = null;
if (cache) {
dataResource = delegator.findByPrimaryKeyCache("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
} else {
dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId));
}
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, true);
} 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);
// render the FTL template
FreeMarkerWorker.renderTemplate("DataResource:" + dataResourceId, templateText, templateContext, out);
} catch (TemplateException e) {
throw new GeneralException("Error rendering FTL template", e);
}
} else if ("XSLT".equals(dataTemplateTypeId)) {
File sourceFileLocation = null;
File targetFileLocation = new File(System.getProperty("ofbiz.home")+"/runtime/tempfiles/docbook.css");
if (templateContext.get("visualThemeId") != null) {
Map<String, Object> layoutSettings = UtilGenerics.checkMap(templateContext.get("layoutSettings"));
List<String> docbookStyleSheets = UtilGenerics.checkList(layoutSettings.get("VT_DOCBOOKSTYLESHEET"));
String docbookStyleLocation = docbookStyleSheets.get(0);
sourceFileLocation = new File(System.getProperty("ofbiz.home")+"/themes"+docbookStyleLocation);
}
if (sourceFileLocation != null && sourceFileLocation.exists()) {
UtilMisc.copyFile(sourceFileLocation,targetFileLocation);
} else {
String defaultVisualThemeId = UtilProperties.getPropertyValue("general", "VISUAL_THEME");
if (defaultVisualThemeId != null) {
GenericValue themeValue = delegator.findByPrimaryKeyCache("VisualThemeResource", UtilMisc.toMap("visualThemeId", defaultVisualThemeId, "resourceTypeEnumId", "VT_DOCBOOKSTYLESHEET", "sequenceId", "01"));
sourceFileLocation = new File(System.getProperty("ofbiz.home") + "/themes" + themeValue.get("resourceValue"));
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 = FastMap.newInstance();
String mapKey = (String) context.get("mapKey");
if (mapKey != null) {
prc.put(mapKey, mapKey);
}
prc.put("body", textData); // used for default screen defs
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(UtilProperties.getPropertyValue("widget", "screen.name"), UtilProperties.getPropertyValue("widget", "screen.screenrenderer"));
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)) {
Map.Entry<String, ModelScreen> entry = modelScreenMap.entrySet().iterator().next(); // get first entry, only one screen allowed per file
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 e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (ParserConfigurationException e) {
throw new GeneralException("Error rendering Screen template", e);
} catch (TemplateException e) {
throw new GeneralException("Error creating Screen renderer", e);
}
} else {
throw new GeneralException("The dataTemplateTypeId [" + dataTemplateTypeId + "] is not yet supported");
}
}
}