--- /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.ArrayList;
+import java.util.List;
+
+import javax.naming.NamingException;
+
+import com.liferay.contacts.util.OrganizationFilter;
+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.model.Organization;
+import com.liferay.portal.model.RoleConstants;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+
+/**
+ * Utilitary class that converts schools into JSON format
+ *
+ * @author Cedric Lecarpentier
+ */
+public class JsonEtabFactory {
+
+ /**
+ * Converts a given school into JSONObject
+ */
+ public static JSONObject wrapSchool(Organization school) {
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+ ret.put("etabId", school.getOrganizationId());
+ ret.put("name", ENTDisplayUtil.formatSCONETName(school.getCompanyId(), school.getName()));
+ return ret;
+ }
+
+ /**
+ * Converts a given school into JSONObject
+ */
+ public static JSONArray getUserSchools(User u) throws NamingException, Exception {
+ return getUserSchools(u, true);
+ }
+
+ /**
+ * Returns all schools linked to the given user depending on its role
+ */
+ public static JSONArray getUserSchools(User user, boolean addEmptySchool) throws NamingException, Exception {
+ JSONArray ret = JSONFactoryUtil.createJSONArray();
+ final Organization etabRattach = OrganizationFinderServiceUtil.getEtabRatachement(user);
+
+ boolean isDirectionGrpAdminOrAdmin = RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),RoleConstants.ADMINISTRATOR, false)
+ || RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),ENTRolesConstants.GROUP_ADMIN, false)
+ || RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),ENTRolesConstants.NATIONAL_4, false);
+
+ // Student or parent => only rattach school
+ if (OrganizationFinderServiceUtil.hasRestriction(user)) {
+ ret.put(wrapSchool(etabRattach));
+ }
+ else{
+ List<Organization> schools;
+ if (isDirectionGrpAdminOrAdmin) {
+ // Get all schools
+ Organization rootOrg = ENTOrganizationsUtil.getOrCreateRootOrg(user.getCompanyId());
+ schools = rootOrg.getSuborganizations();
+ }
+ else{
+ // Get rattach school + associated complex schools
+ List<Organization> allSchools = new ArrayList<Organization>();
+ schools = OrganizationFilter.getUserSchools(user);
+ for (Organization school : schools) {
+ // Check if school hasn't been added by other school in the same complex
+ if (!allSchools.contains(school)) {
+ allSchools.addAll(OrganizationFilter.getSchoolComplexSchools(school));
+ }
+ }
+ schools = allSchools;
+ }
+
+ // Add an empty school
+ if (addEmptySchool) {
+ JSONObject emptySchool = JSONFactoryUtil.createJSONObject();
+ emptySchool.put("etabId", "0");
+ emptySchool.put("name", " ");
+ ret.put(emptySchool);
+ }
+
+ for (Organization org : schools) {
+ if (!ENTDisplayUtil.formatSCONETName(user.getCompanyId(), org.getName()).equals("")) {
+ ret.put(wrapSchool(org));
+ }
+ }
+ }
+ return ret;
+ }
+}