Search in sources :

Example 6 with MacroScreenRenderer

use of org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer in project ofbiz-framework by apache.

the class OutputServices method createFileFromScreen.

public static Map<String, Object> createFileFromScreen(DispatchContext dctx, Map<String, ? extends Object> serviceContext) {
    Locale locale = (Locale) serviceContext.get("locale");
    Delegator delegator = dctx.getDelegator();
    VisualTheme visualTheme = (VisualTheme) serviceContext.get("visualTheme");
    String screenLocation = (String) serviceContext.remove("screenLocation");
    Map<String, Object> screenContext = UtilGenerics.checkMap(serviceContext.remove("screenContext"));
    String contentType = (String) serviceContext.remove("contentType");
    String filePath = (String) serviceContext.remove("filePath");
    String fileName = (String) serviceContext.remove("fileName");
    if (UtilValidate.isEmpty(screenContext)) {
        screenContext = new HashMap<>();
    }
    screenContext.put("locale", locale);
    if (UtilValidate.isEmpty(contentType)) {
        contentType = "application/pdf";
    }
    try {
        MapStack<String> screenContextTmp = MapStack.create();
        screenContextTmp.put("locale", locale);
        Writer writer = new StringWriter();
        // substitute the freemarker variables...
        ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme().getType("screenfop"), visualTheme.getModelTheme().getScreenRendererLocation("screenfop"));
        ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContextTmp, foScreenStringRenderer);
        screensAtt.populateContextForService(dctx, screenContext);
        screenContextTmp.putAll(screenContext);
        screensAtt.getContext().put("formStringRenderer", foFormRenderer);
        screensAtt.render(screenLocation);
        // create the input stream for the generation
        StreamSource src = new StreamSource(new StringReader(writer.toString()));
        // create the output stream for the generation
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF);
        ApacheFopWorker.transform(src, null, fop);
        baos.flush();
        baos.close();
        fileName += UtilDateTime.nowAsString();
        if ("application/pdf".equals(contentType)) {
            fileName += ".pdf";
        } else if ("application/postscript".equals(contentType)) {
            fileName += ".ps";
        } else if ("text/plain".equals(contentType)) {
            fileName += ".txt";
        }
        if (UtilValidate.isEmpty(filePath)) {
            filePath = EntityUtilProperties.getPropertyValue("content", "content.output.path", "/output", delegator);
        }
        File file = new File(filePath, fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baos.toByteArray());
        fos.close();
    } catch (IOException | TemplateException | GeneralException | SAXException | ParserConfigurationException e) {
        Debug.logError(e, "Error rendering [" + contentType + "]: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentRenderingError", UtilMisc.toMap("contentType", contentType, "errorString", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TemplateException(freemarker.template.TemplateException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) Delegator(org.apache.ofbiz.entity.Delegator) FileOutputStream(java.io.FileOutputStream) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) File(java.io.File) Writer(java.io.Writer) StringWriter(java.io.StringWriter)

Example 7 with MacroScreenRenderer

use of org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer in project ofbiz-framework by apache.

the class OutputServices method sendPrintFromScreen.

public static Map<String, Object> sendPrintFromScreen(DispatchContext dctx, Map<String, ? extends Object> serviceContext) {
    Locale locale = (Locale) serviceContext.get("locale");
    VisualTheme visualTheme = (VisualTheme) serviceContext.get("visualTheme");
    String screenLocation = (String) serviceContext.remove("screenLocation");
    Map<String, Object> screenContext = UtilGenerics.checkMap(serviceContext.remove("screenContext"));
    String contentType = (String) serviceContext.remove("contentType");
    String printerContentType = (String) serviceContext.remove("printerContentType");
    if (UtilValidate.isEmpty(screenContext)) {
        screenContext = new HashMap<>();
    }
    screenContext.put("locale", locale);
    if (UtilValidate.isEmpty(contentType)) {
        contentType = "application/postscript";
    }
    if (UtilValidate.isEmpty(printerContentType)) {
        printerContentType = contentType;
    }
    try {
        MapStack<String> screenContextTmp = MapStack.create();
        screenContextTmp.put("locale", locale);
        Writer writer = new StringWriter();
        // substitute the freemarker variables...
        ScreenStringRenderer foScreenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme().getType("screenfop"), visualTheme.getModelTheme().getScreenRendererLocation("screenfop"));
        ScreenRenderer screensAtt = new ScreenRenderer(writer, screenContextTmp, foScreenStringRenderer);
        screensAtt.populateContextForService(dctx, screenContext);
        screenContextTmp.putAll(screenContext);
        screensAtt.getContext().put("formStringRenderer", foFormRenderer);
        screensAtt.render(screenLocation);
        // create the input stream for the generation
        StreamSource src = new StreamSource(new StringReader(writer.toString()));
        // create the output stream for the generation
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Fop fop = ApacheFopWorker.createFopInstance(baos, MimeConstants.MIME_PDF);
        ApacheFopWorker.transform(src, null, fop);
        baos.flush();
        baos.close();
        // Print is sent
        DocFlavor psInFormat = new DocFlavor.INPUT_STREAM(printerContentType);
        InputStream bais = new ByteArrayInputStream(baos.toByteArray());
        DocAttributeSet docAttributeSet = new HashDocAttributeSet();
        List<Object> docAttributes = UtilGenerics.checkList(serviceContext.remove("docAttributes"));
        if (UtilValidate.isNotEmpty(docAttributes)) {
            for (Object da : docAttributes) {
                Debug.logInfo("Adding DocAttribute: " + da, module);
                docAttributeSet.add((DocAttribute) da);
            }
        }
        Doc myDoc = new SimpleDoc(bais, psInFormat, docAttributeSet);
        PrintService printer = null;
        // lookup the print service for the supplied printer name
        String printerName = (String) serviceContext.remove("printerName");
        if (UtilValidate.isNotEmpty(printerName)) {
            PrintServiceAttributeSet printServiceAttributes = new HashPrintServiceAttributeSet();
            printServiceAttributes.add(new PrinterName(printerName, locale));
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, printServiceAttributes);
            if (printServices.length > 0) {
                printer = printServices[0];
                Debug.logInfo("Using printer: " + printer.getName(), module);
                if (!printer.isDocFlavorSupported(psInFormat)) {
                    return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotSupportDocFlavorFormat", UtilMisc.toMap("psInFormat", psInFormat, "printerName", printer.getName()), locale));
                }
            }
            if (printer == null) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotFound", UtilMisc.toMap("printerName", printerName), locale));
            }
        } else {
            // if no printer name was supplied, try to get the default printer
            printer = PrintServiceLookup.lookupDefaultPrintService();
            if (printer != null) {
                Debug.logInfo("No printer name supplied, using default printer: " + printer.getName(), module);
            }
        }
        if (printer == null) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentPrinterNotAvailable", locale));
        }
        PrintRequestAttributeSet praset = new HashPrintRequestAttributeSet();
        List<Object> printRequestAttributes = UtilGenerics.checkList(serviceContext.remove("printRequestAttributes"));
        if (UtilValidate.isNotEmpty(printRequestAttributes)) {
            for (Object pra : printRequestAttributes) {
                Debug.logInfo("Adding PrintRequestAttribute: " + pra, module);
                praset.add((PrintRequestAttribute) pra);
            }
        }
        DocPrintJob job = printer.createPrintJob();
        job.print(myDoc, praset);
    } catch (PrintException | IOException | TemplateException | GeneralException | SAXException | ParserConfigurationException e) {
        Debug.logError(e, "Error rendering [" + contentType + "]: " + e.toString(), module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentRenderingError", UtilMisc.toMap("contentType", contentType, "errorString", e.toString()), locale));
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashDocAttributeSet(javax.print.attribute.HashDocAttributeSet) DocAttributeSet(javax.print.attribute.DocAttributeSet) PrintService(javax.print.PrintService) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet) PrintRequestAttributeSet(javax.print.attribute.PrintRequestAttributeSet) SAXException(org.xml.sax.SAXException) PrintException(javax.print.PrintException) StringWriter(java.io.StringWriter) SimpleDoc(javax.print.SimpleDoc) StringReader(java.io.StringReader) Doc(javax.print.Doc) SimpleDoc(javax.print.SimpleDoc) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GeneralException(org.apache.ofbiz.base.util.GeneralException) HashDocAttributeSet(javax.print.attribute.HashDocAttributeSet) TemplateException(freemarker.template.TemplateException) Fop(org.apache.fop.apps.Fop) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DocPrintJob(javax.print.DocPrintJob) IOException(java.io.IOException) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) PrinterName(javax.print.attribute.standard.PrinterName) ByteArrayInputStream(java.io.ByteArrayInputStream) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) DocFlavor(javax.print.DocFlavor) Writer(java.io.Writer) StringWriter(java.io.StringWriter) PrintServiceAttributeSet(javax.print.attribute.PrintServiceAttributeSet) HashPrintServiceAttributeSet(javax.print.attribute.HashPrintServiceAttributeSet) HashPrintRequestAttributeSet(javax.print.attribute.HashPrintRequestAttributeSet)

Example 8 with MacroScreenRenderer

use of org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer in project ofbiz-framework by apache.

the class FoPrintServerEvents method getXslFo.

public static byte[] getXslFo(DispatchContext dctx, String screen, Map<String, Object> parameters) throws GeneralException {
    // run as the system user
    VisualTheme visualTheme = (VisualTheme) parameters.get("visualTheme");
    GenericValue system = null;
    try {
        system = dctx.getDelegator().findOne("UserLogin", false, "userLoginId", "system");
    } catch (GenericEntityException e) {
        throw new GeneralException(e.getMessage(), e);
    }
    parameters.put("userLogin", system);
    if (!parameters.containsKey("locale")) {
        parameters.put("locale", Locale.getDefault());
    }
    // render and obtain the XSL-FO
    Writer writer = new StringWriter();
    try {
        ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(visualTheme.getModelTheme().getType("screen"), visualTheme.getModelTheme().getScreenRendererLocation("screen"));
        ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
        screens.populateContextForService(dctx, parameters);
        screens.render(screen);
    } catch (Throwable t) {
        throw new GeneralException("Problems rendering FOP XSL-FO", t);
    }
    return writer.toString().getBytes();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) StringWriter(java.io.StringWriter) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer)

Aggregations

ScreenRenderer (org.apache.ofbiz.widget.renderer.ScreenRenderer)8 VisualTheme (org.apache.ofbiz.widget.renderer.VisualTheme)8 MacroScreenRenderer (org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer)8 TemplateException (freemarker.template.TemplateException)7 IOException (java.io.IOException)7 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 ScreenStringRenderer (org.apache.ofbiz.widget.renderer.ScreenStringRenderer)7 StringWriter (java.io.StringWriter)6 Writer (java.io.Writer)6 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)6 SAXException (org.xml.sax.SAXException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringReader (java.io.StringReader)4 StreamSource (javax.xml.transform.stream.StreamSource)4 Fop (org.apache.fop.apps.Fop)4 Locale (java.util.Locale)3 ModelTheme (org.apache.ofbiz.widget.model.ModelTheme)3 File (java.io.File)2 Map (java.util.Map)2 Delegator (org.apache.ofbiz.entity.Delegator)2