CacheControl cc = new CacheControl();
cc.setNoCache( true );
if(!UtilMethods.isSet(roleId) || roleId.equals("root")) { // Loads Root Roles
JSONArray jsonRoles = new JSONArray();
JSONObject jsonRoleObject = new JSONObject();
jsonRoleObject.put("id", "root");
jsonRoleObject.put("name", "Roles");
jsonRoleObject.put("top", "true");
List<Role> rootRoles = roleAPI.findRootRoles();
JSONArray jsonChildren = new JSONArray();
for(Role r : rootRoles) {
JSONObject jsonRoleChildObject = new JSONObject();
jsonRoleChildObject.put("id", r.getId());
jsonRoleChildObject.put("$ref", r.getId());
jsonRoleChildObject.put("name", UtilMethods.javaScriptify(r.getName()));
jsonRoleChildObject.put("locked", r.isLocked());
jsonRoleChildObject.put("children", true);
jsonChildren.add(jsonRoleChildObject);
}
//In order to add a JsonArray to a JsonObject
//we need to specify that is an object (API bug)
jsonRoleObject.put("children", (Object)jsonChildren);
jsonRoles.add(jsonRoleObject);
return responseResource.response(jsonRoles.toString(), cc);
} else { // Loads Children Roles of given Role ID
Role role = roleAPI.loadRoleById(roleId);
JSONObject jsonRoleObject = new JSONObject();
jsonRoleObject.put("id", role.getId());
jsonRoleObject.put("name", UtilMethods.javaScriptify(role.getName()));
jsonRoleObject.put("locked", role.isLocked());
JSONArray jsonChildren = new JSONArray();
List<String> children = role.getRoleChildren();
if(children != null) {
for(String childId : children) {
Role r = roleAPI.loadRoleById(childId);
JSONObject jsonRoleChildObject = new JSONObject();
jsonRoleChildObject.put("id", r.getId());
jsonRoleChildObject.put("$ref", r.getId());
jsonRoleChildObject.put("name", UtilMethods.javaScriptify(r.getName()));
jsonRoleChildObject.put("locked", r.isLocked());
jsonRoleChildObject.put("children", true);
jsonChildren.add(jsonRoleChildObject);
}
}
//In order to add a JsonArray to a JsonObject