use of org.akaza.openclinica.domain.rule.RuleSetRuleBean in project OpenClinica by OpenClinica.
the class RuleSetRuleDaoTest method testFindByRuleSetBeanAndRuleBean.
public void testFindByRuleSetBeanAndRuleBean() {
// RuleDao ruleDao = (RuleDao) getContext().getBean("ruleDao");
// RuleSetDao ruleSetDao = (RuleSetDao) getContext().getBean("ruleSetDao");
// RuleSetRuleDao ruleSetRuleDao = (RuleSetRuleDao) getContext().getBean("ruleSetRuleDao");
RuleBean persistentRuleBean = ruleDao.findById(-1);
RuleSetBean persistentRuleSetBean = ruleSetDao.findById(-1);
List<RuleSetRuleBean> ruleSetRules = ruleSetRuleDao.findByRuleSetBeanAndRuleBean(persistentRuleSetBean, persistentRuleBean);
assertNotNull("RuleSetRules is null", ruleSetRules);
assertEquals("The size of RuleSetRules should be 1", new Integer(1), new Integer(ruleSetRules.size()));
}
use of org.akaza.openclinica.domain.rule.RuleSetRuleBean in project OpenClinica by OpenClinica.
the class Key method runRulesBulk.
public HashMap<RuleBulkExecuteContainer, HashMap<RuleBulkExecuteContainerTwo, Set<String>>> runRulesBulk(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>>>();
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);
try {
OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(eow);
result = (String) oep.parseAndEvaluateExpression(rule.getExpression().getValue());
itemData = getExpressionService().getItemDataBeanFromDb(ruleSet.getTarget().getValue());
// Actions
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());
HashMap<Key, List<RuleActionBean>> hms = new HashMap<Key, List<RuleActionBean>>();
for (RuleActionContainer ruleActionContainer : entry.getValue()) {
//ruleSet.setTarget(ruleAction.getRuleSetExpression());
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 = null;
ap.execute(RuleRunnerMode.RULSET_BULK, executionMode, ruleActionContainer.getRuleAction(), ruleActionContainer.getItemDataBean(), DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents(ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getRuleSetRule(), currentStudy, ruleActionContainer.getRuleAction()));
if (rab != null) {
Key k = new Key(ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getExpressionEvaluatesTo().toString(), ruleActionContainer.getRuleAction().getRuleSetRule().getRuleBean());
if (hms.containsKey(k)) {
hms.get(k).add(ruleActionContainer.getRuleAction());
} else {
List<RuleActionBean> theActionBeansToShow = new ArrayList<RuleActionBean>();
theActionBeansToShow.add(ruleActionContainer.getRuleAction());
hms.put(k, theActionBeansToShow);
}
}
}
for (Map.Entry<Key, List<RuleActionBean>> theEntry : hms.entrySet()) {
Key key = theEntry.getKey();
List<RuleActionBean> value = theEntry.getValue();
crfViewSpecificOrderedObjects = populateForCrfBasedRulesView(crfViewSpecificOrderedObjects, key.getRuleSet(), key.getRule(), key.getResult(), currentStudy, value);
}
}
//logCrfViewSpecificOrderedObjects(crfViewSpecificOrderedObjects);
return crfViewSpecificOrderedObjects;
}
use of org.akaza.openclinica.domain.rule.RuleSetRuleBean in project OpenClinica by OpenClinica.
the class DataEntryRuleRunner method runRules.
public MessageContainer runRules(List<RuleSetBean> ruleSets, ExecutionMode executionMode, StudyBean currentStudy, HashMap<String, String> variableAndValue, UserAccountBean ub, Phase phase, HttpServletRequest request) {
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>>();
switch(executionMode) {
case SAVE:
{
toBeExecuted = (HashMap<String, ArrayList<RuleActionContainer>>) request.getAttribute("toBeExecuted");
if (//Break only if the action is insertAction;
request.getAttribute("insertAction") == null) {
break;
} else {
toBeExecuted = new HashMap<String, ArrayList<RuleActionContainer>>();
}
}
case DRY_RUN:
{
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 = (String) 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();
String firstDDE = "firstDDEInsert_" + ruleSetRule.getOid() + "_" + itemData.getId();
while (itr.hasNext()) {
RuleActionBean ruleActionBean = itr.next();
if (ruleActionBean.getActionType() == ActionType.INSERT) {
request.setAttribute("insertAction", true);
if (phase == Phase.DOUBLE_DATA_ENTRY && itemData.getStatus().getId() == 4 && request.getAttribute(firstDDE) == null) {
request.setAttribute(firstDDE, true);
}
}
if (request.getAttribute(firstDDE) == Boolean.TRUE) {
} else {
String itemDataValueFromForm = "";
if (variableAndValue.containsKey(key)) {
itemDataValueFromForm = variableAndValue.get(key);
} else {
logger.info("Cannot find value from variableAndValue for item=" + key + ". " + "Used itemData.getValue()");
itemDataValueFromForm = itemData.getValue();
}
RuleActionRunLogBean ruleActionRunLog = new RuleActionRunLogBean(ruleActionBean.getActionType(), itemData, itemDataValueFromForm, 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
}
}
}
}
request.setAttribute("toBeExecuted", toBeExecuted);
break;
}
}
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());
ItemDataBean itemData = getExpressionService().getItemDataBeanFromDb(ruleActionContainer.getRuleSetBean().getTarget().getValue());
RuleActionBean rab = ap.execute(RuleRunnerMode.DATA_ENTRY, executionMode, ruleActionContainer.getRuleAction(), itemData, DiscrepancyNoteBean.ITEM_DATA, currentStudy, ub, prepareEmailContents(ruleActionContainer.getRuleSetBean(), ruleActionContainer.getRuleAction().getRuleSetRule(), currentStudy, ruleActionContainer.getRuleAction()));
if (rab != null) {
if (rab instanceof ShowActionBean) {
messageContainer.add(getExpressionService().getGroupOidOrdinal(ruleActionContainer.getRuleSetBean().getTarget().getValue()), rab);
} else {
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;
}
use of org.akaza.openclinica.domain.rule.RuleSetRuleBean in project OpenClinica by OpenClinica.
the class ImportDataRuleRunner method populateToBeExpected.
@Transactional
private HashMap<String, ArrayList<RuleActionContainer>> populateToBeExpected(ImportDataRuleRunnerContainer container, StudyBean study, UserAccountBean ub) {
//copied code for toBeExpected from DataEntryServlet runRules
HashMap<String, ArrayList<RuleActionContainer>> toBeExecuted = new HashMap<String, ArrayList<RuleActionContainer>>();
HashMap<String, String> variableAndValue = (HashMap<String, String>) container.getVariableAndValue();
if (variableAndValue == null || variableAndValue.isEmpty()) {
logger.warn("No rule target item with value found.");
return toBeExecuted;
}
List<RuleSetBean> ruleSets = container.getImportDataTrueRuleSets();
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);
ExpressionObjectWrapper eow = new ExpressionObjectWrapper(ds, study, rule.getExpression(), ruleSet, variableAndValue);
try {
OpenClinicaExpressionParser oep = new OpenClinicaExpressionParser(eow);
result = (String) oep.parseAndEvaluateExpression(rule.getExpression().getValue());
itemData = getExpressionService().getItemDataBeanFromDb(ruleSet.getTarget().getValue());
// Actions
List<RuleActionBean> actionListBasedOnRuleExecutionResult = ruleSetRule.getActions(result, Phase.IMPORT);
if (itemData != null) {
Iterator<RuleActionBean> itr = actionListBasedOnRuleExecutionResult.iterator();
//String firstDDE = "firstDDEInsert_"+ruleSetRule.getOid()+"_"+itemData.getId();
while (itr.hasNext()) {
RuleActionBean ruleActionBean = itr.next();
/*
if(ruleActionBean.getActionType()==ActionType.INSERT) {
request.setAttribute("insertAction", true);
if(phase==Phase.DOUBLE_DATA_ENTRY && itemData.getStatus().getId()==4
&& request.getAttribute(firstDDE)==null) {
request.setAttribute(firstDDE, true);
}
}
if(request.getAttribute(firstDDE)==Boolean.TRUE) {
} else {
*/
String itemDataValueFromImport = "";
if (variableAndValue.containsKey(key)) {
itemDataValueFromImport = variableAndValue.get(key);
} else {
logger.info("Cannot find value from variableAndValue for item=" + key + ". " + "Used itemData.getValue()");
itemDataValueFromImport = itemData.getValue();
}
RuleActionRunLogBean ruleActionRunLog = new RuleActionRunLogBean(ruleActionBean.getActionType(), itemData, itemDataValueFromImport, 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. ", new Object[] { ruleSet.getTarget().getValue(), rule.getName(), result, actionListBasedOnRuleExecutionResult.size() });
} catch (OpenClinicaSystemException osa) {
// TODO: report something useful
}
}
}
}
return toBeExecuted;
}
use of org.akaza.openclinica.domain.rule.RuleSetRuleBean 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
}
}
}
// }
}
}
}
Aggregations