use of org.akaza.openclinica.bean.submit.ItemBean in project OpenClinica by OpenClinica.
the class ImportDataRuleRunnerContainer method initRuleSetsAndTargets.
/**
* Populate importDataTrueRuleSets and variableAndValue.
* Precondition: import data file passed validation which means all OIDs are not empty.
* @param ds
* @param studyBean
* @param subjectDataBean
* @param ruleSetService
*/
@Transactional
public void initRuleSetsAndTargets(DataSource ds, StudyBean studyBean, SubjectDataBean subjectDataBean, RuleSetServiceInterface ruleSetService) {
this.shouldRunRules = this.shouldRunRules == null ? Boolean.FALSE : this.shouldRunRules;
this.importDataTrueRuleSets = this.importDataTrueRuleSets == null ? new ArrayList<RuleSetBean>() : this.importDataTrueRuleSets;
this.variableAndValue = this.variableAndValue == null ? new HashMap<String, String>() : this.variableAndValue;
studyOid = studyBean.getOid();
studySubjectOid = subjectDataBean.getSubjectOID();
StudySubjectBean studySubject = new StudySubjectDAO<String, ArrayList>(ds).findByOid(studySubjectOid);
HashMap<String, StudyEventDefinitionBean> seds = new HashMap<String, StudyEventDefinitionBean>();
HashMap<String, CRFVersionBean> cvs = new HashMap<String, CRFVersionBean>();
ArrayList<StudyEventDataBean> studyEventDataBeans = subjectDataBean.getStudyEventData();
for (StudyEventDataBean studyEventDataBean : studyEventDataBeans) {
String sedOid = studyEventDataBean.getStudyEventOID();
StudyEventDefinitionBean sed;
if (seds.containsKey(sedOid))
sed = seds.get(sedOid);
else {
sed = new StudyEventDefinitionDAO<String, ArrayList>(ds).findByOid(sedOid);
seds.put(sedOid, sed);
}
ArrayList<FormDataBean> formDataBeans = studyEventDataBean.getFormData();
for (FormDataBean formDataBean : formDataBeans) {
String cvOid = formDataBean.getFormOID();
CRFVersionBean crfVersion;
if (cvs.containsKey(cvOid))
crfVersion = cvs.get(cvOid);
else {
crfVersion = new CRFVersionDAO<String, ArrayList>(ds).findByOid(cvOid);
cvs.put(cvOid, crfVersion);
}
String sedOrd = studyEventDataBean.getStudyEventRepeatKey();
Integer sedOrdinal = sedOrd != null && !sedOrd.isEmpty() ? Integer.valueOf(sedOrd) : 1;
StudyEventBean studyEvent = (StudyEventBean) new StudyEventDAO(ds).findByStudySubjectIdAndDefinitionIdAndOrdinal(studySubject.getId(), sed.getId(), sedOrdinal);
List<RuleSetBean> ruleSets = ruleSetService.getRuleSetsByCrfStudyAndStudyEventDefinition(studyBean, sed, crfVersion);
//Set<String> targetItemOids = new HashSet<String>();
if (ruleSets != null && !ruleSets.isEmpty()) {
ruleSets = filterByImportDataEntryTrue(ruleSets);
if (ruleSets != null && !ruleSets.isEmpty()) {
ruleSets = ruleSetService.filterByStatusEqualsAvailable(ruleSets);
ruleSets = ruleSetService.filterRuleSetsByStudyEventOrdinal(ruleSets, studyEvent, crfVersion, sed);
//ruleSets = ruleSetService.filterRuleSetsByHiddenItems(ruleSets, eventCrfBean, crfVersion, new ArrayList<ItemBean>());
shouldRunRules = ruleSetService.shouldRunRulesForRuleSets(ruleSets, Phase.IMPORT);
if (shouldRunRules != null && shouldRunRules == Boolean.TRUE) {
//targetItemOids = collectTargetItemOids(ruleSets);
HashMap<String, Integer> grouped = new HashMap<String, Integer>();
ArrayList<ImportItemGroupDataBean> itemGroupDataBeans = formDataBean.getItemGroupData();
for (ImportItemGroupDataBean itemGroupDataBean : itemGroupDataBeans) {
ArrayList<ImportItemDataBean> itemDataBeans = itemGroupDataBean.getItemData();
for (ImportItemDataBean importItemDataBean : itemDataBeans) {
//if(targetItemOids.contains(importItemDataBean.getItemOID())) {
ItemBean item = new ItemDAO<String, ArrayList>(ds).findByOid(importItemDataBean.getItemOID()).get(0);
String igOid = itemGroupDataBean.getItemGroupOID();
String igOrd = itemGroupDataBean.getItemGroupRepeatKey();
Integer igOrdinal = igOrd != null && !igOrd.isEmpty() ? Integer.valueOf(igOrd) : 1;
//logic from DataEntryServlet method: populateRuleSpecificHashMaps()
if (isRepeatIGForSure(ds, crfVersion.getId(), igOid, igOrdinal, item.getId())) {
String key1 = igOid + "[" + igOrdinal + "]." + importItemDataBean.getItemOID();
String key = igOid + "." + importItemDataBean.getItemOID();
variableAndValue.put(key1, importItemDataBean.getValue());
if (grouped.containsKey(key)) {
grouped.put(key, grouped.get(key) + 1);
} else {
grouped.put(key, 1);
}
} else {
variableAndValue.put(importItemDataBean.getItemOID(), importItemDataBean.getValue());
grouped.put(importItemDataBean.getItemOID(), 1);
}
//
//}
}
}
ruleSets = ruleSetService.solidifyGroupOrdinalsUsingFormProperties(ruleSets, grouped);
importDataTrueRuleSets.addAll(ruleSets);
}
}
}
}
}
}
use of org.akaza.openclinica.bean.submit.ItemBean in project OpenClinica by OpenClinica.
the class RuleRunner method prepareEmailContents.
HashMap<String, String> prepareEmailContents(RuleSetBean ruleSet, RuleSetRuleBean ruleSetRule, StudyBean currentStudy, RuleActionBean ruleAction) {
// get the Study Event
StudyEventBean studyEvent = (StudyEventBean) getStudyEventDao().findByPK(Integer.valueOf(getExpressionService().getStudyEventDefenitionOrdninalCurated(ruleSet.getTarget().getValue())));
// get the Study Subject
StudySubjectBean studySubject = (StudySubjectBean) getStudySubjectDao().findByPK(studyEvent.getStudySubjectId());
// get Study/Site Associated with Subject
StudyBean theStudy = (StudyBean) getStudyDao().findByPK(studySubject.getStudyId());
String theStudyName, theSiteName = "";
if (theStudy.getParentStudyId() > 0) {
StudyBean theParentStudy = (StudyBean) getStudyDao().findByPK(theStudy.getParentStudyId());
theStudyName = theParentStudy.getName() + " / " + theParentStudy.getIdentifier();
theSiteName = theStudy.getName() + " / " + theStudy.getIdentifier();
} else {
theStudyName = theStudy.getName() + " / " + theStudy.getIdentifier();
}
// get the eventCrf & subsequently the CRF Version
//EventCRFBean eventCrf = (EventCRFBean) getEventCrfDao().findAllByStudyEvent(studyEvent).get(0);
EventCRFBean eventCrf = (EventCRFBean) getEventCrfDao().findAllByStudyEventAndCrfOrCrfVersionOid(studyEvent, getExpressionService().getCrfOid(ruleSet.getTarget().getValue())).get(0);
CRFVersionBean crfVersion = (CRFVersionBean) getCrfVersionDao().findByPK(eventCrf.getCRFVersionId());
CRFBean crf = (CRFBean) getCrfDao().findByPK(crfVersion.getCrfId());
String studyEventDefinitionName = getExpressionService().getStudyEventDefinitionFromExpression(ruleSet.getTarget().getValue(), currentStudy).getName();
studyEventDefinitionName += " [" + studyEvent.getSampleOrdinal() + "]";
String itemGroupName = getExpressionService().getItemGroupNameAndOrdinal(ruleSet.getTarget().getValue());
ItemGroupBean itemGroupBean = getExpressionService().getItemGroupExpression(ruleSet.getTarget().getValue());
ItemBean itemBean = getExpressionService().getItemExpression(ruleSet.getTarget().getValue(), itemGroupBean);
String itemName = itemBean.getName();
SectionBean section = (SectionBean) getSectionDAO().findByPK(getItemFormMetadataDAO().findByItemIdAndCRFVersionId(itemBean.getId(), crfVersion.getId()).getSectionId());
StringBuffer sb = new StringBuffer();
ResourceBundle respage = ResourceBundleProvider.getPageMessagesBundle();
sb.append(respage.getString("email_header_1"));
sb.append(" " + contextPath + " ");
sb.append(respage.getString("email_header_2"));
sb.append(" '" + currentStudy.getName() + "' ");
sb.append(respage.getString("email_header_3"));
sb.append(" \n\n ");
sb.append(respage.getString("email_body_1") + " " + theStudyName + " \n ");
sb.append(respage.getString("email_body_1_a") + " " + theSiteName + " \n ");
sb.append(respage.getString("email_body_2") + " " + studySubject.getName() + " \n ");
sb.append(respage.getString("email_body_3") + " " + studyEventDefinitionName + " \n ");
sb.append(respage.getString("email_body_4") + " " + crf.getName() + " " + crfVersion.getName() + " \n ");
sb.append(respage.getString("email_body_5") + " " + section.getTitle() + " \n ");
sb.append(respage.getString("email_body_6") + " " + itemGroupName + " \n ");
sb.append(respage.getString("email_body_7") + " " + itemName + " \n ");
sb.append(respage.getString("email_body_8") + " " + ruleAction.getCuratedMessage() + " \n ");
sb.append(" \n\n ");
sb.append(respage.getString("email_body_9"));
sb.append(" " + contextPath + " ");
sb.append(respage.getString("email_body_10"));
sb.append(" \n");
requestURLMinusServletPath = requestURLMinusServletPath == null ? "" : requestURLMinusServletPath;
sb.append(requestURLMinusServletPath + "/ViewSectionDataEntry?ecId=" + eventCrf.getId() + "§ionId=" + section.getId() + "&tabId=" + section.getOrdinal());
// &eventId="+ studyEvent.getId());
sb.append("\n\n");
sb.append(respage.getString("email_footer"));
String subject = contextPath + " - [" + currentStudy.getName() + "] ";
String ruleSummary = ruleAction.getSummary() != null ? ruleAction.getSummary() : "";
String message = ruleSummary.length() < 20 ? ruleSummary : ruleSummary.substring(0, 20) + " ... ";
subject += message;
HashMap<String, String> emailContents = new HashMap<String, String>();
emailContents.put("body", sb.toString());
emailContents.put("subject", subject);
return emailContents;
}
use of org.akaza.openclinica.bean.submit.ItemBean 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.ItemBean 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.ItemBean 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