//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();