1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.utils;
37 import java.util.List;
39 import com.liferay.portal.kernel.exception.SystemException;
40 import com.liferay.portal.kernel.json.JSONArray;
41 import com.liferay.portal.kernel.json.JSONFactoryUtil;
42 import com.liferay.portal.kernel.json.JSONObject;
43 import com.liferay.portal.kernel.language.LanguageUtil;
44 import com.liferay.portal.kernel.log.Log;
45 import com.liferay.portal.kernel.log.LogFactoryUtil;
46 import com.liferay.portal.model.User;
47 import com.liferay.portal.webserver.WebServerServletTokenUtil;
48 import com.pentila.entSavoie.ENTDisplayUtil;
49 import com.pentila.entSavoie.amis.model.Relationship;
50 import com.pentila.entSavoie.amis.service.RelationshipLocalServiceUtil;
51 import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
52 import com.pentila.entSavoie.userProperties.model.UserProperties;
53 import com.pentila.entSavoie.userProperties.service.UserPropertiesLocalServiceUtil;
55 public class JsonUserProxyFactory {
57 public final static String SIMPLE_USER_SEARCH = "search";
58 public final static String USER_SEARCH_WITH_FRIENDS_STATUS = "friendsSearch";
59 public final static String USER_SEARCH_WITH_PICTURES = "picturesSearch";
60 public final static String USER_SEARCH_WITH_LOGIN_PASSWORD = "passwordSearch";
62 private static Log logger = LogFactoryUtil.getLog(JsonUserProxyFactory.class);
65 * This function return a JSONObject list for a user given to display his informations in search
66 * @param curUser The user currently searching
67 * @param u The one user from which we want the informations
68 * @return a JSONObject containing user informations
70 public JSONObject format(User curUser, User u) {
71 JSONObject o = JSONFactoryUtil.createJSONObject();
73 o.put("id", u.getUserId());
74 o.put("userId", u.getUserId());
75 o.put("nom", u.getLastName());
76 o.put("prenom", u.getFirstName());
77 o.put("statut", ENTDisplayUtil.displayUserRoles(curUser, u));
78 o.put("type", "user");
82 etabRatt = ENTDisplayUtil.formatSCONETName(curUser, OrganizationFinderServiceUtil.getEtabRatachement(u).getName());
84 etabRatt = LanguageUtil.get(curUser.getLocale(), "inconnu");
86 o.put("etabRatt", etabRatt);
92 * This function return a JSONObject list for a user given to display his informations in search
93 * We add here login and password to search and manage them.
94 * @param curUser The user currently searching
95 * @param u The one user from which we want the informations
96 * @param themeImagesUrl a String that represents the actual images path
97 * @return a JSONObject containing user informations
99 public JSONObject passwordFormat(User curUser, User u) {
100 JSONObject o = format(curUser, u);
103 o.put("login", u.getScreenName());
104 } catch (Exception e) {
105 o.put("login", LanguageUtil.get(curUser.getLocale(), "inconnu"));
108 o.put("password", LanguageUtil.get(curUser.getLocale(), "inconnu"));
114 * This function return a JSONObject list for a user given to display his informations in search
115 * Here we add the user profile picture to display it in the users list
116 * @param curUser The user currently searching
117 * @param u The one user from which we want the informations
118 * @param themeImagesUrl a String that represents the actual images path
119 * @return a JSONObject containing user informations
121 public JSONObject picturesFormat(User curUser, User u, String themeImagesUrl) {
122 JSONObject o = format(curUser, u);
126 boolean isFemale = false;
128 isFemale = u.isFemale();
129 } catch (Exception e) {
134 url = themeImagesUrl + "/user_female_portrait?img_id=";
136 url = themeImagesUrl + "/user_male_portrait?img_id=";
139 boolean portraitVisible = false;
140 boolean detailsVisible = false;
143 UserProperties up = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(u.getUserId());
144 portraitVisible = up.getPortraitVisible();
145 detailsVisible = up.isShareDetails();
146 } catch (Exception e){
152 urlImg = url + u.getPortraitId() + "&t=" + WebServerServletTokenUtil.getToken(u.getPortraitId());
157 o.put("img", urlImg);
158 o.put("details", detailsVisible);
164 * This function return a JSONObject list for a user given to display his informations in search
165 * Here we add the user profile picture and the friends relationship status to display them in the users list
166 * @param curUser The user currently searching
167 * @param u The one user from which we want the informations
168 * @param themeImagesUrl a String that represents the actual images path
169 * @return a JSONObject containing user informations
171 public JSONObject friendsFormat(User curUser, User u, String themeImagesUrl) {
172 JSONObject o = picturesFormat(curUser, u, themeImagesUrl);
173 // Relationship status:
174 // O = en attente d'acceptation (receveur)
175 // 1 = en attente de validation (emetteur)
177 List<Relationship> listRel2;
178 List<Relationship> listRel1;
179 List<Relationship> listRel0;
183 listRel2 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 2);
184 listRel1 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 1);
185 listRel0 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 0);
187 if(!listRel2.isEmpty()){
188 amiStatus = LanguageUtil.get(curUser.getLocale(), "ami");
190 else if(!listRel1.isEmpty()){
191 amiStatus = LanguageUtil.get(curUser.getLocale(), "en-attente");
193 else if(!listRel0.isEmpty()){
194 amiStatus = LanguageUtil.get(curUser.getLocale(), "en-attente");
199 } catch (SystemException e) {
204 o.put("amiStatus",amiStatus);
210 * Convert a users list into a JSONArray that will be return to the javascript
211 * @param curUser The user currently searching
212 * @param l The one user from which we want to format informations
213 * @param searchType the search type which allows us to build the JSONObject expected
214 * @param themeImagesUrl a String that represents the actual images path
215 * @return a JSONArray containing a users list with all their informations formatted into JSONObjects
217 public JSONArray format(User curUser, List<User> l, String searchType, String themeImagesUrl) {
218 JSONArray ret = JSONFactoryUtil.createJSONArray();
220 if (searchType.equals(USER_SEARCH_WITH_FRIENDS_STATUS)) {
221 ret.put(friendsFormat(curUser, u, themeImagesUrl));
223 else if (searchType.equals(USER_SEARCH_WITH_PICTURES)) {
224 ret.put(picturesFormat(curUser, u, themeImagesUrl));
226 else if (searchType.equals(USER_SEARCH_WITH_LOGIN_PASSWORD)) {
227 ret.put(passwordFormat(curUser, u));
230 ret.put(format(curUser, u));