Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.rice.kew.rule;
 
 
 import java.util.List;
 import java.util.Map;


Generates Action Requests for a Document using the rule system and the specified org.kuali.rice.kew.rule.bo.RuleTemplateBo.

 
 public class FlexRM {
 
 	private static final Logger LOG = Logger.getLogger(FlexRM.class);

The default type of rule selector implementation to use if none is explicitly specified for the node.
 
 	public static final String DEFAULT_RULE_SELECTOR = "Template";
Package in which rule selector implementations live
 
 	private static final String RULE_SELECTOR_PACKAGE = "org.kuali.rice.kew.rule";
The class name suffix all rule selectors should have; e.g. FooRuleSelector
 
 	private static final String RULE_SELECTOR_SUFFIX"RuleSelector";
 
 	private final Timestamp effectiveDate;
An accumulator that keeps track of the number of rules that have been selected over the lifespan of this FlexRM instance.
 
 	private int selectedRules;
 
 	public FlexRM() {
 		this. = null;
 	}
 
 	public FlexRM(Timestamp effectiveDate) {
 		this. = effectiveDate;
 	}
 
	/*public List<ActionRequestValue> getActionRequests(DocumentRouteHeaderValue routeHeader, String ruleTemplateName) throws KEWUserNotFoundException, WorkflowException {
	return getActionRequests(routeHeader, null, ruleTemplateName);
    }*/
	// loads a RuleSelector implementation
	protected RuleSelector loadRuleSelector(RouteNode routeNodeDefRouteNodeInstance nodeInstance) {
		// first see if there ruleselector is configured on a nodeinstance basis
		NodeState ns = null;
		if (nodeInstance != null) {
		}
		String ruleSelectorName = null;
		if (ns != null) {
			ruleSelectorName = ns.getValue();
else {
			// otherwise pull it from the RouteNode definition/prototype
			Map<StringStringnodeCfgParams = Utilities.getKeyValueCollectionAsMap(
					routeNodeDef.
			ruleSelectorName = nodeCfgParams.get(.);
		}
		if (ruleSelectorName == null) {
			ruleSelectorName = ;
		}
		ruleSelectorName = StringUtils.capitalize(ruleSelectorName);
		// load up the rule selection implementation
		String className =  + "." + ruleSelectorName + ;
		Class<?> ruleSelectorClass;
		try {
			ruleSelectorClass = ClassLoaderUtils.getDefaultClassLoader().loadClass(className);
catch (ClassNotFoundException cnfe) {
			throw new IllegalStateException("Rule selector implementation '" + className + "' not found"cnfe);
		}
		if (!RuleSelector.class.isAssignableFrom(ruleSelectorClass)) {
			throw new IllegalStateException("Specified class '" + ruleSelectorClass + "' does not implement RuleSelector interface");
		}
		RuleSelector ruleSelector;
		try {
			ruleSelector = ((Class<RuleSelector>) ruleSelectorClass).newInstance();
catch (Exception e) {
			if (e instanceof RuntimeException) {
			}
			throw new IllegalStateException("Error instantiating rule selector implementation '" + ruleSelectorClass + "'"e);
		}
		return ruleSelector;
	}

Generates action requests

Parameters:
routeHeader the document route header
nodeInstance the route node instance; this may NOT be null
ruleTemplateName the rule template
Returns:
list of action requests
Throws:
org.kuali.rice.kew.api.exception.WorkflowException
	public List<ActionRequestValuegetActionRequests(DocumentRouteHeaderValue routeHeaderRouteNodeInstance nodeInstanceString ruleTemplateName) {
		return getActionRequests(routeHeadernodeInstance.getRouteNode(), nodeInstanceruleTemplateName);
	}

Generates action requests

Parameters:
routeHeader the document route header
routeNodeDef the RouteNode definition of the route node instance
nodeInstance the route node instance; this may be null!
ruleTemplateName the rule template
Returns:
list of action requests
Throws:
org.kuali.rice.kew.api.exception.WorkflowException
	public List<ActionRequestValuegetActionRequests(DocumentRouteHeaderValue routeHeaderRouteNode routeNodeDefRouteNodeInstance nodeInstanceString ruleTemplateName) {
		RouteContext context = RouteContext.getCurrentRouteContext();
		// TODO really the route context just needs to be able to support nested create and clears
		// (i.e. a Stack model similar to transaction intercepting in Spring) and we wouldn't have to do this
		if (context.getDocument() == null) {
			context.setDocument(routeHeader);
		}
		if (context.getNodeInstance() == null) {
			context.setNodeInstance(nodeInstance);
		}
		.debug("Making action requests for document " + routeHeader.getDocumentId());
		RuleSelector ruleSelector = loadRuleSelector(routeNodeDefnodeInstance);
		List<Rulerules = ruleSelector.selectRules(contextrouteHeadernodeInstanceruleTemplateName);
		// XXX: FIXME: this is a special case hack to expose info from the default selection implementation
		// this is used in exactly one place, RoutingReportAction, to make a distinction between no rules being
		// selected, and no rules actually matching when evaluated
		// if (numberOfRules == 0) {
		//   errors.add(new WorkflowServiceErrorImpl("There are no rules.", "routereport.noRules"));
		// } else {
		//   errors.add(new WorkflowServiceErrorImpl("There are rules, but no matches.", "routereport.noMatchingRules"));
		// }
		if (ruleSelector instanceof TemplateRuleSelector) {
		}
		PerformanceLogger performanceLogger = new PerformanceLogger();
		ActionRequestFactory arFactory = new ActionRequestFactory(routeHeadercontext.getNodeInstance());
		if (rules != null) {
			.info("Total number of rules selected by RuleSelector for documentType=" + routeHeader.getDocumentType().getName() + " and ruleTemplate=" + ruleTemplateName + ": " + rules.size());
			for (Rule rulerules) {
				RuleExpressionResult result = rule.evaluate(rulecontext);
				if (result.isSuccess() && result.getResponsibilities() != null) {
					// actionRequests.addAll(makeActionRequests(context, rule, routeHeader, null, null));
                    org.kuali.rice.kew.api.rule.Rule ruleDef = org.kuali.rice.kew.api.rule.Rule.Builder.create(rule.getDefinition()).build();
					makeActionRequests(arFactoryresult.getResponsibilities(), contextruleDefrouteHeadernullnull);
				}
			}
		}
		actionRequests = new ArrayList<ActionRequestValue>(arFactory.getRequestGraphs());
		performanceLogger.log("Time to make action request for template " + ruleTemplateName);
		return actionRequests;
	}
	public ResponsibleParty resolveResponsibilityId(String responsibilityId) {
		if (responsibilityId == null) {
			throw new IllegalArgumentException("A null responsibilityId was passed to resolve responsibility!");
		}
		ResponsibleParty responsibleParty = new ResponsibleParty();
		if (resp!=null && resp.isUsingRole()) {
			responsibleParty.setRoleName(resp.getResolvedRoleName());
else if (resp!=null && resp.isUsingPrincipal()) {
			responsibleParty.setPrincipalId(resp.getPrincipalId());
else if (resp!=null && resp.isUsingGroup()) {
			responsibleParty.setGroupId(resp.getGroupId());
else {
			throw new RiceRuntimeException("Failed to resolve responsibility from responsibility ID " + responsibilityId + ".  Responsibility was an invalid type: " + resp);
		}
		return responsibleParty;
	}
	private void makeActionRequests(ActionRequestFactory arFactoryRouteContext contextorg.kuali.rice.kew.api.rule.Rule ruleDocumentRouteHeaderValue routeHeaderActionRequestValue parentRequestRuleDelegation ruleDelegation)
		makeActionRequests(arFactoryresponsibilitiescontextrulerouteHeaderparentRequestruleDelegation);
	}
	public void makeActionRequests(ActionRequestFactory arFactoryList<org.kuali.rice.kew.api.rule.RuleResponsibilityresponsibilitiesRouteContext contextorg.kuali.rice.kew.api.rule.Rule ruleDocumentRouteHeaderValue routeHeaderActionRequestValue parentRequestRuleDelegation ruleDelegation) {
		//	Set actionRequests = new HashSet();
        for (org.kuali.rice.kew.api.rule.RuleResponsibility responsibility : responsibilities)
        {
            //	    arFactory = new ActionRequestFactory(routeHeader);
            if (responsibility.isUsingRole())
            {
                makeRoleActionRequests(arFactorycontextruleresponsibilityrouteHeaderparentRequestruleDelegation);
            } else
            {
                makeActionRequest(arFactorycontextrulerouteHeaderresponsibilityparentRequestruleDelegation);
            }
            //	    if (arFactory.getRequestGraph() != null) {
            //	    actionRequests.add(arFactory.getRequestGraph());
            //	    }
        }
	}
	private void buildDelegationGraph(ActionRequestFactory arFactoryRouteContext context
			org.kuali.rice.kew.api.rule.Rule delegationRuleDocumentRouteHeaderValue routeHeaderValueActionRequestValue parentRequestRuleDelegation ruleDelegation) {
		context.setActionRequest(parentRequest);
        RuleBaseValues delRuleBo = KEWServiceLocator.getRuleService().getRuleByName(delegationRule.getName());
		if (delegationRule.isActive()) {
            for (org.kuali.rice.kew.api.rule.RuleResponsibility delegationResp : delegationRule.getRuleResponsibilities())
            {
                if (delegationResp.isUsingRole())
                {
                    makeRoleActionRequests(arFactorycontextdelegationRuledelegationResprouteHeaderValueparentRequestruleDelegation);
                } else if (delRuleBo.isMatch(context.getDocumentContent()))
                {
                    makeActionRequest(arFactorycontextdelegationRulerouteHeaderValuedelegationRespparentRequestruleDelegation);
                }
            }
		}
	}

Generates action requests for a role responsibility
	private void makeRoleActionRequests(ActionRequestFactory arFactoryRouteContext context
			RuleDelegation ruleDelegation)
	{
		String roleName = resp.getResolvedRoleName();
		//RoleAttribute roleAttribute = resp.resolveRoleAttribute();
        RoleAttribute roleAttribute = null;
        if (resp.isUsingRole()) {
            //get correct extension definition
            roleAttribute = (RoleAttribute) GlobalResourceLoader.getResourceLoader().getObject(new ObjectDefinition(
                    resp.getRoleAttributeName()));
            if (roleAttribute instanceof XmlConfiguredAttribute) {
                ExtensionDefinition roleAttributeDefinition = null;
                for (RuleTemplateAttribute ruleTemplateAttribute : rule.getRuleTemplate().getRuleTemplateAttributes()) {
                    if (resp.getRoleAttributeName().equals(ruleTemplateAttribute.getRuleAttribute().getResourceDescriptor())) {
                        roleAttributeDefinition = ruleTemplateAttribute.getRuleAttribute();
                        break;
                    }
                }
                ((XmlConfiguredAttribute)roleAttribute).setExtensionDefinition(roleAttributeDefinition);
            }
        }
		//setRuleAttribute(roleAttribute, rule, resp.getRoleAttributeName());
		List<StringqualifiedRoleNames = new ArrayList<String>();
		if (parentRequest != null && parentRequest.getQualifiedRoleName() != null) {
			qualifiedRoleNames.add(parentRequest.getQualifiedRoleName());
else {
	        qualifiedRoleNames.addAll(roleAttribute.getQualifiedRoleNames(roleNamecontext.getDocumentContent()));
		}
        for (String qualifiedRoleName : qualifiedRoleNames) {
            if (parentRequest == null && isDuplicateActionRequestDetected(routeHeadercontext.getNodeInstance(), respqualifiedRoleName)) {
                continue;
            }
            ResolvedQualifiedRole resolvedRole = roleAttribute.resolveQualifiedRole(contextroleNamequalifiedRoleName);
            RoleRecipient recipient = new RoleRecipient(roleNamequalifiedRoleNameresolvedRole);
            if (parentRequest == null) {
                ActionRequestValue roleRequest = arFactory.addRoleRequest(recipientresp.getActionRequestedCd(),
                        resp.getApprovePolicy(), resp.getPriority(), resp.getResponsibilityId(), rule.isForceAction(),
                        rule.getDescription(), rule.getId());
                List<RuleDelegationruleDelegations = getRuleService().getRuleDelegationsByResponsibiltityId(resp.getResponsibilityId());
                if (ruleDelegations != null && !ruleDelegations.isEmpty()) {
                    // create delegations for all the children
                    for (ActionRequestValue request : roleRequest.getChildrenRequests()) {
                        for (RuleDelegation childRuleDelegation : ruleDelegations) {
                            buildDelegationGraph(arFactorycontextchildRuleDelegation.getDelegationRule(), routeHeaderrequestchildRuleDelegation);
                        }
                    }
                }
            } else {
                arFactory.addDelegationRoleRequest(parentRequestresp.getApprovePolicy(), recipientresp.getResponsibilityId(), rule.isForceAction(), ruleDelegation.getDelegationType(), rule.getDescription(), rule.getId());
            }
        }
	}

Determines if the attribute has a setRuleAttribute method and then sets the value appropriately if it does.
	/*private void setRuleAttribute(RoleAttribute roleAttribute, org.kuali.rice.kew.api.rule.Rule rule, String roleAttributeName) {
		// look for a setRuleAttribute method on the RoleAttribute
		Method setRuleAttributeMethod = null;
		try {
			setRuleAttributeMethod = roleAttribute.getClass().getMethod("setExtensionDefinition", RuleAttribute.class);
		} catch (NoSuchMethodException e) {
            LOG.info("method setRuleAttribute not found on " + RuleAttribute.class.getName());
        }
		if (setRuleAttributeMethod == null) {
			return;
		}
		// find the RuleAttribute by looking through the RuleTemplate
		RuleTemplate ruleTemplate = rule.getRuleTemplate();
		if (ruleTemplate != null) {
            for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes())
            {
                RuleAttribute ruleAttribute = ExtensionUtils.loadExtension(ruleTemplateAttribute.getRuleAttribute());
                if (ruleAttribute.getResourceDescriptor().equals(roleAttributeName))
                {
                    // this is our RuleAttribute!
                    try
                    {
                        setRuleAttributeMethod.invoke(roleAttribute, ruleAttribute);
                        break;
                    } catch (Exception e)
                    {
                        throw new WorkflowRuntimeException("Failed to set ExtensionDefinition on our RoleAttribute!", e);
                    }
                }
            }
		}
	}*/

Generates action requests for a non-role responsibility, either a user or workgroup

			RuleDelegation ruleDelegation) {
		if (parentRequest == null && isDuplicateActionRequestDetected(routeHeadercontext.getNodeInstance(), respnull)) {
			return;
		}
		Recipient recipient;
		if (resp.isUsingPrincipal()) {
	        recipient = new KimPrincipalRecipient(resp.getPrincipalId());
        } else if (resp.isUsingGroup()) {
            recipient = new KimGroupRecipient(resp.getGroupId());
        } else {
            throw new RiceRuntimeException("Illegal rule responsibility type encountered");
        }
		ActionRequestValue actionRequest;
		if (parentRequest == null) {
			actionRequest = arFactory.addRootActionRequest(resp.getActionRequestedCd(),
					recipient,
					rule.getId());
                    resp.getResponsibilityId());
			if (ruleDelegations != null && !ruleDelegations.isEmpty()) {
				for (RuleDelegation childRuleDelegation : ruleDelegations) {
					buildDelegationGraph(arFactorycontextchildRuleDelegation.getDelegationRule(), routeHeaderactionRequestchildRuleDelegation);
				}
			}
else {
			arFactory.addDelegationRequest(parentRequestrecipientresp.getResponsibilityId(), rule.isForceAction(), ruleDelegation.getDelegationType(), rule.getDescription(), rule.getId());
		}
	}
	private boolean isDuplicateActionRequestDetected(DocumentRouteHeaderValue routeHeaderRouteNodeInstance nodeInstanceorg.kuali.rice.kew.api.rule.RuleResponsibility respString qualifiedRoleName) {
        for (ActionRequestValue request : requests)
        {
            if (((nodeInstance != null
                    && request.getNodeInstance() != null
                    && request.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())
                 ) || request.getRouteLevel().equals(routeHeader.getDocRouteLevel())
                )
                    && request.getResponsibilityId().equals(resp.getResponsibilityId())
                        && ObjectUtils.equals(request.getQualifiedRoleName(), qualifiedRoleName)) {
                return true;
            }
        }
		return false;
	}
		return KewApiServiceLocator.getRuleService();
	}
		return KEWServiceLocator.getActionRequestService();
	}
	public int getNumberOfMatchingRules() {
	}
New to GrepCode? Check out our FAQ X