Search in sources :

Example 1 with FormTemplate

use of org.jaffa.modules.printing.domain.FormTemplate in project jaffa-framework by jaffa-projects.

the class FormDefinitionViewerTx method loadFormTemplate.

public byte[] loadFormTemplate(FormDefinitionViewerInDto input) throws FrameworkException, ApplicationExceptions {
    UOW uow = new UOW();
    try {
        Criteria criteria = new Criteria();
        criteria.setTable(FormTemplateMeta.getName());
        criteria.addCriteria(FormTemplateMeta.FORM_ID, input.getFormId());
        Iterator itr = uow.query(criteria).iterator();
        if (itr.hasNext()) {
            FormTemplate formTemplate = (FormTemplate) itr.next();
            formTemplateContents = formTemplate.getTemplateData();
        }
    } finally {
        if (uow != null)
            uow.rollback();
    }
    return formTemplateContents;
}
Also used : FormTemplate(org.jaffa.modules.printing.domain.FormTemplate) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW)

Example 2 with FormTemplate

use of org.jaffa.modules.printing.domain.FormTemplate in project jaffa-framework by jaffa-projects.

the class FormCache method getTemplate.

/**
 * Get the template file out of the BLOB's, write them to a file, and
 * return the path to that file.
 * A temp location for this can be defined, and in this case it will
 * cache them there and only read them from the database if they don't exist,
 * or are older that the database time stamp.
 */
public String getTemplate(FormDefinition form, String engineType) throws FrameworkException {
    String extn = ".tpl";
    if (FormPrintFactory.ENGINE_TYPE_ITEXT.equals(engineType) || FormPrintFactory.ENGINE_TYPE_PDFLIB.equals(engineType)) {
        extn = ".pdf";
    } else if (FormPrintFactory.ENGINE_TYPE_FOP.equals(engineType)) {
        extn = ".xslt";
    }
    File temp = new File(getTemplatePath(), form.getFormId() + extn);
    // See if a cached version can be returned
    if (temp.exists() && ((form.getLastChangedOn() == null && form.getCreatedOn() != null && temp.lastModified() > form.getCreatedOn().timeInMillis()) || (form.getLastChangedOn() != null && temp.lastModified() > form.getLastChangedOn().timeInMillis()))) {
        // If this is itext, make sure the CSV file is also current
        if (FormPrintFactory.ENGINE_TYPE_ITEXT.equals(engineType)) {
            File tempCsv = new File(getTemplatePath(), form.getFormId() + extn + ".csv");
            // See if a cached version can be returned
            if (tempCsv.exists() && ((form.getLastChangedOn() == null && form.getCreatedOn() != null && tempCsv.lastModified() > form.getCreatedOn().timeInMillis()) || (form.getLastChangedOn() != null && tempCsv.lastModified() > form.getLastChangedOn().timeInMillis()))) {
                log.debug("Use Cached Template/CSV :" + temp.getAbsolutePath());
                return temp.getAbsolutePath();
            }
        } else {
            log.debug("Use Cached Template :" + temp.getAbsolutePath());
            return temp.getAbsolutePath();
        }
    }
    // Extract this from the Database
    FormTemplate formTemp = form.getFormTemplateObject();
    if (formTemp == null)
        return null;
    // Write out the template file
    if (temp.exists())
        temp.delete();
    if (formTemp.getTemplateData() != null) {
        if (!temp.getParentFile().exists())
            temp.getParentFile().mkdirs();
        try {
            if (FormPrintFactory.ENGINE_TYPE_FOP.equals(engineType)) {
                InputStream xsltTransformer = URLHelper.getInputStream(XSLT_TRANSFORMER);
                try {
                    // Do all necessary transformations to the form XSLT file
                    XmlTransformerUtil.transform(formTemp.getTemplateData(), xsltTransformer, temp);
                } catch (TransformerException e) {
                    String err = "Failed to Transform and write the Form Template " + temp.getAbsolutePath();
                    log.error(err, e);
                    throw new EngineProcessingException(err, e);
                }
            } else {
                try (FileOutputStream fos = new FileOutputStream(temp)) {
                    fos.write(formTemp.getTemplateData());
                }
            }
            log.debug("Written Template File " + temp.getAbsolutePath());
        } catch (IOException e) {
            String err = "Failed to write Template File " + temp.getAbsolutePath();
            log.error(err, e);
            throw new EngineProcessingException(err, e);
        }
    } else {
        log.warn("No Template In Form Definition " + form.getFormId() + "(Name=" + form.getFormName() + ",Alt=" + form.getFormAlternate() + ",Variation=" + form.getFormVariation() + ",Output" + form.getOutputType());
    }
    File tempLayout = null;
    // If the engine type is itext, specify where the CSV file should be written
    if (FormPrintFactory.ENGINE_TYPE_ITEXT.equals(engineType))
        tempLayout = new File(getTemplatePath(), form.getFormId() + ".pdf.csv");
    // See if there is layout data needed
    if (tempLayout != null) {
        if (tempLayout.exists())
            tempLayout.delete();
        if (formTemp.getLayoutData() != null) {
            try (FileOutputStream fos = new FileOutputStream(tempLayout)) {
                fos.write(formTemp.getLayoutData());
                log.debug("Written Template Layout File " + tempLayout.getAbsolutePath());
            } catch (IOException e) {
                String err = "Failed to write Layout File " + tempLayout.getAbsolutePath();
                log.error(err, e);
                throw new EngineProcessingException(err, e);
            }
        }
    }
    // return the temp file
    return temp.getAbsolutePath();
}
Also used : InputStream(java.io.InputStream) FormTemplate(org.jaffa.modules.printing.domain.FormTemplate) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) EngineProcessingException(org.jaffa.modules.printing.services.exceptions.EngineProcessingException)

Example 3 with FormTemplate

use of org.jaffa.modules.printing.domain.FormTemplate in project jaffa-framework by jaffa-projects.

the class FormTemplateLookupTx method buildDto.

// .//GEN-END:_buildCriteria_3_be
// .//GEN-BEGIN:_buildDto_1_be
private FormTemplateLookupOutDto buildDto(UOW uow, Collection results, FormTemplateLookupInDto input) throws UOWException {
    FormTemplateLookupOutDto output = new FormTemplateLookupOutDto();
    int maxRecords = input.getMaxRecords() != null ? input.getMaxRecords().intValue() : 0;
    int counter = 0;
    for (Iterator i = results.iterator(); i.hasNext(); ) {
        if (++counter > maxRecords && maxRecords > 0) {
            output.setMoreRecordsExist(Boolean.TRUE);
            break;
        }
        FormTemplateLookupOutRowDto row = new FormTemplateLookupOutRowDto();
        FormTemplate formTemplate = (FormTemplate) i.next();
        // .//GEN-END:_buildDto_1_be
        // Add custom code before all the setters //GEN-FIRST:_buildDto_1
        // .//GEN-LAST:_buildDto_1
        // .//GEN-BEGIN:_buildDto_FormId_1_be
        row.setFormId(formTemplate.getFormId());
        // .//GEN-END:_buildDto_FormId_1_be
        // .//GEN-BEGIN:_buildDto_TemplateData_1_be
        row.setTemplateData(formTemplate.getTemplateData());
        // .//GEN-END:_buildDto_TemplateData_1_be
        // .//GEN-BEGIN:_buildDto_LayoutData_1_be
        row.setLayoutData(formTemplate.getLayoutData());
        // .//GEN-END:_buildDto_LayoutData_1_be
        // Add custom code to pass values to the dto //GEN-FIRST:_buildDto_2
        // .//GEN-LAST:_buildDto_2
        // .//GEN-BEGIN:_buildDto_3_be
        output.addRows(row);
    }
    return output;
}
Also used : FormTemplateLookupOutDto(org.jaffa.modules.printing.components.formtemplatelookup.dto.FormTemplateLookupOutDto) FormTemplate(org.jaffa.modules.printing.domain.FormTemplate) FormTemplateLookupOutRowDto(org.jaffa.modules.printing.components.formtemplatelookup.dto.FormTemplateLookupOutRowDto)

Example 4 with FormTemplate

use of org.jaffa.modules.printing.domain.FormTemplate in project jaffa-framework by jaffa-projects.

the class FormTemplateMaintenanceTx method load.

// .//GEN-END:_preprocessUpdate_2_be
// .//GEN-BEGIN:_loadUpdate_1_be
/**
 * Retrieve the domain object.
 */
private FormTemplate load(UOW uow, FormTemplateMaintenanceUpdateInDto input) throws FrameworkException, ApplicationExceptions {
    FormTemplate domain = null;
    Criteria criteria = new Criteria();
    criteria.setTable(FormTemplateMeta.getName());
    // .//GEN-END:_loadUpdate_1_be
    // Add custom criteria //GEN-FIRST:_loadUpdate_1
    // .//GEN-LAST:_loadUpdate_1
    // .//GEN-BEGIN:_loadUpdate_2_be
    criteria.addCriteria(FormTemplateMeta.FORM_ID, input.getFormId());
    criteria.setLocking(Criteria.LOCKING_PARANOID);
    Iterator itr = uow.query(criteria).iterator();
    if (itr.hasNext())
        domain = (FormTemplate) itr.next();
    // .//GEN-BEGIN:_loadUpdate_3_be
    if (domain == null) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new DomainObjectNotFoundException(FormTemplateMeta.getLabelToken()));
        throw appExps;
    }
    return domain;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) FormTemplate(org.jaffa.modules.printing.domain.FormTemplate) Criteria(org.jaffa.persistence.Criteria)

Example 5 with FormTemplate

use of org.jaffa.modules.printing.domain.FormTemplate in project jaffa-framework by jaffa-projects.

the class FormTemplateMaintenanceTx method prevalidateCreate.

// .//GEN-END:_destroy_2_be
// .//GEN-BEGIN:_prevalidateCreate_1_be
/**
 * This method is used to perform prevalidations before creating a new instance of FormTemplate.
 * @param input The new values for the domain object.
 * @throws ApplicationExceptions This will be thrown if the input contains invalid data.
 * @throws FrameworkException Indicates some system error.
 * @return The object details.
 */
public FormTemplateMaintenancePrevalidateOutDto prevalidateCreate(FormTemplateMaintenanceCreateInDto input) throws FrameworkException, ApplicationExceptions {
    UOW uow = null;
    try {
        // Print Debug Information for the input
        if (log.isDebugEnabled())
            log.debug("Input: " + input);
        // create the UOW
        uow = new UOW();
        // Preprocess the input
        preprocess(uow, input);
        // Do not allow a Duplicate record
        duplicateCheck(uow, input);
        // Validate the foreign objects
        validateForeignObjects(uow, input);
        // Create the domain object
        FormTemplate domain = createDomain(uow, input, true);
        // Perform post create processing
        postCreate(uow, input, domain, true);
        // Build the outbound dto
        FormTemplateMaintenancePrevalidateOutDto output = createPrevalidateOutDto(uow, domain, input);
        // Print Debug Information for the output
        if (log.isDebugEnabled())
            log.debug("Output: " + output);
        return output;
    } catch (FrameworkException e) {
        // If it is, then re-throw as ApplicationsExceptions, else throw the FrameworkException.
        if (e.getCause() != null && e.getCause() instanceof ApplicationExceptions) {
            throw (ApplicationExceptions) e.getCause();
        } else if (e.getCause() != null && e.getCause() instanceof ApplicationException) {
            ApplicationExceptions appExps = new ApplicationExceptions();
            appExps.add((ApplicationException) e.getCause());
            throw appExps;
        } else
            throw e;
    } finally {
        if (uow != null)
            uow.rollback();
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ApplicationException(org.jaffa.exceptions.ApplicationException) FrameworkException(org.jaffa.exceptions.FrameworkException) FormTemplate(org.jaffa.modules.printing.domain.FormTemplate) UOW(org.jaffa.persistence.UOW)

Aggregations

FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)23 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)15 ApplicationException (org.jaffa.exceptions.ApplicationException)11 UOW (org.jaffa.persistence.UOW)10 FrameworkException (org.jaffa.exceptions.FrameworkException)9 Criteria (org.jaffa.persistence.Criteria)7 FormDefinition (org.jaffa.modules.printing.domain.FormDefinition)4 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)3 ValidationException (org.jaffa.datatypes.ValidationException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 TransformerException (javax.xml.transform.TransformerException)1 FormTemplateFinderOutDto (org.jaffa.modules.printing.components.formtemplatefinder.dto.FormTemplateFinderOutDto)1 FormTemplateFinderOutRowDto (org.jaffa.modules.printing.components.formtemplatefinder.dto.FormTemplateFinderOutRowDto)1 FormTemplateLookupOutDto (org.jaffa.modules.printing.components.formtemplatelookup.dto.FormTemplateLookupOutDto)1 FormTemplateLookupOutRowDto (org.jaffa.modules.printing.components.formtemplatelookup.dto.FormTemplateLookupOutRowDto)1 FormTemplateViewerOutDto (org.jaffa.modules.printing.components.formtemplateviewer.dto.FormTemplateViewerOutDto)1 EngineProcessingException (org.jaffa.modules.printing.services.exceptions.EngineProcessingException)1