Search in sources :

Example 1 with ModelTheme

use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.

the class HtmlTreeRenderer method getScreenStringRenderer.

public ScreenStringRenderer getScreenStringRenderer(Map<String, Object> context) {
    VisualTheme visualTheme = (VisualTheme) context.get("visualTheme");
    ModelTheme modelTheme = visualTheme.getModelTheme();
    ScreenRenderer screenRenderer = (ScreenRenderer) context.get("screens");
    if (screenRenderer != null) {
        screenStringRenderer = screenRenderer.getScreenStringRenderer();
    } else {
        if (screenStringRenderer == null) {
            try {
                screenStringRenderer = new MacroScreenRenderer(modelTheme.getType("screen"), modelTheme.getScreenRendererLocation("screen"));
            } catch (TemplateException | IOException e) {
                Debug.logError(e, module);
            }
        }
    }
    return screenStringRenderer;
}
Also used : TemplateException(freemarker.template.TemplateException) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) IOException(java.io.IOException) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer)

Example 2 with ModelTheme

use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.

the class MacroFormRenderer method makeHyperlinkByType.

public void makeHyperlinkByType(Appendable writer, String linkType, String linkStyle, String targetType, String target, Map<String, String> parameterMap, String description, String targetWindow, String confirmation, ModelFormField modelFormField, HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) throws IOException {
    String realLinkType = WidgetWorker.determineAutoLinkType(linkType, target, targetType, request);
    String encodedDescription = encode(description, modelFormField, context);
    // get the parameterized pagination index and size fields
    int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
    ModelForm modelForm = modelFormField.getModelForm();
    ModelTheme modelTheme = visualTheme.getModelTheme();
    String viewIndexField = modelForm.getMultiPaginateIndexField(context);
    String viewSizeField = modelForm.getMultiPaginateSizeField(context);
    int viewIndex = Paginator.getViewIndex(modelForm, context);
    int viewSize = Paginator.getViewSize(modelForm, context);
    if (("viewIndex" + "_" + paginatorNumber).equals(viewIndexField)) {
        viewIndexField = "VIEW_INDEX" + "_" + paginatorNumber;
    }
    if (("viewSize" + "_" + paginatorNumber).equals(viewSizeField)) {
        viewSizeField = "VIEW_SIZE" + "_" + paginatorNumber;
    }
    if ("hidden-form".equals(realLinkType)) {
        parameterMap.put(viewIndexField, Integer.toString(viewIndex));
        parameterMap.put(viewSizeField, Integer.toString(viewSize));
        if ("multi".equals(modelForm.getType())) {
            WidgetWorker.makeHiddenFormLinkAnchor(writer, linkStyle, encodedDescription, confirmation, modelFormField, request, response, context);
            // this is a bit trickier, since we can't do a nested form we'll have to put the link to submit the form in place, but put the actual form def elsewhere, ie after the big form is closed
            Map<String, Object> wholeFormContext = UtilGenerics.checkMap(context.get("wholeFormContext"));
            Appendable postMultiFormWriter = wholeFormContext != null ? (Appendable) wholeFormContext.get("postMultiFormWriter") : null;
            if (postMultiFormWriter == null) {
                postMultiFormWriter = new StringWriter();
                wholeFormContext.put("postMultiFormWriter", postMultiFormWriter);
            }
            WidgetWorker.makeHiddenFormLinkForm(postMultiFormWriter, target, targetType, targetWindow, parameterMap, modelFormField, request, response, context);
        } else {
            WidgetWorker.makeHiddenFormLinkForm(writer, target, targetType, targetWindow, parameterMap, modelFormField, request, response, context);
            WidgetWorker.makeHiddenFormLinkAnchor(writer, linkStyle, encodedDescription, confirmation, modelFormField, request, response, context);
        }
    } else {
        if ("layered-modal".equals(realLinkType)) {
            String uniqueItemName = "Modal_".concat(UUID.randomUUID().toString().replace("-", "_"));
            String width = (String) this.request.getAttribute("width");
            if (UtilValidate.isEmpty(width)) {
                width = String.valueOf(modelTheme.getLinkDefaultLayeredModalWidth());
                this.request.setAttribute("width", width);
            }
            String height = (String) this.request.getAttribute("height");
            if (UtilValidate.isEmpty(height)) {
                height = String.valueOf(modelTheme.getLinkDefaultLayeredModalHeight());
                this.request.setAttribute("height", height);
            }
            this.request.setAttribute("uniqueItemName", uniqueItemName);
            makeHyperlinkString(writer, linkStyle, targetType, target, parameterMap, encodedDescription, confirmation, modelFormField, request, response, context, targetWindow);
            this.request.removeAttribute("uniqueItemName");
            this.request.removeAttribute("height");
            this.request.removeAttribute("width");
        } else {
            makeHyperlinkString(writer, linkStyle, targetType, target, parameterMap, encodedDescription, confirmation, modelFormField, request, response, context, targetWindow);
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) ModelForm(org.apache.ofbiz.widget.model.ModelForm)

Example 3 with ModelTheme

use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.

the class MacroScreenRenderer method renderScreenletSubWidget.

public void renderScreenletSubWidget(Appendable writer, Map<String, Object> context, ModelScreenWidget subWidget, ModelScreenWidget.Screenlet screenlet) throws GeneralException, IOException {
    if (subWidget.equals(screenlet.getNavigationForm())) {
        HttpServletRequest request = (HttpServletRequest) context.get("request");
        HttpServletResponse response = (HttpServletResponse) context.get("response");
        VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
        ModelTheme modelTheme = visualTheme.getModelTheme();
        if (request != null && response != null) {
            Map<String, Object> globalCtx = UtilGenerics.checkMap(context.get("globalContext"));
            globalCtx.put("NO_PAGINATOR", true);
            FormStringRenderer savedRenderer = (FormStringRenderer) context.get("formStringRenderer");
            MacroFormRenderer renderer = null;
            try {
                renderer = new MacroFormRenderer(modelTheme.getFormRendererLocation("screen"), request, response);
            } catch (TemplateException e) {
                Debug.logError("Not rendering content, error on MacroFormRenderer creation.", module);
            }
            renderer.setRenderPagination(false);
            context.put("formStringRenderer", renderer);
            subWidget.renderWidgetString(writer, context, this);
            context.put("formStringRenderer", savedRenderer);
        }
    } else {
        subWidget.renderWidgetString(writer, context, this);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) TemplateException(freemarker.template.TemplateException) FormStringRenderer(org.apache.ofbiz.widget.renderer.FormStringRenderer) HttpServletResponse(javax.servlet.http.HttpServletResponse) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme)

Example 4 with ModelTheme

use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.

the class ScreenFopViewHandler method render.

/**
 * @see org.apache.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
    ModelTheme modelTheme = visualTheme.getModelTheme();
    // render and obtain the XSL-FO
    Writer writer = new StringWriter();
    try {
        ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType(getName()), modelTheme.getScreenRendererLocation(getName()));
        FormStringRenderer formStringRenderer = new MacroFormRenderer(modelTheme.getFormRendererLocation(getName()), request, response);
        // TODO: uncomment these lines when the renderers are implemented
        // TreeStringRenderer treeStringRenderer = new MacroTreeRenderer(modelTheme.getTreeRendererLocation(getName()), writer);
        // MenuStringRenderer menuStringRenderer = new MacroMenuRenderer(modelTheme.getMenuRendererLocation(getName()), writer);
        ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
        screens.populateContextForRequest(request, response, servletContext);
        // this is the object used to render forms from their definitions
        screens.getContext().put("formStringRenderer", formStringRenderer);
        screens.getContext().put("simpleEncoder", UtilCodec.getEncoder(modelTheme.getEncoder(getName())));
        screens.render(page);
    } catch (IOException | GeneralException | SAXException | ParserConfigurationException | TemplateException e) {
        renderError("Problems with the response writer/output stream", e, "[Not Yet Rendered]", request, response);
        return;
    }
    // set the input source (XSL-FO) and generate the output stream of contentType
    String screenOutString = writer.toString();
    if (!screenOutString.startsWith("<?xml")) {
        screenOutString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + screenOutString;
    }
    if (Debug.verboseOn())
        Debug.logVerbose("XSL:FO Screen Output: " + screenOutString, module);
    if (UtilValidate.isEmpty(contentType)) {
        contentType = modelTheme.getContentType(getName());
    }
    // get encryption related parameters
    FOUserAgent foUserAgent = null;
    String userPassword = request.getParameter("userPassword");
    String ownerPassword = request.getParameter("ownerPassword");
    boolean allowPrint = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrint")) ? ApacheFopWorker.getAllowPrintDefault() : request.getParameter("allowPrint"));
    boolean allowCopyContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowCopyContent")) ? ApacheFopWorker.getAllowCopyContentDefault() : request.getParameter("allowCopyContent"));
    boolean allowEditContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditContent")) ? ApacheFopWorker.getAllowEditContentDefault() : request.getParameter("allowEditContent"));
    boolean allowEditAnnotations = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowEditAnnotations")) ? ApacheFopWorker.getAllowEditAnnotationsDefault() : request.getParameter("allowEditAnnotations"));
    if (UtilValidate.isNotEmpty(userPassword) || UtilValidate.isNotEmpty(ownerPassword) || !allowPrint || !allowCopyContent || allowEditContent || !allowEditAnnotations) {
        int encryptionLength = 128;
        try {
            encryptionLength = Integer.parseInt(request.getParameter("encryption-length"));
        } catch (NumberFormatException e) {
            try {
                encryptionLength = Integer.parseInt(ApacheFopWorker.getEncryptionLengthDefault());
            } catch (NumberFormatException e1) {
            // ignore
            }
        }
        boolean encryptMetadata = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("encrypt-metadata")) ? ApacheFopWorker.getEncryptMetadataDefault() : request.getParameter("encrypt-metadata"));
        boolean allowFillInForms = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowFillInForms")) ? ApacheFopWorker.getAllowFillInFormsDefault() : request.getParameter("allowFillInForms"));
        boolean allowAccessContent = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAccessContent")) ? ApacheFopWorker.getAllowAccessContentDefault() : request.getParameter("allowAccessContent"));
        boolean allowAssembleDocument = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowAssembleDocument")) ? ApacheFopWorker.getAllowAssembleDocumentDefault() : request.getParameter("allowAssembleDocument"));
        boolean allowPrintHq = Boolean.parseBoolean(UtilValidate.isEmpty(request.getParameter("allowPrintHq")) ? ApacheFopWorker.getAllowPrintHqDefault() : request.getParameter("allowPrintHq"));
        FopFactory fopFactory = ApacheFopWorker.getFactoryInstance();
        foUserAgent = fopFactory.newFOUserAgent();
        PDFEncryptionParams pdfEncryptionParams = new PDFEncryptionParams(userPassword, ownerPassword, allowPrint, allowCopyContent, allowEditContent, allowEditAnnotations, encryptMetadata);
        pdfEncryptionParams.setAllowFillInForms(allowFillInForms);
        pdfEncryptionParams.setAllowAccessContent(allowAccessContent);
        pdfEncryptionParams.setAllowAssembleDocument(allowAssembleDocument);
        pdfEncryptionParams.setAllowPrintHq(allowPrintHq);
        pdfEncryptionParams.setEncryptionLengthInBits(encryptionLength);
        foUserAgent.getRendererOptions().put(PDFEncryptionOption.ENCRYPTION_PARAMS, pdfEncryptionParams);
    }
    Reader reader = new StringReader(screenOutString);
    StreamSource src = new StreamSource(reader);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    /* Debug area, uncomment this to view the xml file generate before analyse by fop
        try { 
                java.io.FileWriter fw = new java.io.FileWriter(new java.io.File("/tmp/temp.xsl.fo"));
                fw.write(screenOutString);
                fw.close();
            } catch (IOException e) {
                Debug.logError(e, "Couldn't save xls debug file: " + e.toString(), module);
            }
        */
    try {
        Fop fop = ApacheFopWorker.createFopInstance(out, contentType, foUserAgent);
        ApacheFopWorker.transform(src, null, fop);
    } catch (Exception e) {
        renderError("Unable to transform FO file", e, screenOutString, request, response);
        return;
    }
    // set the content type and length
    response.setContentType(contentType);
    response.setContentLength(out.size());
    // write to the browser
    try {
        out.writeTo(response.getOutputStream());
        response.getOutputStream().flush();
    } catch (IOException e) {
        renderError("Unable to write to OutputStream", e, screenOutString, request, response);
    }
}
Also used : FOUserAgent(org.apache.fop.apps.FOUserAgent) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) MacroFormRenderer(org.apache.ofbiz.widget.renderer.macro.MacroFormRenderer) Reader(java.io.Reader) StringReader(java.io.StringReader) FopFactory(org.apache.fop.apps.FopFactory) 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) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) TemplateException(freemarker.template.TemplateException) ViewHandlerException(org.apache.ofbiz.webapp.view.ViewHandlerException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) FormStringRenderer(org.apache.ofbiz.widget.renderer.FormStringRenderer) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) StringWriter(java.io.StringWriter) Writer(java.io.Writer) PDFEncryptionParams(org.apache.fop.pdf.PDFEncryptionParams)

Example 5 with ModelTheme

use of org.apache.ofbiz.widget.model.ModelTheme in project ofbiz-framework by apache.

the class ScreenFopViewHandler method renderError.

protected void renderError(String msg, Exception e, String screenOutString, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
    Debug.logError(msg + ": " + e + "; Screen XSL:FO text was:\n" + screenOutString, module);
    try {
        Delegator delegator = (Delegator) request.getAttribute("delegator");
        Writer writer = new StringWriter();
        VisualTheme visualTheme = UtilHttp.getVisualTheme(request);
        ModelTheme modelTheme = visualTheme.getModelTheme();
        ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(modelTheme.getType("screen"), modelTheme.getScreenRendererLocation("screen"));
        ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
        screens.populateContextForRequest(request, response, servletContext);
        screens.getContext().put("errorMessage", msg + ": " + e);
        screens.render(DEFAULT_ERROR_TEMPLATE);
        response.setContentType("text/html");
        response.getWriter().write(writer.toString());
        writer.close();
    } catch (IOException | GeneralException | SAXException | ParserConfigurationException | TemplateException x) {
        Debug.logError("Multiple errors rendering FOP", module);
        throw new ViewHandlerException("Multiple errors rendering FOP", x);
    }
}
Also used : ViewHandlerException(org.apache.ofbiz.webapp.view.ViewHandlerException) GeneralException(org.apache.ofbiz.base.util.GeneralException) TemplateException(freemarker.template.TemplateException) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) ScreenRenderer(org.apache.ofbiz.widget.renderer.ScreenRenderer) ModelTheme(org.apache.ofbiz.widget.model.ModelTheme) IOException(java.io.IOException) ScreenStringRenderer(org.apache.ofbiz.widget.renderer.ScreenStringRenderer) MacroScreenRenderer(org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer) SAXException(org.xml.sax.SAXException) Delegator(org.apache.ofbiz.entity.Delegator) StringWriter(java.io.StringWriter) VisualTheme(org.apache.ofbiz.widget.renderer.VisualTheme) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Aggregations

ModelTheme (org.apache.ofbiz.widget.model.ModelTheme)10 VisualTheme (org.apache.ofbiz.widget.renderer.VisualTheme)9 TemplateException (freemarker.template.TemplateException)7 IOException (java.io.IOException)5 StringWriter (java.io.StringWriter)5 ScreenRenderer (org.apache.ofbiz.widget.renderer.ScreenRenderer)5 ScreenStringRenderer (org.apache.ofbiz.widget.renderer.ScreenStringRenderer)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 GeneralException (org.apache.ofbiz.base.util.GeneralException)4 MacroScreenRenderer (org.apache.ofbiz.widget.renderer.macro.MacroScreenRenderer)4 SAXException (org.xml.sax.SAXException)4 Writer (java.io.Writer)3 HashMap (java.util.HashMap)3 Delegator (org.apache.ofbiz.entity.Delegator)3 ViewHandlerException (org.apache.ofbiz.webapp.view.ViewHandlerException)3 FormStringRenderer (org.apache.ofbiz.widget.renderer.FormStringRenderer)3 Map (java.util.Map)2 WeakHashMap (java.util.WeakHashMap)2