public MessageContainer runRules( List<RuleSetBean> ruleSets, ExecutionMode executionMode, StudyBean currentStudy, HashMap<String, String> variableAndValue, UserAccountBean ub, Phase phase) { if (variableAndValue == null || variableAndValue.isEmpty()) { logger.warn("You must be executing Rules in Batch"); variableAndValue = new HashMap<String, String>(); } MessageContainer messageContainer = new MessageContainer(); HashMap<String, ArrayList<RuleActionContainer>> toBeExecuted = new HashMap<String, ArrayList<RuleActionContainer>>(); for (RuleSetBean ruleSet : ruleSets) { String key = getExpressionService().getItemOid(ruleSet.getOriginalTarget().getValue()); List<RuleActionContainer> allActionContainerListBasedOnRuleExecutionResult = null; if (toBeExecuted.containsKey(key)) { allActionContainerListBasedOnRuleExecutionResult = toBeExecuted.get(key); } else { toBeExecuted.put(key, new ArrayList<RuleActionContainer>()); allActionContainerListBasedOnRuleExecutionResult = toBeExecuted.get(key); } ItemDataBean itemData = null; for (ExpressionBean expressionBean : ruleSet.getExpressions()) { ruleSet.setTarget(expressionBean); for (RuleSetRuleBean ruleSetRule : ruleSet.getRuleSetRules()) { String result = null; RuleBean rule = ruleSetRule.getRuleBean(); ExpressionObjectWrapper eow = new ExpressionObjectWrapper( ds, currentStudy, rule.getExpression(), ruleSet, variableAndValue, ecb); try { OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(eow); result = oep.parseAndEvaluateExpression(rule.getExpression().getValue()); itemData = getExpressionService().getItemDataBeanFromDb(ruleSet.getTarget().getValue()); // Actions List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result, phase); if (itemData != null) { Iterator<RuleActionBean> itr = actionListBasedOnRuleExecutionResult.iterator(); while (itr.hasNext()) { RuleActionBean ruleActionBean = itr.next(); RuleActionRunLogBean ruleActionRunLog = new RuleActionRunLogBean( ruleActionBean.getActionType(), itemData, itemData.getValue(), ruleSetRule.getRuleBean().getOid()); if (getRuleActionRunLogDao().findCountByRuleActionRunLogBean(ruleActionRunLog) > 0) { itr.remove(); } } } for (RuleActionBean ruleActionBean : actionListBasedOnRuleExecutionResult) { RuleActionContainer ruleActionContainer = new RuleActionContainer(ruleActionBean, expressionBean, itemData, ruleSet); allActionContainerListBasedOnRuleExecutionResult.add(ruleActionContainer); } logger.info( "RuleSet with target : {} , Ran Rule : {} The Result was : {} , Based on that {} action will be executed in {} mode. ", new Object[] { ruleSet.getTarget().getValue(), rule.getName(), result, actionListBasedOnRuleExecutionResult.size(), executionMode.name() }); } catch (OpenClinicaSystemException osa) { // TODO: report something useful } } } } for (Map.Entry<String, ArrayList<RuleActionContainer>> entry : toBeExecuted.entrySet()) { // Sort the list of actions Collections.sort(entry.getValue(), new RuleActionContainerComparator()); for (RuleActionContainer ruleActionContainer : entry.getValue()) { logger.info( "START Expression is : {} , RuleAction : {} , ExecutionMode : {} ", new Object[] { ruleActionContainer.getExpressionBean().getValue(), ruleActionContainer.getRuleAction().toString(), executionMode }); ruleActionContainer.getRuleSetBean().setTarget(ruleActionContainer.getExpressionBean()); ruleActionContainer .getRuleAction() .setCuratedMessage( curateMessage( ruleActionContainer.getRuleAction(), (ruleActionContainer.getRuleAction().getRuleSetRule()))); ActionProcessor ap = ActionProcessorFacade.getActionProcessor( ruleActionContainer.getRuleAction().getActionType(), ds, getMailSender(), dynamicsMetadataService, ruleActionContainer.getRuleSetBean(), getRuleActionRunLogDao(), ruleActionContainer.getRuleAction().getRuleSetRule()); RuleActionBean rab = ap.execute( RuleRunnerMode.DATA_ENTRY, executionMode, ruleActionContainer.getRuleAction(), ruleActionContainer.getItemDataBean(), DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents( ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getRuleSetRule(), currentStudy, ruleActionContainer.getRuleAction())); if (rab != null) { messageContainer.add( getExpressionService() .getGroupOrdninalConcatWithItemOid( ruleActionContainer.getRuleSetBean().getTarget().getValue()), ruleActionContainer.getRuleAction()); } logger.info( "END Expression is : {} , RuleAction : {} , ExecutionMode : {} ", new Object[] { ruleActionContainer.getExpressionBean().getValue(), ruleActionContainer.getRuleAction().toString(), executionMode }); } } return messageContainer; }
public HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>> runRulesBulk( List<RuleSetBean> ruleSets, Boolean dryRun, StudyBean currentStudy, HashMap<String, String> variableAndValue, UserAccountBean ub) { if (variableAndValue == null || variableAndValue.isEmpty()) { logger.warn("You must be executing Rules in Batch"); variableAndValue = new HashMap<String, String>(); } HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>> crfViewSpecificOrderedObjects = new HashMap< RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>>(); for (RuleSetBean ruleSet : ruleSets) { for (ExpressionBean expressionBean : ruleSet.getExpressions()) { ruleSet.setTarget(expressionBean); for (RuleSetRuleBean ruleSetRule : ruleSet.getRuleSetRules()) { String result = null; RuleBean rule = ruleSetRule.getRuleBean(); ExpressionObjectWrapper eow = new ExpressionObjectWrapper( ds, currentStudy, rule.getExpression(), ruleSet, variableAndValue); try { OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(eow); result = oep.parseAndEvaluateExpression(rule.getExpression().getValue()); // Actions List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result); if (dryRun && actionListBasedOnRuleExecutionResult.size() > 0) { crfViewSpecificOrderedObjects = populateForCrfBasedRulesView( crfViewSpecificOrderedObjects, ruleSet, rule, result, currentStudy, actionListBasedOnRuleExecutionResult); } // If not a dryRun meaning run Actions if (!dryRun) { for (RuleActionBean ruleAction : actionListBasedOnRuleExecutionResult) { int itemDataBeanId = getExpressionService() .getItemDataBeanFromDb(ruleSet.getTarget().getValue()) .getId(); ruleAction.setCuratedMessage(curateMessage(ruleAction, ruleSetRule)); // getDiscrepancyNoteService().saveFieldNotes(ruleAction.getSummary(), // itemDataBeanId, "ItemData", currentStudy, ub); ActionProcessor ap = ActionProcessorFacade.getActionProcessor(ruleAction.getActionType(), ds); ap.execute( ruleAction, itemDataBeanId, "ItemData", currentStudy, ub, prepareEmailContents(ruleSet, ruleSetRule, currentStudy, ruleAction)); } } } catch (OpenClinicaSystemException osa) { // TODO: Auditing might happen here failed rule logger.warn( "RuleSet with target : {} , Ran Rule : {} , It resulted in an error due to : {}", new Object[] {ruleSet.getTarget().getValue(), rule.getName(), osa.getMessage()}); } } } } logCrfViewSpecificOrderedObjects(crfViewSpecificOrderedObjects); return crfViewSpecificOrderedObjects; }