Search in sources :

Example 1 with FormCache

use of org.jaffa.modules.printing.services.FormCache in project jaffa-framework by jaffa-projects.

the class VelocityTemplateHelper method renderTemplateLines.

public String[] renderTemplateLines() throws FormPrintException {
    String[] lines;
    String line;
    if (log.isDebugEnabled())
        log.debug("Begin Velocity template rendering process... ");
    if (getDataSource() == null) {
        log.debug("Data source is not set, returning.");
        return null;
    }
    if (getTemplate() == null) {
        log.debug("Template definition is not set, returning.");
        return null;
    }
    try {
        // Since Velocity can only be initialized one time, set the template path used by label printing.
        FormCache cache = new FormCache();
        String path = cache.getTemplatePath();
        Velocity.setProperty("file.resource.loader.path", path);
        Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
        // init the runtime engine.  Use mostly defaulted values.
        Velocity.init();
        if (log.isDebugEnabled()) {
            log.debug("Initialized Velocity");
            log.debug("Velocity file.resource.loader.path property = " + Velocity.getProperty("file.resource.loader.path"));
        }
    } catch (Exception e) {
        String err = "Velocity Initialization - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    // make a Context and put data into it
    if (log.isDebugEnabled())
        log.debug("Set Up Context");
    VelocityContext context = new VelocityContext();
    context.put("data", getDataSource());
    fmt = new FormFormatHelper();
    context.put("fmt", fmt);
    context.put("context", ContextManagerFactory.instance().getThreadContext());
    if (log.isDebugEnabled()) {
        log.debug("*** Template Source Data = \n" + BeanMoulder.printBean(getDataSource()) + "\n*** End of Source Data.");
        log.debug("*** Velocity Template = \n" + getTemplate() + "\n*** End of Velocity Template.");
    }
    try {
        m_output = new StringWriter();
        if (log.isDebugEnabled())
            log.debug("Evaluate the Template... ");
        // Render the template
        Velocity.evaluate(context, m_output, "formTemplate", getTemplate());
        line = m_output.toString();
        lines = line.split("\n");
        if (log.isDebugEnabled()) {
            log.debug("*** Rendered Template = \n" + line + "\n*** End of Rendered Template.");
        }
    } catch (Exception e) {
        String err = "Velocity Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    if (log.isDebugEnabled())
        log.debug("Finished Velocity template rendering process. ");
    return lines;
}
Also used : FormCache(org.jaffa.modules.printing.services.FormCache) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Aggregations

IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 VelocityContext (org.apache.velocity.VelocityContext)1 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)1 ParseErrorException (org.apache.velocity.exception.ParseErrorException)1 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)1 FormCache (org.jaffa.modules.printing.services.FormCache)1 EngineProcessingException (org.jaffa.modules.printing.services.exceptions.EngineProcessingException)1 FormPrintException (org.jaffa.modules.printing.services.exceptions.FormPrintException)1