Search in sources :

Example 1 with EngineInstantiationException

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

the class FormPrintFactory method newInstance.

/**
 * Returns an instance of IFormPrintEngine.
 * It reads the 'framework.printing.formPrintFactory' property from the framework.properties file.
 * It then instantiates the class returned by the property.
 *
 * @return an instance of IFormPrintEngine.
 * @throws EngineInstantiationException if the property is not defined, or if
 *                                      the class cannot be found, or if the class canot be instantiated.
 */
public static IFormPrintEngine newInstance(String engineType) throws EngineInstantiationException {
    if (log.isDebugEnabled())
        log.debug("Creating an instance of a FormPrintEngine, type=" + engineType);
    if (externalFactory != null) {
        IFormPrintEngine engine = externalFactory.newInstance(engineType);
        if (engine != null) {
            return engine;
        }
    }
    Class engineClass = null;
    String rule = "jaffa.printing.formPrintEngines." + engineType;
    String engineClassName = (String) ContextManagerFactory.instance().getProperty(rule);
    if (engineClassName == null) {
        if (engineType.equals("default"))
            engineClass = FormPrintEngineIText.class;
        else {
            String str = "Unknown Engine Type '" + engineType + "'. Should be defined under the name " + rule;
            log.error(str);
            throw new EngineInstantiationException(new String[] { str });
        }
    }
    if (engineClass == null)
        try {
            engineClass = Class.forName(engineClassName);
        } catch (ClassNotFoundException e) {
            String str = "Class " + engineClass + " not found for Engine Type '" + engineType + "'";
            log.error(str);
            throw new EngineInstantiationException(new String[] { str });
        }
    if (!IFormPrintEngine.class.isAssignableFrom(engineClass)) {
        String str = "Engine Class " + engineClass + " must implement interface IFormPrintEngine";
        log.error(str);
        throw new EngineInstantiationException(new String[] { str });
    }
    try {
        if (log.isDebugEnabled())
            log.debug("Creating an instance of the Form Print Engine: " + engineType + " -> " + engineClassName);
        return (IFormPrintEngine) engineClass.newInstance();
    } catch (Exception e) {
        String str = "The Form Print Engine '" + engineClassName + "' could not be instantiated";
        log.error(str, e);
        throw new EngineInstantiationException(new String[] { str }, e);
    }
}
Also used : EngineInstantiationException(org.jaffa.modules.printing.services.exceptions.EngineInstantiationException) EngineInstantiationException(org.jaffa.modules.printing.services.exceptions.EngineInstantiationException)

Example 2 with EngineInstantiationException

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

Aggregations

EngineInstantiationException (org.jaffa.modules.printing.services.exceptions.EngineInstantiationException)2 DocumentException (com.lowagie.text.DocumentException)1 IOException (java.io.IOException)1 EngineProcessingException (org.jaffa.modules.printing.services.exceptions.EngineProcessingException)1 FormPrintException (org.jaffa.modules.printing.services.exceptions.FormPrintException)1 PdfProcessingException (org.jaffa.modules.printing.services.exceptions.PdfProcessingException)1