Search in sources :

Example 11 with EngineProcessingException

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

the class FormPrintEnginePdfLib method initEngine.

/**
 * Perform any initialization activity for generating the document
 */
protected void initEngine() throws FormPrintException {
    if (log.isDebugEnabled())
        log.debug("Instantiate the PDFLib engine");
    try {
        m_pdf = new pdflib();
        m_pdf.set_parameter("SearchPath", getTemplateSearchPath());
        // Set permissions if there are any
        if (m_permissions != null) {
            m_pdf.set_parameter("masterpassword", "anabahabic");
            m_pdf.set_parameter("permissions", m_permissions);
        }
        // Generate a PDF in memory; insert a file name to create PDF on disk
        if (m_pdf.begin_document("", "") == -1) {
            log.error("Creating Blank PDF - " + m_pdf.get_errmsg());
        }
    } catch (PDFlibException e) {
        log.error("PDFlibException: " + m_pdf.get_errmsg());
        throw new EngineProcessingException(m_pdf.get_errmsg());
    }
}
Also used : PDFlibException(com.pdflib.PDFlibException) com.pdflib.pdflib(com.pdflib.pdflib) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Example 12 with EngineProcessingException

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

the class FormPrintEngineVelocity method generate.

public void generate() throws FormPrintException {
    // make a Context and put data into it
    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());
    log.debug("data=" + BeanMoulder.printBean(getDataSource()));
    // Split the template file between path and name
    File templateFile = new File(getTemplateName());
    if (!templateFile.exists()) {
        String err = "Velocity Template Not Found - " + getTemplateName();
        log.error(err);
        throw new EngineProcessingException(err);
    }
    String path = templateFile.getParent();
    // (templateFile.getParent()==null?getTemplateName():getTemplateName().substring(templateFile.getParent().length()));
    String file = templateFile.getName();
    if (path != null) {
        Velocity.setProperty("file.resource.loader.path", path);
    }
    log.debug("SearchPath = " + path + ", template = " + file);
    // init the runtime engine.  Defaults are fine.
    try {
        Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
        Velocity.init();
        log.debug("Initialized Velocity");
    } catch (Exception e) {
        String err = "Velocity Initialization - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    // Try and load the template
    Template template = null;
    try {
        log.debug("Velocity file.resource.loader.path property = " + Velocity.getProperty("file.resource.loader.path"));
        template = Velocity.getTemplate(file);
    } catch (ResourceNotFoundException e) {
        String err = "Error opening template - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    } catch (ParseErrorException e) {
        String err = "Template Parse Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    } catch (Exception e) {
        String err = "Template Load Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
    try {
        m_output = new StringWriter();
        // render a template
        template.merge(context, m_output);
        // log.debug("Generated outbut based on template...\n" + m_output.getBuffer().toString());
        m_processed = true;
    } catch (Exception e) {
        String err = "Velocity Error - " + e.getLocalizedMessage();
        log.error(err, e);
        throw new EngineProcessingException(err, e);
    }
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) SplitString(org.jaffa.util.SplitString) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) 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) Template(org.apache.velocity.Template)

Example 13 with EngineProcessingException

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

the class MultiFormPrintEngine method generate.

/**
 * The core method in the engine is the generate() method, it must only be
 * called once, and must be called before you call any method that
 * accessed the generated PDF ( like writeForm() or getGeneratedForm() )
 *
 * @throws FormPrintException This is thrown if there is any error in generating the PDF document.
 * The details will be in the message text
 */
public void generate() throws FormPrintException {
    if (m_processed)
        throw new IllegalStateException("The form has already been processed");
    int pageOffset = 0;
    // Create a factory for each object, and initialize
    int index = -1;
    for (String templateName : m_templateFilenames) {
        index++;
        // Create a print engine
        IFormPrintEngine engine = FormPrintFactory.newInstance(m_engineType);
        if (engine == null) {
            String err = "No Engine Created. Type=" + m_engineType;
            if (log.isDebugEnabled())
                log.error(err);
            throw new EngineInstantiationException(err);
        }
        m_engines.add(engine);
        // Set all values on print engine
        engine.setTemplateName(templateName);
        engine.setDataSource(m_objectModels.get(index));
        engine.setPermissions(m_canPrint, m_canCopy, m_canModify);
        if (m_documentProperties != null)
            engine.setDocumentProperties(m_documentProperties);
        if (m_searchPath != null)
            engine.setTemplateSearchPath(m_searchPath);
        engine.initialize();
        m_totalPages += engine.getTotalPages();
    }
    // IFormPrintEngine engine = it.next();
    for (IFormPrintEngine engine : m_engines) {
        // Set all values on print engine
        engine.setTotalPagesOverride(m_totalPages);
        engine.setCurrentPageOffset(pageOffset);
        engine.generate();
        m_documents.add(engine.getGeneratedForm());
        pageOffset += engine.getTotalPages();
    }
    // Now merge the outputs
    try {
        if (FormPrintFactory.ENGINE_TYPE_ITEXT.equals(m_engineType) || FormPrintFactory.ENGINE_TYPE_FOP.equals(m_engineType) || FormPrintFactory.ENGINE_TYPE_PDFLIB.equals(m_engineType)) {
            m_output = mergePdf(m_documents);
            if (getPageSize() != null) {
                m_output = PdfHelper.scalePdfPages(m_output, getPageSize(), false, true);
            }
        } else {
            m_output = mergeText(m_documents);
        }
    } catch (FormPrintException fpe) {
        throw fpe;
    } catch (Exception e) {
        log.error("Failed to merge PDF documents", e);
        throw new EngineProcessingException("Merge Failed", e);
    }
    m_processed = true;
}
Also used : EngineInstantiationException(org.jaffa.modules.printing.services.exceptions.EngineInstantiationException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) IOException(java.io.IOException) DocumentException(com.lowagie.text.DocumentException) EngineInstantiationException(org.jaffa.modules.printing.services.exceptions.EngineInstantiationException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException) PdfProcessingException(org.jaffa.modules.printing.services.exceptions.PdfProcessingException) FormPrintException(org.jaffa.modules.printing.services.exceptions.FormPrintException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Example 14 with EngineProcessingException

use of org.jaffa.modules.printing.services.exceptions.EngineProcessingException 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)

Example 15 with EngineProcessingException

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

the class DomainDataBean method populate.

public void populate() throws FrameworkException, ApplicationExceptions {
    // Lookup the "findByPK" method for this domain object
    log.debug("Get the findByPK method for " + m_beanClass.getName());
    try {
        for (Method method : m_beanClass.getDeclaredMethods()) {
            if (method.getName().equals("findByPK") && method.getParameterTypes()[0].equals(UOW.class) && method.getParameterTypes().length == m_keys.size() + 1) {
                // We have found a sutable method
                log.debug("Found Method : " + method.toString());
                Object[] args = new Object[method.getParameterTypes().length];
                args[0] = (UOW) null;
                int argCount = 1;
                for (Object arg : m_keys) {
                    log.debug("    Argument " + argCount + " = " + arg);
                    args[argCount++] = arg;
                }
                try {
                    m_domainObject = method.invoke(null, args);
                    log.debug("Found Domain Object : " + m_domainObject);
                } catch (IllegalAccessException e) {
                    log.error("Can't invoke " + method.toString(), e);
                    throw new EngineProcessingException(e.getMessage(), e);
                } catch (InvocationTargetException e) {
                    log.error("Can't invoke " + method.toString(), e);
                    throw new EngineProcessingException(e.getMessage(), e);
                }
                if (m_domainObject == null) {
                    throw new DataNotFoundException(new String[] { "No Method", m_beanClass.getName() });
                }
                return;
            }
        }
        // Error - method not found
        throw new DataNotFoundException("No findByPK method");
    } catch (ApplicationException e) {
        throw new ApplicationExceptions(e);
    }
}
Also used : DataNotFoundException(org.jaffa.modules.printing.services.exceptions.DataNotFoundException) ApplicationException(org.jaffa.exceptions.ApplicationException) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Aggregations

EngineProcessingException (org.jaffa.modules.printing.services.exceptions.EngineProcessingException)18 PDFlibException (com.pdflib.PDFlibException)7 IOException (java.io.IOException)7 FormPrintException (org.jaffa.modules.printing.services.exceptions.FormPrintException)5 File (java.io.File)4 DocumentException (com.lowagie.text.DocumentException)3 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 StringWriter (java.io.StringWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 VelocityContext (org.apache.velocity.VelocityContext)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 ParseErrorException (org.apache.velocity.exception.ParseErrorException)2 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)2 BadElementException (com.lowagie.text.BadElementException)1 Document (com.lowagie.text.Document)1 Image (com.lowagie.text.Image)1 Phrase (com.lowagie.text.Phrase)1