Search in sources :

Example 1 with RuleMetaDataCriteria

use of org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria in project jaffa-framework by jaffa-projects.

the class ApplicationRulesUtilities method getDefaultValue.

/**
 * Get the default value for a property
 * @param className the class whose default value is desired
 * @param propertyName the property whose default value is desired
 * @return the default value
 */
private String getDefaultValue(String className, String propertyName) {
    String defaultValue = null;
    RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
    criteria.setClassName(className);
    criteria.setPropertyName(propertyName);
    criteria.setRuleName("default-value");
    RuleMetaData ruleMetaData = RuleMetaHelper.findRule(criteria);
    if (ruleMetaData != null) {
        defaultValue = ruleMetaData.getText();
    }
    return defaultValue;
}
Also used : RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Example 2 with RuleMetaDataCriteria

use of org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria in project jaffa-framework by jaffa-projects.

the class MetaDataReader method findRule.

/**
 * Returns the first applicable rule for the className/propertyName/ruleName
 * combination.
 */
private static RuleMetaData findRule(String className, String propertyName, Object obj, String ruleName) {
    RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
    criteria.setClassName(className);
    criteria.setPropertyName(propertyName);
    criteria.setRuleName(ruleName);
    criteria.setObj(obj);
    return RuleMetaHelper.findRule(criteria);
}
Also used : RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria)

Example 3 with RuleMetaDataCriteria

use of org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria in project jaffa-framework by jaffa-projects.

the class MetaDataIntrospector method findRule.

/**
 * Returns the first applicable rule for the className/propertyName/ruleName combination.
 */
RuleMetaData findRule(String className, String propertyName, Object obj, String ruleName) {
    RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
    criteria.setClassName(className);
    criteria.setPropertyName(propertyName);
    criteria.setRuleName(ruleName);
    criteria.setObj(obj);
    return RuleMetaHelper.findRule(criteria);
}
Also used : RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria)

Example 4 with RuleMetaDataCriteria

use of org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria 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 5 with RuleMetaDataCriteria

use of org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria in project jaffa-framework by jaffa-projects.

the class RuleMetaDataHelper method showRules.

/**
 * Find the rules for the input Class.
 */
public void showRules(String className, Writer out) throws Exception {
    // Load the Class
    Class clazz = null;
    try {
        clazz = Class.forName(className);
        if (log.isDebugEnabled())
            log.debug("Print Out Properties From " + className);
        String outputClassName = clazz.getSimpleName();
        out.write("RuleMetaData['" + outputClassName + "'] = {\n  properties: {\n  ");
    } catch (Exception e) {
        if (log.isDebugEnabled())
            log.debug("Print Out Properties From Virtual Class " + className);
        String[] splitClass = className.split("\\.");
        out.write("RuleMetaData['" + splitClass[splitClass.length - 1] + "'] = {\n  properties: {\n  ");
    }
    // Show all the Property attributes
    showProperties(className, clazz, out);
    out.write("  }\n");
    // Show Object attributes
    IObjectRuleIntrospector ori = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(className, null);
    Properties[] flexInfoArray = null;
    flexInfoArray = ori.getDeclaredFlexInfo();
    if (flexInfoArray != null && flexInfoArray.length > 0) {
        StringBuilder buf = new StringBuilder();
        for (Properties p : flexInfoArray) {
            if (buf.length() > 0)
                buf.append(", ");
            buf.append(toJSON(p));
        }
        out.write("  ,flexInfo: [");
        out.write(buf.toString());
        out.write("]\n");
    }
    String label = ori.getLabel();
    if (label != null) {
        out.write("  ,label: '");
        out.write(toHtml(MessageHelper.replaceTokens(label)));
        out.write("'\n");
    }
    RuleMetaDataCriteria criteria = new RuleMetaDataCriteria();
    criteria.setClassName(className);
    criteria.setRuleName("template");
    RuleMetaData rule = RuleMetaHelper.findRule(criteria);
    if (rule != null) {
        out.write("      ,ruleTemplate: '" + rule.getParameter("value"));
        out.write("'\n");
    }
    out.write("};");
}
Also used : IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) RuleMetaDataCriteria(org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Aggregations

RuleMetaDataCriteria (org.jaffa.rules.rulemeta.data.RuleMetaDataCriteria)8 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)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 IObjectRuleIntrospector (org.jaffa.rules.IObjectRuleIntrospector)1 GraphMapping (org.jaffa.soa.dataaccess.GraphMapping)1 GraphDataObject (org.jaffa.soa.graph.GraphDataObject)1