public static JSONArray createPermissions(Collection<Permission> permissions, ModuleManager moduleController) {
Map<String, List<Permission>> pMap = new TreeMap();
for (Permission permission : permissions) {
if (moduleController != null) {
PermissionTemplate permissionTemplate = moduleController.getPermissionTemplate(permission.getKey());
if (permissionTemplate != null) {
List<Permission> ps = pMap.get(permissionTemplate.getModule().getName());
if (ps == null) {
ps = new ArrayList<Permission>();
pMap.put(permissionTemplate.getModule().getName(), ps);
}
ps.add(permission);
}
}
}
JSONArray jmodules = new JSONArray();
for (String module : pMap.keySet()) {
JSONObject jModule = new JSONObject();
jmodules.element(jModule);
jModule.element("name", module);
JSONArray jpermissions = new JSONArray();
jModule.element("permissions", jpermissions);
Collections.sort(pMap.get(module));
for (Permission permission : pMap.get(module)) {
JSONObject jpermission = new JSONObject();
jpermissions.element(jpermission);
jpermission.element("key", permission.getKey());
jpermission.element("value", permission.getValue());
jpermission.element("isValuePermission", permission.isValuePermission());
if (moduleController != null) {
PermissionTemplate permissionTemplate = moduleController.getPermissionTemplate(permission.getKey());
if (permissionTemplate != null) {
jpermission.element("editor", "v2js_" + permissionTemplate.getEditorTemplate());
if (!permissionTemplate.getValues().isEmpty()) {
JSONArray jvalues = new JSONArray();
jpermission.element("values", jvalues);
for (KeyValue value : permissionTemplate.getValues()) {
JSONObject jvalue = new JSONObject();
jvalues.element(jvalue);
jvalue.element("key", value.getKey());
jvalue.element("text", value.getValue());
}