--- /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.portlets.gestionUtilisateurs.jsp;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.Attributes;
+
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONFactoryUtil;
+import com.liferay.portal.kernel.json.JSONObject;
+
+public class JsonHelper {
+
+ public static JSONObject addWrapper(JSONObject ret, String root,
+ Map<String, String> items) {
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+
+ for (Entry<String, String> e : items.entrySet()) {
+ JSONObject a = JSONFactoryUtil.createJSONObject();
+ a.put("id", e.getKey());
+ a.put("name", e.getValue());
+ array.put(a);
+ }
+
+ ret.put(root, array);
+ ret.put("success", true);
+
+ return ret;
+ }
+
+ public static JSONObject createWrapper(Attributes obj) {
+
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+
+ NamingEnumeration<String> it = obj.getIDs();
+
+ while (it.hasMoreElements()) {
+ try {
+ String key = it.next();
+
+ Attribute current = obj.get(key);
+
+ if (key.equals("userPassword")) {
+ continue;
+ }
+
+ if (key.equals("ENTAuxEnsClasses")
+ || key.equals("ENTEleveClasses")) {
+ key = "groups";
+ }
+
+ if(key.equals("ENTEleveGroupes")) {
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+ for (int i = 0; i < current.size(); i++) {
+ try {
+ String instrument = current.get(i).toString().split("\\,")[0].split("\\$")[1];
+ array.put(instrument);
+ } catch (Exception e) {
+ String instrument = current.get().toString().split("\\,")[0].split("\\$")[1];
+ array.put(instrument);
+ break;
+ }
+ }
+ ret.put("instrumentsId", array);
+ }
+
+ if (key.equals("ENTPersonDateNaissance")) {
+ String v = current.get().toString();
+ v = LDAPUtil.convertLdapDate(v);
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+ array.put(v);
+ ret.put(key, array);
+ continue;
+ }
+
+ if (key.equals("ENTPersonFonctions")) {
+ String fct = current.get().toString().split("\\$")[2];
+
+ fct = CSVLoader.getFonctionByValue(fct).get("ID");
+
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+ array.put(fct);
+ ret.put("fonctionId", array);
+
+ String idDiscipline = current.get().toString().split("\\$")[3];
+ JSONArray array2 = JSONFactoryUtil.createJSONArray();
+ array2.put(idDiscipline);
+ ret.put("disciplineId", array2);
+ continue;
+ }
+
+ if (key.equals("ENTEleveRegime")) {
+ String v = current.get().toString();
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+ array.put(v);
+ ret.put(key, array);
+ continue;
+ }
+
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+
+ for (int i = 0; i < current.size(); i++) {
+ try {
+ array.put(current.get(i).toString());
+ } catch (Exception e) {
+ array.put(current.get().toString());
+ break;
+ }
+ }
+
+ ret.put(key, array);
+
+ } catch (NamingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ ret.put("success", true);
+ return ret;
+ }
+
+ public static JSONObject createWrapper(Map<String, String> items) {
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+
+ for (Entry<String, String> e : items.entrySet()) {
+ JSONObject a = JSONFactoryUtil.createJSONObject();
+ a.put("id", e.getKey());
+ a.put("name", e.getValue());
+ array.put(a);
+ }
+
+ ret.put("items", array);
+
+ ret.put("success", true);
+ return ret;
+ }
+
+ public static JSONObject createWrapper(Set<Map<String, String>> items) {
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+
+ int totalCount = -1;
+
+ for (Map<String, String> map : items) {
+
+ if (map.containsKey("totalCount")) {
+ totalCount = Integer.parseInt(map.get("totalCount"));
+ continue;
+ }
+
+ JSONObject user = JSONFactoryUtil.createJSONObject();
+
+ for (Entry<String, String> sd : map.entrySet()) {
+ String key = sd.getKey();
+ String value = sd.getValue();
+ user.put(key, value);
+
+ }
+
+ array.put(user);
+ }
+ ret.put("items", array);
+ if (totalCount != -1) {
+ ret.put("totalCount", totalCount);
+ }
+ ret.put("success", true);
+ return ret;
+ }
+
+ public static String success() {
+ JSONObject ret = JSONFactoryUtil.createJSONObject();
+ ret.put("success", true);
+ return ret.toString();
+ }
+
+ public static JSONArray createArray(Set<Map<String,String>> items){
+
+ JSONArray array = JSONFactoryUtil.createJSONArray();
+
+ for (Map<String, String> se : items ){
+ JSONObject i = JSONFactoryUtil.createJSONObject();
+ for (Entry<String,String> e : se.entrySet()){
+ if (e.getKey().equals("leaf") || e.getKey().equals("checked")){
+ i.put(e.getKey(), e.getValue().equals("true"));
+ }else{
+ i.put(e.getKey(), e.getValue());
+ }
+ }
+
+ array.put(i);
+ }
+
+ return array;
+ }
+
+}