Search in sources :

Example 11 with OpenClinicaExpressionParser

use of org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser in project OpenClinica by OpenClinica.

the class BeanPropertyRuleRunner method runRules.

public void runRules(List<RuleSetBean> ruleSets, DataSource ds, BeanPropertyService beanPropertyService, StudyEventDao studyEventDaoHib, StudyEventDefinitionDao studyEventDefDaoHib, StudyEventChangeDetails changeDetails, Integer userId, JavaMailSenderImpl mailSender) {
    for (RuleSetBean ruleSet : ruleSets) {
        List<ExpressionBean> expressions = ruleSet.getExpressions();
        for (ExpressionBean expressionBean : expressions) {
            ruleSet.setTarget(expressionBean);
            StudyEvent studyEvent = studyEventDaoHib.findByStudyEventId(Integer.valueOf(getExpressionService().getStudyEventDefenitionOrdninalCurated(ruleSet.getTarget().getValue())));
            int eventOrdinal = studyEvent.getSampleOrdinal();
            int studySubjectBeanId = studyEvent.getStudySubject().getStudySubjectId();
            List<RuleSetRuleBean> ruleSetRules = ruleSet.getRuleSetRules();
            for (RuleSetRuleBean ruleSetRule : ruleSetRules) {
                Object result = null;
                if (ruleSetRule.getStatus() == Status.AVAILABLE) {
                    RuleBean rule = ruleSetRule.getRuleBean();
                    //       StudyBean currentStudy = rule.getStudy();//TODO:Fix me!
                    StudyDAO sdao = new StudyDAO(ds);
                    StudyBean currentStudy = (StudyBean) sdao.findByPK(rule.getStudyId());
                    ExpressionBeanObjectWrapper eow = new ExpressionBeanObjectWrapper(ds, currentStudy, rule.getExpression(), ruleSet, studySubjectBeanId, studyEventDaoHib, studyEventDefDaoHib);
                    try {
                        // StopWatch sw = new StopWatch();
                        ExpressionObjectWrapper ew = new ExpressionObjectWrapper(ds, currentStudy, rule.getExpression(), ruleSet);
                        ew.setStudyEventDaoHib(studyEventDaoHib);
                        ew.setStudySubjectId(studySubjectBeanId);
                        ew.setExpressionContext(ExpressionObjectWrapper.CONTEXT_EXPRESSION);
                        OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(ew);
                        // eow.setUserAccountBean(ub);
                        eow.setStudyBean(currentStudy);
                        result = oep.parseAndEvaluateExpression(rule.getExpression().getValue());
                        // sw.stop();
                        logger.debug("Rule Expression Evaluation Result: " + result);
                        // Actions
                        List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result.toString());
                        for (RuleActionBean ruleActionBean : actionListBasedOnRuleExecutionResult) {
                            // ActionProcessor ap =ActionProcessorFacade.getActionProcessor(ruleActionBean.getActionType(), ds, null, null,ruleSet, null, ruleActionBean.getRuleSetRule());
                            if (ruleActionBean instanceof EventActionBean) {
                                beanPropertyService.runAction(ruleActionBean, eow, userId, changeDetails.getRunningInTransaction());
                            } else if (ruleActionBean instanceof NotificationActionBean) {
                                notificationActionProcessor = new NotificationActionProcessor(ds, mailSender, ruleSetRule);
                                notificationActionProcessor.runNotificationAction(ruleActionBean, ruleSet, studySubjectBeanId, eventOrdinal);
                            }
                        }
                    } catch (OpenClinicaSystemException osa) {
                        // 	osa.printStackTrace();
                        logger.error("Rule Runner received exception: " + osa.getMessage());
                        logger.error(ExceptionUtils.getStackTrace(osa));
                    // TODO: report something useful
                    }
                }
            }
        //     	}
        }
    }
}
Also used : RuleActionBean(org.akaza.openclinica.domain.rule.action.RuleActionBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean) NotificationActionBean(org.akaza.openclinica.domain.rule.action.NotificationActionBean) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) ExpressionBeanObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionBeanObjectWrapper) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean) OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) EventActionBean(org.akaza.openclinica.domain.rule.action.EventActionBean) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) NotificationActionProcessor(org.akaza.openclinica.domain.rule.action.NotificationActionProcessor) StudyEvent(org.akaza.openclinica.domain.datamap.StudyEvent) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) RuleSetBean(org.akaza.openclinica.domain.rule.RuleSetBean) RuleBean(org.akaza.openclinica.domain.rule.RuleBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean)

Example 12 with OpenClinicaExpressionParser

use of org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser in project OpenClinica by OpenClinica.

the class Key method runRulesBulkOLD.

@Deprecated
public HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>> runRulesBulkOLD(List<RuleSetBean> ruleSets, ExecutionMode executionMode, 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 = (String) oep.parseAndEvaluateExpression(rule.getExpression().getValue());
                    // Actions
                    List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result, Phase.BATCH);
                    ItemDataBean itemData = getExpressionService().getItemDataBeanFromDb(ruleSet.getTarget().getValue());
                    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();
                            }
                        }
                    }
                    List<RuleActionBean> actionBeansToShow = new ArrayList<RuleActionBean>();
                    if (actionListBasedOnRuleExecutionResult.size() > 0) {
                        //if (!dryRun) {
                        for (RuleActionBean ruleAction : actionListBasedOnRuleExecutionResult) {
                            ruleAction.setCuratedMessage(curateMessage(ruleAction, ruleSetRule));
                            // getDiscrepancyNoteService().saveFieldNotes(ruleAction.getSummary(), itemDataBeanId, "ItemData", currentStudy, ub);
                            ActionProcessor ap = ActionProcessorFacade.getActionProcessor(ruleAction.getActionType(), ds, getMailSender(), dynamicsMetadataService, ruleSet, getRuleActionRunLogDao(), ruleSetRule);
                            RuleActionBean rab = ap.execute(RuleRunnerMode.RULSET_BULK, executionMode, ruleAction, itemData, DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents(ruleSet, ruleSetRule, currentStudy, ruleAction));
                            if (rab != null) {
                                actionBeansToShow.add(ruleAction);
                            }
                        }
                        if (actionBeansToShow.size() > 0) {
                            crfViewSpecificOrderedObjects = populateForCrfBasedRulesView(crfViewSpecificOrderedObjects, ruleSet, rule, result, currentStudy, actionBeansToShow);
                        }
                    }
                } catch (OpenClinicaSystemException osa) {
                    String errorMessage = "RuleSet with target  : " + ruleSet.getTarget().getValue() + " , Ran Rule : " + rule.getName() + " , It resulted in an error due to : " + osa.getMessage();
                    // log error
                    logger.warn(errorMessage);
                }
            }
        }
    }
    logCrfViewSpecificOrderedObjects(crfViewSpecificOrderedObjects);
    return crfViewSpecificOrderedObjects;
}
Also used : RuleActionBean(org.akaza.openclinica.domain.rule.action.RuleActionBean) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean) ArrayList(java.util.ArrayList) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean) RuleBulkExecuteContainerTwo(org.akaza.openclinica.domain.rule.RuleBulkExecuteContainerTwo) OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) RuleActionRunLogBean(org.akaza.openclinica.domain.rule.action.RuleActionRunLogBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) RuleBulkExecuteContainer(org.akaza.openclinica.domain.rule.RuleBulkExecuteContainer) ActionProcessor(org.akaza.openclinica.domain.rule.action.ActionProcessor) RuleSetBean(org.akaza.openclinica.domain.rule.RuleSetBean) RuleBean(org.akaza.openclinica.domain.rule.RuleBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean)

Aggregations

OpenClinicaExpressionParser (org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser)12 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)11 RuleBean (org.akaza.openclinica.domain.rule.RuleBean)7 RuleSetBean (org.akaza.openclinica.domain.rule.RuleSetBean)7 RuleSetRuleBean (org.akaza.openclinica.domain.rule.RuleSetRuleBean)7 RuleActionBean (org.akaza.openclinica.domain.rule.action.RuleActionBean)7 ExpressionBean (org.akaza.openclinica.domain.rule.expression.ExpressionBean)7 ExpressionObjectWrapper (org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper)7 ArrayList (java.util.ArrayList)6 ItemDataBean (org.akaza.openclinica.bean.submit.ItemDataBean)6 RuleActionRunLogBean (org.akaza.openclinica.domain.rule.action.RuleActionRunLogBean)6 HashMap (java.util.HashMap)5 ActionProcessor (org.akaza.openclinica.domain.rule.action.ActionProcessor)5 MessageFormat (java.text.MessageFormat)4 Map (java.util.Map)3 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 StudyEvent (org.akaza.openclinica.domain.datamap.StudyEvent)2 RuleBulkExecuteContainer (org.akaza.openclinica.domain.rule.RuleBulkExecuteContainer)2