Search in sources :

Example 11 with DuplicateKeyException

use of org.jaffa.exceptions.DuplicateKeyException in project jaffa-framework by jaffa-projects.

the class PrimaryKeyValidator method validateProperty.

@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
    // No need to perform validations for an IPersistent instance that exists in the database
    if (targetObject instanceof IPersistent && ((IPersistent) targetObject).isDatabaseOccurence()) {
        if (log.isDebugEnabled()) {
            log.debug(getName() + " check not performed since the target object already exists in the database");
        }
        return;
    }
    RuleMetaData rule = rules.get(0);
    if (log.isDebugEnabled()) {
        log.debug("Applying " + rule + " on " + targetObject);
    }
    Criteria criteria = null;
    String pk = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
    String[] pkFields = pk.split(",");
    for (String pkField : pkFields) {
        Object pkValue = null;
        try {
            pkValue = BeanHelper.getField(targetObject, pkField);
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("No Such Method Exception: " + targetObject.getClass().getName() + "." + pkField, e);
            }
            return;
        }
        if (pkValue == null) {
            if (log.isDebugEnabled()) {
                log.debug(getName() + " check not performed since the key field " + pkField + " is null");
            }
            return;
        } else {
            // Add to criteria
            if (criteria == null) {
                criteria = new Criteria();
            }
            criteria.addCriteria(pkField, pkValue);
        }
    }
    if (criteria != null) {
        criteria.setTable(getActualClassName(targetObject.getClass()));
        if (uow.query(criteria).iterator().hasNext()) {
            if (log.isDebugEnabled()) {
                log.debug("Primary key '" + pk + "' is not unique for the object " + targetObject);
            }
            String objectLabel = getObjectLabel(getActualClassName(targetObject.getClass()), targetObject);
            throw wrapException(new DuplicateKeyException(objectLabel), targetObject, rule);
        }
    }
}
Also used : IPersistent(org.jaffa.persistence.IPersistent) RuleMetaData(org.jaffa.rules.meta.RuleMetaData) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Example 12 with DuplicateKeyException

use of org.jaffa.exceptions.DuplicateKeyException in project jaffa-framework by jaffa-projects.

the class FormGroupMaintenanceTx method duplicateCheck.

// .//GEN-END:_preprocessCreate_2_be
// .//GEN-BEGIN:_duplicateCheck_1_be
/**
 * Ensure that a duplicate record is not created.
 */
private void duplicateCheck(UOW uow, FormGroupMaintenanceCreateInDto input) throws FrameworkException, ApplicationExceptions {
    // .//GEN-BEGIN:_duplicateCheck_2_be
    if (input.getFormName() == null)
        return;
    Criteria criteria = new Criteria();
    criteria.setTable(FormGroupMeta.getName());
    // .//GEN-END:_duplicateCheck_2_be
    // Add custom criteria//GEN-FIRST:_duplicateCheck_2
    // .//GEN-LAST:_duplicateCheck_2
    // .//GEN-BEGIN:_duplicateCheck_3_be
    criteria.addCriteria(FormGroupMeta.FORM_NAME, input.getFormName());
    Collection col = uow.query(criteria);
    // .//GEN-BEGIN:_duplicateCheck_4_be
    if (col != null && !col.isEmpty()) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new DuplicateKeyException(FormGroupMeta.getLabelToken()));
        throw appExps;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Example 13 with DuplicateKeyException

use of org.jaffa.exceptions.DuplicateKeyException in project jaffa-framework by jaffa-projects.

the class FormTemplateMaintenanceTx method duplicateCheck.

// .//GEN-END:_preprocessCreate_2_be
// .//GEN-BEGIN:_duplicateCheck_1_be
/**
 * Ensure that a duplicate record is not created.
 */
private void duplicateCheck(UOW uow, FormTemplateMaintenanceCreateInDto input) throws FrameworkException, ApplicationExceptions {
    // .//GEN-BEGIN:_duplicateCheck_2_be
    if (input.getFormId() == null)
        return;
    Criteria criteria = new Criteria();
    criteria.setTable(FormTemplateMeta.getName());
    // .//GEN-END:_duplicateCheck_2_be
    // Add custom criteria //GEN-FIRST:_duplicateCheck_2
    // .//GEN-LAST:_duplicateCheck_2
    // .//GEN-BEGIN:_duplicateCheck_3_be
    criteria.addCriteria(FormTemplateMeta.FORM_ID, input.getFormId());
    Collection col = uow.query(criteria);
    // .//GEN-BEGIN:_duplicateCheck_4_be
    if (col != null && !col.isEmpty()) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new DuplicateKeyException(FormTemplateMeta.getLabelToken()));
        throw appExps;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Example 14 with DuplicateKeyException

use of org.jaffa.exceptions.DuplicateKeyException in project jaffa-framework by jaffa-projects.

the class OutputCommandMaintenanceTx method duplicateCheck.

// .//GEN-END:_preprocessCreate_2_be
// .//GEN-BEGIN:_duplicateCheck_1_be
/**
 * Ensure that a duplicate record is not created.
 */
private void duplicateCheck(UOW uow, OutputCommandMaintenanceCreateInDto input) throws FrameworkException, ApplicationExceptions {
    // .//GEN-BEGIN:_duplicateCheck_2_be
    if (input.getOutputCommandId() == null)
        return;
    Criteria criteria = new Criteria();
    criteria.setTable(OutputCommandMeta.getName());
    // .//GEN-END:_duplicateCheck_2_be
    // Add custom criteria//GEN-FIRST:_duplicateCheck_2
    // .//GEN-LAST:_duplicateCheck_2
    // .//GEN-BEGIN:_duplicateCheck_3_be
    criteria.addCriteria(OutputCommandMeta.OUTPUT_COMMAND_ID, input.getOutputCommandId());
    Collection col = uow.query(criteria);
    // .//GEN-BEGIN:_duplicateCheck_4_be
    if (col != null && !col.isEmpty()) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new DuplicateKeyException(OutputCommandMeta.getLabelToken()));
        throw appExps;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Example 15 with DuplicateKeyException

use of org.jaffa.exceptions.DuplicateKeyException in project jaffa-framework by jaffa-projects.

the class FormDefinitionMaintenanceTx method duplicateCheck.

// .//GEN-END:_preprocessCreate_2_be
// .//GEN-BEGIN:_duplicateCheck_1_be
/**
 * Ensure that a duplicate record is not created.
 */
private void duplicateCheck(UOW uow, FormDefinitionMaintenanceCreateInDto input) throws FrameworkException, ApplicationExceptions {
    // .//GEN-BEGIN:_duplicateCheck_2_be
    if (input.getFormId() == null)
        return;
    Criteria criteria = new Criteria();
    criteria.setTable(FormDefinitionMeta.getName());
    // .//GEN-END:_duplicateCheck_2_be
    // Add custom criteria//GEN-FIRST:_duplicateCheck_2
    // .//GEN-LAST:_duplicateCheck_2
    // .//GEN-BEGIN:_duplicateCheck_3_be
    criteria.addCriteria(FormDefinitionMeta.FORM_ID, input.getFormId());
    Collection col = uow.query(criteria);
    // .//GEN-BEGIN:_duplicateCheck_4_be
    if (col != null && !col.isEmpty()) {
        ApplicationExceptions appExps = new ApplicationExceptions();
        appExps.add(new DuplicateKeyException(FormDefinitionMeta.getLabelToken()));
        throw appExps;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) Criteria(org.jaffa.persistence.Criteria) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException)

Aggregations

DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)15 Criteria (org.jaffa.persistence.Criteria)13 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)12 IPersistent (org.jaffa.persistence.IPersistent)2 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)1 ValidationException (org.jaffa.datatypes.ValidationException)1 ApplicationException (org.jaffa.exceptions.ApplicationException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 AddFailedException (org.jaffa.persistence.exceptions.AddFailedException)1 PreAddFailedException (org.jaffa.persistence.exceptions.PreAddFailedException)1 UOWException (org.jaffa.persistence.exceptions.UOWException)1 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)1