public String registerStudy(String studyOid, String hostName) {
    String ocUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
    String pManageUrl =
        CoreResources.getField("portalURL")
            + "/app/rest/oc/authorizations?studyoid="
            + studyOid
            + "&instanceurl="
            + ocUrl;
    Authorization authRequest = new Authorization();
    Study authStudy = new Study();
    authStudy.setStudyOid(studyOid);
    authStudy.setInstanceUrl(ocUrl);
    authStudy.setHost(hostName);
    authRequest.setStudy(authStudy);

    CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory();
    requestFactory.setReadTimeout(PARTICIPATE_READ_TIMEOUT);
    RestTemplate rest = new RestTemplate(requestFactory);

    try {
      Authorization response = rest.postForObject(pManageUrl, authRequest, Authorization.class);
      if (response != null && response.getAuthorizationStatus() != null)
        return response.getAuthorizationStatus().getStatus();
    } catch (Exception e) {
      logger.error(e.getMessage());
      logger.error(ExceptionUtils.getStackTrace(e));
    }
    return "";
  }
  /**
   * @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();
  }