Search in sources :

Example 1 with FlexProperty

use of org.jaffa.flexfields.FlexProperty in project jaffa-framework by jaffa-projects.

the class CandidateKeyValidator method invoke.

/**
 * Apply the candidate-key validation.
 */
private void invoke(String targetClassName, IPersistent targetObject, UOW uow, RuleMetaData rule, AtomicCriteria criteriaToDiscardCurrentObject, Map<String, Object> keyValueMap) throws ApplicationException, ApplicationExceptions, FrameworkException {
    if (log.isDebugEnabled()) {
        log.debug("Applying " + rule + " on " + targetObject);
    }
    Criteria criteria = null;
    String ck = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
    String[] ckFields = ck.split(",");
    Boolean ignoreNull = Parser.parseBoolean(rule.getParameter("ignore-null"));
    // Flex fields having null values are deleted from the database table.
    // The persistence engine does not support queries of the type 'where not exists...'.
    // Hence we'll retrieve all rows and throw an exception only if the fields in the collection are null in any of the rows
    Collection<String> nullFields = null;
    FlexClass flexClass = targetObject instanceof IFlexFields && ((IFlexFields) targetObject).getFlexBean() != null ? (FlexClass) ((IFlexFields) targetObject).getFlexBean().getDynaClass() : null;
    boolean keyModified = false;
    for (String ckField : ckFields) {
        // determine if this is a flex field
        FlexProperty flexProperty = flexClass != null ? flexClass.getDynaProperty(ckField) : null;
        // Obtain the value
        Object ckValue;
        try {
            ckValue = BeanHelper.getField(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField);
            if (ignoreNull != null && ignoreNull && ckValue == null) {
                if (log.isDebugEnabled())
                    log.debug("Rule will be ignored since " + ckField + " is null");
                return;
            }
        } catch (Exception e) {
            if (log.isDebugEnabled())
                log.debug("Exception thrown while trying to get value for the property " + ckField + ". It may not exist in the targetObject", e);
            return;
        }
        // Perform the check only if the field is modified, or is NULL and Object has not been saved yet
        if (!keyModified)
            keyModified = isModified(flexProperty != null ? ((IFlexFields) targetObject).getFlexBean() : targetObject, ckField) || (ckValue == null && !targetObject.isDatabaseOccurence());
        // and any other rule may cause the actual update to be executed.
        if (flexProperty != null && flexProperty.getFlexInfo() != null && flexProperty.getFlexInfo().getProperty("domain-mapping") != null) {
            ckField = flexProperty.getFlexInfo().getProperty("domain-mapping");
            flexProperty = null;
        }
        // Add to criteria
        if (criteria == null)
            criteria = new Criteria();
        if (ckValue == null) {
            if (flexProperty != null) {
                if (nullFields == null)
                    nullFields = new LinkedList<String>();
                nullFields.add(ckField);
            } else {
                criteria.addCriteria(StringHelper.getUpper1(ckField), Criteria.RELATIONAL_IS_NULL);
            }
        } else {
            if (flexProperty != null) {
                Criteria flexCriteria = new Criteria();
                flexCriteria.setTable(FlexFieldMeta.getName());
                int i = 0;
                for (String key : keyValueMap.keySet()) flexCriteria.addInnerCriteria("Key" + ++i, StringHelper.getUpper1(key));
                flexCriteria.addCriteria(FlexFieldMeta.OBJECT_NAME, flexClass.getLogicalName());
                flexCriteria.addCriteria(FlexFieldMeta.FIELD_NAME, flexProperty.getLogicalName());
                flexCriteria.addCriteria(FlexFieldMeta.VALUE, DataTypeMapper.instance().map(ckValue, String.class, flexProperty.getFlexInfo() != null ? flexProperty.getFlexInfo().getProperty("layout") : null));
                criteria.addAggregate(flexCriteria);
            } else {
                criteria.addCriteria(StringHelper.getUpper1(ckField), ckValue);
            }
        }
    }
    if (keyModified && criteria != null) {
        if (criteriaToDiscardCurrentObject != null)
            criteria.addAtomic(criteriaToDiscardCurrentObject);
        criteria.setTable(targetClassName);
        // try {
        for (Object o : uow.query(criteria)) {
            boolean violation = nullFields == null;
            if (!violation) {
                // Not a violation if any of the fields is not null
                for (String ckField : nullFields) {
                    try {
                        violation = BeanHelper.getField(targetObject, ckField) == null;
                        if (!violation) {
                            break;
                        }
                    } catch (Exception e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Error in invoke method during query execution.", e);
                        }
                        throw new JaffaRulesFrameworkException("Error in invoke method during query execution on object: " + targetClassName, null, e);
                    }
                }
            }
            if (violation) {
                if (log.isDebugEnabled()) {
                    log.debug("Candidate key '" + ck + "' is not unique for the object " + targetObject);
                }
                String objectLabel = getObjectLabel(targetClassName, targetObject);
                StringBuilder ckLabel = new StringBuilder();
                for (int i = 0; i < ckFields.length; i++) {
                    if (i > 0) {
                        ckLabel.append(',');
                    }
                    ckLabel.append(getPropertyLabel(targetObject, ckFields[i]));
                }
                throw wrapException(new DuplicateCandidateKeyException(objectLabel, ckLabel.toString()), (T) targetObject, rule);
            }
        }
    }
}
Also used : FlexProperty(org.jaffa.flexfields.FlexProperty) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria) IFlexFields(org.jaffa.flexfields.IFlexFields) FlexClass(org.jaffa.flexfields.FlexClass) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) FrameworkException(org.jaffa.exceptions.FrameworkException) DuplicateCandidateKeyException(org.jaffa.exceptions.DuplicateCandidateKeyException) ApplicationException(org.jaffa.exceptions.ApplicationException) JaffaRulesFrameworkException(org.jaffa.rules.JaffaRulesFrameworkException) DuplicateCandidateKeyException(org.jaffa.exceptions.DuplicateCandidateKeyException)

Aggregations

ApplicationException (org.jaffa.exceptions.ApplicationException)1 DuplicateCandidateKeyException (org.jaffa.exceptions.DuplicateCandidateKeyException)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 FlexClass (org.jaffa.flexfields.FlexClass)1 FlexProperty (org.jaffa.flexfields.FlexProperty)1 IFlexFields (org.jaffa.flexfields.IFlexFields)1 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)1 Criteria (org.jaffa.persistence.Criteria)1 JaffaRulesFrameworkException (org.jaffa.rules.JaffaRulesFrameworkException)1