Search in sources :

Example 11 with FieldMetaData

use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.

the class PersistentHelper method generateKeyCriteria.

/**
 * This will generate a Criteria for reloading the input persistent object, based on the persistent class name and its key values.
 * Note: This method will determine the key fields by looking up the getKeyFields method in the corresponding meta class for the input persistent object.
 *
 * @param object The persistent object.
 * @throws ClassNotFoundException if the Meta class for the input persistent class is not found.
 * @throws NoSuchMethodException if the Meta class does not have the 'public static FieldMetaData[] getKeyFields()' method.
 * @throws IllegalAccessException if the 'public static FieldMetaData[] getKeyFields()' method of the Meta class enforces Java language access control and the underlying method is inaccessible.
 * @throws InvocationTargetException if the 'public static FieldMetaData[] getKeyFields()' method of the Meta class throws an exception.
 * @throws IllegalArgumentException if the input persistent class does not have any key-fields or if any of the key-fields is null.
 * @return a Criteria for reloading the persistent object.
 */
public static Criteria generateKeyCriteria(IPersistent object) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, IllegalArgumentException {
    Criteria criteria = new Criteria();
    criteria.setTable(object.getClass().getName());
    FieldMetaData[] keyFields = getKeyFields(object.getClass().getName());
    if (keyFields != null && keyFields.length > 0) {
        for (FieldMetaData keyField : keyFields) {
            String keyFieldName = keyField.getName();
            Object keyFieldValue = BeanHelper.getField(object, keyFieldName);
            if (keyFieldValue != null) {
                criteria.addCriteria(keyFieldName, keyFieldValue);
            } else {
                String str = "KeyCriteria cannot be generated. The input persistent object has a null value for its key-field " + object + ':' + keyFieldName;
                if (log.isDebugEnabled())
                    log.debug(str);
                throw new IllegalArgumentException(str);
            }
        }
    } else {
        String str = "KeyCriteria cannot be generated. The input persistent object does not have any key fields defined in its meta class: " + object.getClass().getName();
        if (log.isDebugEnabled())
            log.debug(str);
        throw new IllegalArgumentException(str);
    }
    return criteria;
}
Also used : FieldMetaData(org.jaffa.metadata.FieldMetaData) Criteria(org.jaffa.persistence.Criteria)

Example 12 with FieldMetaData

use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.

the class FlexCriteriaBean method findKeys.

/**
 * Returns an array of key field names for the input domain class.
 */
private String[] findKeys(String domainClassName) throws FrameworkException {
    // Search for the first available class-level primary-key rule
    IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(domainClassName, null);
    String[] keys = introspector.getPrimaryKey();
    // Search for corresponding DomainMeta class, if required
    if (keys == null || keys.length == 0) {
        try {
            FieldMetaData[] keyFields = PersistentHelper.getKeyFields(domainClassName);
            if (keyFields != null) {
                keys = new String[keyFields.length];
                for (int i = 0; i < keyFields.length; i++) keys[i] = keyFields[i].getName();
            }
        } catch (Exception e) {
        // do nothing
        }
    }
    return keys != null && keys.length > 0 ? keys : null;
}
Also used : FieldMetaData(org.jaffa.metadata.FieldMetaData) IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) FrameworkException(org.jaffa.exceptions.FrameworkException)

Example 13 with FieldMetaData

use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.

the class Persistent method hashCode.

/**
 * Returns the hashCode of this object based on it's primary key.
 *
 * @return the hashCode of this object based on it's primary key.
 */
public int hashCode() {
    // return (getAssetTk() != null ? getAssetTk().hashCode() : 0);
    int result = 0;
    // IStoredProcedure implementations does not have hashCode or key fields, so just return the super.hashCode().
    if (IStoredProcedure.class.isAssignableFrom(this.getClass())) {
        return super.hashCode();
    }
    try {
        FieldMetaData[] keyFields = PersistentHelper.getKeyFields(this.getClass().getName());
        if (keyFields != null && keyFields.length > 0) {
            for (FieldMetaData keyField : keyFields) {
                String keyFieldName = keyField.getName();
                Object thisValue = BeanHelper.getField(this, keyFieldName);
                if (thisValue != null)
                    result += thisValue.hashCode();
            }
            if (log.isDebugEnabled())
                log.debug("Output=" + result);
            return result;
        } else {
            if (log.isDebugEnabled())
                log.debug("Will invoke super.hashCode() since key fields cannot be determined for the class '" + this.getClass() + '\'');
        }
    } catch (Exception e) {
        log.warn("Will invoke super.hashCode() since an error occurred while evaluating the key fields", e);
    }
    result = super.hashCode();
    if (log.isDebugEnabled())
        log.debug("Output from super.hashCode() is " + result);
    return result;
}
Also used : FieldMetaData(org.jaffa.metadata.FieldMetaData) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) FrameworkException(org.jaffa.exceptions.FrameworkException) ValidationException(org.jaffa.datatypes.ValidationException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 14 with FieldMetaData

use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.

the class Persistent method equals.

/**
 * Compares this object with another Persistent object.
 * Returns a true if both the objects have the same primary key.
 *
 * @param obj the other Persistent object.
 * @return a true if both the objects have the same primary key.
 */
public boolean equals(Object obj) {
    boolean result = false;
    if (obj != null && this.getClass() == obj.getClass()) {
        try {
            FieldMetaData[] keyFields = PersistentHelper.getKeyFields(this.getClass().getName());
            if (keyFields != null && keyFields.length > 0) {
                for (FieldMetaData keyField : keyFields) {
                    String keyFieldName = keyField.getName();
                    Object thisValue = BeanHelper.getField(this, keyFieldName);
                    Object targetValue = BeanHelper.getField(obj, keyFieldName);
                    result = thisValue == null ? targetValue == null : thisValue.equals(targetValue);
                    if (log.isDebugEnabled())
                        log.debug(keyFieldName + ": thisValue=" + thisValue + ", targetValue=" + targetValue + ", comparison=" + result);
                    if (!result)
                        break;
                }
                if (log.isDebugEnabled())
                    log.debug("Output=" + result);
                return result;
            } else {
                if (log.isDebugEnabled())
                    log.debug("Will invoke super.equals() since key fields cannot be determined for the class '" + this.getClass() + '\'');
            }
        } catch (Exception e) {
            log.warn("Will invoke super.equals() since an error occurred while comparing the key fields", e);
        }
    } else {
        if (log.isDebugEnabled())
            log.debug("Will invoke super.equals() since the " + (obj == null ? "argument is null" : "argument's class '" + obj.getClass() + "' does not match the current class '" + this.getClass() + '\''));
    }
    result = super.equals(obj);
    if (log.isDebugEnabled())
        log.debug("Output from super.equals() is " + result);
    return result;
}
Also used : FieldMetaData(org.jaffa.metadata.FieldMetaData) DuplicateKeyException(org.jaffa.exceptions.DuplicateKeyException) FrameworkException(org.jaffa.exceptions.FrameworkException) ValidationException(org.jaffa.datatypes.ValidationException) ApplicationException(org.jaffa.exceptions.ApplicationException)

Example 15 with FieldMetaData

use of org.jaffa.metadata.FieldMetaData in project jaffa-framework by jaffa-projects.

the class EditBoxForm method getFieldLinkedToCCWM.

public WidgetModel getFieldLinkedToCCWM() {
    if (w_fieldLinkedToCC == null) {
        FieldMetaData meta = new StringFieldMetaData("SomeName2", "SomeToken2", Boolean.FALSE, null, null, FieldMetaData.UPPER_CASE);
        w_fieldLinkedToCC = new EditBoxModel(meta);
        w_fieldLinkedToCC.setValue(getFieldLinkedToCC());
    }
    return w_fieldLinkedToCC;
}
Also used : FieldMetaData(org.jaffa.metadata.FieldMetaData) StringFieldMetaData(org.jaffa.metadata.StringFieldMetaData) IntegerFieldMetaData(org.jaffa.metadata.IntegerFieldMetaData) DateOnlyFieldMetaData(org.jaffa.metadata.DateOnlyFieldMetaData) DateTimeFieldMetaData(org.jaffa.metadata.DateTimeFieldMetaData) DecimalFieldMetaData(org.jaffa.metadata.DecimalFieldMetaData) StringFieldMetaData(org.jaffa.metadata.StringFieldMetaData) EditBoxModel(org.jaffa.presentation.portlet.widgets.model.EditBoxModel)

Aggregations

FieldMetaData (org.jaffa.metadata.FieldMetaData)16 FrameworkException (org.jaffa.exceptions.FrameworkException)7 ApplicationException (org.jaffa.exceptions.ApplicationException)4 PropertyRuleIntrospectorUsingFieldMetaData (org.jaffa.metadata.PropertyRuleIntrospectorUsingFieldMetaData)4 IPropertyRuleIntrospector (org.jaffa.rules.IPropertyRuleIntrospector)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 IOException (java.io.IOException)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 JspException (javax.servlet.jsp.JspException)2 ValidationException (org.jaffa.datatypes.ValidationException)2 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)2 DateOnlyFieldMetaData (org.jaffa.metadata.DateOnlyFieldMetaData)2 DateTimeFieldMetaData (org.jaffa.metadata.DateTimeFieldMetaData)2 DecimalFieldMetaData (org.jaffa.metadata.DecimalFieldMetaData)2 IntegerFieldMetaData (org.jaffa.metadata.IntegerFieldMetaData)2 StringFieldMetaData (org.jaffa.metadata.StringFieldMetaData)2 EditBoxModel (org.jaffa.presentation.portlet.widgets.model.EditBoxModel)2 MissingParametersRuntimeException (org.jaffa.presentation.portlet.widgets.taglib.exceptions.MissingParametersRuntimeException)2