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.List;
39 import javax.portlet.ResourceRequest;
41 import com.liferay.portal.kernel.exception.PortalException;
42 import com.liferay.portal.kernel.exception.SystemException;
43 import com.liferay.portal.kernel.json.JSONArray;
44 import com.liferay.portal.kernel.json.JSONFactoryUtil;
45 import com.liferay.portal.kernel.json.JSONObject;
46 import com.liferay.portal.kernel.util.ParamUtil;
47 import com.liferay.portal.kernel.util.Validator;
48 import com.liferay.portal.model.User;
49 import com.liferay.portal.util.PortalUtil;
50 import com.pentila.entSavoie.shortCuts.model.ShortCut;
51 import com.pentila.entSavoie.shortCuts.model.ShortCutFolder;
52 import com.pentila.entSavoie.shortCuts.service.ShortCutFolderLocalServiceUtil;
53 import com.pentila.entSavoie.shortCuts.service.ShortCutLocalServiceUtil;
55 public class ShortCutUtil {
57 public static boolean isFavorite(long userId, long entryId) throws PortalException, SystemException{
58 List<ShortCut> shortCuts = ShortCutLocalServiceUtil.getShortCutByUserId(userId);
59 boolean isFavor = false;
60 for(ShortCut shortCut: shortCuts){
61 if(shortCut.getEntryId() == entryId){
69 public static void addShortCutFolderChildsInJSONArray(long shortCutFolderId, long userId, int tabulation, JSONArray jsonShortCutFolder) throws PortalException, SystemException{
70 List<ShortCutFolder> lscf = ShortCutFolderLocalServiceUtil.getShortCutFolderByParentShortCutFolderId_UserId(shortCutFolderId, userId);
74 for(ShortCutFolder scf: lscf){
75 JSONObject shortCutFolder = JSONFactoryUtil.createJSONObject();
76 shortCutFolder.put("shortCutFolderId", scf.getShortCutFolderId());
77 shortCutFolder.put("shortCutFolderName", scf.getShortCutFolderName());
78 shortCutFolder.put("tabulation", tabulation);
79 jsonShortCutFolder.put(shortCutFolder);
80 addShortCutFolderChildsInJSONArray(scf.getShortCutFolderId(), userId, tabulation + 10, jsonShortCutFolder);
84 public static JSONObject renameShortCut(ResourceRequest resourceRequest) throws PortalException, SystemException {
86 JSONObject result = JSONFactoryUtil.createJSONObject();
88 final long shortCutId = ParamUtil.getLong(resourceRequest, "shortCutId", new Long(0));
89 String shortCutName = ParamUtil.getString(resourceRequest, "shortCutName", "");
92 ShortCutFolder scf = ShortCutFolderLocalServiceUtil.getShortCutFolder(shortCutId);
93 if(Validator.isNull(shortCutName)){
94 shortCutName = "Dossier";
96 scf.setShortCutFolderName(shortCutName);
97 ShortCutFolderLocalServiceUtil.updateShortCutFolder(scf);
99 result.put("id", shortCutId);
100 result.put("shortCutName", shortCutName);
101 result.put("success", true);
106 public static JSONObject deleteShortCut(ResourceRequest resourceRequest) throws PortalException, SystemException {
108 JSONObject result = JSONFactoryUtil.createJSONObject();
110 final long shortCutId = ParamUtil.getLong(resourceRequest, "shortCutId", new Long(0));
111 User user = PortalUtil.getUser(resourceRequest);
113 deleteShortCutFolders(shortCutId, user.getUserId());
114 ShortCutFolderLocalServiceUtil.deleteShortCutFolder(shortCutId);
116 result.put("id", shortCutId);
117 result.put("success", true);
122 private static void deleteShortCutFolders(long shortCutId, long userId) throws PortalException, SystemException {
123 List<ShortCutFolder> lscf = ShortCutFolderLocalServiceUtil.getShortCutFolderByParentShortCutFolderId_UserId(shortCutId, userId);
124 ShortCutLocalServiceUtil.deleteByShortCutFolderId_UserId(shortCutId, userId);
125 ShortCutFolderLocalServiceUtil.deleteShortCutFolderByParentShortCutFolderId_UserId(shortCutId, userId);
126 for(ShortCutFolder scf: lscf){
127 //On supprime tout le contenu du dossier
128 deleteShortCutFolders(scf.getShortCutFolderId(), userId);
132 public static JSONObject addShortCutFolder(ResourceRequest resourceRequest) throws PortalException, SystemException {
134 JSONObject result = JSONFactoryUtil.createJSONObject();
136 String folderTitle = ParamUtil.getString(resourceRequest, "folderTitle", "");
137 final long targetFolderId = ParamUtil.getLong(resourceRequest, "targetFolderId");
139 User user = PortalUtil.getUser(resourceRequest);
141 if(Validator.isNull(folderTitle)){
142 folderTitle = "Dossier";
145 ShortCutFolder scf = ShortCutFolderLocalServiceUtil.addShortCutFolder(user.getUserId(), folderTitle, targetFolderId);
146 result.put("id", scf.getShortCutFolderId());
147 result.put("title", folderTitle);
148 result.put("targetFolderId", targetFolderId);
149 result.put("success", true);
154 public static JSONObject moveShortCut(ResourceRequest resourceRequest) throws PortalException, SystemException {
156 JSONObject result = JSONFactoryUtil.createJSONObject();
158 final long shortCutId = ParamUtil.getLong(resourceRequest, "shortCutId");
159 final long targetFolderId = ParamUtil.getLong(resourceRequest, "targetFolderId");
161 ShortCutFolder scf = ShortCutFolderLocalServiceUtil.getShortCutFolder(shortCutId);
162 scf.setParentShortCutFolderId(targetFolderId);
163 ShortCutFolderLocalServiceUtil.updateShortCutFolder(scf);
164 result.put("success", true);