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;
38 import java.util.ArrayList;
39 import java.util.List;
41 import javax.naming.NamingException;
43 import com.liferay.contacts.util.OrganizationFilter;
44 import com.liferay.portal.kernel.json.JSONArray;
45 import com.liferay.portal.kernel.json.JSONFactoryUtil;
46 import com.liferay.portal.kernel.json.JSONObject;
47 import com.liferay.portal.model.Organization;
48 import com.liferay.portal.model.RoleConstants;
49 import com.liferay.portal.model.User;
50 import com.liferay.portal.service.RoleLocalServiceUtil;
51 import com.pentila.entSavoie.ENTDisplayUtil;
52 import com.pentila.entSavoie.ENTRolesConstants;
53 import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
56 * Utilitary class that converts schools into JSON format
58 * @author Cedric Lecarpentier
60 public class JsonEtabFactory {
63 * Converts a given school into JSONObject
65 public static JSONObject wrapSchool(Organization school) {
66 JSONObject ret = JSONFactoryUtil.createJSONObject();
67 ret.put("etabId", school.getOrganizationId());
68 ret.put("name", ENTDisplayUtil.formatSCONETName(school.getCompanyId(), school.getName()));
73 * Converts a given school into JSONObject
75 public static JSONArray getUserSchools(User u) throws NamingException, Exception {
76 return getUserSchools(u, true);
80 * Returns all schools linked to the given user depending on its role
82 public static JSONArray getUserSchools(User user, boolean addEmptySchool) throws NamingException, Exception {
83 JSONArray ret = JSONFactoryUtil.createJSONArray();
84 final Organization etabRattach = OrganizationFinderServiceUtil.getEtabRatachement(user);
86 boolean isDirectionGrpAdminOrAdmin = RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),RoleConstants.ADMINISTRATOR, false)
87 || RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),ENTRolesConstants.GROUP_ADMIN, false)
88 || RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),ENTRolesConstants.NATIONAL_4, false);
90 // Student or parent => only rattach school
91 if (OrganizationFinderServiceUtil.hasRestriction(user)) {
92 ret.put(wrapSchool(etabRattach));
95 List<Organization> schools;
96 if (isDirectionGrpAdminOrAdmin) {
98 Organization rootOrg = ENTOrganizationsUtil.getOrCreateRootOrg(user.getCompanyId());
99 schools = rootOrg.getSuborganizations();
102 // Get rattach school + associated complex schools
103 List<Organization> allSchools = new ArrayList<Organization>();
104 schools = OrganizationFilter.getUserSchools(user);
105 for (Organization school : schools) {
106 // Check if school hasn't been added by other school in the same complex
107 if (!allSchools.contains(school)) {
108 allSchools.addAll(OrganizationFilter.getSchoolComplexSchools(school));
111 schools = allSchools;
114 // Add an empty school
115 if (addEmptySchool) {
116 JSONObject emptySchool = JSONFactoryUtil.createJSONObject();
117 emptySchool.put("etabId", "0");
118 emptySchool.put("name", " ");
119 ret.put(emptySchool);
122 for (Organization org : schools) {
123 if (!ENTDisplayUtil.formatSCONETName(user.getCompanyId(), org.getName()).equals("")) {
124 ret.put(wrapSchool(org));