Search in sources :

Example 1 with GraphMapping

use of org.jaffa.soa.dataaccess.GraphMapping in project jaffa-framework by jaffa-projects.

the class GraphDataObject 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.
 */
@Override
public int hashCode() {
    int result = 0;
    try {
        GraphMapping mapping = MappingFactory.getInstance(this);
        Collection<String> keyFields = mapping != null ? mapping.getKeyFields() : null;
        if (keyFields != null && keyFields.size() > 0) {
            for (String keyField : keyFields) {
                Object thisValue = BeanHelper.getField(this, keyField);
                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) {
        if (log.isDebugEnabled())
            log.debug("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 : FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationException(org.jaffa.exceptions.ApplicationException) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Example 2 with GraphMapping

use of org.jaffa.soa.dataaccess.GraphMapping in project jaffa-framework by jaffa-projects.

the class GraphDataObject method equals.

/**
 * Compares this object with another Graph object.
 * Returns a true if both the objects have the same primary key.
 *
 * @param obj the other Graph object.
 * @return a true if both the objects have the same primary key.
 */
@Override
public boolean equals(Object obj) {
    boolean result = false;
    if (obj != null && this.getClass() == obj.getClass()) {
        try {
            GraphMapping mapping = MappingFactory.getInstance(this);
            Collection<String> keyFields = mapping != null ? mapping.getKeyFields() : null;
            if (keyFields != null && keyFields.size() > 0) {
                for (String keyField : keyFields) {
                    Object thisValue = BeanHelper.getField(this, keyField);
                    Object targetValue = BeanHelper.getField(obj, keyField);
                    result = thisValue == null ? targetValue == null : thisValue.equals(targetValue);
                    if (log.isDebugEnabled())
                        log.debug(keyField + ": 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) {
            if (log.isDebugEnabled())
                log.debug("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 : FrameworkException(org.jaffa.exceptions.FrameworkException) ApplicationException(org.jaffa.exceptions.ApplicationException) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Example 3 with GraphMapping

use of org.jaffa.soa.dataaccess.GraphMapping in project jaffa-framework by jaffa-projects.

the class FinderMetaDataHelper method getFieldMap.

/**
 * Returns a Map of fields and attributes for each field.
 */
public static Map<String, Map<String, String>> getFieldMap(Class serviceClass, Class outputClass, Class domainClass, boolean graphBased, String[] keys) throws Exception {
    GraphMapping graphMapping = graphBased ? MappingFactory.getInstance(outputClass) : null;
    // determine the field list
    Collection<String> fieldNames = new TreeSet<String>();
    if (graphBased) {
        fieldNames.addAll(graphMapping.getKeyFields());
        fieldNames.addAll(graphMapping.getFields());
        fieldNames.addAll(graphMapping.getForeignFields());
    } else {
        PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(outputClass).getPropertyDescriptors();
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) fieldNames.add(StringHelper.getJavaBeanStyle(propertyDescriptor.getName()));
    }
    // If any lookups properties have been defined then enable lookup rule filtering for this finder
    // Also build array of fields that should be used in the lookup
    boolean hasLookup = false;
    ArrayList<String> lookupFields = new ArrayList<String>();
    for (String fieldName : fieldNames) {
        if (graphMapping != null && graphMapping.isForeignField(fieldName)) {
            // For a foreign object, generate MetaData for all foreign-key fields.
            Class foreignGraph = BeanHelper.getPropertyType(outputClass, fieldName);
            if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                List<String> foreignKeys = graphMapping.getForeignKeys(fieldName);
                Set<String> foreignGraphKeys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                if (foreignKeys != null && foreignGraphKeys != null && foreignKeys.size() == foreignGraphKeys.size()) {
                    int i = 0;
                    for (String foreignGraphKey : foreignGraphKeys) {
                        String title = fieldName + '.' + foreignGraphKey;
                        String foreignKey = foreignKeys.get(i);
                        RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
                        criteria.setClassName(outputClass.getName());
                        criteria.setPropertyName(foreignKey);
                        criteria.setRuleName("lookup");
                        if (RuleMetaHelper.findRules(criteria) != null) {
                            lookupFields.add(foreignKey);
                            hasLookup = true;
                        }
                        i++;
                    }
                }
            }
        } else {
            RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
            criteria.setClassName(outputClass.getName());
            criteria.setPropertyName(fieldName);
            criteria.setRuleName("lookup");
            if (RuleMetaHelper.findRules(criteria) != null) {
                lookupFields.add(fieldName);
                hasLookup = true;
            }
        }
    }
    Map<String, Map<String, String>> output = new LinkedHashMap<String, Map<String, String>>();
    Map<String, ForeignKeyLookup> foreignKeyMap = new HashMap<String, ForeignKeyLookup>();
    // now create the field map
    Map<String, Map<String, String>> fieldMap = new LinkedHashMap<String, Map<String, String>>();
    for (String fieldName : fieldNames) {
        if (graphMapping != null && graphMapping.isForeignField(fieldName)) {
            // For a foreign object, generate MetaData for all foreign-key fields.
            Class foreignGraph = BeanHelper.getPropertyType(outputClass, fieldName);
            if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                List<String> foreignKeys = graphMapping.getForeignKeys(fieldName);
                Set<String> foreignGraphKeys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                String[] foreignGraphKeysArr = foreignGraphKeys.toArray(new String[0]);
                if (foreignKeys != null && foreignGraphKeys != null && foreignKeys.size() == foreignGraphKeys.size()) {
                    int i = 0;
                    for (String foreignKey : foreignKeys) {
                        String foreignGraphKey = foreignGraphKeysArr[i];
                        String title = fieldName + '.' + foreignGraphKey;
                        // If lookup property filtering is being used, only add metadata for fields that have the lookup rule or key fields.
                        if (!(hasLookup == true && lookupFields.indexOf(foreignKey) < 0 && java.util.Arrays.asList(keys).indexOf(foreignKey) < 0)) {
                            if (foreignKeyMap.containsKey(foreignKey)) {
                                ForeignKeyLookup fkl = foreignKeyMap.get(foreignKey);
                                if (fkl.getForeignKeySize() > foreignKeys.size()) {
                                    // Remove from map
                                    foreignKeyMap.remove(foreignKey);
                                    // Add this to the Map
                                    ForeignKeyLookup foreignKeyLookup = new ForeignKeyLookup();
                                    // foreignKeyLookup.setForeignGraphKey(foreignGraphKey);
                                    foreignKeyLookup.setTitle(title);
                                    foreignKeyLookup.setForeignKey(foreignKey);
                                    foreignKeyLookup.setForeignGraphKey(foreignGraphKey);
                                    foreignKeyLookup.setForeignKeySize(foreignKeys.size());
                                    foreignKeyMap.put(foreignKey, foreignKeyLookup);
                                }
                            } else {
                                // Add this to the Map
                                ForeignKeyLookup foreignKeyLookup = new ForeignKeyLookup();
                                // foreignKeyLookup.setForeignGraphKey(foreignGraphKey);
                                foreignKeyLookup.setTitle(title);
                                foreignKeyLookup.setForeignKey(foreignKey);
                                foreignKeyLookup.setForeignGraphKey(foreignGraphKey);
                                foreignKeyLookup.setForeignKeySize(foreignKeys.size());
                                foreignKeyMap.put(foreignKey, foreignKeyLookup);
                            }
                        }
                        i++;
                    }
                }
            }
        } else {
            // If lookup property filtering is being used, only add metadata for fields that have the lookup rule or key fields.
            if (!(hasLookup && lookupFields.indexOf(fieldName) < 0 && java.util.Arrays.asList(keys).indexOf(fieldName) < 0)) {
                Map<String, String> m = determinePropertyMetaData(outputClass, domainClass, graphMapping, fieldName);
                if (m != null)
                    fieldMap.put(fieldName, m);
            }
        }
    }
    // Now add foreign keys to the map
    Set<String> foreignKeys = foreignKeyMap.keySet();
    for (String foreignKey : foreignKeys) {
        ForeignKeyLookup fkl = foreignKeyMap.get(foreignKey);
        String title = fkl.getTitle();
        Map<String, String> m = determinePropertyMetaData(outputClass, domainClass, null, fkl.getForeignKey());
        if (m != null) {
            m.put("mapping", '\'' + title + '\'');
            m.put("filterFieldName", '\'' + fkl.getForeignKey() + '\'');
            fieldMap.put(title, m);
        }
    }
    // The output Map should contain the key fields followed by all others
    if (keys != null) {
        for (String key : keys) output.put(key, fieldMap.remove(key));
        output.putAll(fieldMap);
    }
    if (log.isDebugEnabled())
        log.debug("FieldMap for " + serviceClass.getSimpleName() + " is " + output);
    return output;
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Example 4 with GraphMapping

use of org.jaffa.soa.dataaccess.GraphMapping in project jaffa-framework by jaffa-projects.

the class ClassMetaDataHelper method showPropertiesRefactored.

/**
 * Show Properties.
 * @see #showProperties
 */
// Implementation NOTE: the primary difference between this and showProperties is
// that this calls addPropertyMetaDataRefactored instead of addPropertyMetaData.
// Also, some methods have been extracted.
void showPropertiesRefactored(String className, Class clazz, Object bean, boolean legacy, Writer out) throws Exception {
    String[] propertyNames = determinePropertyNames(className, clazz);
    if (propertyNames != null && propertyNames.length > 0) {
        GraphMapping mapping = null;
        try {
            mapping = clazz != null && GraphDataObject.class.isAssignableFrom(clazz) ? MappingFactory.getInstance(clazz) : null;
        } catch (InstantiationError ignore) {
        }
        StringBuilder buf = new StringBuilder();
        Object domainInstance = null;
        Object graphInstance = null;
        if (clazz != null && mapping != null) {
            try {
                domainInstance = mapping.getDomainClass().newInstance();
                graphInstance = clazz.newInstance();
                String[] filter = { "*" };
                MappingFilter mappingFilter = MappingFilter.getInstance(mapping, filter);
                DataTransformer.buildGraphFromDomain(domainInstance, graphInstance, mapping, mappingFilter, null, false);
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Object beanInstance = null;
        if (graphInstance == null) {
            try {
                FlexClass fc = FlexClass.instance(className);
                beanInstance = fc.newInstance();
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Map<String, String> foreignKeyMappings = buildForeignKeyMappings(clazz, propertyNames, mapping);
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                /*
          For a foreign object, generate MetaData for all foreign-key fields.
          Consider the following scenario:
          - Graph has a foreign-object "deliveredPart : PartGraph"
          - The corresponding domain class has the foreign-key fields
               "deliveredPart : String" and "deliveredCage : String"
          - The foreign Graph has the key fields "part : String" and "cage : String"
          - MetaData will be generated for the fields -
               "deliveredPart.part: {...}" and "deliveredPart.cage: {...}"
          - If legacy==true, MetaData will continue to be generated for the field - "deliveredPart"
        */
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            addPropertyMetaDataRefactored(className, foreignKey, bean, buf, title, mapping.getDomainClass(), graphInstance, beanInstance, foreignKeyMappings);
                        }
                    }
                }
                // For new code, do not generate MetaData for the propertyName
                if (!legacy) {
                    continue;
                }
            }
            addPropertyMetaDataRefactored(className, propertyName, bean, buf, propertyName, null, graphInstance, beanInstance, foreignKeyMappings);
        }
        if (buf.length() > 0) {
            buf.append('\n');
        }
        out.write(buf.toString());
    }
}
Also used : GraphDataObject(org.jaffa.soa.graph.GraphDataObject) MappingFilter(org.jaffa.soa.dataaccess.MappingFilter) FlexClass(org.jaffa.flexfields.FlexClass) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) JSONObject(net.sf.json.JSONObject) FlexClass(org.jaffa.flexfields.FlexClass) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Example 5 with GraphMapping

use of org.jaffa.soa.dataaccess.GraphMapping in project jaffa-framework by jaffa-projects.

the class ClassMetaDataHelper method showProperties.

/**
 * Show Properties.
 * @see #showPropertiesRefactored
 */
void showProperties(String className, Class clazz, Object bean, boolean legacy, Writer out) throws Exception {
    String[] propertyNames = determinePropertyNames(className, clazz);
    if (propertyNames != null && propertyNames.length > 0) {
        GraphMapping mapping = null;
        try {
            mapping = clazz != null && GraphDataObject.class.isAssignableFrom(clazz) ? MappingFactory.getInstance(clazz) : null;
        } catch (InstantiationError ignore) {
        }
        StringBuilder buf = new StringBuilder();
        Object domainInstance = null;
        Object graphInstance = null;
        if (clazz != null && mapping != null) {
            try {
                domainInstance = mapping.getDomainClass().newInstance();
                graphInstance = clazz.newInstance();
                String[] filter = { "*" };
                MappingFilter mappingFilter = MappingFilter.getInstance(mapping, filter);
                DataTransformer.buildGraphFromDomain(domainInstance, graphInstance, mapping, mappingFilter, null, false);
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        Object beanInstance = null;
        if (graphInstance == null) {
            try {
                FlexClass fc = FlexClass.instance(className);
                beanInstance = fc.newInstance();
            } catch (Exception e) {
                log.info(e.toString());
            }
        }
        // Map<String, String> foreignKeyMappings =
        // buildForeignKeyMappings(clazz, propertyNames, mapping);
        Map<String, String> foreignKeyMappings = new HashMap<String, String>();
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            foreignKeyMappings.put(foreignKey, title);
                        }
                    }
                }
            }
        }
        for (String propertyName : propertyNames) {
            if (mapping != null && mapping.isForeignField(propertyName)) {
                /*
          For a foreign object, generate MetaData for all foreign-key fields.
          Consider the following scenario:
          - Graph has a foreign-object "deliveredPart : PartGraph"
          - The corresponding domain class has the foreign-key fields
               "deliveredPart : String" and "deliveredCage : String"
          - The foreign Graph has the key fields "part : String" and "cage : String"
          - MetaData will be generated for the fields -
               "deliveredPart.part: {...}" and "deliveredPart.cage: {...}"
          - If legacy==true, MetaData will continue to be generated for the field - "deliveredPart"
        */
                Class foreignGraph = BeanHelper.getPropertyType(clazz, propertyName);
                if (foreignGraph != null && GraphDataObject.class.isAssignableFrom(foreignGraph)) {
                    List<String> foreignKeys = mapping.getForeignKeys(propertyName);
                    Set<String> keys = MappingFactory.getInstance(foreignGraph).getKeyFields();
                    if (foreignKeys != null && keys != null && foreignKeys.size() == keys.size()) {
                        int i = 0;
                        for (String key : keys) {
                            String title = propertyName + '.' + key;
                            String foreignKey = foreignKeys.get(i++);
                            addPropertyMetaData(className, foreignKey, bean, buf, title, mapping.getDomainClass(), graphInstance, beanInstance, foreignKeyMappings);
                        }
                    }
                }
                // For new code, do not generate MetaData for the propertyName
                if (!legacy)
                    continue;
            }
            addPropertyMetaData(className, propertyName, bean, buf, propertyName, null, graphInstance, beanInstance, foreignKeyMappings);
        }
        if (buf.length() > 0)
            buf.append('\n');
        out.write(buf.toString());
    }
}
Also used : GraphDataObject(org.jaffa.soa.graph.GraphDataObject) MappingFilter(org.jaffa.soa.dataaccess.MappingFilter) FlexClass(org.jaffa.flexfields.FlexClass) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) GraphDataObject(org.jaffa.soa.graph.GraphDataObject) JSONObject(net.sf.json.JSONObject) FlexClass(org.jaffa.flexfields.FlexClass) GraphMapping(org.jaffa.soa.dataaccess.GraphMapping)

Aggregations

GraphMapping (org.jaffa.soa.dataaccess.GraphMapping)5 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)3 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 JSONObject (net.sf.json.JSONObject)2 ApplicationException (org.jaffa.exceptions.ApplicationException)2 FrameworkException (org.jaffa.exceptions.FrameworkException)2 FlexClass (org.jaffa.flexfields.FlexClass)2 MappingFilter (org.jaffa.soa.dataaccess.MappingFilter)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 RuleMetaDataCriteria (org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria)1