1 package com.pentila.entSavoie.utils;
3 import com.liferay.portal.kernel.json.JSONArray;
4 import com.liferay.portal.kernel.json.JSONFactoryUtil;
5 import com.liferay.portal.kernel.json.JSONObject;
6 import com.liferay.portal.kernel.language.LanguageUtil;
7 import com.liferay.portal.model.Role;
8 import com.liferay.portal.model.User;
9 import com.pentila.entSavoie.ENTRolesUtil;
11 public class JsonRoleFactory {
14 * Get roles to fill the search combo-box
15 * @param user The user currently searching other users
16 * @return a JSONObject that contains a roles list on which the user has visibility
18 public static JSONObject getJSONSearchableRolesForUser(User user) {
19 return getJSONSearchableRolesForUser(user, true);
23 * Get roles to fill the search combo-box
24 * @param user The user currently searching other users
25 * @return a JSONObject that contains a roles list on which the user has visibility
27 public static JSONObject getJSONSearchableRolesForUser(User user, boolean addEmptyRole) {
28 JSONObject ret = JSONFactoryUtil.createJSONObject();
29 JSONArray roles = JSONFactoryUtil.createJSONArray();
31 // Empty role to search them all (require the visibility on every roles)
32 if (ENTRolesUtil.hasSearchOnAllRoles(user) && addEmptyRole) {
33 JSONObject emptyRole = JSONFactoryUtil.createJSONObject();
34 emptyRole.put("roleId", "0");
35 emptyRole.put("roleCode", "0");
36 emptyRole.put("label", " ");
40 // Create JSON object for visible roles
41 for (Role r : ENTRolesUtil.getSearchableRoles(user)) {
42 JSONObject currRole = JSONFactoryUtil.createJSONObject();
43 currRole.put("roleId", r.getRoleId());
44 currRole.put("roleCode", r.getName());
45 currRole.put("label", LanguageUtil.get(user.getLocale(), r.getName()));
49 ret.put("roles", roles);
50 ret.put("success", true);