--- /dev/null
+package com.pentila.entSavoie.utils;
+
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+import com.liferay.portal.kernel.language.LanguageUtil;
+import com.liferay.portal.model.Role;
+import com.liferay.portal.model.User;
+import com.pentila.entSavoie.ENTRolesUtil;
+
+public class JsonRoleFactory {
+
+ /**
+ * Get roles to fill the search combo-box
+ * @param user The user currently searching other users
+ * @return a JSONObject that contains a roles list on which the user has visibility
+ */
+ public static JSONObject getJSONSearchableRolesForUser(User user) {
+ return getJSONSearchableRolesForUser(user, true);
+ }
+
+ /**
+ * Get roles to fill the search combo-box
+ * @param user The user currently searching other users
+ * @return a JSONObject that contains a roles list on which the user has visibility
+ */
+ public static JSONObject getJSONSearchableRolesForUser(User user, boolean addEmptyRole) {
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+ JSONArray roles = JSONFactoryUtil.createJSONArray();
+
+ // Empty role to search them all (require the visibility on every roles)
+ if (ENTRolesUtil.hasSearchOnAllRoles(user) && addEmptyRole) {
+ JSONObject emptyRole = JSONFactoryUtil.createJSONObject();
+ emptyRole.put("roleId", "0");
+ emptyRole.put("roleCode", "0");
+ emptyRole.put("label", " ");
+ roles.put(emptyRole);
+ }
+
+ // Create JSON object for visible roles
+ for (Role r : ENTRolesUtil.getSearchableRoles(user)) {
+ JSONObject currRole = JSONFactoryUtil.createJSONObject();
+ currRole.put("roleId", r.getRoleId());
+ currRole.put("roleCode", r.getName());
+ currRole.put("label", LanguageUtil.get(user.getLocale(), r.getName()));
+ roles.put(currRole);
+ }
+
+ ret.put("roles", roles);
+ ret.put("success", true);
+
+ return ret;
+ }
+}