use of org.akaza.openclinica.bean.submit.ItemDataBean 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.bean.submit.ItemDataBean 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.bean.submit.ItemDataBean 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;
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class DeleteCRFVersionServlet method processRequest.
@Override
public void processRequest() throws Exception {
FormProcessor fp = new FormProcessor(request);
int versionId = fp.getInt(VERSION_ID, true);
String action = request.getParameter("action");
if (versionId == 0) {
addPageMessage(respage.getString("please_choose_a_CRF_version_to_delete"));
forwardPage(Page.CRF_LIST_SERVLET);
} else {
CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
CRFDAO cdao = new CRFDAO(sm.getDataSource());
EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
StudyEventDefinitionDAO sedDao = new StudyEventDefinitionDAO(sm.getDataSource());
StudyEventDAO seDao = new StudyEventDAO(sm.getDataSource());
ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());
StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
CRFVersionBean version = (CRFVersionBean) cvdao.findByPK(versionId);
// find definitions using this version
ArrayList definitions = edcdao.findByDefaultVersion(version.getId());
for (Object edcBean : definitions) {
StudyEventDefinitionBean sedBean = (StudyEventDefinitionBean) sedDao.findByPK(((EventDefinitionCRFBean) edcBean).getStudyEventDefinitionId());
((EventDefinitionCRFBean) edcBean).setEventName(sedBean.getName());
}
// find event crfs using this version
ArrayList<ItemDataBean> idBeans = iddao.findByCRFVersion(version);
ArrayList<EventCRFBean> eCRFs = ecdao.findAllByCRF(version.getCrfId());
for (EventCRFBean eCRF : eCRFs) {
StudySubjectBean ssBean = (StudySubjectBean) ssdao.findByPK(eCRF.getStudySubjectId());
eCRF.setStudySubject(ssBean);
StudyEventBean seBean = (StudyEventBean) seDao.findByPK(eCRF.getStudyEventId());
StudyEventDefinitionBean sedBean = (StudyEventDefinitionBean) sedDao.findByPK(seBean.getStudyEventDefinitionId());
seBean.setStudyEventDefinition(sedBean);
eCRF.setStudyEvent(seBean);
}
ArrayList eventCRFs = ecdao.findAllByCRFVersion(versionId);
boolean canDelete = true;
if (!definitions.isEmpty()) {
// used in definition
canDelete = false;
request.setAttribute("definitions", definitions);
addPageMessage(respage.getString("this_CRF_version") + " " + version.getName() + respage.getString("has_associated_study_events_definitions_cannot_delete"));
} else if (!idBeans.isEmpty()) {
canDelete = false;
request.setAttribute("eventCRFs", eCRFs);
request.setAttribute("itemDataForVersion", idBeans);
addPageMessage(respage.getString("this_CRF_version") + " " + version.getName() + respage.getString("has_associated_item_data_cannot_delete"));
} else if (!eventCRFs.isEmpty()) {
canDelete = false;
request.setAttribute("eventsForVersion", eventCRFs);
addPageMessage(respage.getString("this_CRF_version") + " " + version.getName() + respage.getString("has_associated_study_events_cannot_delete"));
}
if ("confirm".equalsIgnoreCase(action)) {
request.setAttribute(VERSION_TO_DELETE, version);
forwardPage(Page.DELETE_CRF_VERSION);
} else {
// submit
if (canDelete) {
ArrayList items = cvdao.findNotSharedItemsByVersion(versionId);
NewCRFBean nib = new NewCRFBean(sm.getDataSource(), version.getCrfId());
nib.setDeleteQueries(cvdao.generateDeleteQueries(versionId, items));
nib.deleteFromDB();
addPageMessage(respage.getString("the_CRF_version_has_been_deleted_succesfully"));
} else {
addPageMessage(respage.getString("the_CRF_version_cannot_be_deleted"));
}
forwardPage(Page.CRF_LIST_SERVLET);
}
}
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class RemoveCRFServlet method processRequest.
@Override
public void processRequest() throws Exception {
CRFDAO cdao = new CRFDAO(sm.getDataSource());
CRFVersionDAO cvdao = new CRFVersionDAO(sm.getDataSource());
FormProcessor fp = new FormProcessor(request);
// checks which module the requests are from
String module = fp.getString(MODULE);
request.setAttribute(MODULE, module);
int crfId = fp.getInt("id", true);
String action = request.getParameter("action");
if (crfId == 0) {
addPageMessage(respage.getString("please_choose_a_CRF_to_remove"));
forwardPage(Page.CRF_LIST_SERVLET);
} else {
CRFBean crf = (CRFBean) cdao.findByPK(crfId);
ArrayList versions = cvdao.findAllByCRFId(crfId);
crf.setVersions(versions);
EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
ArrayList edcs = (ArrayList) edcdao.findAllByCRF(crfId);
SectionDAO secdao = new SectionDAO(sm.getDataSource());
EventCRFDAO evdao = new EventCRFDAO(sm.getDataSource());
ArrayList eventCRFs = evdao.findAllByCRF(crfId);
StudyEventDAO seDao = new StudyEventDAO(sm.getDataSource());
StudyEventDefinitionDAO sedDao = new StudyEventDefinitionDAO(sm.getDataSource());
for (Object ecBean : eventCRFs) {
StudyEventBean seBean = (StudyEventBean) seDao.findByPK(((EventCRFBean) ecBean).getStudyEventId());
StudyEventDefinitionBean sedBean = (StudyEventDefinitionBean) sedDao.findByPK(seBean.getStudyEventDefinitionId());
((EventCRFBean) ecBean).setEventName(sedBean.getName());
}
if ("confirm".equalsIgnoreCase(action)) {
request.setAttribute("crfToRemove", crf);
request.setAttribute("eventCRFs", eventCRFs);
forwardPage(Page.REMOVE_CRF);
} else {
logger.info("submit to remove the crf");
crf.setStatus(Status.DELETED);
crf.setUpdater(ub);
crf.setUpdatedDate(new Date());
cdao.update(crf);
for (int i = 0; i < versions.size(); i++) {
CRFVersionBean version = (CRFVersionBean) versions.get(i);
if (!version.getStatus().equals(Status.DELETED)) {
version.setStatus(Status.AUTO_DELETED);
version.setUpdater(ub);
version.setUpdatedDate(new Date());
cvdao.update(version);
ArrayList sections = secdao.findAllByCRFVersionId(version.getId());
for (int j = 0; j < sections.size(); j++) {
SectionBean section = (SectionBean) sections.get(j);
if (!section.getStatus().equals(Status.DELETED)) {
section.setStatus(Status.AUTO_DELETED);
section.setUpdater(ub);
section.setUpdatedDate(new Date());
secdao.update(section);
}
}
}
}
for (int i = 0; i < edcs.size(); i++) {
EventDefinitionCRFBean edc = (EventDefinitionCRFBean) edcs.get(i);
if (!edc.getStatus().equals(Status.DELETED)) {
edc.setStatus(Status.AUTO_DELETED);
edc.setUpdater(ub);
edc.setUpdatedDate(new Date());
edcdao.update(edc);
}
}
ItemDataDAO idao = new ItemDataDAO(sm.getDataSource());
for (int i = 0; i < eventCRFs.size(); i++) {
EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(i);
if (!eventCRF.getStatus().equals(Status.DELETED)) {
eventCRF.setStatus(Status.AUTO_DELETED);
eventCRF.setUpdater(ub);
eventCRF.setUpdatedDate(new Date());
evdao.update(eventCRF);
ArrayList items = idao.findAllByEventCRFId(eventCRF.getId());
for (int j = 0; j < items.size(); j++) {
ItemDataBean item = (ItemDataBean) items.get(j);
if (!item.getStatus().equals(Status.DELETED)) {
item.setStatus(Status.AUTO_DELETED);
item.setUpdater(ub);
item.setUpdatedDate(new Date());
idao.update(item);
}
}
}
}
addPageMessage(respage.getString("the_CRF") + crf.getName() + " " + respage.getString("has_been_removed_succesfully"));
forwardPage(Page.CRF_LIST_SERVLET);
}
}
}
Aggregations