@GET
  @Path("/{studyOID}/getSchedule")
  @Produces(MediaType.APPLICATION_XML)
  public String getSchedule(
      @Context HttpServletRequest request,
      @Context HttpServletResponse response,
      @Context ServletContext context,
      @PathParam("studyOID") String studyOID,
      @RequestHeader("Authorization") String authorization)
      throws Exception {

    String ssoid = request.getParameter("studySubjectOID");
    StudySubjectDAO ssdao = new StudySubjectDAO<String, ArrayList>(dataSource);
    StudySubjectBean ssBean = ssdao.findByOid(ssoid);
    if (!mayProceedSubmission(studyOID, ssBean)) return null;

    HashMap<String, String> urlCache =
        (HashMap<String, String>) context.getAttribute("pformURLCache");
    context.getAttribute("subjectContextCache");
    if (ssoid == null) {
      return "<error>studySubjectOID is null :(</error>";
    }

    try {
      // Need to retrieve crf's for next event
      StudyEventDAO eventDAO = new StudyEventDAO(getDataSource());
      StudyEventBean nextEvent = (StudyEventBean) eventDAO.getNextScheduledEvent(ssoid);
      CRFVersionDAO versionDAO = new CRFVersionDAO(getDataSource());
      ArrayList<CRFVersionBean> crfs =
          versionDAO.findDefCRFVersionsByStudyEvent(nextEvent.getStudyEventDefinitionId());
      PFormCache cache = PFormCache.getInstance(context);
      for (CRFVersionBean crfVersion : crfs) {
        String enketoURL = cache.getPFormURL(studyOID, crfVersion.getOid());
        String contextHash =
            cache.putSubjectContext(
                ssoid,
                String.valueOf(nextEvent.getStudyEventDefinitionId()),
                String.valueOf(nextEvent.getSampleOrdinal()),
                crfVersion.getOid());
      }
    } catch (Exception e) {
      LOGGER.debug(e.getMessage());
      LOGGER.debug(ExceptionUtils.getStackTrace(e));
      return "<error>" + e.getMessage() + "</error>";
    }

    response.setHeader("Content-Type", "text/xml; charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=\"schedule.xml\";");
    response.setContentType("text/xml; charset=utf-8");
    return "<result>success</result>";
  }
  /**
   * @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();
  }