Search in sources :

Example 16 with FieldMetaData

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

the class CandidateKeyValidator method getKeyValueMap.

/**
 * Obtain a Map containing key fields and corresponding values.
 */
private Map<String, Object> getKeyValueMap(String targetClassName, IPersistent targetObject) throws ApplicationExceptions, FrameworkException {
    // Search for the first available class-level primary-key rule
    String[] keys = null;
    Map<String, List<RuleMetaData>> pkMap = getPropertyRuleMap(targetClassName, targetObject, "primary-key");
    if (pkMap != null && pkMap.containsKey(null)) {
        List<RuleMetaData> rules = pkMap.get(null);
        if (rules != null && rules.size() > 0) {
            RuleMetaData rule = rules.get(0);
            keys = rule.getParameter(RuleMetaData.PARAMETER_VALUE).split(",");
        }
    }
    // Search for corresponding DomainMeta class, if required
    if (keys == null || keys.length == 0) {
        try {
            FieldMetaData[] keyFields = PersistentHelper.getKeyFields(targetObject.getClass().getName());
            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
        }
    }
    // Create the keyValueMap
    if (keys != null && keys.length > 0) {
        Map<String, Object> keyValueMap = new LinkedHashMap<String, Object>();
        for (String key : keys) {
            Object value = null;
            try {
                value = BeanHelper.getField(targetObject, key);
            } catch (NoSuchMethodException e) {
                if (log.isDebugEnabled()) {
                    log.debug("Could not get the field: " + targetObject.getClass().getName() + "." + key, e);
                }
                throw new JaffaRulesFrameworkException(targetObject.getClass().getName() + "." + key, null, e);
            }
            keyValueMap.put(key, value);
        }
        return keyValueMap;
    } else {
        return null;
    }
}
Also used : RuleMetaData(org.jaffa.rules.meta.RuleMetaData) 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) FieldMetaData(org.jaffa.metadata.FieldMetaData)

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