--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie.utils;
+
+import java.util.List;
+
+import com.liferay.portal.kernel.exception.SystemException;
+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.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.User;
+import com.liferay.portal.webserver.WebServerServletTokenUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.amis.model.Relationship;
+import com.pentila.entSavoie.amis.service.RelationshipLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.userProperties.model.UserProperties;
+import com.pentila.entSavoie.userProperties.service.UserPropertiesLocalServiceUtil;
+
+public class JsonUserProxyFactory {
+
+ public final static String SIMPLE_USER_SEARCH = "search";
+ public final static String USER_SEARCH_WITH_FRIENDS_STATUS = "friendsSearch";
+ public final static String USER_SEARCH_WITH_PICTURES = "picturesSearch";
+ public final static String USER_SEARCH_WITH_LOGIN_PASSWORD = "passwordSearch";
+
+ private static Log logger = LogFactoryUtil.getLog(JsonUserProxyFactory.class);
+
+ /**
+ * This function return a JSONObject list for a user given to display his informations in search
+ * @param curUser The user currently searching
+ * @param u The one user from which we want the informations
+ * @return a JSONObject containing user informations
+ */
+ public JSONObject format(User curUser, User u) {
+ JSONObject o = JSONFactoryUtil.createJSONObject();
+
+ o.put("id", u.getUserId());
+ o.put("userId", u.getUserId());
+ o.put("nom", u.getLastName());
+ o.put("prenom", u.getFirstName());
+ o.put("statut", ENTDisplayUtil.displayUserRoles(curUser, u));
+ o.put("type", "user");
+
+ String etabRatt = "";
+ try {
+ etabRatt = ENTDisplayUtil.formatSCONETName(curUser, OrganizationFinderServiceUtil.getEtabRatachement(u).getName());
+ }catch(Exception e){
+ etabRatt = LanguageUtil.get(curUser.getLocale(), "inconnu");
+ }
+ o.put("etabRatt", etabRatt);
+
+ return o;
+ }
+
+ /**
+ * This function return a JSONObject list for a user given to display his informations in search
+ * We add here login and password to search and manage them.
+ * @param curUser The user currently searching
+ * @param u The one user from which we want the informations
+ * @param themeImagesUrl a String that represents the actual images path
+ * @return a JSONObject containing user informations
+ */
+ public JSONObject passwordFormat(User curUser, User u) {
+ JSONObject o = format(curUser, u);
+
+ try {
+ o.put("login", u.getScreenName());
+ } catch (Exception e) {
+ o.put("login", LanguageUtil.get(curUser.getLocale(), "inconnu"));
+ }
+
+ o.put("password", LanguageUtil.get(curUser.getLocale(), "inconnu"));
+
+ return o;
+ }
+
+ /**
+ * This function return a JSONObject list for a user given to display his informations in search
+ * Here we add the user profile picture to display it in the users list
+ * @param curUser The user currently searching
+ * @param u The one user from which we want the informations
+ * @param themeImagesUrl a String that represents the actual images path
+ * @return a JSONObject containing user informations
+ */
+ public JSONObject picturesFormat(User curUser, User u, String themeImagesUrl) {
+ JSONObject o = format(curUser, u);
+
+ String url = "";
+
+ boolean isFemale = false;
+ try {
+ isFemale = u.isFemale();
+ } catch (Exception e) {
+ logger.error(e);
+ }
+
+ if (isFemale) {
+ url = themeImagesUrl + "/user_female_portrait?img_id=";
+ } else {
+ url = themeImagesUrl + "/user_male_portrait?img_id=";
+ }
+
+ boolean portraitVisible = false;
+ boolean detailsVisible = false;
+
+ try {
+ UserProperties up = UserPropertiesLocalServiceUtil.getUserPropertiesByUserId(u.getUserId());
+ portraitVisible = up.getPortraitVisible();
+ detailsVisible = up.isShareDetails();
+ } catch (Exception e){
+ logger.error(e);
+ }
+
+ String urlImg = "";
+ if(portraitVisible){
+ urlImg = url + u.getPortraitId() + "&t=" + WebServerServletTokenUtil.getToken(u.getPortraitId());
+ } else {
+ urlImg = url + "0";
+ }
+
+ o.put("img", urlImg);
+ o.put("details", detailsVisible);
+
+ return o;
+ }
+
+ /**
+ * This function return a JSONObject list for a user given to display his informations in search
+ * Here we add the user profile picture and the friends relationship status to display them in the users list
+ * @param curUser The user currently searching
+ * @param u The one user from which we want the informations
+ * @param themeImagesUrl a String that represents the actual images path
+ * @return a JSONObject containing user informations
+ */
+ public JSONObject friendsFormat(User curUser, User u, String themeImagesUrl) {
+ JSONObject o = picturesFormat(curUser, u, themeImagesUrl);
+ // Relationship status:
+ // O = en attente d'acceptation (receveur)
+ // 1 = en attente de validation (emetteur)
+ // 2 = valide
+ List<Relationship> listRel2;
+ List<Relationship> listRel1;
+ List<Relationship> listRel0;
+
+ String amiStatus;
+ try {
+ listRel2 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 2);
+ listRel1 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 1);
+ listRel0 = RelationshipLocalServiceUtil.getRelationshipByUserIdFriendIdStateId(curUser.getUserId(), u.getUserId(), 0);
+
+ if(!listRel2.isEmpty()){
+ amiStatus = LanguageUtil.get(curUser.getLocale(), "ami");
+ }
+ else if(!listRel1.isEmpty()){
+ amiStatus = LanguageUtil.get(curUser.getLocale(), "en-attente");
+ }
+ else if(!listRel0.isEmpty()){
+ amiStatus = LanguageUtil.get(curUser.getLocale(), "en-attente");
+ }else{
+ amiStatus = "";
+ }
+
+ } catch (SystemException e) {
+ amiStatus = "";
+ logger.error(e);
+ }
+
+ o.put("amiStatus",amiStatus);
+
+ return o;
+ }
+
+ /**
+ * Convert a users list into a JSONArray that will be return to the javascript
+ * @param curUser The user currently searching
+ * @param l The one user from which we want to format informations
+ * @param searchType the search type which allows us to build the JSONObject expected
+ * @param themeImagesUrl a String that represents the actual images path
+ * @return a JSONArray containing a users list with all their informations formatted into JSONObjects
+ */
+ public JSONArray format(User curUser, List<User> l, String searchType, String themeImagesUrl) {
+ JSONArray ret = JSONFactoryUtil.createJSONArray();
+ for (User u : l) {
+ if (searchType.equals(USER_SEARCH_WITH_FRIENDS_STATUS)) {
+ ret.put(friendsFormat(curUser, u, themeImagesUrl));
+ }
+ else if (searchType.equals(USER_SEARCH_WITH_PICTURES)) {
+ ret.put(picturesFormat(curUser, u, themeImagesUrl));
+ }
+ else if (searchType.equals(USER_SEARCH_WITH_LOGIN_PASSWORD)) {
+ ret.put(passwordFormat(curUser, u));
+ }
+ else {
+ ret.put(format(curUser, u));
+ }
+ }
+ return ret;
+ }
+}