--- /dev/null
+/*******************************************************************************
+ * Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ *
+ * 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.List;
+import java.util.ResourceBundle;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+
+import com.liferay.portal.PortalException;
+import com.liferay.portal.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.util.ParamUtil;
+import com.liferay.portal.kernel.util.Validator;
+import com.liferay.portal.model.User;
+import com.liferay.portal.util.PortalUtil;
+import com.pentila.entSavoie.shortCuts.model.ShortCut;
+import com.pentila.entSavoie.shortCuts.model.ShortCutFolder;
+import com.pentila.entSavoie.shortCuts.service.ShortCutFolderLocalServiceUtil;
+import com.pentila.entSavoie.shortCuts.service.ShortCutLocalServiceUtil;
+
+public class ShortCutUtil {
+
+ public static boolean isFavorite(long userId, long entryId) throws PortalException, SystemException{
+ List<ShortCut> shortCuts = ShortCutLocalServiceUtil.getShortCutByUserId(userId);
+ boolean isFavor = false;
+ for(ShortCut shortCut: shortCuts){
+ if(shortCut.getEntryId() == entryId){
+ isFavor = true;
+ break;
+ }
+ }
+ return isFavor;
+ }
+
+ public static void addShortCutFolderChildsInJSONArray(long shortCutFolderId, long userId, int tabulation, JSONArray jsonShortCutFolder) throws PortalException, SystemException{
+ List<ShortCutFolder> lscf = ShortCutFolderLocalServiceUtil.getShortCutFolderByParentShortCutFolderId_UserId(shortCutFolderId, userId);
+ if(tabulation>100){
+ tabulation = 100;
+ }
+ for(ShortCutFolder scf: lscf){
+ JSONObject shortCutFolder = JSONFactoryUtil.createJSONObject();
+ shortCutFolder.put("shortCutFolderId", scf.getShortCutFolderId());
+ shortCutFolder.put("shortCutFolderName", scf.getShortCutFolderName());
+ shortCutFolder.put("tabulation", tabulation);
+ jsonShortCutFolder.put(shortCutFolder);
+ addShortCutFolderChildsInJSONArray(scf.getShortCutFolderId(), userId, tabulation + 10, jsonShortCutFolder);
+ }
+ }
+
+ public static String renameShortCut(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws PortalException, SystemException {
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ final long shortCutId = ParamUtil.getLong(actionRequest, "shortCutId", new Long(0));
+ String shortCutName = ParamUtil.getString(actionRequest, "shortCutName", "");
+
+ //Cas d'un folder
+ ShortCutFolder scf = ShortCutFolderLocalServiceUtil.getShortCutFolder(shortCutId);
+ if(Validator.isNull(shortCutName)){
+ shortCutName = "Dossier";
+ }
+ scf.setShortCutFolderName(shortCutName);
+ ShortCutFolderLocalServiceUtil.updateShortCutFolder(scf);
+
+
+ result.put("id", shortCutId);
+ result.put("shortCutName", shortCutName);
+
+ return result.toString();
+ }
+
+ public static String deleteShortCut(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws PortalException, SystemException {
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ final long shortCutId = ParamUtil.getLong(actionRequest, "shortCutId", new Long(0));
+ User user = PortalUtil.getUser(actionRequest);
+
+
+ deleteShortCutFolders(shortCutId, user.getUserId());
+ ShortCutFolderLocalServiceUtil.deleteShortCutFolder(shortCutId);
+
+ result.put("id", shortCutId);
+
+ return result.toString();
+
+ }
+
+ private static void deleteShortCutFolders(long shortCutId, long userId) throws PortalException, SystemException {
+ List<ShortCutFolder> lscf = ShortCutFolderLocalServiceUtil.getShortCutFolderByParentShortCutFolderId_UserId(shortCutId, userId);
+ ShortCutLocalServiceUtil.deleteByShortCutFolderId_UserId(shortCutId, userId);
+ ShortCutFolderLocalServiceUtil.deleteShortCutFolderByParentShortCutFolderId_UserId(shortCutId, userId);
+ for(ShortCutFolder scf: lscf){
+ //On supprime tout le contenu du dossier
+ deleteShortCutFolders(scf.getShortCutFolderId(), userId);
+ }
+ }
+
+ public static String addShortCutFolder(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws PortalException, SystemException {
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ String folderTitle = ParamUtil.getString(actionRequest, "folderTitle", "");
+ final long targetFolderId = ParamUtil.getLong(actionRequest, "targetFolderId");
+
+ User user = PortalUtil.getUser(actionRequest);
+
+ if(Validator.isNull(folderTitle)){
+ folderTitle = "Dossier";
+ }
+
+ ShortCutFolder scf = ShortCutFolderLocalServiceUtil.addShortCutFolder(user.getUserId(), folderTitle, targetFolderId);
+ result.put("success", true);
+ result.put("id", scf.getShortCutFolderId());
+ result.put("title", folderTitle);
+ result.put("targetFolderId", targetFolderId);
+
+ return result.toString();
+ }
+
+ public static String moveShortCut(ActionRequest actionRequest,
+ ActionResponse actionResponse) throws PortalException, SystemException {
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ final long shortCutId = ParamUtil.getLong(actionRequest, "shortCutId");
+ final long targetFolderId = ParamUtil.getLong(actionRequest, "targetFolderId");
+
+ ShortCutFolder scf = ShortCutFolderLocalServiceUtil.getShortCutFolder(shortCutId);
+ scf.setParentShortCutFolderId(targetFolderId);
+ ShortCutFolderLocalServiceUtil.updateShortCutFolder(scf);
+
+ return result.toString();
+ }
+
+
+}