--- /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.menuGroup;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.ResourceBundle;
+
+import javax.naming.NamingException;
+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.dao.orm.QueryUtil;
+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.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.OrderByComparator;
+import com.liferay.portal.kernel.util.ParamUtil;
+import com.liferay.portal.kernel.workflow.WorkflowConstants;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.Role;
+import com.liferay.portal.model.RoleConstants;
+import com.liferay.portal.model.User;
+import com.liferay.portal.model.UserGroupRole;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portal.util.comparator.UserLastNameComparator;
+import com.liferay.util.UniqueList;
+import com.pentila.entSavoie.ENTRoleUtilFactory;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.communityInfos.service.GroupInfosLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.utils.ENTMainUtilsLocalServiceUtil;
+import com.pentila.entSavoie.utils.JSPPortletUtil;
+import com.pentila.entSavoie.utils.GroupCacheUtil;
+
+/**
+ * <a href="JSPPortlet.java.html"><b><i>View Source</i></b></a>
+ *
+ * @author Brian Wing Shun Chan
+ *
+ */
+public class JSPPortlet extends GenericPortlet {
+
+ 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 && 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);
+
+ // definition de la vue par defaut
+ if (cmd.equals("defineDefaultView")) {
+ try {
+ result = this.defineDefaultView(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ // Récupération des administrateurs du groupe
+ else if (cmd.equals("getAdmins")) {
+ try {
+ result = this.getAdmins(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ // Création d'un nouveau message
+ else if (cmd.equals("createMessage")) {
+ try {
+ result = this.createMessage(resourceRequest);
+ } catch (Exception e) {
+ _log.error(e);
+ }
+ }
+ // Ajout d'une pièce jointe à partir du disk
+ else if (cmd.equals("addFileEntry")) {
+ try {
+ result = this.addFileEntry(resourceRequest);
+ } catch (Exception e) {
+ //File Size Exception
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", user.getLocale());
+ String messageSizeTooImportant = messages.getString("file-uploaded-overweight");
+ long maxUploadSize = JSPPortletUtil.getSizeInMegaOctet(
+ ENTMainUtilsLocalServiceUtil.getUploadServletRequestMaxSize(user.getCompanyId()));
+ result.put("portal_message", messageSizeTooImportant.replace("$1", String.valueOf(maxUploadSize)));
+ } catch (Exception e1) {
+ _log.error(e1);
+ }
+ }
+ }
+ //get the max file upload size
+ else if (cmd.equals("getMaxFileUploadSize")) {
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ long uploadMaxSize = ENTMainUtilsLocalServiceUtil.getUploadServletRequestMaxSize(user.getCompanyId());
+ result.put("uploadMaxSize", JSPPortletUtil.getSizeInMegaOctet(uploadMaxSize));
+ result.put("success", true);
+ } catch(Exception e){
+ _log.error(e);
+ }
+ }
+ //Ajout d'une image pour le CKEditor
+ else if(cmd.equals("uploadPictureCKEditor")){
+ try {
+ result = this.uploadPictureCKEditor(resourceRequest);
+ } catch (Exception e) {
+ //File Size Exception
+ try {
+ User user = PortalUtil.getUser(resourceRequest);
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", user.getLocale());
+ result.put("portal_message", messages.getString("taille-fichier-trop-importante"));
+ } catch (Exception e1) {
+ _log.error(e1);
+ }
+ }
+ }
+ //Ajout d'une image sur le ckeditor depuis le cartable
+ else if(cmd.equals("pictureChoosedCKEditor")){
+ try{
+ result = this.pictureChoosedCKEditor(resourceRequest);
+ } catch(Exception e){
+ _log.error(e);
+ }
+ }
+ else {
+ if (_log.isDebugEnabled()) {
+ _log.debug(ParamUtil.getString(resourceRequest, "resource"));
+ }
+
+ super.serveResource(resourceRequest, resourceResponse);
+ }
+
+ httpResponse.getWriter().write(result.toString());
+ }
+
+ public void processAction(
+ ActionRequest actionRequest, ActionResponse actionResponse)
+ throws IOException, PortletException {
+
+ }
+
+ static public boolean isPrincipalOrganization(User user, long groupId){
+ try {
+ Group gr = GroupLocalServiceUtil.getGroup(groupId);
+ if (gr.isOrganization()) {
+ Organization org = OrganizationLocalServiceUtil.getOrganization(gr.getOrganizationId());
+ Organization orgRoot = OrganizationFinderServiceUtil.getEtabRatachement(user);
+ if((org == null) || (org.getOrganizationId() == orgRoot.getOrganizationId())){
+ return true;
+ }else{
+ return false;
+ }
+ }
+ } catch (Exception e) {}
+
+
+ return false;
+ }
+
+ private JSONObject defineDefaultView(ResourceRequest resourceRequest) throws PortalException, SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ result.put("success", true);
+ final User user = PortalUtil.getUser(resourceRequest);
+ long groupId = PortalUtil.getScopeGroupId(resourceRequest);
+ final String service = ParamUtil.getString(resourceRequest, "defaultView", "");
+ if (!service.equals("")) {
+ try {
+ GroupInfosLocalServiceUtil.setGroupDefaultView(groupId, service);
+ // Update cache infos for current user
+ GroupCacheUtil.updateDefaultView(user, groupId, service);
+ } catch (Exception e) {
+ _log.error(e);
+ result.put("success", false);
+ }
+ }
+
+ return result;
+ }
+
+ private JSONObject getAdmins(ResourceRequest resourceRequest) throws PortalException, SystemException {
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+
+ result.put("success", true);
+
+ JSONArray receivers = JSONFactoryUtil.createJSONArray();
+
+ final User user = PortalUtil.getUser(resourceRequest);
+ final long groupId = ParamUtil.getLong(resourceRequest, "groupId", 0);
+ Group gr = GroupLocalServiceUtil.getGroup(groupId);
+
+ try {
+
+ String adminName = "";
+
+ if(gr.isOrganization()){
+ Role profPrincipalRole = RoleLocalServiceUtil.getRole(user.getCompanyId(),ENTRolesConstants.ORGANIZATION_PROF_PRINCIPAL);
+ List<UserGroupRole> communityPP = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(groupId, profPrincipalRole.getRoleId());
+
+
+ if(communityPP.size() > 0){
+ for (UserGroupRole userGroupRole : communityPP) {
+ User uTmp = UserLocalServiceUtil.getUser(userGroupRole.getUserId());
+
+ adminName = uTmp.getLastName() + " " + uTmp.getFirstName();
+
+ JSONObject receiver = JSONFactoryUtil.createJSONObject();
+
+ receiver.put("id", userGroupRole.getUserId());
+ receiver.put("name", adminName);
+ receivers.put(receiver);
+ }
+ }else{
+ Organization org = OrganizationLocalServiceUtil.getOrganization(gr.getOrganizationId());
+ Organization orgRoot = OrganizationFinderServiceUtil.getEtabRatachement(user);
+ if((org != null) && (org.getOrganizationId() != orgRoot.getOrganizationId())){
+ Organization search = null;
+ Organization tempOrg = org.getParentOrganization();
+ if(tempOrg != null){
+ if(tempOrg.getOrganizationId() == orgRoot.getOrganizationId()){
+ search = org;
+ }else{
+ search = tempOrg;
+ }
+
+ long roleId = ENTRoleUtilFactory.getInstance(user.getCompanyId()).getRole(ENTRolesConstants.NATIONAL_4).getRoleId();
+ final LinkedHashMap<String, Object> userParams = new LinkedHashMap<String, Object>();
+ userParams.put("usersOrgs", new Long(search.getOrganizationId()));
+ userParams.put("usersRoles", new Long(roleId));
+
+ OrderByComparator odb = new UserLastNameComparator();
+
+ List<User> listPrincipal = UserLocalServiceUtil.search(
+ user.getCompanyId(),
+ null,
+ null,
+ null,
+ null,
+ null,
+ WorkflowConstants.STATUS_APPROVED,
+ userParams,
+ true,
+ QueryUtil.ALL_POS,
+ QueryUtil.ALL_POS,
+ odb);
+
+ for (User uTmp : listPrincipal) {
+ adminName = uTmp.getLastName() + " " + uTmp.getFirstName();
+
+ JSONObject receiver = JSONFactoryUtil.createJSONObject();
+
+ receiver.put("id", uTmp.getUserId());
+ receiver.put("name", adminName);
+ receivers.put(receiver);
+ }
+ }
+ }
+ }
+
+ }else{
+ List<UserGroupRole> admins = new UniqueList<UserGroupRole>();
+
+ Role communityAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(),RoleConstants.SITE_ADMINISTRATOR);
+ List<UserGroupRole> communityAdmins = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(groupId, communityAdminRole.getRoleId());
+ List<Long> adminsId = new ArrayList<Long>();
+
+ admins.addAll(communityAdmins);
+ for(UserGroupRole ugr: communityAdmins){
+ adminsId.add(ugr.getUserId());
+ }
+
+ Role communityOwnerRole = RoleLocalServiceUtil.getRole(user.getCompanyId(),RoleConstants.SITE_OWNER);
+ List<UserGroupRole> communityOwners = UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(groupId, communityOwnerRole.getRoleId());
+
+ // admins.addAll(communityOwners);
+ for(UserGroupRole ugr: communityOwners){
+ if(!adminsId.contains(ugr.getUserId())){
+ admins.add(ugr);
+ }
+ }
+
+
+
+ for (UserGroupRole userGroupRole : admins) {
+ User uTmp = UserLocalServiceUtil.getUser(userGroupRole.getUserId());
+
+ adminName = uTmp.getLastName() + " " + uTmp.getFirstName();
+
+ JSONObject receiver = JSONFactoryUtil.createJSONObject();
+
+ receiver.put("id", userGroupRole.getUserId());
+ receiver.put("name", adminName);
+ receivers.put(receiver);
+ }
+ }
+
+ result.put("receivers", receivers);
+
+ } catch (Exception e) {
+ if (_log.isErrorEnabled()) {
+ _log.error(e);
+ }
+ result.put("success", false);
+ }
+
+ return result;
+ }
+
+ // Création d'un nouveau message
+ private JSONObject createMessage(ResourceRequest resourceRequest) throws NamingException, Exception {
+ return JSPPortletUtil.createMessage(resourceRequest);
+ }
+
+ // Ajout d'un nouveau fichier depluis le disk
+ private JSONObject addFileEntry(ResourceRequest resourceRequest) throws PortalException, SystemException {
+ final User user = PortalUtil.getUser(resourceRequest);
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", user.getLocale());
+
+ return JSPPortletUtil.addFileEntry(resourceRequest, messages);
+ }
+
+ //Ajout d'une image sur le ckeditor depuis le cartable
+ private JSONObject pictureChoosedCKEditor(ResourceRequest resourceRequest) throws PortalException, SystemException, IOException {
+ return JSPPortletUtil.pictureChoosedCKEditor(resourceRequest);
+ }
+
+ //Ajout d'une image pour le CKEditor
+ private JSONObject uploadPictureCKEditor(ResourceRequest resourceRequest) throws PortalException, SystemException, IOException {
+ return JSPPortletUtil.uploadPictureCKEditor(resourceRequest);
+ }
+
+ 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);
+
+}