Search in sources :

Example 1 with IObjectRuleIntrospector

use of org.jaffa.rules.IObjectRuleIntrospector in project jaffa-framework by jaffa-projects.

the class AuditTransactionViewService method getAuditableProperties.

public Map<String, Properties> getAuditableProperties(String className) throws FrameworkException {
    IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getAuditRuleIntrospector(className);
    Map<String, Properties> auditInfoForProperties = introspector.getAuditInfoForProperties();
    // Add additional information for each property
    if (auditInfoForProperties != null) {
        for (String propertyName : auditInfoForProperties.keySet()) addExtraInfo(className, propertyName, auditInfoForProperties.get(propertyName));
    }
    // Add properties from any associated flex class
    Properties[] flexInfos = introspector.getDeclaredFlexInfo();
    if (flexInfos != null && flexInfos.length > 0) {
        if (auditInfoForProperties == null)
            auditInfoForProperties = new LinkedHashMap<String, Properties>();
        for (Properties flexInfo : flexInfos) {
            String flexClassName = flexInfo.getProperty("source");
            IObjectRuleIntrospector i = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(flexClassName, null);
            Map<String, Properties> auditInfoForFlexProperties = i.getAuditInfoForProperties();
            if (auditInfoForFlexProperties != null) {
                for (String propertyName : auditInfoForFlexProperties.keySet()) {
                    addExtraInfo(flexClassName, propertyName, auditInfoForFlexProperties.get(propertyName));
                    auditInfoForFlexProperties.get(propertyName).setProperty("flex", "true");
                }
                auditInfoForProperties.putAll(auditInfoForFlexProperties);
            }
        }
    }
    return auditInfoForProperties;
}
Also used : IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) Properties(java.util.Properties) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with IObjectRuleIntrospector

use of org.jaffa.rules.IObjectRuleIntrospector in project jaffa-framework by jaffa-projects.

the class AuditTransactionHandler method findClassNameByObjectName.

/**
 * Builds up the internal Map of objectName to className, if the Map has not been initialzed.
 * Returns the className for the input objectName.
 * A null is returned if the domain-info rule is not found for the given objectName.
 */
private String findClassNameByObjectName(String objectName) throws FrameworkException {
    if (classNameByObjectName == null) {
        classNameByObjectName = new HashMap<String, String>();
        String[] classNames = RulesEngineFactory.getRulesEngine().getClassNamesByRuleName("audit");
        if (classNames != null) {
            for (String className : classNames) {
                // IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(className, null);
                IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getAuditRuleIntrospector(className);
                Properties domainInfo = introspector.getAuditInfo();
                if (domainInfo != null)
                    classNameByObjectName.put(domainInfo.getProperty("name"), className);
            }
        }
    }
    return classNameByObjectName.get(objectName);
}
Also used : IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) Properties(java.util.Properties)

Example 3 with IObjectRuleIntrospector

use of org.jaffa.rules.IObjectRuleIntrospector in project jaffa-framework by jaffa-projects.

the class AuditTransactionHandler method findPropertyNameByFieldName.

/**
 * Builds up the internal Map of objectName+fieldName to className+propertyName, if the Map has not been initialzed.
 * Returns the className+propertyName for the input objectName+fieldName.
 * A null is returned if the property-info rule is not found for the input.
 */
private String[] findPropertyNameByFieldName(String objectName, String fieldName) throws FrameworkException {
    if (propertyNameByFieldName == null)
        propertyNameByFieldName = new HashMap<String, Map<String, String[]>>();
    if (!propertyNameByFieldName.containsKey(objectName)) {
        Map<String, String[]> m = new HashMap<String, String[]>();
        String className = findClassNameByObjectName(objectName);
        if (className != null) {
            // Find all the property-info rules, and then add name/propertyName pairs to the Map
            IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(className, null);
            Map<String, Properties> infoForProperties = introspector.getInfoForProperties();
            if (infoForProperties != null) {
                for (Map.Entry<String, Properties> me : infoForProperties.entrySet()) m.put(me.getValue().getProperty("name"), new String[] { className, me.getKey() });
            }
            // Add properties from any associated flex classes
            Properties[] flexInfos = introspector.getDeclaredFlexInfo();
            if (flexInfos != null) {
                for (Properties flexInfo : flexInfos) {
                    String flexClassName = flexInfo.getProperty("source");
                    Map<String, Properties> infoForFlexProperties = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(flexClassName, null).getInfoForProperties();
                    if (infoForFlexProperties != null) {
                        for (Map.Entry<String, Properties> me : infoForFlexProperties.entrySet()) m.put(me.getValue().getProperty("name"), new String[] { flexClassName, me.getKey() });
                    }
                }
            }
        }
        propertyNameByFieldName.put(objectName, m);
    }
    return propertyNameByFieldName.get(objectName).get(fieldName);
}
Also used : HashMap(java.util.HashMap) IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) Properties(java.util.Properties) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with IObjectRuleIntrospector

use of org.jaffa.rules.IObjectRuleIntrospector in project jaffa-framework by jaffa-projects.

the class AuditLogger method audit.

private void audit(IPersistent object, ChangeType changeType) throws ApplicationExceptions, FrameworkException {
    IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(object);
    Properties auditInfo = introspector.getAuditInfo();
    if (auditInfo != null) {
        // find auditInfo for the various properties
        Map<String, Properties> auditInfoMap = introspector.getAuditInfoForProperties();
        // obtain information for flex fields
        FlexBean flexBean = null;
        IObjectRuleIntrospector flexIntrospector = null;
        Map<String, Properties> auditInfoOfFlexFields = null;
        if (object instanceof IFlexFields) {
            flexBean = ((IFlexFields) object).getFlexBean();
            if (flexBean != null) {
                flexIntrospector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(flexBean.getDynaClass().getName(), flexBean);
                auditInfoOfFlexFields = flexIntrospector.getAuditInfoForProperties();
            }
        }
        // determine if auditing is required
        boolean doAudit = changeType == ChangeType.INSERT || changeType == ChangeType.DELETE;
        if (changeType == ChangeType.UPDATE) {
            // In update-mode, auditing is required only if one of the audit fields has changed
            for (String propertyName : auditInfoMap.keySet()) {
                if (object.isModified(propertyName) || object.isModified(StringHelper.getUpper1(propertyName))) {
                    doAudit = true;
                    break;
                }
            }
            // check if flex fields have changed
            if (!doAudit && auditInfoOfFlexFields != null) {
                for (String propertyName : auditInfoOfFlexFields.keySet()) {
                    if (flexBean.hasChanged(propertyName)) {
                        doAudit = true;
                        break;
                    }
                }
            }
        }
        if (doAudit) {
            // Raise PendingEventException if reason is required, but is missing in the MDC
            Boolean reasonRequired = Parser.parseBoolean(auditInfo.getProperty("reason-required"));
            if (reasonRequired != null && reasonRequired && MDC.get(AuditTransactionMeta.REASON) == null) {
                if (log.isDebugEnabled())
                    log.debug("Throwing PendingEventException. 'Reason' needs to be provided in the MDC, since the reason-required attribute has been set to true in the audit rule for " + object.getClass());
                throw new ApplicationExceptions(new PendingEventException("ReasonRequiredForAuditing"));
            }
            // Create AuditTransaction
            createAuditTransaction();
            // Create AuditTransactionObject
            String objectId = createAuditTransactionObject(auditInfo.getProperty("name"), changeType);
            // Create AuditTransactionField instances
            createAuditTransactionFields(objectId, object, auditInfoMap, flexBean, auditInfoOfFlexFields);
        } else {
            if (log.isDebugEnabled())
                log.debug("Audit logging not required since none of the auditable fields have changed in " + object);
        }
    } else {
        if (log.isDebugEnabled())
            log.debug("Audit logging not enabled for " + object.getClass());
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) PendingEventException(org.jaffa.soa.rules.PendingEventException) Properties(java.util.Properties) IFlexFields(org.jaffa.flexfields.IFlexFields) FlexBean(org.jaffa.flexfields.FlexBean)

Example 5 with IObjectRuleIntrospector

use of org.jaffa.rules.IObjectRuleIntrospector in project jaffa-framework by jaffa-projects.

the class DomainModelDocService method createTree.

/**
 * @return JSONArray.
 */
private JSONArray createTree() {
    JSONArray array = new JSONArray();
    Map<String, TreeNode> rootNodeCache = new HashMap<String, TreeNode>();
    Map<String, TreeNode> child1NodeCache = new HashMap<String, TreeNode>();
    try {
        Properties props = new Properties();
        String[] classNames = RulesEngineFactory.getRulesEngine().getClassNamesByRuleName("domain-info");
        if (classNames != null) {
            for (String className : classNames) {
                IObjectRuleIntrospector introspector = RulesEngineFactory.getRulesEngine().getObjectRuleIntrospector(className, null);
                List<Properties> propList = introspector.getMetaDataByRule("domain-info");
                for (Properties p : propList) {
                    if (p != null && p.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        String module = (String) p.get("module");
                        String subModule = (String) p.get("sub-module");
                        String domainName = (String) p.get("name");
                        String dbTable = (String) p.get("db-table");
                        if ((module != null && subModule != null) && (!"null".equals(module) && !"null".equals(subModule))) {
                            sb.append(module).append(".").append(subModule).append(".").append(domainName);
                            if (!props.containsKey(sb.toString())) {
                                if (dbTable != null && !"null".equals(dbTable)) {
                                    StringBuilder value = new StringBuilder();
                                    value.append(className).append("~").append(dbTable);
                                    props.put(sb.toString(), value.toString());
                                } else {
                                    props.put(sb.toString(), className);
                                }
                            }
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("key :" + sb.toString());
                            log.debug("className :" + className);
                        }
                    }
                }
            }
        }
        Enumeration<String> enumeration = (Enumeration<String>) props.propertyNames();
        int rootNodeCounter = 1;
        TreeNode globalRoot = new TreeNode(0, "API Documentation");
        globalRoot.setLeaf(false);
        globalRoot.setIconCls("icon-docs");
        while (enumeration.hasMoreElements()) {
            String serviceName = enumeration.nextElement();
            String[] treeNodeName = serviceName.split("\\.");
            TreeNode root = null;
            TreeNode child1 = null;
            boolean hasRoot = false;
            for (int i = 0; i < treeNodeName.length; i++) {
                boolean hasChild1 = false;
                if (i == 0) {
                    if (!rootNodeCache.containsKey(treeNodeName[i])) {
                        root = new TreeNode(rootNodeCounter, treeNodeName[i]);
                        root.setLeaf(false);
                        root.setIconCls("icon-pkg");
                        rootNodeCache.put(treeNodeName[i], root);
                    } else {
                        root = rootNodeCache.get(treeNodeName[i]);
                        hasRoot = true;
                    }
                } else if (i == 1) {
                    if (!child1NodeCache.containsKey(treeNodeName[i - 1] + treeNodeName[i])) {
                        child1 = new TreeNode(Integer.parseInt(rootNodeCounter + "" + i), treeNodeName[i]);
                        child1.setLeaf(false);
                        child1.setIconCls("icon-pkg");
                        child1NodeCache.put(treeNodeName[i - 1] + treeNodeName[i], child1);
                    } else {
                        child1 = child1NodeCache.get(treeNodeName[i - 1] + treeNodeName[i]);
                        root.setIconCls("icon-pkg");
                        hasChild1 = true;
                    }
                    if (!hasChild1) {
                        root.addChild(child1);
                    }
                } else if (i == 2) {
                    String[] nodeAtt = props.getProperty(serviceName).split("~");
                    String className = nodeAtt.length > 0 ? nodeAtt[0] : null;
                    String dbTable = nodeAtt.length > 1 ? nodeAtt[1] : null;
                    TreeNode child2 = new TreeNode(Integer.parseInt(rootNodeCounter + "" + i), treeNodeName[i]);
                    child2.setLeaf(true);
                    child2.setIconCls("icon-cls");
                    child2.setIsClass(true);
                    child2.setClassName(className);
                    child2.setServiceName(serviceName);
                    child2.setDbTableName(dbTable);
                    child1.addChild(child2);
                }
            }
            if (!hasRoot) {
                globalRoot.addChild(root);
            }
            rootNodeCounter++;
        }
        array.add(globalRoot.toJson());
    } catch (RulesEngineException ex) {
        log.error(ex);
        ex.printStackTrace(System.out);
    }
    return array;
}
Also used : Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) IObjectRuleIntrospector(org.jaffa.rules.IObjectRuleIntrospector) RulesEngineException(org.jaffa.rules.RulesEngineException) JSONArray(net.sf.json.JSONArray) Properties(java.util.Properties)

Aggregations

IObjectRuleIntrospector (org.jaffa.rules.IObjectRuleIntrospector)8 Properties (java.util.Properties)7 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Enumeration (java.util.Enumeration)1 JSONArray (net.sf.json.JSONArray)1 DynaClass (org.apache.commons.beanutils.DynaClass)1 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)1 FrameworkException (org.jaffa.exceptions.FrameworkException)1 FlexBean (org.jaffa.flexfields.FlexBean)1 IFlexFields (org.jaffa.flexfields.IFlexFields)1 FieldMetaData (org.jaffa.metadata.FieldMetaData)1 RulesEngineException (org.jaffa.rules.RulesEngineException)1 PendingEventException (org.jaffa.soa.rules.PendingEventException)1