/**
   * Returns a JSON representation of the environments (with servers) that the Role with the given
   * roleid can push to <br>
   * Each Environment node contains: id, name.
   *
   * <p>Usage: /loadenvironments/{roleid}
   *
   * @throws JSONException
   */
  @GET
  @Path("/loadenvironments/{params:.*}")
  @Produces("application/json")
  public Response loadEnvironments(
      @Context HttpServletRequest request, @PathParam("params") String params)
      throws DotStateException, DotDataException, DotSecurityException, LanguageException,
          JSONException {

    InitDataObject initData = init(params, true, request, true);

    // Creating an utility response object
    ResourceResponse responseResource = new ResourceResponse(initData.getParamsMap());

    String roleId = initData.getParamsMap().get("roleid");

    // Using JsonArray instead of manually creating the json object
    JSONArray jsonEnvironments = new JSONArray();

    // First objects is expected to be blank
    JSONObject jsonEnvironmentFirst = new JSONObject();
    jsonEnvironmentFirst.put("id", "0");
    jsonEnvironmentFirst.put("name", "");

    jsonEnvironments.add(jsonEnvironmentFirst);

    Role role = APILocator.getRoleAPI().loadRoleById(roleId);
    User user = APILocator.getUserAPI().loadUserById(role.getRoleKey());
    boolean isAdmin = APILocator.getUserAPI().isCMSAdmin(user);

    List<Role> roles = APILocator.getRoleAPI().loadRolesForUser(user.getUserId(), true);
    Set<Environment> environments = new HashSet<Environment>();
    if (isAdmin) {
      List<Environment> app = APILocator.getEnvironmentAPI().findEnvironmentsWithServers();
      for (Environment e : app) environments.add(e);
    } else
      for (Role r : roles)
        environments.addAll(APILocator.getEnvironmentAPI().findEnvironmentsByRole(r.getId()));

    // For each env, create one json and add it to the array
    for (Environment e : environments) {

      JSONObject environmentBundle = new JSONObject();
      environmentBundle.put("id", e.getId());
      // Escape name for cases like: dotcms's
      environmentBundle.put("name", StringEscapeUtils.unescapeJava(e.getName()));

      jsonEnvironments.add(environmentBundle);
    }

    CacheControl cc = new CacheControl();
    cc.setNoCache(true);
    return Response.ok(jsonEnvironments.toString(), MediaType.APPLICATION_JSON_TYPE)
        .cacheControl(cc)
        .build();
  }
  /**
   * Returns a JSON representation of the environments that the Role with the given roleid can push
   * to <br>
   * Each Environment node contains: id, name.
   *
   * <p>Usage: /loadenvironments/{roleid}
   */
  @GET
  @Path("/loadenvironments/{params:.*}")
  @Produces("application/json")
  public String loadEnvironments(
      @Context HttpServletRequest request, @PathParam("params") String params)
      throws DotStateException, DotDataException, DotSecurityException, LanguageException {
    InitDataObject initData = init(params, true, request, true);

    String roleId = initData.getParamsMap().get("roleid");

    StringBuilder json = new StringBuilder();

    json.append("[");

    json.append("{id: '0', ");
    json.append("name: '' }, ");

    int environmentCounter = 0;

    Role role = APILocator.getRoleAPI().loadRoleById(roleId);
    User user = APILocator.getUserAPI().loadUserById(role.getRoleKey());
    boolean isAdmin = APILocator.getUserAPI().isCMSAdmin(user);

    List<Role> roles = APILocator.getRoleAPI().loadRolesForUser(user.getUserId(), true);
    Set<Environment> environments = new HashSet<Environment>();
    if (isAdmin) {
      List<Environment> app = APILocator.getEnvironmentAPI().findAllEnvironments();
      for (Environment e : app) environments.add(e);
    } else
      for (Role r : roles)
        environments.addAll(APILocator.getEnvironmentAPI().findEnvironmentsByRole(r.getId()));

    for (Environment e : environments) {

      json.append("{id: '").append(e.getId()).append("', ");
      json.append("name: '").append(e.getName()).append("' } ");

      if (environmentCounter + 1 < environments.size()) {
        json.append(", ");
      }

      environmentCounter++;
    }

    json.append("]");

    return json.toString();
  }