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.ArrayList;
38 import java.util.Calendar;
39 import java.util.Date;
40 import java.util.List;
42 import com.liferay.contacts.util.OrganizationConstants;
43 import com.liferay.contacts.util.OrganizationFilter;
44 import com.liferay.portal.kernel.exception.SystemException;
45 import com.liferay.portal.kernel.json.JSONArray;
46 import com.liferay.portal.kernel.json.JSONFactoryUtil;
47 import com.liferay.portal.kernel.json.JSONObject;
48 import com.liferay.portal.kernel.log.Log;
49 import com.liferay.portal.kernel.log.LogFactoryUtil;
50 import com.liferay.portal.model.Organization;
51 import com.liferay.portal.service.OrganizationLocalServiceUtil;
52 import com.pentila.entSavoie.ENTDisplayUtil;
55 * Utilitary class used to convert organizations into Json format
56 * @author Cedric Lecarpentier
58 public class JsonOrganizationFactory {
60 private static Log logger = LogFactoryUtil.getLog(JsonOrganizationFactory.class);
62 private final static String ARCHIVED_CLASSES = "Classes Archivées";
63 private final static String NON_ARCHIVED_CLASSES = "Classes";
64 private final static String ALL_CLASSES = "Toutes les classes";
67 * Converts a given organization into Json format
69 private static JSONObject createWrapper(Organization org, boolean exportGroupId) throws SystemException {
70 long orgId = org.getOrganizationId();
72 orgId = org.getGroup().getGroupId();
74 long groupId = org.getGroup().getGroupId();
75 JSONObject ret = JSONFactoryUtil.createJSONObject();
76 ret.put("organizationId", orgId);
77 ret.put("groupId", groupId);
78 ret.put("name", ENTDisplayUtil.formatSCONETName(org.getName(), false));
79 ret.put("group", (OrganizationFilter.isArchived(org) ? ARCHIVED_CLASSES : NON_ARCHIVED_CLASSES));
83 public static JSONArray wrapClasseOrganizations(long schoolId, boolean withParentsOrgs, boolean includeAllClasses) {
84 long time0 = System.currentTimeMillis();
85 JSONArray ret = JSONFactoryUtil.createJSONArray();
88 if (includeAllClasses) {
89 JSONObject emptyEtab = JSONFactoryUtil.createJSONObject();
90 emptyEtab.put("organizationId", "0");
91 emptyEtab.put("name", ALL_CLASSES);
96 Organization school = OrganizationLocalServiceUtil.getOrganization(schoolId);
98 if (school.getSuborganizationsSize() > 0) {
99 for (Organization classOrg : school.getSuborganizations()) {
100 if (!OrganizationFilter.isArchived(classOrg)) {
101 if (withParentsOrgs || !classOrg.getName().endsWith(OrganizationConstants.ORG_SUFFIX_PARENTS)) {
102 ret.put(createWrapper(classOrg, false));
108 } catch (Exception e) {
112 long time1 = System.currentTimeMillis();
113 logger.info("Wrapped "+ret.length()+" organizations in "+(time1 - time0)+" ms");
119 * Converts a given organization and its sub-organizations into Json format
121 public static JSONArray wrapClasseOrganizations(long schoolId) {
122 return wrapClasseOrganizations(schoolId, false);
125 public static JSONArray wrapClasseOrganizations(long schoolId, boolean withParentsOrgs) {
126 return wrapClasseOrganizations(schoolId, false, true);
131 * Converts a given organization and its sub-organizations into Json format
133 public static JSONArray wrapClasseOrganizationsUtil(long orgId) {
134 return wrapClasseOrganizationsUtil(orgId, false);
138 * Converts a given organization and its sub-organizations into Json format
139 * - without archived orgs
140 * - without suffixed orgs
142 public static JSONArray wrapClasseOrganizationsUtil(long schoolId, boolean exportGroupId) {
143 long time0 = System.currentTimeMillis();
144 JSONArray ret = JSONFactoryUtil.createJSONArray();
147 JSONObject emptyEtab = JSONFactoryUtil.createJSONObject();
148 emptyEtab.put("organizationId", "0");
149 emptyEtab.put("name", ALL_CLASSES);
152 Organization school = OrganizationLocalServiceUtil.getOrganization(schoolId);
154 if (school.getSuborganizationsSize()>0) {
155 for (Organization classOrg : school.getSuborganizations()) {
156 // PortletPreferences prefs = classOrg.getPreferences();
157 // if (!prefs.getValue("migrate", "none").equals("done")) {
158 if (!OrganizationFilter.isArchived(classOrg)) {
159 if (!ENTDisplayUtil.formatSCONETName(school.getCompanyId(), classOrg.getName()).equals("") &&
160 !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_PARENTS) &&
161 !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_TEACHERS) &&
162 !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_PERSONNELS) ) {
163 ret.put(createWrapper(classOrg, exportGroupId));
169 } catch (Exception e) {
172 long time1 = System.currentTimeMillis();
173 System.out.println("Wrapped "+ret.length()+" organizations (util) in "+(time1 - time0)+" ms");
179 // private static List<Organization> getLears(Organization org) throws SystemException {
180 // List<Organization> ret = new ArrayList<Organization>();
181 // if (org.getSuborganizationsSize() > 0) {
182 // for (Organization directChild : org.getSuborganizations()) {
183 // ret.addAll(getLears(directChild));