use of org.akaza.openclinica.domain.datamap.EventCrf 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.EventCrf in project OpenClinica by OpenClinica.
the class EnketoUrlService method getEditUrl.
public String getEditUrl(String subjectContextKey, PFormCacheSubjectContextEntry subjectContext, String studyOid, FormLayout formLayout, StudyEvent studyEvent, String flavor) throws Exception {
Study study = enketoCredentials.getParentStudy(studyOid);
studyOid = study.getOc_oid();
String editURL = null;
StudyEventDefinition eventDef;
StudySubject subject;
String goTo = null;
if (subjectContext.getItemName() != null) {
goTo = (subjectContext.isItemInRepeatingGroup()) ? "//" + subjectContext.getItemRepeatGroupName() + "[" + subjectContext.getItemRepeatOrdinal() + "]//" + subjectContext.getItemName() : "//" + subjectContext.getItemName();
}
if (studyEvent == null) {
// Lookup relevant data
eventDef = studyEventDefinitionDao.findByStudyEventDefinitionId(subjectContext.getStudyEventDefinitionId());
subject = studySubjectDao.findByOcOID(subjectContext.getStudySubjectOid());
studyEvent = studyEventDao.fetchByStudyEventDefOIDAndOrdinal(eventDef.getOc_oid(), Integer.valueOf(subjectContext.getOrdinal()), subject.getStudySubjectId());
} else {
eventDef = studyEvent.getStudyEventDefinition();
subject = studyEvent.getStudySubject();
}
if (formLayout == null) {
formLayout = formLayoutDao.findByOcOID(subjectContext.getFormLayoutOid());
}
EventCrf eventCrf = eventCrfDao.findByStudyEventIdStudySubjectIdFormLayoutId(studyEvent.getStudyEventId(), subject.getStudySubjectId(), formLayout.getFormLayoutId());
CrfVersion crfVersion = eventCrf.getCrfVersion();
// Load populated instance
String populatedInstance = populateInstance(crfVersion, formLayout, eventCrf, studyOid, flavor);
// Call Enketo api to get edit url
EnketoAPI enketo = new EnketoAPI(EnketoCredentials.getInstance(studyOid));
// Build redirect url
String redirectUrl = getRedirectUrl(subject.getOcOid(), studyOid);
boolean markComplete = true;
if (eventCrf.getStatusId() == Status.UNAVAILABLE.getCode()) {
markComplete = false;
}
// Return Enketo URL
List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByEventCrfId(eventCrf.getEventCrfId());
EnketoURLResponse eur = enketo.getEditURL(formLayout, flavor, populatedInstance, subjectContextKey, redirectUrl, markComplete, studyOid, mediaList, goTo);
editURL = eur.getEdit_url();
int hashIndex = editURL.lastIndexOf("#");
String part1 = "";
String part2 = "";
if (hashIndex != -1) {
part1 = editURL.substring(0, hashIndex);
part2 = editURL.substring(hashIndex);
editURL = part1 + "&ecid=" + subjectContextKey + part2;
} else {
editURL = editURL + "&ecid=" + subjectContextKey;
}
logger.debug("Generating Enketo edit url for form: " + editURL);
return editURL;
}
use of org.akaza.openclinica.domain.datamap.EventCrf 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);
}
use of org.akaza.openclinica.domain.datamap.EventCrf in project OpenClinica by OpenClinica.
the class ItemProcessor method process.
public void process(SubmissionContainer container) throws Exception {
logger.info("Executing Item Processor.");
ArrayList<HashMap> listOfUploadFilePaths = container.getListOfUploadFilePaths();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(container.getRequestBody()));
Document doc = db.parse(is);
String itemName;
String itemValue;
String groupNodeName = "";
NodeList instanceNodeList = doc.getElementsByTagName("instance");
// Instance loop
for (int i = 0; i < instanceNodeList.getLength(); i = i + 1) {
Node instanceNode = instanceNodeList.item(i);
if (instanceNode instanceof Element) {
NodeList crfNodeList = instanceNode.getChildNodes();
// Form loop
for (int j = 0; j < crfNodeList.getLength(); j = j + 1) {
Node crfNode = crfNodeList.item(j);
if (crfNode instanceof Element) {
CrfVersion crfVersion = container.getEventCrf().getCrfVersion();
EventCrf eventCrf = container.getEventCrf();
ArrayList<ItemData> itemDataList = new ArrayList<ItemData>();
HashMap<Integer, Set<Integer>> groupOrdinalMapping = new HashMap<Integer, Set<Integer>>();
NodeList groupNodeList = crfNode.getChildNodes();
List<Item> items = itemDao.findAllByCrfVersionId(crfVersion.getCrfVersionId());
List<ItemData> itemDatas = itemDataDao.findAllByEventCrf(container.getEventCrf().getEventCrfId());
List<ItemGroup> itemGroups = itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
List<ItemGroupMetadata> itemGroupMetadatas = itemGroupMetadataDao.findAllByCrfVersion(crfVersion.getCrfVersionId());
List<ItemFormMetadata> itemFormMetadatas = itemFormMetadataDao.findAllByCrfVersion(crfVersion.getCrfVersionId());
// Group loop
for (int k = 0; k < groupNodeList.getLength(); k = k + 1) {
Node groupNode = groupNodeList.item(k);
if (groupNode instanceof Element && !groupNode.getNodeName().startsWith("SECTION_")) {
groupNodeName = groupNode.getNodeName();
ItemGroup itemGroup = lookupItemGroup(groupNodeName, crfVersion, itemGroups);
if (itemGroup == null) {
logger.error("Failed to lookup item group: '" + groupNodeName + "'. Continuing with submission.");
continue;
}
if (itemGroup != null && !groupOrdinalMapping.containsKey(itemGroup.getItemGroupId()))
groupOrdinalMapping.put(itemGroup.getItemGroupId(), new TreeSet<Integer>());
NodeList itemNodeList = groupNode.getChildNodes();
// Item loop
for (int m = 0; m < itemNodeList.getLength(); m = m + 1) {
Node itemNode = itemNodeList.item(m);
if (itemNode instanceof Element && !itemNode.getNodeName().endsWith(".HEADER") && !itemNode.getNodeName().endsWith(".SUBHEADER") && !itemNode.getNodeName().equals("OC.REPEAT_ORDINAL") && !itemNode.getNodeName().equals("OC.STUDY_SUBJECT_ID") && !itemNode.getNodeName().equals("OC.STUDY_SUBJECT_ID_CONFIRM")) {
itemName = itemNode.getNodeName().trim();
itemValue = itemNode.getTextContent();
Item item = lookupItem(itemName, crfVersion, items);
if (item == null) {
logger.error("Failed to lookup item: '" + itemName + "'. Continuing with submission.");
continue;
}
ItemGroupMetadata itemGroupMeta = lookupItemGroupMetadata(item.getItemId(), crfVersion.getCrfVersionId(), itemGroupMetadatas);
ItemFormMetadata itemFormMetadata = lookupItemFormMetadata(item.getItemId(), crfVersion.getCrfVersionId(), itemFormMetadatas);
Integer itemOrdinal = getItemOrdinal(groupNode, itemGroupMeta.isRepeatingGroup(), itemDataList, item);
// Convert space separated Enketo multiselect values to comma separated OC multiselect values
Integer responseTypeId = itemFormMetadata.getResponseSet().getResponseType().getResponseTypeId();
if (responseTypeId == 3 || responseTypeId == 7) {
itemValue = itemValue.replaceAll(" ", ",");
}
if (responseTypeId == 4) {
for (HashMap uploadFilePath : listOfUploadFilePaths) {
if ((boolean) uploadFilePath.containsKey(itemValue) && itemValue != "") {
itemValue = (String) uploadFilePath.get(itemValue);
break;
}
}
}
// Build set of submitted row numbers to be used to find deleted DB rows later
Set<Integer> ordinals = groupOrdinalMapping.get(itemGroup.getItemGroupId());
ordinals.add(itemOrdinal);
groupOrdinalMapping.put(itemGroup.getItemGroupId(), ordinals);
ItemData newItemData = createItemData(item, itemValue, itemOrdinal, eventCrf, container.getStudy(), container.getSubject(), container.getUser());
Errors itemErrors = validateItemData(newItemData, item, responseTypeId);
if (itemErrors.hasErrors()) {
container.getErrors().addAllErrors(itemErrors);
throw new Exception("Item validation error. Rolling back submission changes.");
} else {
itemDataList.add(newItemData);
}
ItemData existingItemData = lookupItemData(item.getItemId(), eventCrf.getEventCrfId(), itemOrdinal, itemDatas);
if (existingItemData == null) {
// No existing value, create new item.
if (newItemData.getOrdinal() < 0) {
newItemData.setOrdinal(itemDataDao.getMaxGroupRepeat(eventCrf.getEventCrfId(), item.getItemId()) + 1);
groupOrdinalMapping.get(itemGroup.getItemGroupId()).add(newItemData.getOrdinal());
}
itemDataDao.saveOrUpdate(newItemData);
newItemData.setStatus(Status.UNAVAILABLE);
itemDataDao.saveOrUpdate(newItemData);
} else if (existingItemData.getValue().equals(newItemData.getValue())) {
// Existing item. Value unchanged. Do nothing.
} else {
// Existing item. Value changed. Update existing value.
existingItemData.setValue(newItemData.getValue());
existingItemData.setUpdateId(container.getUser().getUserId());
existingItemData.setDateUpdated(new Date());
itemDataDao.saveOrUpdate(existingItemData);
}
}
}
}
}
// Delete rows that have been removed
removeDeletedRows(groupOrdinalMapping, eventCrf, crfVersion, container.getStudy(), container.getSubject(), container.getLocale(), container.getUser());
}
}
}
}
}
use of org.akaza.openclinica.domain.datamap.EventCrf in project OpenClinica by OpenClinica.
the class EventProcessor method createEventCrf.
private EventCrf createEventCrf(CrfVersion crfVersion, StudyEvent studyEvent, StudySubject studySubject, UserAccount user) {
EventCrf eventCrf = new EventCrf();
Date currentDate = new Date();
eventCrf.setAnnotations("");
eventCrf.setDateCreated(currentDate);
eventCrf.setCrfVersion(crfVersion);
eventCrf.setInterviewerName("");
eventCrf.setDateInterviewed(null);
eventCrf.setUserAccount(user);
eventCrf.setStatusId(Status.AVAILABLE.getCode());
// setCompletionStatusId(1);
eventCrf.setCompletionStatus(completionStatusDao.findByCompletionStatusId(1));
eventCrf.setStudySubject(studySubject);
eventCrf.setStudyEvent(studyEvent);
eventCrf.setValidateString("");
eventCrf.setValidatorAnnotations("");
eventCrf.setUpdateId(user.getUserId());
eventCrf.setDateUpdated(new Date());
eventCrf.setValidatorId(0);
eventCrf.setOldStatusId(0);
eventCrf.setSdvUpdateId(0);
eventCrfDao.saveOrUpdate(eventCrf);
eventCrf = eventCrfDao.findByStudyEventIdStudySubjectIdCrfVersionId(studyEvent.getStudyEventId(), studySubject.getStudySubjectId(), crfVersion.getCrfVersionId());
logger.debug("*********CREATED EVENT CRF");
return eventCrf;
}
Aggregations