Search in sources :

Example 1 with OpenClinicaExpressionParser

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

the class OpenClinicaV1ExpressionProcessor method testEvaluateExpression.

public HashMap<String, String> testEvaluateExpression(HashMap<String, String> testValues) {
    try {
        oep = new OpenClinicaExpressionParser(expressionWrapper);
        HashMap<String, String> resultAndTestValues = oep.parseAndTestEvaluateExpression(e.getValue(), testValues);
        String returnedResult = resultAndTestValues.get("result");
        logger.debug("Test Result : " + returnedResult);
        resultAndTestValues.put("ruleValidation", "rule_valid");
        resultAndTestValues.put("ruleEvaluatesTo", returnedResult);
        return resultAndTestValues;
    } catch (OpenClinicaSystemException e) {
        MessageFormat mf = new MessageFormat("");
        mf.applyPattern(respage.getString(e.getErrorCode()));
        Object[] arguments = e.getErrorParams();
        testValues.put("ruleValidation", "rule_invalid");
        testValues.put("ruleValidationFailMessage", e.getErrorCode() + " : " + mf.format(arguments));
        testValues.put("ruleEvaluatesTo", "");
        return testValues;
    }
}
Also used : OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) MessageFormat(java.text.MessageFormat) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException)

Example 2 with OpenClinicaExpressionParser

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

the class OpenClinicaV1ExpressionProcessor method isRuleExpressionValid.

public String isRuleExpressionValid() {
    try {
        oep = new OpenClinicaExpressionParser(expressionWrapper);
        String result = oep.parseAndTestEvaluateExpression(e.getValue());
        logger.debug("Test Result : " + result);
        return null;
    } catch (OpenClinicaSystemException e) {
        MessageFormat mf = new MessageFormat("");
        mf.applyPattern(respage.getString(e.getErrorCode()));
        Object[] arguments = e.getErrorParams();
        return e.getErrorCode() + " : " + mf.format(arguments);
    }
}
Also used : OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) MessageFormat(java.text.MessageFormat) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException)

Example 3 with OpenClinicaExpressionParser

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

the class RuleSetBulkRuleRunner method runRulesBulkFromRuleSetScreenOLD.

public List<RuleSetBasedViewContainer> runRulesBulkFromRuleSetScreenOLD(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>();
    }
    List<RuleSetBasedViewContainer> ruleSetBasedView = new ArrayList<RuleSetBasedViewContainer>();
    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());
                    // HashMap<String, ArrayList<RuleActionBean>> actionsToBeExecuted = ruleSetRule.getAllActionsWithEvaluatesToAsKey(result);
                    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();
                            }
                        }
                    }
                    logger.info("RuleSet with target  : {} , Ran Rule : {}  The Result was : {} , Based on that {} action will be executed ", new Object[] { ruleSet.getTarget().getValue(), rule.getName(), result, actionListBasedOnRuleExecutionResult.size() });
                    if (actionListBasedOnRuleExecutionResult.size() > 0) {
                        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.SAVE, ruleAction, itemData, DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents(ruleSet, ruleSetRule, currentStudy, ruleAction));
                            if (rab != null) {
                                ruleSetBasedView = populateForRuleSetBasedView(ruleSetBasedView, ruleSet, rule, result, ruleAction);
                            }
                        }
                    }
                } 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);
                }
            }
        }
    }
    return ruleSetBasedView;
}
Also used : RuleActionBean(org.akaza.openclinica.domain.rule.action.RuleActionBean) RuleSetBasedViewContainer(org.akaza.openclinica.domain.rule.RuleSetBasedViewContainer) 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) 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) 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)

Example 4 with OpenClinicaExpressionParser

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

the class BeanPropertyService method runAction.

/**
     * Complete adding to this Service to evaluate Event action
     * @param ruleActionBean
     * @param eow
     * @param isTransaction 
     */
public void runAction(RuleActionBean ruleActionBean, ExpressionBeanObjectWrapper eow, Integer userId, Boolean isTransaction) {
    boolean statusMatch = false;
    OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(eow);
    ExpressionBeanService ebs = new ExpressionBeanService(eow);
    StudyEvent studyEvent = ebs.getStudyEventFromOID(((EventActionBean) ruleActionBean).getOc_oid_reference());
    RuleActionRunBean runOnStatuses = ruleActionBean.getRuleActionRun();
    if (studyEvent != null) {
        switch(SubjectEventStatus.getByCode(studyEvent.getSubjectEventStatusId())) {
            case NOT_SCHEDULED:
                if (runOnStatuses.getNot_started())
                    statusMatch = true;
                break;
            case SCHEDULED:
                if (runOnStatuses.getScheduled())
                    statusMatch = true;
                break;
            case DATA_ENTRY_STARTED:
                if (runOnStatuses.getData_entry_started())
                    statusMatch = true;
                break;
            case COMPLETED:
                if (runOnStatuses.getComplete())
                    statusMatch = true;
                break;
            case SKIPPED:
                if (runOnStatuses.getSkipped())
                    statusMatch = true;
                break;
            case STOPPED:
                if (runOnStatuses.getStopped())
                    statusMatch = true;
                break;
            case SIGNED:
            case LOCKED:
            default:
                statusMatch = false;
                break;
        }
    } else //Special case if destination study event doesn't exist yet, ie not scheduled.
    {
        if (runOnStatuses.getNot_started())
            statusMatch = true;
    }
    if (statusMatch) {
        for (PropertyBean propertyBean : ((EventActionBean) ruleActionBean).getProperties()) {
            // This will execute the contents of <ValueExpression>SS.ENROLLMENT_DATE + 2</ValueExpression>
            LOGGER.debug("Values:expression??::" + propertyBean.getValueExpression().getValue());
            Object result = oep.parseAndEvaluateExpression(propertyBean.getValueExpression().getValue());
            executeAction(result, propertyBean, eow, (EventActionBean) ruleActionBean, userId, isTransaction);
        }
    }
}
Also used : OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) ExpressionBeanService(org.akaza.openclinica.service.rule.expression.ExpressionBeanService) EventActionBean(org.akaza.openclinica.domain.rule.action.EventActionBean) RuleActionRunBean(org.akaza.openclinica.domain.rule.action.RuleActionRunBean) StudyEvent(org.akaza.openclinica.domain.datamap.StudyEvent) PropertyBean(org.akaza.openclinica.domain.rule.action.PropertyBean)

Example 5 with OpenClinicaExpressionParser

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

the class RuleSetBulkRuleRunner method runRulesBulkFromRuleSetScreen.

public List<RuleSetBasedViewContainer> runRulesBulkFromRuleSetScreen(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>();
    }
    List<RuleSetBasedViewContainer> ruleSetBasedView = new ArrayList<RuleSetBasedViewContainer>();
    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);
            System.out.println("tg expression:" + ruleSet.getTarget().getValue());
            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());
                    itemData = getExpressionService().getItemDataBeanFromDb(ruleSet.getTarget().getValue());
                    System.out.println("The result: " + result);
                    List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result, Phase.BATCH);
                    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()) {
            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.RULSET_BULK, executionMode, ruleActionContainer.getRuleAction(), ruleActionContainer.getItemDataBean(), DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents(ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getRuleSetRule(), currentStudy, ruleActionContainer.getRuleAction()));
            System.out.println(" Action Trigger: " + ap.toString());
            if (rab != null) {
                ruleSetBasedView = populateForRuleSetBasedView(ruleSetBasedView, ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getRuleSetRule().getRuleBean(), ruleActionContainer.getRuleAction().getExpressionEvaluatesTo().toString(), ruleActionContainer.getRuleAction());
            }
        }
    }
    return ruleSetBasedView;
}
Also used : RuleActionBean(org.akaza.openclinica.domain.rule.action.RuleActionBean) RuleSetBasedViewContainer(org.akaza.openclinica.domain.rule.RuleSetBasedViewContainer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OpenClinicaExpressionParser(org.akaza.openclinica.logic.expressionTree.OpenClinicaExpressionParser) RuleActionRunLogBean(org.akaza.openclinica.domain.rule.action.RuleActionRunLogBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) RuleSetRuleBean(org.akaza.openclinica.domain.rule.RuleSetRuleBean) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) ExpressionBean(org.akaza.openclinica.domain.rule.expression.ExpressionBean) ExpressionObjectWrapper(org.akaza.openclinica.domain.rule.expression.ExpressionObjectWrapper) ActionProcessor(org.akaza.openclinica.domain.rule.action.ActionProcessor) HashMap(java.util.HashMap) Map(java.util.Map) 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