--- /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>.
+ ******************************************************************************/
+/**
+ * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+package com.pentila.entSavoie.portlets.dashboard;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.ResourceBundle;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.GenericPortlet;
+import javax.portlet.PortletException;
+import javax.portlet.PortletRequestDispatcher;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.portlet.ResourceRequest;
+import javax.portlet.ResourceResponse;
+import javax.servlet.http.HttpServletResponse;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.json.JSONArray;
+import com.liferay.portal.kernel.json.JSONException;
+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.kernel.util.ContentTypes;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.model.CompanyConstants;
+import com.liferay.portal.model.User;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portlet.documentlibrary.conversion.util.ENTDocumentConversionServiceUtil;
+import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
+import com.pentila.entSavoie.ENTRolesUtil;
+import com.pentila.entSavoie.blogEntryInfos.model.BlogEntryInfos;
+import com.pentila.entSavoie.blogEntryInfos.service.BlogEntryInfosLocalServiceUtil;
+import com.pentila.entSavoie.dashboard.widgets.DashboardUtil;
+import com.pentila.entSavoie.dashboard.widgets.EDTUtil;
+import com.pentila.entSavoie.dashboard.widgets.HomeworkUtil;
+import com.pentila.entSavoie.dashboard.widgets.NewsUtil;
+import com.pentila.entSavoie.dashboard.widgets.pronote.PronoteUtil;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+import com.pentila.entSavoie.utils.JSPPortletUtil;
+
+/**
+ * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
+ *
+ * @author Brian Wing Shun Chan
+ *
+ */
+public class JSPPortlet extends GenericPortlet {
+
+ public static DateFormat returnDF = new SimpleDateFormat("MM/dd/yyyy HH:mm");
+ public static String hasPermissions = new String("hasPermissions");
+ public static String permissionMessage = new String("permissionMessage");
+
+ private static Log logger = LogFactoryUtil.getLog(JSPPortlet.class);
+
+ public void init() throws PortletException {
+ editJSP = getInitParameter("edit-jsp");
+ helpJSP = getInitParameter("help-jsp");
+ viewJSP = getInitParameter("view-jsp");
+ }
+
+ public void doDispatch(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ String jspPage = renderRequest.getParameter("jspPage");
+
+ if (jspPage != null) {
+ include(jspPage, renderRequest, renderResponse);
+ }
+ else {
+ super.doDispatch(renderRequest, renderResponse);
+ }
+ }
+
+ public void doEdit(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ if (renderRequest.getPreferences() == null) {
+ super.doEdit(renderRequest, renderResponse);
+ }
+ else {
+ include(editJSP, renderRequest, renderResponse);
+ }
+
+ }
+
+ public void doHelp(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(helpJSP, renderRequest, renderResponse);
+ }
+
+ public void doView(
+ RenderRequest renderRequest, RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ include(viewJSP, renderRequest, renderResponse);
+ }
+
+ @Override
+ public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
+ throws PortletException, IOException {
+
+ final String cmd = ParamUtil.getString(resourceRequest, "cmd");
+
+ HttpServletResponse httpResponse = PortalUtil.getHttpServletResponse(resourceResponse);
+ httpResponse.setContentType(ContentTypes.APPLICATION_JSON);
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", false);
+
+ if (cmd.equals("addNews")) {
+ result = NewsUtil.addNews(resourceRequest);
+ }
+ if (cmd.equals("editNews")) {
+ result = NewsUtil.editNews(resourceRequest);
+ }
+ else if (cmd.equals("deleteNews")) {
+ result = NewsUtil.deleteNews(resourceRequest);
+ }
+ else if (cmd.equals("getUserNewsPermissionsAndContext")) {
+ result = NewsUtil.getUserNewsPermissionsAndContext(resourceRequest);
+ }
+ else if (cmd.equals("getMaxFileUploadSize")) {
+ result = getMaxFileUploadSize(resourceRequest);
+ }
+ else if (cmd.equals("getAllNews")) {
+ result = NewsUtil.getAllNews(resourceRequest);
+ }
+ else if (cmd.equals("getAllNewsHM")) {
+ result = NewsUtil.getAllNews(resourceRequest);
+ }
+ else if (cmd.equals("getFilesTree")) {
+ result = getFilesTree(resourceRequest);
+ }
+ else if (cmd.equals("getFileBase64")) {
+ result = getFileBase64(resourceRequest);
+ }
+ else if (cmd.equals("getThumbnailUrl")) {
+ result = getThumbnailUrl(resourceRequest);
+ }
+ else if (cmd.equals("validateHighPrioNewsRead")){
+ result = NewsUtil.validateHighPrioNewsRead(resourceRequest);
+ }
+ else if (cmd.equals("getDelegate")) {
+ result = NewsUtil.getDelegate(resourceRequest);
+ }
+ else if (cmd.equals("validateDelegate")) {
+ result = NewsUtil.validateDelegate(resourceRequest);
+ }
+ else if (cmd.equals("addFileEntry")) {
+ result = addFileEntry(resourceRequest);
+ }
+ else if (cmd.equals("sendToDropbox")) {
+ result = NewsUtil.sendToDropbox(resourceRequest);
+ }
+ else if (cmd.equals("getBroadcastGroups")) {
+ result = NewsUtil.getBroadcastGroups(resourceRequest);
+ }
+ else if (cmd.equals("getUserListAvalaibleForDelegation")) {
+ result = NewsUtil.getUserListAvalaibleForDelegation(resourceRequest);
+ }
+ else if (cmd.equals("getStudentSchedule")) {
+ result = EDTUtil.getStudentSchedule(resourceRequest);
+ }
+ else if (cmd.equals("getTeacherSchedule")) {
+ result = EDTUtil.getTeacherSchedule(resourceRequest);
+ }
+ else if (cmd.equals("getChildren")) {
+ result = EDTUtil.getChildren(resourceRequest);
+ }
+ else if (cmd.equals("getUserInfos")) {
+ result = EDTUtil.getUserInfos(resourceRequest);
+ }
+ else if (cmd.equals("getProfile")) {
+ result = getUserProfile(resourceRequest);
+ }
+ else if (cmd.equals("saveDashboardConfig")) {
+ result = DashboardUtil.saveDashboardConfig(resourceRequest);
+ }
+ else if (cmd.equals("loadDashboardConfig")) {
+ result = DashboardUtil.loadDashboardConfig(resourceRequest);
+ }
+ else if (cmd.equals("getStudentEvents")) {
+ result = PronoteUtil.getStudentEvents(resourceRequest);
+ }
+ else if (cmd.equals("getParentEvents")) {
+ result = PronoteUtil.getParentEvents(resourceRequest);
+ }
+ else if (cmd.equals("getStudentHomeworks")) {
+ result = HomeworkUtil.getStudentHomework(resourceRequest);
+ }
+ else if (cmd.equals("getAttachmentConversionKey")) {
+ result = getAttachmentConversionKey(resourceRequest);
+ }
+ else if (cmd.equals("getConversionProgress")) {
+ result = getConversionProgress(resourceRequest);
+ }
+
+ else if (cmd.equals("killConversionProcess")) {
+ result = killConversionProcess(resourceRequest);
+ }
+
+ httpResponse.getWriter().write(result.toString());
+ }
+
+ public void processAction(
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws IOException, PortletException {
+
+ }
+
+ /**
+ * TODO Put content in ext
+ * Returns the user profile
+ * @param userid
+ * @return
+ * @return
+ * @throws SystemException
+ * @throws PortalException
+ */
+ private JSONObject getUserProfile(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", false);
+
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+
+ int userProfile = ENTRolesUtil.getUserProfile(user);
+
+ if(userProfile != 0){
+ result.put("profile", userProfile);
+ result.put("success", true);
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ result.put("success", false);
+ result.put("error_msg", "Error when looping over user's roles.");
+ }
+ return result;
+ }
+
+ /**
+ * get the file max size possible to upload
+ * @param resourceRequest
+ * @return
+ */
+ private static JSONObject getMaxFileUploadSize(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ User user;
+ try {
+ user = PortalUtil.getUser(resourceRequest);
+ long uploadMaxSize = ENTMainUtilsLocalServiceUtil.getUploadServletRequestMaxSize(user.getCompanyId());
+ result.put("success", true);
+ result.put("uploadMaxSize", JSPPortletUtil.getSizeInMegaOctet(uploadMaxSize));
+ } catch (PortalException e) {
+ _log.error("Can't get current user", e);
+ result.put("success", false);
+ } catch (SystemException e) {
+ _log.error("Can't get current user", e);
+ result.put("success", false);
+ }
+ return result;
+ }
+
+ /**
+ * Returns the files and folders children of the given folderId
+ * @param resourceRequest
+ * @throws Exception
+ */
+ private JSONObject getFilesTree(ResourceRequest resourceRequest){
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ long folderId = ParamUtil.getLong(resourceRequest, "folderId");
+
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", user.getLocale());
+ JSONArray files = JSPPortletUtil.getFilesTree(user, folderId, messages);
+
+ result.put("success", true);
+ result.put("files", files);
+ } catch (PortalException e) {
+ _log.error("Can't get current user", e);
+ result.put("success", false);
+ } catch (SystemException e) {
+ _log.error("Can't get current user", e);
+ result.put("success", false);
+ }
+ return result;
+ }
+
+ /**
+ * Return a fileList with the file base64 attribute
+ * @param resourceRequest
+ * @throws JSONException
+ */
+ private JSONObject getFileBase64(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ try {
+ final User currentUser = PortalUtil.getUser(resourceRequest);
+ JSONArray fileList = JSONFactoryUtil.createJSONArray(ParamUtil.getString(resourceRequest, "fileList"));
+
+ fileList = JSPPortletUtil.getFileListBase64(fileList, currentUser);
+ result.put("success", true);
+ result.put("fileList", fileList);
+ }
+ catch (Exception e) {
+ logger.error(e);
+ result.put("success", false);
+ }
+
+ return result;
+ }
+
+ private JSONObject getThumbnailUrl(ResourceRequest resourceRequest){
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ try {
+ //String urlThumb = DLAppUtil.getImageSrc(DLAppLocalServiceUtil.getFileEntry(fileEntryId), themeDisplay, DLPreviewableProcessor.THUMBNAIL_INDEX_DEFAULT);
+ //result.put("urlThumb", urlThumb);
+ result = JSPPortletUtil.pictureChoosedProcess(resourceRequest);
+ result.put("success", true);
+ } catch (Exception e) {
+ _log.error("Can't get current user or add new file entry", e);
+ result.put("success", false);
+ }
+
+ return result;
+ }
+
+ // Add new file for computer
+ private JSONObject addFileEntry(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", user.getLocale());
+ result = JSPPortletUtil.addFileEntry(resourceRequest, messages);
+ } catch (PortalException e) {
+ _log.error("Can't get current user or add new file entry", e);
+ result.put("success", false);
+ } catch (SystemException e) {
+ _log.error("Can't get current user or add new file entry", e);
+ result.put("success", false);
+ }
+
+ return result;
+ }
+
+ /**
+ * Return the attachment key used by its conversion process
+ * @param resourceRequest
+ */
+ private JSONObject getAttachmentConversionKey(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ try {
+ final User user = PortalUtil.getUser(resourceRequest);
+ long blogEntryInfosId = ParamUtil.getLong(resourceRequest, "blogEntryInfosId", 0);
+ String fileName = ParamUtil.getString(resourceRequest, "fileName");
+
+ BlogEntryInfos bei = BlogEntryInfosLocalServiceUtil.getBlogEntryInfos(blogEntryInfosId);
+
+ String path = BlogEntryInfosLocalServiceUtil.getAttachmentsDir(bei) + "/" + fileName;
+
+ InputStream isForId = DLStoreUtil.getFileAsStream(
+ user.getCompanyId(), CompanyConstants.SYSTEM, path);
+ String idSecure = ENTDocumentConversionServiceUtil.getFileChecksum(isForId);
+ isForId.close();
+
+ result.put("idSecure", idSecure);
+ result.put("success", true);
+ }
+ catch (Exception e) {
+ logger.error(e);
+ result.put("success", false);
+ }
+
+ return result;
+ }
+
+ /**
+ * Return the conversion state for the attachment
+ * @param resourceRequest
+ */
+ private JSONObject getConversionProgress(ResourceRequest resourceRequest) {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ String idSecure = ParamUtil.getString(resourceRequest, "idSecure");
+ String state = ENTDocumentConversionServiceUtil.getConvertState(idSecure);
+
+ if (state.equals("error")) {
+ result.put("success", false);
+ }
+ else {
+ result.put("state", state);
+ result.put("success", true);
+ }
+
+ return result;
+ }
+
+ /**
+ * Kill the file conversion process when closing the viewer
+ * @param resourceRequest
+ */
+ private JSONObject killConversionProcess(ResourceRequest resourceRequest) {
+
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ String idSecure = ParamUtil.getString(resourceRequest, "idSecure");
+
+ ENTDocumentConversionServiceUtil.killConvertProcess(idSecure);
+
+ result.put("success", true);
+ return result;
+ }
+
+ public static Long[] decodeFileIdFromJSONArray(JSONArray attachFiles) {
+ Long[] fileIds = new Long[attachFiles.length()];
+ for (int i=0; i<attachFiles.length(); i++) {
+
+ fileIds[i] = attachFiles.getLong(i);
+ }
+ return fileIds;
+ }
+
+
+ protected void include(
+ String path, RenderRequest renderRequest,
+ RenderResponse renderResponse)
+ throws IOException, PortletException {
+
+ PortletRequestDispatcher portletRequestDispatcher =
+ getPortletContext().getRequestDispatcher(path);
+
+ if (portletRequestDispatcher == null) {
+ _log.error(path + " is not a valid include");
+ }
+ else {
+ portletRequestDispatcher.include(renderRequest, renderResponse);
+ }
+ }
+
+ protected String editJSP;
+ protected String helpJSP;
+ protected String viewJSP;
+
+ private static Log _log = LogFactoryUtil.getLog(JSPPortlet.class);
+
+}