use of org.akaza.openclinica.domain.datamap.StudyEvent in project OpenClinica by OpenClinica.
the class ExpressionService method evaluateExpression.
public String evaluateExpression(String expression) throws OpenClinicaSystemException {
String value = null;
Map<Integer, ItemBean> itemBeansI = new HashMap<Integer, ItemBean>();
if (items != null) {
Iterator<ItemBean> iter = items.values().iterator();
while (iter.hasNext()) {
ItemBean item = iter.next();
itemBeansI.put(item.getId(), item);
}
}
if (expressionWrapper.getRuleSet() != null) {
if (checkIfExpressionIsForScheduling(expression)) {
StudyEvent studyEvent;
if (expression.endsWith(this.STARTDATE)) {
String oid = expression.substring(0, expression.indexOf(this.STARTDATE));
studyEvent = getStudyEventFromOID(oid);
if (studyEvent != null) {
logger.debug("Study Event Start Date: " + studyEvent.getDateStart().toString().substring(0, 10).trim());
return studyEvent.getDateStart().toString().substring(0, 10).trim();
} else
return "";
} else {
String oid = expression.substring(0, expression.indexOf(this.STATUS));
studyEvent = getStudyEventFromOID(oid);
if (studyEvent != null) {
logger.debug("Status: " + SubjectEventStatus.getSubjectEventStatusName(studyEvent.getSubjectEventStatusId()));
return SubjectEventStatus.getSubjectEventStatusName(studyEvent.getSubjectEventStatusId());
} else
return "";
}
}
if (isExpressionPartial(expression)) {
String fullExpression = constructFullExpressionIfPartialProvided(expression, expressionWrapper.getRuleSet().getTarget().getValue());
List<ItemDataBean> itemDatas = getItemDatas(fullExpression);
fullExpression = fixGroupOrdinal(fullExpression, expressionWrapper.getRuleSet().getTarget().getValue(), itemDatas, expressionWrapper.getEventCrf());
if (checkSyntax(fullExpression)) {
String valueFromForm = null;
if (items == null) {
valueFromForm = getValueFromForm(fullExpression);
} else {
valueFromForm = getValueFromForm(fullExpression, items);
}
String valueFromDb = null;
if (itemBeansI == null) {
valueFromDb = getValueFromDb(fullExpression, itemDatas);
} else {
valueFromDb = getValueFromDb(fullExpression, itemDatas, itemBeansI);
}
logger.debug("valueFromForm : {} , valueFromDb : {}", valueFromForm, valueFromDb);
if (valueFromForm == null && valueFromDb == null) {
throw new OpenClinicaSystemException("OCRERR_0017", new Object[] { fullExpression, expressionWrapper.getRuleSet().getTarget().getValue() });
}
/*
* if (valueFromForm != null) { // TODO: Do this if type a
* date String dateFormat =
* ResourceBundleProvider.getFormatBundle
* ().getString("date_format_string"); String dateRegexp =
* ResourceBundleProvider
* .getFormatBundle().getString("date_regexp");
* valueFromForm =
* ExpressionTreeHelper.isValidDate(valueFromForm,
* dateFormat, dateRegexp); }
*/
value = valueFromForm == null ? valueFromDb : valueFromForm;
}
} else {
// So Expression is not Partial
HashMap<String, String> map = getValueFromDbb(expression);
String valueFromDb = null;
String matchEvents = null;
String valueFromForm = null;
if (checkSyntax(expression)) {
valueFromDb = map.get("value");
matchEvents = map.get("match");
// if se_id are a match go in , otherwise nothing
if (matchEvents != null && matchEvents.equals("true")) {
if (items == null) {
valueFromForm = getValueFromForm(expression);
} else {
valueFromForm = getValueFromForm(expression, items);
}
}
logger.debug("valueFromDb : {}", valueFromDb);
value = valueFromForm == null ? valueFromDb : valueFromForm;
if (value == null) {
logger.info("The value is " + value + " for expression" + expression);
throw new OpenClinicaSystemException("OCRERR_0018", new Object[] { expression });
}
}
}
}
return value;
}
use of org.akaza.openclinica.domain.datamap.StudyEvent 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
}
}
}
// }
}
}
}
use of org.akaza.openclinica.domain.datamap.StudyEvent in project OpenClinica by OpenClinica.
the class OpenRosaSubmissionController method markComplete.
// @RequestMapping(value = "/{studyOID}/fieldsubmission/complete", method = RequestMethod.POST)
public ResponseEntity<String> markComplete(HttpServletRequest request, HttpServletResponse response, @PathVariable("studyOID") String studyOID, @RequestParam(FORM_CONTEXT) String ecid) throws Exception {
HashMap<String, String> subjectContext = null;
PFormCache cache = PFormCache.getInstance(context);
subjectContext = cache.getSubjectContext(ecid);
int studyEventDefinitionID = Integer.valueOf(subjectContext.get("studyEventDefinitionID"));
int userAccountID = Integer.valueOf(subjectContext.get("userAccountID"));
String studySubjectOID = subjectContext.get("studySubjectOID");
String formLayoutOID = subjectContext.get("formLayoutOID");
int studyEventOrdinal = Integer.valueOf(subjectContext.get("studyEventOrdinal"));
UserAccount userAccount = userAccountDao.findById(userAccountID);
StudySubject studySubject = studySubjectDao.findByOcOID(studySubjectOID);
Study study = studyDao.findByOcOID(studyOID);
StudyEventDefinition sed = studyEventDefinitionDao.findById(studyEventDefinitionID);
FormLayout formLayout = formLayoutDao.findByOcOID(formLayoutOID);
CrfVersion crfVersion = crfVersionDao.findAllByCrfId(formLayout.getCrf().getCrfId()).get(0);
StudyEvent studyEvent = studyEventDao.fetchByStudyEventDefOIDAndOrdinalTransactional(sed.getOc_oid(), studyEventOrdinal, studySubject.getStudySubjectId());
EventCrf eventCrf = eventCrfDao.findByStudyEventIdStudySubjectIdFormLayoutId(studyEvent.getStudyEventId(), studySubject.getStudySubjectId(), formLayout.getFormLayoutId());
if (eventCrf == null) {
eventCrf = createEventCrf(formLayout, studyEvent, studySubject, userAccount);
List<Item> items = itemDao.findAllByCrfVersion(crfVersion.getCrfVersionId());
createItemData(items.get(0), "", eventCrf, userAccount);
}
eventCrf.setStatusId(org.akaza.openclinica.domain.Status.UNAVAILABLE.getCode());
eventCrf.setUserAccount(userAccount);
eventCrf.setUpdateId(userAccount.getUserId());
eventCrf.setDateUpdated(new Date());
eventCrfDao.saveOrUpdate(eventCrf);
List<EventCrf> eventCrfs = eventCrfDao.findByStudyEventIdStudySubjectId(studyEvent.getStudyEventId(), studySubject.getOcOid());
List<EventDefinitionCrf> eventDefinitionCrfs = eventDefinitionCrfDao.findAvailableByStudyEventDefStudy(sed.getStudyEventDefinitionId(), study.getStudyId());
int count = 0;
for (EventCrf evCrf : eventCrfs) {
if (evCrf.getStatusId() == org.akaza.openclinica.domain.Status.UNAVAILABLE.getCode() || evCrf.getStatusId() == org.akaza.openclinica.domain.Status.DELETED.getCode() || evCrf.getStatusId() == org.akaza.openclinica.domain.Status.AUTO_DELETED.getCode()) {
for (EventDefinitionCrf eventDefinitionCrf : eventDefinitionCrfs) {
if (eventDefinitionCrf.getCrf().getCrfId() == evCrf.getFormLayout().getCrf().getCrfId()) {
count++;
break;
}
}
}
}
if (count == eventDefinitionCrfs.size()) {
studyEvent.setSubjectEventStatusId(SubjectEventStatus.COMPLETED.getCode());
studyEvent.setUserAccount(userAccount);
studyEventDao.saveOrUpdate(studyEvent);
} else if (studyEvent.getSubjectEventStatusId() == SubjectEventStatus.SCHEDULED.getCode()) {
studyEvent.setSubjectEventStatusId(SubjectEventStatus.DATA_ENTRY_STARTED.getCode());
studyEvent.setUserAccount(userAccount);
studyEventDao.saveOrUpdate(studyEvent);
}
String responseMessage = "<OpenRosaResponse xmlns=\"http://openrosa.org/http/response\">" + "<message>success</message>" + "</OpenRosaResponse>";
return new ResponseEntity<String>(responseMessage, HttpStatus.CREATED);
}
use of org.akaza.openclinica.domain.datamap.StudyEvent in project OpenClinica by OpenClinica.
the class EventProcessor method processParticipant.
private void processParticipant(SubmissionContainer container, Errors errors, StudySubject studySubject, StudyEventDefinition studyEventDefinition) throws Exception {
Integer ordinal = Integer.valueOf(container.getSubjectContext().get("studyEventOrdinal"));
StudyEvent existingEvent = studyEventDao.fetchByStudyEventDefOIDAndOrdinal(studyEventDefinition.getOc_oid(), ordinal, studySubject.getStudySubjectId());
if (existingEvent == null) {
container.setStudyEvent(createStudyEvent(studySubject, studyEventDefinition, ordinal, container.getUser()));
} else
container.setStudyEvent(existingEvent);
// Create event crf if it doesn't exist
if (studyEventDefinition.getStatus() != Status.AVAILABLE) {
logger.info("This Crf Version has a Status Not available in this Study Event Defn");
errors.reject("This Crf Version has a Status Not available in this Study Event Defn");
throw new Exception("This Crf Version has a Status Not available in this Study Event Defn");
}
EventCrf existingEventCrf = eventCrfDao.findByStudyEventIdStudySubjectIdCrfId(container.getStudyEvent().getStudyEventId(), container.getSubject().getStudySubjectId(), container.getFormLayout().getCrf().getCrfId());
if (existingEventCrf == null) {
logger.info("***New EventCrf is created***");
// create event crf
container.setEventCrf(createEventCrf(container.getFormLayout(), container.getStudyEvent(), container.getSubject(), container.getUser()));
} else if (existingEventCrf.getCrfVersion().getOcOid().equals(container.getCrfVersion().getOcOid())) {
logger.info("*** Existing EventCrf with same CRF Version ***");
// use existing event crf
container.setEventCrf(existingEventCrf);
} else {
// different version already exists. log error and abort submission
errors.reject("Existing EventCrf with other CRF version");
logger.info("*** Existing EventCrf with other CRF version ***");
throw new Exception("*** Existing EventCrf with other CRF version ***");
}
}
use of org.akaza.openclinica.domain.datamap.StudyEvent in project OpenClinica by OpenClinica.
the class StudyEventController method completeParticipantEvent.
/**
* @api {put} /pages/auth/api/v1/studyevent/studysubject/{studySubjectOid}/studyevent/{studyEventDefOid}/ordinal/{ordinal}/complete Complete a Participant Event
* @apiName completeParticipantEvent
* @apiPermission Authenticate using api-key. admin
* @apiVersion 1.0.0
* @apiParam {String} studySubjectOid Study Subject OID.
* @apiParam {String} studyEventDefOid Study Event Definition OID.
* @apiParam {Integer} ordinal Ordinal of Study Event Repetition.
* @apiGroup Form
* @apiHeader {String} api_key Users unique access-key.
* @apiDescription Completes a participant study event.
* @apiErrorExample {json} Error-Response:
* HTTP/1.1 403 Forbidden
* {
* "code": "403",
* "message": "Request Denied. Operation not allowed."
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* "code": "200",
* "message": "Success."
* }
*/
@RequestMapping(value = "/studysubject/{studySubjectOid}/studyevent/{studyEventDefOid}/ordinal/{ordinal}/complete", method = RequestMethod.PUT)
@ResponseBody
public Map<String, String> completeParticipantEvent(HttpServletRequest request, @PathVariable("studySubjectOid") String studySubjectOid, @PathVariable("studyEventDefOid") String studyEventDefOid, @PathVariable("ordinal") Integer ordinal) throws Exception {
StudySubject subject = studySubjectDao.findByOcOID(studySubjectOid);
StudyEvent studyEvent = studyEventDao.fetchByStudyEventDefOIDAndOrdinal(studyEventDefOid, ordinal, subject.getStudySubjectId());
StudyEventDefinition studyEventDefinition = studyEventDefinitionDao.findByStudyEventDefinitionId(studyEvent.getStudyEventDefinition().getStudyEventDefinitionId());
Study study = studyEventDefinition.getStudy();
Map<String, String> response = new HashMap<String, String>();
// Verify this request is allowed.
if (!mayProceed(study)) {
response.put("code", String.valueOf(HttpStatus.FORBIDDEN.value()));
response.put("message", "Request Denied. Operation not allowed.");
return response;
}
// Get list of eventCRFs
// By this point we can assume all Participant forms have been submitted at least once and have an event_crf entry.
// Non-Participant forms may not have an entry.
List<EventDefinitionCrf> eventDefCrfs = eventDefinitionCrfDao.findByStudyEventDefinitionId(studyEventDefinition.getStudyEventDefinitionId());
List<EventCrf> eventCrfs = eventCrfDao.findByStudyEventIdStudySubjectId(studyEvent.getStudyEventId(), studySubjectOid);
try {
completeData(studyEvent, eventDefCrfs, eventCrfs);
} catch (Exception e) {
// Transaction has been rolled back due to an exception.
logger.error("Error encountered while completing Study Event: " + e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
response.put("code", String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()));
response.put("message", "Error encountered while completing participant event.");
return response;
}
response.put("code", String.valueOf(HttpStatus.OK.value()));
response.put("message", "Success.");
return response;
//return new ResponseEntity<String>("<message>Success</message>", org.springframework.http.HttpStatus.OK);
}
Aggregations