use of org.akaza.openclinica.service.crfdata.xform.EnketoURLResponse 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;
}
Aggregations