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 "";
  }
  public String getStudyHost(String studyOid) throws Exception {

    String ocUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
    String pManageUrl = CoreResources.getField("portalURL");
    String pManageUrlFull =
        pManageUrl + "/app/rest/oc/authorizations?studyoid=" + studyOid + "&instanceurl=" + ocUrl;

    CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory();
    requestFactory.setReadTimeout(PARTICIPATE_READ_TIMEOUT);
    RestTemplate rest = new RestTemplate(requestFactory);
    try {
      Authorization[] response = rest.getForObject(pManageUrlFull, Authorization[].class);
      if (response.length > 0
          && response[0].getStudy() != null
          && response[0].getStudy().getHost() != null
          && !response[0].getStudy().getHost().equals("")) {
        URL url = new URL(pManageUrl);
        String port = "";
        if (url.getPort() > 0) port = ":" + String.valueOf(url.getPort());
        return url.getProtocol()
            + "://"
            + response[0].getStudy().getHost()
            + "."
            + url.getHost()
            + port
            + "/#/login";
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
      logger.error(ExceptionUtils.getStackTrace(e));
    }
    return "";
  }
  public String sendEmailThruMandrillViaOcui(ParticipantDTO participantDTO, String hostname) {
    String host = hostname.substring(0, hostname.indexOf("/#/login"));
    String pManageUrl = host + "/app/rest/oc/email";

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

    try {
      ParticipantDTO response =
          rest.postForObject(pManageUrl, participantDTO, ParticipantDTO.class);
    } catch (Exception e) {
      logger.error(e.getMessage());
      logger.error(ExceptionUtils.getStackTrace(e));
    }
    return "";
  }
 public String getHostNameAvailability(String hostName) {
   String pManageUrl =
       CoreResources.getField("portalURL") + "/app/permit/studys/name?hostName=" + hostName;
   CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory();
   requestFactory.setReadTimeout(PARTICIPATE_READ_TIMEOUT);
   RestTemplate rest = new RestTemplate(requestFactory);
   String response = null;
   try {
     if (!validHostNameCheck(hostName)) return INVALID;
     response = rest.getForObject(pManageUrl, String.class);
     if (response.equals("UNAVAILABLE")) return UNAVAILABLE;
     else if (response.equals("INVALID")) return INVALID;
     else if (response.equals("AVAILABLE")) return AVAILABLE;
   } catch (Exception e) {
     logger.error(e.getMessage());
     logger.error(ExceptionUtils.getStackTrace(e));
   }
   return UNKNOWN;
 }
 private String loadRegistrationStatus(String studyOid) {
   String ocUrl = CoreResources.getField("sysURL.base") + "rest2/openrosa/" + studyOid;
   String pManageUrl =
       CoreResources.getField("portalURL")
           + "/app/rest/oc/authorizations?studyoid="
           + studyOid
           + "&instanceurl="
           + ocUrl;
   CommonsClientHttpRequestFactory requestFactory = new CommonsClientHttpRequestFactory();
   requestFactory.setReadTimeout(PARTICIPATE_READ_TIMEOUT);
   RestTemplate rest = new RestTemplate(requestFactory);
   try {
     Authorization[] response = rest.getForObject(pManageUrl, Authorization[].class);
     if (response.length > 0 && response[0].getAuthorizationStatus() != null)
       return response[0].getAuthorizationStatus().getStatus();
   } catch (Exception e) {
     logger.error(e.getMessage());
     logger.debug(ExceptionUtils.getStackTrace(e));
   }
   return "";
 }