--- /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.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import com.liferay.contacts.util.OrganizationConstants;
+import com.liferay.contacts.util.OrganizationFilter;
+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.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.pentila.entSavoie.ENTDisplayUtil;
+
+/**
+ * Utilitary class used to convert organizations into Json format
+ * @author Cedric Lecarpentier
+ */
+public class JsonOrganizationFactory {
+
+ private static Log logger = LogFactoryUtil.getLog(JsonOrganizationFactory.class);
+
+ private final static String ARCHIVED_CLASSES = "Classes Archivées";
+ private final static String NON_ARCHIVED_CLASSES = "Classes";
+ private final static String ALL_CLASSES = "Toutes les classes";
+
+ /**
+ * Converts a given organization into Json format
+ */
+ private static JSONObject createWrapper(Organization org, boolean exportGroupId) throws SystemException {
+ long orgId = org.getOrganizationId();
+ if (exportGroupId) {
+ orgId = org.getGroup().getGroupId();
+ }
+ long groupId = org.getGroup().getGroupId();
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+ ret.put("organizationId", orgId);
+ ret.put("groupId", groupId);
+ ret.put("name", ENTDisplayUtil.formatSCONETName(org.getName(), false));
+ ret.put("group", (OrganizationFilter.isArchived(org) ? ARCHIVED_CLASSES : NON_ARCHIVED_CLASSES));
+ return ret;
+ }
+
+ public static JSONArray wrapClasseOrganizations(long schoolId, boolean withParentsOrgs, boolean includeAllClasses) {
+ long time0 = System.currentTimeMillis();
+ JSONArray ret = JSONFactoryUtil.createJSONArray();
+ if (schoolId != 0) {
+ try {
+ if (includeAllClasses) {
+ JSONObject emptyEtab = JSONFactoryUtil.createJSONObject();
+ emptyEtab.put("organizationId", "0");
+ emptyEtab.put("name", ALL_CLASSES);
+
+ ret.put(emptyEtab);
+ }
+
+ Organization school = OrganizationLocalServiceUtil.getOrganization(schoolId);
+
+ if (school.getSuborganizationsSize() > 0) {
+ for (Organization classOrg : school.getSuborganizations()) {
+ if (!OrganizationFilter.isArchived(classOrg)) {
+ if (withParentsOrgs || !classOrg.getName().endsWith(OrganizationConstants.ORG_SUFFIX_PARENTS)) {
+ ret.put(createWrapper(classOrg, false));
+ }
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ }
+ long time1 = System.currentTimeMillis();
+ logger.info("Wrapped "+ret.length()+" organizations in "+(time1 - time0)+" ms");
+ return ret;
+
+ }
+
+ /**
+ * Converts a given organization and its sub-organizations into Json format
+ */
+ public static JSONArray wrapClasseOrganizations(long schoolId) {
+ return wrapClasseOrganizations(schoolId, false);
+ }
+
+ public static JSONArray wrapClasseOrganizations(long schoolId, boolean withParentsOrgs) {
+ return wrapClasseOrganizations(schoolId, false, true);
+ }
+
+
+ /**
+ * Converts a given organization and its sub-organizations into Json format
+ */
+ public static JSONArray wrapClasseOrganizationsUtil(long orgId) {
+ return wrapClasseOrganizationsUtil(orgId, false);
+ }
+
+ /**
+ * Converts a given organization and its sub-organizations into Json format
+ * - without archived orgs
+ * - without suffixed orgs
+ */
+ public static JSONArray wrapClasseOrganizationsUtil(long schoolId, boolean exportGroupId) {
+ long time0 = System.currentTimeMillis();
+ JSONArray ret = JSONFactoryUtil.createJSONArray();
+ try {
+
+ JSONObject emptyEtab = JSONFactoryUtil.createJSONObject();
+ emptyEtab.put("organizationId", "0");
+ emptyEtab.put("name", ALL_CLASSES);
+ ret.put(emptyEtab);
+
+ Organization school = OrganizationLocalServiceUtil.getOrganization(schoolId);
+
+ if (school.getSuborganizationsSize()>0) {
+ for (Organization classOrg : school.getSuborganizations()) {
+// PortletPreferences prefs = classOrg.getPreferences();
+// if (!prefs.getValue("migrate", "none").equals("done")) {
+ if (!OrganizationFilter.isArchived(classOrg)) {
+ if (!ENTDisplayUtil.formatSCONETName(school.getCompanyId(), classOrg.getName()).equals("") &&
+ !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_PARENTS) &&
+ !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_TEACHERS) &&
+ !classOrg.getName().contains(OrganizationConstants.ORG_SUFFIX_PERSONNELS) ) {
+ ret.put(createWrapper(classOrg, exportGroupId));
+ }
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ long time1 = System.currentTimeMillis();
+ System.out.println("Wrapped "+ret.length()+" organizations (util) in "+(time1 - time0)+" ms");
+ return ret;
+ }
+
+
+
+// private static List<Organization> getLears(Organization org) throws SystemException {
+// List<Organization> ret = new ArrayList<Organization>();
+// if (org.getSuborganizationsSize() > 0) {
+// for (Organization directChild : org.getSuborganizations()) {
+// ret.addAll(getLears(directChild));
+// }
+// } else {
+// ret.add(org);
+// }
+// return ret;
+// }
+}