use of org.akaza.openclinica.bean.submit.ItemDataBean 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;
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class ScoreCalculator method redoCalculations.
/**
* Re-do calculations if funcs include changed item(s) and funcs are not
* included in the current section. If calculation can not sucessfully redo,
* old value will be erased and "<erased>" will be saved in database. <br>
* The parameter 'itemdata' might be overwritten.
*
* @param itemGroupSizes
* @param items
* @param itemdata
* @param oldItemdata
* @param updatedData
* @param sectionId
* @return ArrayList<String> which records left_item_text of items who
* failed to be updated into database.
*/
public ArrayList<String> redoCalculations(HashMap<String, ItemBean> items, HashMap<String, String> itemdata, TreeSet<String> changedItems, HashMap<Integer, TreeSet<Integer>> itemOrdinals, int sectionId) {
ArrayList<String> updateFailedItems = new ArrayList<String>();
if (itemdata == null) {
logger.error("In ScoreCalculator redoCalculations(), itemdata is empty!");
errors.add("In ScoreCalculator redoCalculations(), 'itemdata' map is empty!");
return updateFailedItems;
}
if (changedItems == null) {
logger.error("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
errors.add("In ScoreCalculator redoCalculations(), 'changeItems' set is empty!");
return updateFailedItems;
}
ItemFormMetadataDAO ifmdao = new ItemFormMetadataDAO(sm.getDataSource());
ItemDAO idao = new ItemDAO(sm.getDataSource());
ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
NumberFormat nf = NumberFormat.getInstance();
Parser parser = new Parser(items, itemdata);
try {
// for calculation type
List<ItemFormMetadataBean> derivedItemList = ifmdao.findAllByCRFVersionIdAndResponseTypeId(ecb.getCRFVersionId(), ResponseType.CALCULATION.getId());
if (derivedItemList.size() > 0) {
Collections.sort(derivedItemList);
for (ItemFormMetadataBean ifmb : derivedItemList) {
if (ifmb.getSectionId() != sectionId) {
ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
int groupsize = 1;
if (itemOrdinals.containsKey(ib.getId())) {
groupsize = itemOrdinals.get(ib.getId()).size();
}
String value = "";
ArrayList<ScoreToken> parsedExp = new ArrayList<ScoreToken>();
for (int i = 0; i < groupsize; ++i) {
ItemDataBean idb = iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), i + 1);
// is there any changed item
Parser p = new Parser(items, itemdata);
parsedExp = parser.parseScoreTokens(rob.getValue());
if (p.isChanged(changedItems, parsedExp)) {
StringBuffer err = new StringBuffer();
parsedExp = parser.assignVariables(parsedExp, i + 1);
// before, set "<erased>"
if (parser.getErrors().length() > 0) {
err.append(parser.getErrors());
if (idb.isActive()) {
idb.setValue("<erased>");
idb.setStatus(Status.UNAVAILABLE);
idb = (ItemDataBean) iddao.update(idb);
if (!idb.isActive()) {
String key = i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
updateFailedItems.add(key);
}
}
parser.setErrors(new StringBuffer());
} else // otherwise do calculation
{
try {
value = ScoreUtil.eval(parsedExp);
} catch (ScoreException se) {
logger.error(se.getMessage());
}
String exp = rob.getValue();
exp = exp.replace("##", ",");
if (writeToDB(ib, ifmb, idb, exp, value, err)) {
changedItems.add(ib.getName());
itemdata.put(ib.getId() + "_" + (i + 1), idb.getValue());
} else {
String key = i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
updateFailedItems.add(key);
}
}
if (err.length() > 0) {
String key = i + 1 > 1 ? ifmb.getLeftItemText() + "_" + (i + 1) : ifmb.getLeftItemText();
errors.add("Item " + key + " contains calculation errors: " + err.toString());
}
}
}
}
}
}
List<ItemFormMetadataBean> itemList = ifmdao.findAllByCRFVersionIdAndResponseTypeId(ecb.getCRFVersionId(), ResponseType.GROUP_CALCULATION.getId());
if (itemList.size() > 0) {
Collections.sort(itemList);
for (ItemFormMetadataBean ifmb : itemList) {
if (ifmb.getSectionId() != sectionId) {
ItemBean ib = (ItemBean) idao.findByPK(ifmb.getItemId());
ResponseOptionBean rob = (ResponseOptionBean) ifmb.getResponseSet().getOptions().get(0);
String value = "";
Parser p = new Parser(items, itemdata);
ArrayList<ScoreToken> parsedExp = parser.parseScoreTokens(rob.getValue());
if (p.isChanged(changedItems, parsedExp)) {
StringBuffer err = new StringBuffer();
parser.setErrors(err);
parsedExp = parser.assignVariables(parsedExp, itemOrdinals);
ItemDataBean idb = iddao.findByItemIdAndEventCRFIdAndOrdinal(ifmb.getItemId(), ecb.getId(), 1);
if (parser.getErrors().length() > 0) {
err.append(parser.getErrors());
if (idb.isActive()) {
idb.setValue("<erased>");
idb.setStatus(Status.UNAVAILABLE);
idb = (ItemDataBean) iddao.update(idb);
if (!idb.isActive()) {
updateFailedItems.add(ifmb.getLeftItemText());
}
}
} else {
try {
value = ScoreUtil.eval(parsedExp);
} catch (ScoreException se) {
logger.error(se.getMessage());
}
String exp = rob.getValue();
exp = exp.replace("##", ",");
if (writeToDB(ib, ifmb, idb, exp, value, err)) {
changedItems.add(ib.getName());
itemdata.put(ib.getId() + "_" + idb.getOrdinal(), idb.getValue());
} else {
updateFailedItems.add(ifmb.getLeftItemText());
}
}
if (err.length() > 0) {
errors.add("Item " + ifmb.getLeftItemText() + " contains calculation errors: " + err.toString());
}
}
}
}
}
} catch (OpenClinicaException e) {
logger.error(e.getMessage());
}
return updateFailedItems;
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class RandomizeService method getExpressionValue.
private String getExpressionValue(String expr, EventCRFBean eventCrfBean, RuleSetBean ruleSet) {
String expression = getExpressionService().constructFullExpressionIfPartialProvided(expr, ruleSet.getTarget().getValue());
ItemDataBean itemData = null;
if (expression != null && !expression.isEmpty()) {
ItemBean itemBean = getExpressionService().getItemBeanFromExpression(expression);
String itemGroupBOrdinal = getExpressionService().getGroupOrdninalCurated(expression);
itemData = getItemDataDAO().findByItemIdAndEventCRFIdAndOrdinal(itemBean.getId(), eventCrfBean.getId(), itemGroupBOrdinal == "" ? 1 : Integer.valueOf(itemGroupBOrdinal));
}
return itemData.getValue();
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class DynamicsMetadataService method oneToOne.
private ItemDataBean oneToOne(ItemDataBean itemDataBeanA, EventCRFBean eventCrfBeanA, ItemGroupMetadataBean itemGroupMetadataBeanA, ItemBean itemBeanB, ItemGroupMetadataBean itemGroupMetadataBeanB, EventCRFBean eventCrfBeanB, UserAccountBean ub, Integer ordinal) {
ordinal = ordinal == null ? 1 : ordinal;
itemGroupMetadataBeanB.getRepeatNum();
ItemDataBean oidBasedItemData = getItemData(itemBeanB, eventCrfBeanB, ordinal);
if (oidBasedItemData.getId() == 0) {
oidBasedItemData = createItemData(oidBasedItemData, itemBeanB, ordinal, eventCrfBeanB, ub);
oidBasedItemData.setUpdaterId(oidBasedItemData.getOwnerId());
} else {
if (oidBasedItemData.getUpdaterId() == 0)
oidBasedItemData.setUpdaterId(oidBasedItemData.getOwnerId());
}
return oidBasedItemData;
}
use of org.akaza.openclinica.bean.submit.ItemDataBean in project OpenClinica by OpenClinica.
the class DynamicsMetadataService method getValue.
private String getValue(PropertyBean property, RuleSetBean ruleSet, EventCRFBean eventCrfBean, List<StratificationFactorBean> stratificationFactorBeans) {
String value = null;
if (property.getValue() != null && property.getValue().length() > 0) {
logger.info("Value from property value is : {}", value);
value = property.getValue();
}
if (property.getValueExpression() == null) {
logger.info("There is no ValueExpression for property =" + property.getOid());
if (stratificationFactorBeans != null)
try {
value = getRandomizeService().getRandomizationCode(eventCrfBean, stratificationFactorBeans, ruleSet);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
String expression = getExpressionService().constructFullExpressionIfPartialProvided(property.getValueExpression().getValue(), ruleSet.getTarget().getValue());
if (expression != null && !expression.isEmpty()) {
ItemBean itemBean = getExpressionService().getItemBeanFromExpression(expression);
String itemGroupBOrdinal = getExpressionService().getGroupOrdninalCurated(expression);
ItemDataBean itemData = getItemDataDAO().findByItemIdAndEventCRFIdAndOrdinal(itemBean.getId(), eventCrfBean.getId(), itemGroupBOrdinal == "" ? 1 : Integer.valueOf(itemGroupBOrdinal));
if (itemData.getId() == 0) {
logger.info("Cannot get Value for ExpressionValue {}", expression);
} else {
value = itemData.getValue();
logger.info("Value from ExpressionValue '{}' is : {}", expression, value);
}
}
}
return value;
}
Aggregations