--- /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>.
+ ******************************************************************************/
+package com.pentila.entSavoie.utils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import javax.naming.NamingException;
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.ResourceRequest;
+
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.repository.model.FileEntry;
+import com.liferay.portal.kernel.repository.model.Folder;
+import com.liferay.portal.kernel.util.MimeTypesOverrideUtil;
+import com.liferay.portal.kernel.util.ParamUtil;
+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.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portal.service.ServiceContext;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.PortalUtil;
+import com.liferay.portlet.documentlibrary.DuplicateFileException;
+import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
+import com.pentila.entSavoie.ENTRoleUtilFactory;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.cahierDeTexte.model.EventCDT;
+import com.pentila.entSavoie.cahierDeTexte.model.Matiere;
+import com.pentila.entSavoie.cahierDeTexte.service.MatiereLocalServiceUtil;
+import com.pentila.entSavoie.cahierDeTexte.util.CDTUtil;
+import com.pentila.entSavoie.cartable.service.CasierEntryLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+
+public class ExportUtil {
+
+ static SimpleDateFormat classicDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ static SimpleDateFormat textFrenchDateFormat = new SimpleDateFormat("EEEE dd MMM yyyy", Locale.FRENCH);
+
+ public static List<EventCDT> calendarEvent_list(ResourceRequest resourceRequest) throws NamingException, Exception {
+ final Long userId = Long.valueOf(resourceRequest.getParameter("userId"));
+
+ User user = PortalUtil.getUser(resourceRequest);
+ Role principalRole = ENTRoleUtilFactory.getInstance(user.getCompanyId()).getRole(ENTRolesConstants.NATIONAL_4);
+ boolean isPrincipal = RoleLocalServiceUtil.hasUserRole(user.getUserId(), principalRole.getRoleId());
+
+ Role adminRole = ENTRoleUtilFactory.getInstance(user.getCompanyId()).getRole(RoleConstants.ADMINISTRATOR);
+ boolean isAdmin = RoleLocalServiceUtil.hasUserRole(user.getUserId(), adminRole.getRoleId());
+
+ long filteredEtabOrgId = 0;
+ if (isPrincipal || isAdmin) {
+ filteredEtabOrgId = OrganizationFinderServiceUtil.getEtabRatachement(user).getOrganizationId();
+ }
+
+
+
+ final Integer showFilter = ParamUtil.getInteger(resourceRequest, "showFilter", 1);
+ final Long calendarIdShow = Long.valueOf(resourceRequest.getParameter("calendarIdShow"));
+
+ //final String filterSubjectId = resourceRequest.getParameter("filterSubjectId");
+ final String filterSubjectIds = resourceRequest.getParameter("filterSubjectId");
+ final Boolean onlyCreatorEvent = ParamUtil.getBoolean(resourceRequest, "onlyCreatorEvent", true);
+ Date startDate = null;
+ try {
+ startDate = classicDateFormat.parse(resourceRequest.getParameter("startDay"));
+ } catch (ParseException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+ Date endDate = null;
+ try {
+ endDate = classicDateFormat.parse(resourceRequest.getParameter("endDay"));
+ } catch (ParseException e1) {
+ // TODO Auto-generated catch block
+ e1.printStackTrace();
+ }
+
+ boolean sortByDate = ParamUtil.getBoolean(resourceRequest, "sortByDate", false);
+ boolean sortByUser = ParamUtil.getBoolean(resourceRequest, "sortByUser", false);
+ boolean sortByCalendar = ParamUtil.getBoolean(resourceRequest, "sortByCalendar", false);
+ boolean sortBySubject = ParamUtil.getBoolean(resourceRequest, "sortBySubject", false);
+
+ List<EventCDT> listEvt = CDTUtil.calendarEvent_list(userId, showFilter, calendarIdShow, filterSubjectIds, onlyCreatorEvent, startDate, endDate, sortByDate, sortByUser, sortByCalendar, sortBySubject, filteredEtabOrgId);
+
+
+ return listEvt;
+
+ }
+
+ // Ajout de l'export dans le casier de l'enseignant
+ public static String sendToCasier(ResourceRequest resourceRequest, User currUser, byte[] b, String startDay, String endDay, boolean isXLS) throws PortalException, SystemException {
+
+ try {
+ //Recuperation du dossier cible
+ Folder cible = FoldersUtil.getDropBox(currUser.getUserId());
+ ResourceBundle messages = ResourceBundle.getBundle("content.Language", currUser.getLocale());
+ Date startDayDate = classicDateFormat.parse(startDay);
+ Date endDayDate = classicDateFormat.parse(endDay);
+
+ //Ajout du fichier
+ 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);
+ String extension = isXLS?".xls":".pdf";
+
+ FileEntry newEntry = null;
+
+ boolean fichierEnvoye = false;
+ String name = "";
+ String suffixe = "";
+ int count = 1;
+
+ while (!fichierEnvoye && count<50) {
+ try {
+
+ name = originalTitle+suffixe+extension;
+
+ newEntry = DLAppLocalServiceUtil.addFileEntry(
+ currUser.getUserId(),
+ cible.getGroupId(),
+ cible.getFolderId(),
+ name,
+ MimeTypesOverrideUtil.getContentType(name),
+ name,
+ "",
+ "",
+ b,
+ new ServiceContext());
+
+ CasierEntryLocalServiceUtil.addCasierEntry(currUser.getCompanyId(), cible.getGroupId(), newEntry.getFileEntryId(), currUser.getUserId(), "", false);
+ fichierEnvoye = true;
+ } catch (DuplicateFileException e) {
+ count ++;
+ suffixe = "_" + count;
+ }
+ }
+
+ return "&folderId=" + newEntry.getFolderId() + "&title=" + newEntry.getTitle();
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return "";
+ }
+
+
+ // Methode de cache RAM sur l'export afin d'accelerer un peu le truc!!!
+ public static String getClasseNameCached(Group group) {
+ String cacheValue = "";
+ String cacheKey="CDT_EXPORT_CN_"+group.getGroupId();
+ cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
+ if(cacheValue==null){
+ if (group.isOrganization()) {
+ Organization o;
+ try {
+ o = OrganizationLocalServiceUtil.getOrganization(group.getOrganizationId());
+ cacheValue = o.getName().substring(o.getName().lastIndexOf("-")+2);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ else {
+ cacheValue = group.getName();
+ }
+ ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
+ }
+ return cacheValue;
+ }
+
+ public static String getMatiereNameCached(long subjectCDTId) {
+ String cacheValue = "";
+ String cacheKey="CDT_EXPORT_MN_"+subjectCDTId;
+ cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
+ if(cacheValue==null){
+ Matiere matiere;
+ try {
+ matiere = MatiereLocalServiceUtil.getMatiere(subjectCDTId);
+ cacheValue = matiere.getLabelLong().toLowerCase();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
+ }
+ return cacheValue;
+ }
+
+ public static String getUserNameCached(long userId) {
+ String cacheValue = "";
+ String cacheKey="CDT_EXPORT_UN_"+userId;
+ cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
+ if(cacheValue==null){
+ User user;
+ try {
+ user = UserLocalServiceUtil.getUser(userId);
+ cacheValue = user.getFullName().toLowerCase();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
+ }
+ return cacheValue;
+ }
+
+}