public StudySubjectBean getSSBean(HashMap<String, String> userContext) throws Exception { String studySubjectOid = userContext.get("studySubjectOID"); StudySubjectBean ssBean = null; StudySubjectDAO ssdao = new StudySubjectDAO(dataSource); SubjectDAO subjectdao = new SubjectDAO(dataSource); UserAccountDAO udao = new UserAccountDAO(dataSource); if (studySubjectOid != null) { ssBean = ssdao.findByOid(studySubjectOid); } else { String studyOid = userContext.get("studyOID"); StudyBean studyBean = sdao.findByOid(studyOid); int studyEventDefnId = Integer.valueOf(userContext.get("studyEventDefinitionID")); int studyEventOrdinal = Integer.valueOf(userContext.get("studyEventOrdinal")); UserAccountBean uBean = (UserAccountBean) udao.findByPK(1); // build Subject Account SubjectBean subjectBean = createSubjectBean(uBean); subjectBean = (SubjectBean) subjectdao.findByPK(subjectBean.getId()); // build StudySubject Account ssBean = createStudySubjectBean(studyBean, subjectBean, uBean); ssBean = (StudySubjectBean) ssdao.findByPK(ssBean.getId()); System.out.println("study subject oid: " + ssBean.getOid()); // build User Account UserAccountBean userAccountBean = createUserAccount(uBean, studyBean, ssBean); userAccountBean = (UserAccountBean) udao.findByPK(userAccountBean.getId()); // build and schedule study Event StudyEventBean studyEventBean = createStudyEventBean(ssBean, studyEventDefnId, studyEventOrdinal, userAccountBean); } return ssBean; }
public String getInputUsername(StudyBean studyBean, StudySubjectBean studySubjectBean) { String inputUserName = null; if (studySubjectBean != null) { if (studyBean.getParentStudyId() > 0) studyBean = getStudy(studyBean.getParentStudyId()); inputUserName = studyBean.getOid() + "." + studySubjectBean.getOid(); } return inputUserName; }
/** * @api {post} /pages/api/v1/editform/:studyOid/submission Submit form data * @apiName doSubmission * @apiPermission admin * @apiVersion 1.0.0 * @apiParam {String} studyOid Study Oid. * @apiParam {String} ecid Key that will be used to look up subject context information while * processing submission. * @apiGroup Form * @apiDescription Submits the data from a completed form. */ @POST @Path("/{studyOID}/submission") @Produces(MediaType.APPLICATION_XML) public Response doSubmission( @Context HttpServletRequest request, @Context HttpServletResponse response, @Context ServletContext servletContext, @PathParam("studyOID") String studyOID, @QueryParam(FORM_CONTEXT) String context) { String output = null; Response.ResponseBuilder builder = Response.noContent(); String studySubjectOid = null; Integer studyEventDefnId = null; Integer studyEventOrdinal = null; String crfVersionOID = null; CRFVersionDAO crfvdao = new CRFVersionDAO(dataSource); try { if (ServletFileUpload.isMultipartContent(request)) { LOGGER.warn("WARNING: This prototype doesn't support multipart content."); } if (!mayProceedSubmission(studyOID)) builder.status(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE).build(); PFormCache cache = PFormCache.getInstance(servletContext); HashMap<String, String> userContext = cache.getSubjectContext(context); StudySubjectDAO ssdao = new StudySubjectDAO<String, ArrayList>(dataSource); StudySubjectBean ssBean = getSSBean(userContext); if (!mayProceedSubmission(studyOID, ssBean)) return null; studyEventDefnId = Integer.valueOf(userContext.get("studyEventDefinitionID")); studyEventOrdinal = Integer.valueOf(userContext.get("studyEventOrdinal")); crfVersionOID = userContext.get("crfVersionOID"); StringWriter writer = new StringWriter(); String body = IOUtils.toString(request.getInputStream(), "UTF-8"); CRFVersionBean crfVersion = crfvdao.findByOid(crfVersionOID); if (crfVersion.getXform() != null && !crfVersion.getXform().equals("")) { body = body.substring(body.indexOf("<" + crfVersion.getXformName())); int length = body.indexOf(" "); body = body.replace( body.substring(body.lastIndexOf("<meta>"), body.lastIndexOf("</meta>") + 7), ""); body = body.substring(0, body.lastIndexOf("</" + crfVersion.getXformName()) + length + 2); body = "<instance>" + body + "</instance>"; } else { body = body.substring(body.indexOf("<F_")); int length = body.indexOf(" "); body = body.replace(body.substring(body.indexOf("<meta>"), body.indexOf("</meta>") + 7), ""); body = body.substring(0, body.indexOf("</F_") + length + 2); body = "<instance>" + body + "</instance>"; } Errors errors = getPformSubmissionService() .saveProcess( body, ssBean.getOid(), studyEventDefnId, studyEventOrdinal, crfvdao.findByOid(crfVersionOID)); // Set response headers Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); Date currentDate = new Date(); cal.setTime(currentDate); SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz"); format.setCalendar(cal); builder.header("Date", format.format(currentDate)); builder.header("X-OpenRosa-Version", "1.0"); builder.type("text/xml; charset=utf-8"); if (!errors.hasErrors()) { builder.entity( "<OpenRosaResponse xmlns=\"http://openrosa.org/http/response\">" + "<message>success</message>" + "</OpenRosaResponse>"); LOGGER.debug("Successful OpenRosa submission"); } else { LOGGER.error("Failed OpenRosa submission"); response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.flushBuffer(); } } catch (Exception e) { LOGGER.error(e.getMessage()); LOGGER.error(ExceptionUtils.getStackTrace(e)); return builder.status(javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR).build(); } try { // Notify Participate of successful form submission. String pManageUrl = CoreResources.getField("portalURL") + "/app/rest/oc/submission"; Submission submission = new Submission(); Study pManageStudy = new Study(); pManageStudy.setInstanceUrl( CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOID); pManageStudy.setStudyOid(studyOID); submission.setStudy(pManageStudy); submission.setStudy_event_def_id(studyEventDefnId); submission.setStudy_event_def_ordinal(studyEventOrdinal); submission.setCrf_version_id(crfvdao.findByOid(crfVersionOID).getId()); RestTemplate rest = new RestTemplate(); String result = rest.postForObject(pManageUrl, submission, String.class); LOGGER.debug("Notified Participate of CRF submission with a result of: " + result); } catch (Exception e) { LOGGER.error("Unable to notify Participate of successful CRF submission."); LOGGER.error(e.getMessage()); LOGGER.error(ExceptionUtils.getStackTrace(e)); } return builder.status(javax.ws.rs.core.Response.Status.CREATED).build(); }