Search in sources :

Example 6 with ValidationException

use of org.jaffa.datatypes.ValidationException in project jaffa-framework by jaffa-projects.

the class GenericForeignKeyFieldValidator method validate.

/**
 * The RulesEngine will invoke this method to perform the field validation.
 * @throws ValidationException if any validation rule fails.
 * @throws FrameworkException if any framework error occurs.
 */
public void validate() throws ValidationException, FrameworkException {
    if (getValue() != null) {
        UOW uow = getUow();
        boolean localUow = (uow == null);
        try {
            if (localUow)
                uow = new UOW();
            Criteria c = new Criteria();
            c.setTable(getDomainClassName());
            c.addCriteria(getFieldNameForTable(), getTableName());
            c.addCriteria(getFieldNameForField(), getFieldName());
            c.addCriteria(getFieldNameForValue(), getValue().toString());
            Collection col = uow.query(c);
            if (col.size() == 0) {
                // Invalid value. Display the list of valid values in the error message
                StringBuffer validValues = new StringBuffer();
                c = new Criteria();
                c.setTable(getDomainClassName());
                c.addCriteria(getFieldNameForTable(), getTableName());
                c.addCriteria(getFieldNameForField(), getFieldName());
                c.addOrderBy(getFieldNameForValue(), Criteria.ORDER_BY_ASC);
                for (Iterator i = uow.query(c).iterator(); i.hasNext(); ) {
                    try {
                        Object value = BeanHelper.getField(i.next(), getFieldNameForValue());
                        if (validValues.length() > 0)
                            validValues.append(',');
                        validValues.append(value);
                    } catch (Exception e) {
                    // do nothing
                    }
                }
                String str = "Generic ForeignKey validation failed for the value '" + getValue() + "' against the table/field - " + getTableName() + '/' + getFieldName() + ". Valid values are " + validValues.toString();
                log.error(str);
                throw new InvalidGenericForeignKeyException(getLabelToken(), new Object[] { getTableName(), getFieldName(), validValues.toString() });
            }
        } finally {
            if (localUow && uow != null)
                uow.rollback();
        }
    }
}
Also used : InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW) InvalidGenericForeignKeyException(org.jaffa.datatypes.exceptions.InvalidGenericForeignKeyException) ValidationException(org.jaffa.datatypes.ValidationException) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 7 with ValidationException

use of org.jaffa.datatypes.ValidationException in project jaffa-framework by jaffa-projects.

the class AttachmentMaintenanceTx method createDomain.

// .//GEN-END:_duplicateCheck_4_be
// .//GEN-BEGIN:_createDomain_1_be
/**
 * Create the domain object.
 */
private Attachment createDomain(UOW uow, AttachmentMaintenanceCreateInDto input, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    Attachment domain = new Attachment();
    ApplicationExceptions appExps = null;
    // .//GEN-BEGIN:_createDomain_2_be
    try {
        domain.updateAttachmentId(input.getAttachmentId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSerializedKey(input.getSerializedKey());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateOriginalFileName(input.getOriginalFileName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateAttachmentType(input.getAttachmentType());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateContentType(input.getContentType());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateDescription(input.getDescription());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateRemarks(input.getRemarks());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSupercededBy(input.getSupercededBy());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        if (!fromPrevalidate)
            domain.updateCreatedOn(new DateTime());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        if (!fromPrevalidate && input.getHeaderDto() != null && input.getHeaderDto().getUserId() != null)
            domain.updateCreatedBy(input.getHeaderDto().getUserId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateData(input.getData());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    // .//GEN-BEGIN:_createDomain_3_be
    if (appExps != null && appExps.size() > 0)
        throw appExps;
    return domain;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) Attachment(org.jaffa.components.attachment.domain.Attachment) DateTime(org.jaffa.datatypes.DateTime)

Example 8 with ValidationException

use of org.jaffa.datatypes.ValidationException in project jaffa-framework by jaffa-projects.

the class FormProcessor method process.

/**
 * Process the FormPrintRequest. This will look at the form definition to use, locate it, build the
 * databean to use, and generate the output form. If follow-on forms are needed these will be generated
 * and merged in. It will then either print or email the generate form.<p>
 * Modified so 'null' DocumentObject will not create blank pages. If no pages are added, nothing will be delivered
 *
 * @param request the object that contains the key to the data, the form to be printed and
 * details about the printer and/or email that should be used for the output
 * @throws org.jaffa.exceptions.FrameworkException thrown if there is an infrastructure error
 * @throws org.jaffa.exceptions.ApplicationExceptions throw if there is an application error that should be repoted to the user
 */
public static void process(FormPrintRequest request) throws FrameworkException, ApplicationExceptions {
    if (log.isDebugEnabled()) {
        log.debug("Begin processing form print request.  Form name = " + request.getFormName() + ", Form Alternate = " + request.getFormAlternateName() + ", Form Variation = " + request.getVariation() + ", Printer Id = " + request.getPrinterId() + ", Email Address = " + request.getEmailToAddresses() + ", User = " + request.getUserName() + ", Copies = " + request.getPrintCopies());
    }
    Map<IDataBean, DocumentPrintedListener> printedListeners = new HashMap<IDataBean, DocumentPrintedListener>();
    File outFile = null;
    UOW uow = new UOW();
    FormCache cache = new FormCache();
    MultiFormPrintEngine engine = null;
    // Get Locale Country
    Locale localeObject = LocaleContext.getLocale();
    String country = localeObject != null ? localeObject.getCountry() : null;
    String formOutputType = null;
    try {
        // Lookup the printer, to get the output type
        PrinterDefinition printer = null;
        String outputType = null;
        if (request.getPrinterId() != null) {
            printer = PrinterDefinition.findByPK(uow, request.getPrinterId());
            if (printer == null) {
                log.error("Invalid printer id on form request: Printer = " + request.getPrinterId());
                throw new ApplicationExceptions(new DomainObjectNotFoundException(PrinterDefinitionMeta.getLabelToken()));
            }
            outputType = printer.getOutputType();
        }
        String formName = request.getFormName();
        String alternateName = request.getFormAlternateName();
        String variation = request.getVariation();
        String[] keyNames = null;
        if (request.getKeys() != null) {
            keyNames = (String[]) request.getKeys().keySet().toArray(new String[] {});
        }
        Object firstDom = null;
        int documentCounter = 0;
        // Main assemble loop if this has follow-on forms
        while (formName != null) {
            FormDefinition formDefinition = findFormDefinition(uow, formName, alternateName, variation, outputType, keyNames);
            if (formDefinition == null) {
                if (outputType == null) {
                    log.error("Form Not Found. Name=" + request.getFormName() + ", Alt=" + request.getFormAlternateName() + ", Variation=" + request.getVariation() + ", OutputType=" + outputType);
                    throw new ApplicationExceptions(new DomainObjectNotFoundException(FormDefinitionMeta.getLabelToken()));
                } else {
                    outputType = null;
                    formDefinition = findFormDefinition(uow, formName, alternateName, variation, outputType, keyNames);
                    if (formDefinition == null) {
                        log.error("Form Not Found. Name=" + request.getFormName() + ", Alt=" + request.getFormAlternateName() + ", Variation=" + request.getVariation() + ", OutputType=" + outputType);
                        throw new ApplicationExceptions(new DomainObjectNotFoundException(FormDefinitionMeta.getLabelToken()));
                    } else {
                        throw new ApplicationExceptions(new ApplicationException("exception.org.jaffa.modules.printing.services.FormProcessor.OutputTypeMismatch") {
                        });
                    }
                }
            }
            formOutputType = formDefinition.getOutputType();
            // See if we have the DOM for this form already, if not build it
            IDataBean data = cache.lookupForm(formDefinition);
            if (data == null) {
                // build the form DOM
                data = buildDataBean(request, formDefinition);
                cache.addForm(formDefinition, data);
                // the FormPrintRequest or as a FollowOnFormAlternate.
                if (alternateName == null) {
                    if (data.getFormAlternateName() != null) {
                        log.debug("Form " + formName + " switch to alternateName " + data.getFormAlternateName() + " from " + alternateName);
                        alternateName = data.getFormAlternateName();
                        continue;
                    }
                }
            }
            // Add this form for printing
            if (engine == null) {
                // set up the engine
                engine = new MultiFormPrintEngine();
                // get the form type so the correct factory can be set...
                try {
                    engine.setEngineType(formDefinition.getFormGroupObject().getFormType());
                } catch (ValidationException e) {
                    // This should only happen if the FormGroup object is not found
                    throw new ApplicationExceptions(e);
                }
            }
            String templateName = cache.getTemplate(formDefinition, engine.getEngineType());
            // If data.getDocumentRoot() is an object array, add a document for each entry in the array.
            if (data.getDocumentRoot() != null && data.getDocumentRoot().getClass().isArray()) {
                Object[] doms = (Object[]) data.getDocumentRoot();
                for (int i = 0; i < doms.length; i++) {
                    if (firstDom == null) {
                        firstDom = doms[i];
                    }
                    // Only add document if it has data
                    if (doms[i] != null) {
                        documentCounter++;
                        engine.addDocument(templateName, doms[i]);
                    }
                }
                if ("<CREATE XML FILE>".equals(formDefinition.getRemarks())) {
                    writeXsdAndXml(doms[0]);
                }
            } else {
                if (firstDom == null) {
                    firstDom = data.getDocumentRoot();
                }
                // Only add document if it has data
                if (data.getDocumentRoot() != null) {
                    documentCounter++;
                    engine.addDocument(templateName, data.getDocumentRoot());
                }
                if ("<CREATE XML FILE>".equals(formDefinition.getRemarks())) {
                    writeXsdAndXml(data.getDocumentRoot());
                }
            }
            // Get the DocumentPrintedListerers for this DataBean, if they have not already be aquired
            if (!printedListeners.containsKey(data)) {
                printedListeners.put(data, data.getDocumentPrintedListener());
            }
            // Get the follow-on form so it will be process in the next iteration
            formName = formDefinition.getFollowOnFormName();
            alternateName = formDefinition.getFollowOnFormAlternate();
        }
        if (documentCounter == 0) {
            log.debug("No pages added to engine, dataBeans were null. No output will be generated");
        } else {
            // Set override page size for PDF forms
            if ("PDF".equals(formOutputType)) {
                String pageSize = printer != null && printer.getScaleToPageSize() != null ? printer.getScaleToPageSize() : null;
                if (pageSize == null) {
                    pageSize = (String) ContextManagerFactory.instance().getProperty("jaffa.printing.defaultScaleToPageSize" + "." + country);
                }
                if (pageSize != null && !"".equals(pageSize)) {
                    engine.setPageSize(pageSize);
                }
            }
            // Generate the output
            engine.generate();
            // Get the generated output
            if (request.getSaveFileName() != null) {
                outFile = engine.writeForm(new File(request.getSaveFileName()));
            } else {
                outFile = engine.writeForm();
            }
            if (log.isDebugEnabled()) {
                log.debug("Created file : " + (outFile == null ? "NULL!!" : outFile.getAbsolutePath()));
            }
            // Send file to printer and email if required
            FormDelivery.deliver(request, outFile, firstDom);
            if (log.isDebugEnabled()) {
                log.debug("Returned from delivering the form.  Print File: " + (outFile == null ? "NULL" : outFile.getAbsolutePath()));
            }
            // Invoke all the DocumentPrintedListerer
            for (DocumentPrintedListener listener : printedListeners.values()) {
                if (listener != null) {
                    listener.documentPrinted(new EventObject(request));
                }
            }
            if (log.isDebugEnabled()) {
                log.debug("Returned from setting form printed date.  Print File: " + (outFile == null ? "NULL" : outFile.getAbsolutePath()));
            }
        }
    } finally {
        if (uow != null) {
            // This UOW should have not been used for updates!!!
            uow.rollback();
        }
        // Clean up the temporary file unless it has been written specifically for web publishing
        if (outFile != null) {
            if (log.isDebugEnabled()) {
                log.debug("FILE NOT CLEANED UP IN DEBUG MODE: " + outFile.getAbsolutePath());
            } else if (request.getSaveFileName() == null) {
                if (!outFile.delete()) {
                    log.error("Failed to delete the temporary form file " + outFile.getAbsolutePath());
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) ApplicationException(org.jaffa.exceptions.ApplicationException) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) File(java.io.File)

Example 9 with ValidationException

use of org.jaffa.datatypes.ValidationException in project jaffa-framework by jaffa-projects.

the class UserRequestMaintenanceTx method createDomain.

// .//GEN-END:_duplicateCheck_4_be
// .//GEN-BEGIN:_createDomain_1_be
/**
 * Create the domain object.
 */
private UserRequest createDomain(UOW uow, UserRequestMaintenanceCreateInDto input, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    UserRequest domain = new UserRequest();
    ApplicationExceptions appExps = null;
    // Add custom code //GEN-FIRST:_createDomain_1
    try {
        if (input.getPassword1() != null)
            domain.updatePassword(input.getPassword1());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    // .//GEN-BEGIN:_createDomain_2_be
    try {
        domain.updateRequestId(input.getRequestId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateUserName(input.getUserName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateFirstName(input.getFirstName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateLastName(input.getLastName());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateEMailAddress(input.getEMailAddress());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSecurityQuestion(input.getSecurityQuestion());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateSecurityAnswer(input.getSecurityAnswer());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateRemarks(input.getRemarks());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateCreatedOn(input.getCreatedOn());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateProcessedDatetime(input.getProcessedDatetime());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateProcessedUserId(input.getProcessedUserId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateStatus(input.getStatus());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    // .//GEN-BEGIN:_createDomain_3_be
    if (appExps != null && appExps.size() > 0)
        throw appExps;
    return domain;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) UserRequest(org.jaffa.applications.jaffa.modules.user.domain.UserRequest)

Example 10 with ValidationException

use of org.jaffa.datatypes.ValidationException in project jaffa-framework by jaffa-projects.

the class FormTemplateMaintenanceTx method createDomain.

// .//GEN-END:_duplicateCheck_4_be
// .//GEN-BEGIN:_createDomain_1_be
/**
 * Create the domain object.
 */
private FormTemplate createDomain(UOW uow, FormTemplateMaintenanceCreateInDto input, boolean fromPrevalidate) throws FrameworkException, ApplicationExceptions {
    FormTemplate domain = new FormTemplate();
    ApplicationExceptions appExps = null;
    // .//GEN-BEGIN:_createDomain_2_be
    try {
        domain.updateTemplateData(input.getTemplateData());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateLayoutData(input.getLayoutData());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    try {
        domain.updateFormId(input.getFormId());
    } catch (ValidationException e) {
        if (appExps == null)
            appExps = new ApplicationExceptions();
        appExps.add(e);
    }
    // .//GEN-BEGIN:_createDomain_3_be
    if (appExps != null && appExps.size() > 0)
        throw appExps;
    return domain;
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) ValidationException(org.jaffa.datatypes.ValidationException) FormTemplate(org.jaffa.modules.printing.domain.FormTemplate)

Aggregations

ValidationException (org.jaffa.datatypes.ValidationException)23 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)21 DateTime (org.jaffa.datatypes.DateTime)8 Criteria (org.jaffa.persistence.Criteria)4 UserRole (org.jaffa.applications.jaffa.modules.admin.domain.UserRole)2 UserRequest (org.jaffa.applications.jaffa.modules.user.domain.UserRequest)2 ApplicationException (org.jaffa.exceptions.ApplicationException)2 FrameworkException (org.jaffa.exceptions.FrameworkException)2 FormTemplate (org.jaffa.modules.printing.domain.FormTemplate)2 FormUsage (org.jaffa.modules.printing.domain.FormUsage)2 EmailerBean (org.jaffa.util.EmailerBean)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 MalformedURLException (java.net.MalformedURLException)1 Locale (java.util.Locale)1 User (org.jaffa.applications.jaffa.modules.admin.domain.User)1 UserTimeEntry (org.jaffa.applications.test.modules.time.domain.UserTimeEntry)1 Attachment (org.jaffa.components.attachment.domain.Attachment)1 AuditTransaction (org.jaffa.components.audit.domain.AuditTransaction)1 AuditTransactionObject (org.jaffa.components.audit.domain.AuditTransactionObject)1