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.text.ParseException;
38 import java.text.SimpleDateFormat;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.ResourceBundle;
44 import javax.naming.NamingException;
45 import javax.portlet.ActionRequest;
46 import javax.portlet.ActionResponse;
47 import javax.portlet.ResourceRequest;
49 import com.liferay.portal.kernel.exception.PortalException;
50 import com.liferay.portal.kernel.exception.SystemException;
51 import com.liferay.portal.kernel.repository.model.FileEntry;
52 import com.liferay.portal.kernel.repository.model.Folder;
53 import com.liferay.portal.kernel.util.MimeTypesOverrideUtil;
54 import com.liferay.portal.kernel.util.ParamUtil;
55 import com.liferay.portal.model.Group;
56 import com.liferay.portal.model.Organization;
57 import com.liferay.portal.model.Role;
58 import com.liferay.portal.model.RoleConstants;
59 import com.liferay.portal.model.User;
60 import com.liferay.portal.service.OrganizationLocalServiceUtil;
61 import com.liferay.portal.service.RoleLocalServiceUtil;
62 import com.liferay.portal.service.ServiceContext;
63 import com.liferay.portal.service.UserLocalServiceUtil;
64 import com.liferay.portal.util.PortalUtil;
65 import com.liferay.portlet.documentlibrary.DuplicateFileException;
66 import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
67 import com.pentila.entSavoie.ENTRoleUtilFactory;
68 import com.pentila.entSavoie.ENTRolesConstants;
69 import com.pentila.entSavoie.cahierDeTexte.model.EventCDT;
70 import com.pentila.entSavoie.cahierDeTexte.model.Matiere;
71 import com.pentila.entSavoie.cahierDeTexte.service.MatiereLocalServiceUtil;
72 import com.pentila.entSavoie.cahierDeTexte.util.CDTUtil;
73 import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
74 import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
76 public class ExportUtil {
78 static SimpleDateFormat classicDateFormat = new SimpleDateFormat("yyyy-MM-dd");
79 static SimpleDateFormat textFrenchDateFormat = new SimpleDateFormat("EEEE dd MMM yyyy", Locale.FRENCH);
81 public static List<EventCDT> calendarEvent_list(ResourceRequest resourceRequest) throws NamingException, Exception {
82 final Long userId = Long.valueOf(resourceRequest.getParameter("userId"));
84 User user = PortalUtil.getUser(resourceRequest);
85 Role principalRole = ENTRoleUtilFactory.getInstance(user.getCompanyId()).getRole(ENTRolesConstants.NATIONAL_4);
86 boolean isPrincipal = RoleLocalServiceUtil.hasUserRole(user.getUserId(), principalRole.getRoleId());
88 Role adminRole = ENTRoleUtilFactory.getInstance(user.getCompanyId()).getRole(RoleConstants.ADMINISTRATOR);
89 boolean isAdmin = RoleLocalServiceUtil.hasUserRole(user.getUserId(), adminRole.getRoleId());
91 long filteredEtabOrgId = 0;
92 if (isPrincipal || isAdmin) {
93 filteredEtabOrgId = OrganizationFinderServiceUtil.getEtabRatachement(user).getOrganizationId();
98 final Integer showFilter = ParamUtil.getInteger(resourceRequest, "showFilter", 1);
99 final Long calendarIdShow = Long.valueOf(resourceRequest.getParameter("calendarIdShow"));
101 //final String filterSubjectId = resourceRequest.getParameter("filterSubjectId");
102 final String filterSubjectIds = resourceRequest.getParameter("filterSubjectId");
103 final Boolean onlyCreatorEvent = ParamUtil.getBoolean(resourceRequest, "onlyCreatorEvent", true);
104 Date startDate = null;
106 startDate = classicDateFormat.parse(resourceRequest.getParameter("startDay"));
107 } catch (ParseException e1) {
108 // TODO Auto-generated catch block
109 e1.printStackTrace();
113 endDate = classicDateFormat.parse(resourceRequest.getParameter("endDay"));
114 } catch (ParseException e1) {
115 // TODO Auto-generated catch block
116 e1.printStackTrace();
119 boolean sortByDate = ParamUtil.getBoolean(resourceRequest, "sortByDate", false);
120 boolean sortByUser = ParamUtil.getBoolean(resourceRequest, "sortByUser", false);
121 boolean sortByCalendar = ParamUtil.getBoolean(resourceRequest, "sortByCalendar", false);
122 boolean sortBySubject = ParamUtil.getBoolean(resourceRequest, "sortBySubject", false);
124 List<EventCDT> listEvt = CDTUtil.calendarEvent_list(userId, showFilter, calendarIdShow, filterSubjectIds, onlyCreatorEvent, startDate, endDate, sortByDate, sortByUser, sortByCalendar, sortBySubject, filteredEtabOrgId);
131 // Ajout de l'export dans le casier de l'enseignant
132 public static String sendToCasier(ResourceRequest resourceRequest, User currUser, byte[] b, String startDay, String endDay, boolean isXLS) throws PortalException, SystemException {
135 //Recuperation du dossier cible
136 Folder cible = FoldersUtil.getDropBox(currUser.getUserId());
137 ResourceBundle messages = ResourceBundle.getBundle("content.Language", currUser.getLocale());
138 Date startDayDate = classicDateFormat.parse(startDay);
139 Date endDayDate = classicDateFormat.parse(endDay);
142 String originalTitle = messages.getString("cahier-de-texte") + " : " + messages.getString("export-du") +" " + (new SimpleDateFormat("EEEE dd MMM yyyy", currUser.getLocale())).format(startDayDate) + " " + messages.getString("au") + " " + (new SimpleDateFormat("EEEE dd MMM yyyy", currUser.getLocale())).format(endDayDate);
143 String extension = isXLS?".xls":".pdf";
145 FileEntry newEntry = null;
147 boolean fichierEnvoye = false;
152 while (!fichierEnvoye && count<50) {
155 name = originalTitle+suffixe+extension;
157 newEntry = DLAppLocalServiceUtil.addFileEntry(
158 currUser.getUserId(),
162 MimeTypesOverrideUtil.getContentType(name),
167 new ServiceContext());
169 CasierEntryLocalServiceUtil.addCasierEntry(currUser.getCompanyId(), cible.getGroupId(), newEntry.getFileEntryId(), currUser.getUserId(), "", false);
170 fichierEnvoye = true;
171 } catch (DuplicateFileException e) {
173 suffixe = "_" + count;
177 return "&folderId=" + newEntry.getFolderId() + "&title=" + newEntry.getTitle();
179 } catch (Exception e) {
186 // Methode de cache RAM sur l'export afin d'accelerer un peu le truc!!!
187 public static String getClasseNameCached(Group group) {
188 String cacheValue = "";
189 String cacheKey="CDT_EXPORT_CN_"+group.getGroupId();
190 cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
191 if(cacheValue==null){
192 if (group.isOrganization()) {
195 o = OrganizationLocalServiceUtil.getOrganization(group.getOrganizationId());
196 cacheValue = o.getName().substring(o.getName().lastIndexOf("-")+2);
197 } catch (Exception e) {
202 cacheValue = group.getName();
204 ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
209 public static String getMatiereNameCached(long subjectCDTId) {
210 String cacheValue = "";
211 String cacheKey="CDT_EXPORT_MN_"+subjectCDTId;
212 cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
213 if(cacheValue==null){
216 matiere = MatiereLocalServiceUtil.getMatiere(subjectCDTId);
217 cacheValue = matiere.getLabelLong().toLowerCase();
218 } catch (Exception e) {
222 ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
227 public static String getUserNameCached(long userId) {
228 String cacheValue = "";
229 String cacheKey="CDT_EXPORT_UN_"+userId;
230 cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
231 if(cacheValue==null){
234 user = UserLocalServiceUtil.getUser(userId);
235 cacheValue = user.getFullName().toLowerCase();
236 } catch (Exception e) {
240 ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);