edea003b by Adam Heath

Allow for a "templateLocation" to be used instead of just "template";

this is the first step towards removing the driverup hard-coded values.
1 parent f55c2104
......@@ -3,6 +3,7 @@ package com.brainfood.ofbiz;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
......@@ -16,6 +17,7 @@ import java.io.IOException;
import org.ofbiz.base.util.UtilIO;
import org.ofbiz.base.component.ComponentConfig;
import org.ofbiz.base.component.ComponentException;
import org.ofbiz.base.location.FlexibleLocation;
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
......@@ -24,13 +26,25 @@ import groovy.lang.Writable;
public class LibreOfficeRenderer {
public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context)
throws InterruptedException, IOException, ClassNotFoundException, ComponentException {
String templateLocation = (String) context.get("_templateLocation");
Object templateFile = context.get("_template");
Map<String, String> envVars = new HashMap<String, String>();
String template;
if (templateFile != null) {
String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString();
String template = UtilIO.readString(new FileInputStream(fullPath));
template = UtilIO.readString(new FileInputStream(fullPath));
envVars.put("HOME", "/home/driverup");
envVars.put("USERNAME", "driverup");
} else if (templateLocation != null) {
template = UtilIO.readString(FlexibleLocation.resolveLocation(templateLocation).openConnection().getInputStream());
envVars.put("HOME", "/home/ofbiz");
envVars.put("USERNAME", "ofbiz");
} else {
template = null;
}
if (template != null) {
// FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it.
context.put("Linux_X86_64", "$Linux_X86_64");
context.put("Build", "$Build");
......@@ -53,8 +67,7 @@ public class LibreOfficeRenderer {
ProcessBuilder pb = new ProcessBuilder("libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", outputFile.getParentFile().getPath());
Map<String, String> env = pb.environment();
env.put("HOME", "/home/driverup");
env.put("USERNAME", "driverup");
env.putAll(envVars);
Process process = pb.start();
InputStream is = process.getInputStream();
process.waitFor();
......