--- /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.cahierDeTexte.util;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.Set;
+
+import javax.naming.NamingException;
+
+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.model.Group;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.Role;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.theme.ThemeDisplay;
+import com.pentila.entSavoie.ENTRolesConstants;
+import com.pentila.entSavoie.cahierDeTexte.model.AnnotationCDT;
+import com.pentila.entSavoie.cahierDeTexte.model.EventCDT;
+import com.pentila.entSavoie.cahierDeTexte.model.LockCDT;
+import com.pentila.entSavoie.cahierDeTexte.model.Matiere;
+import com.pentila.entSavoie.cahierDeTexte.model.MatiereEtab;
+import com.pentila.entSavoie.cahierDeTexte.service.AnnotationCDTLocalServiceUtil;
+import com.pentila.entSavoie.cahierDeTexte.service.EventCDTLocalServiceUtil;
+import com.pentila.entSavoie.cahierDeTexte.service.LockCDTLocalServiceUtil;
+import com.pentila.entSavoie.cahierDeTexte.service.MatiereEtabLocalServiceUtil;
+import com.pentila.entSavoie.cahierDeTexte.service.MatiereLocalServiceUtil;
+import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
+import com.pentila.entSavoie.directory.OrganizationFinderServiceUtil;
+import com.pentila.entSavoie.utils.ENTCacheUtils;
+
+
+public class CDTUtil {
+
+ static SimpleDateFormat classicDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ static SimpleDateFormat testDateFormat = new SimpleDateFormat("yyyyMMdd");
+
+ public static String listSubjectCDT(User u) throws PortalException, SystemException, NamingException, Exception {
+ return listSubjectCDT(u, new HashMap<Long, HashSet<Long>>(), new HashMap<Long, HashSet<Long>>());
+ }
+
+ public static String listSubjectCDT(User u, Map<Long, HashSet<Long>> subjectUserMap, Map<Long, HashSet<Long>> subjectCalendarMap) throws SystemException, PortalException, NamingException, Exception {
+ // matieres active de l etablissement de rattachement pour le user
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(u).getOrganizationId();
+
+ JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
+
+
+ List<MatiereEtab> matiereEtabList = MatiereEtabLocalServiceUtil.getMatiereEtabByEtabIdState(etabId, true);
+
+ List<ProxyMatiere> proxyMatiereList = new ArrayList<ProxyMatiere>();
+
+ for (MatiereEtab matiereEtab : matiereEtabList) {
+ Matiere matiere = MatiereLocalServiceUtil.getMatiere(matiereEtab.getMatiereId());
+ ProxyMatiere pm = new ProxyMatiere(matiere.getMatiereId(), matiere.getLabelLong().toLowerCase(), matiereEtab.getColor(), subjectUserMap.get(matiere.getMatiereId()), subjectCalendarMap.get(matiere.getMatiereId()));
+ proxyMatiereList.add(pm);
+ }
+
+ // on tri serveur pour aleger le client
+ Collections.sort(proxyMatiereList);
+
+
+ for (ProxyMatiere pm : proxyMatiereList) {
+ retJsonArray.put(pm.getJSON());
+ }
+
+ return retJsonArray.toString();
+ }
+
+ public static JSONArray jsonSubjectCDT(User u) throws NamingException, Exception {
+ // matieres active de l etablissement de rattachement pour le user
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(u).getOrganizationId();
+
+ JSONArray retJsonArray = JSONFactoryUtil.createJSONArray();
+ List<MatiereEtab> matiereEtabList = MatiereEtabLocalServiceUtil.getMatiereEtabByEtabIdState(etabId, true);
+ for (MatiereEtab matiereEtab : matiereEtabList) {
+ retJsonArray.put(matiereEtab.getMatiereId());
+ }
+
+ return retJsonArray;
+ }
+
+ public static JSONArray jsonOwnSubjectCDT(User u) throws NamingException, Exception {
+
+ if (RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_4, false) ) {
+ // principal --> on retourne toutes les matieres de l'etab
+ return jsonSubjectCDT(u);
+ }
+
+ JSONArray matieresArray = JSONFactoryUtil.createJSONArray();
+
+ if (!RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_3, false) ) {
+ // pas un prof --> on retourne vide
+ return matieresArray;
+ }
+
+ try {
+ List<Matiere> matieresUser = MatiereLocalServiceUtil.getMatieresByUserId(u.getUserId());
+ for (Matiere matiere: matieresUser) {
+ matieresArray.put(matiere.getMatiereId());
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return matieresArray;
+
+ }
+
+ public static String listOwnSubjectCDT(User u) throws PortalException, SystemException, NamingException, Exception {
+ return listOwnSubjectCDT(u, new HashMap<Long, HashSet<Long>>(), new HashMap<Long, HashSet<Long>>());
+ }
+
+ public static String listOwnSubjectCDT(User u, Map<Long, HashSet<Long>> subjectUserMap, Map<Long, HashSet<Long>> subjectCalendarMap) throws PortalException, SystemException, NamingException, Exception {
+
+ if (RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ ENTRolesConstants.NATIONAL_4, false) ) {
+ // principal --> on retourne toutes les matieres de l'etab
+ return listSubjectCDT(u);
+ }
+
+ JSONArray matieresArray = JSONFactoryUtil.createJSONArray();
+
+ // on enleve ce test car maintenant meme un personnel de l'étab peut donner des cours
+ //if (!RoleLocalServiceUtil.hasUserRole(u.getUserId(), u.getCompanyId(),
+ // ENTRolesConstants.NATIONAL_3, false) ) {
+ // // pas un prof --> on retourne vide
+ // return matieresArray.toString();
+ //}
+
+ try {
+ long etabId = OrganizationFinderServiceUtil.getEtabRatachement(u).getOrganizationId();
+ List<Matiere> matieresUser = MatiereLocalServiceUtil.getMatieresByUserId(u.getUserId());
+ for (Matiere matiere: matieresUser) {
+ JSONObject matiereObj = JSONFactoryUtil.createJSONObject();
+ MatiereEtab matiereEtab = MatiereEtabLocalServiceUtil.getMatiereEtabByEtabIdMatiereId(etabId, matiere.getMatiereId());
+
+ matiereObj.put("id", matiere.getMatiereId());
+ matiereObj.put("label", matiere.getLabelLong().toLowerCase());
+ matiereObj.put("color", matiereEtab.getColor());
+
+ HashSet<Long> userIds = subjectUserMap.get(matiere.getMatiereId());
+ JSONArray users = JSONFactoryUtil.createJSONArray();
+ if(userIds != null){
+ for(Long id: userIds){
+ users.put(id);
+ }
+ }
+ matiereObj.put("users", users);
+
+ HashSet<Long> calendarIds = subjectCalendarMap.get(matiere.getMatiereId());
+ JSONArray calendars = JSONFactoryUtil.createJSONArray();
+ if(calendarIds != null){
+ for(Long id: calendarIds){
+ calendars.put(id);
+ }
+ }
+ matiereObj.put("calendars", calendars);
+
+ matieresArray.put(matiereObj);
+
+ }
+
+
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return matieresArray.toString();
+ }
+
+ public static JSONObject calendarEvent_listJSON(Long userId, Long doAsUser, Integer showFilter, Long calendarIdShow, String filterSubjectIds, Boolean onlyCreatorEvent, Date startDate, Date endDate, long filteredEtabOrgId, ThemeDisplay themeDisplay, ResourceBundle messages) throws PortalException, SystemException {
+
+ User user = UserLocalServiceUtil.getUser(userId);
+
+ List<EventCDT> listEvt = calendarEvent_list(doAsUser, showFilter, calendarIdShow, filterSubjectIds, onlyCreatorEvent, startDate, endDate, filteredEtabOrgId);
+
+ // On recupere l ensemble des locks de l utilisateur
+ // et on filtre pour ne garder uniquement que les locks valide (cf publishDay)
+ List<LockCDT> locks = new ArrayList<LockCDT>();
+ try {
+ int today = Integer.parseInt(testDateFormat.format(new Date()));
+ int startEvtRequest = Integer.parseInt(testDateFormat.format(startDate).replaceAll("\"", ""));
+ int endEvtRequest = Integer.parseInt(testDateFormat.format(endDate));
+ List<LockCDT> locksCDT = LockCDTLocalServiceUtil.getLockCDTByCreator(doAsUser);
+ for (LockCDT l: locksCDT){
+
+ int publish = Integer.parseInt(testDateFormat.format(l.getPublishDay()));
+ int startLock = Integer.parseInt(testDateFormat.format(l.getStartDay()));
+ int endLock = Integer.parseInt(testDateFormat.format(l.getEndDay()));
+
+ if ( today >= publish && startEvtRequest <= endLock && endEvtRequest >= startLock ) {
+ locks.add(l);
+ }
+ }
+ } catch (SystemException e) {
+ e.printStackTrace();
+ }
+
+ //Map<Long, AnnotationCDT> mapAnnotation = new HashMap();
+
+ JSONArray evtList = JSONFactoryUtil.createJSONArray();
+
+ // pas de tri car fait dans calendarEvent_list
+ //Collections.sort(listEvt);
+
+ for (EventCDT evt: listEvt) {
+ AnnotationCDT annot = null;
+ try{
+ annot = AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorIdEvtId(userId, evt.getEvtId());
+ }
+ catch (Exception exc) {}
+ ProxyEvent evtCours = new ProxyEvent(evt, userId, annot, locks);
+ evtList.put(evtCours.getJSONEvent(true, user, themeDisplay, messages));
+ }
+
+ // on retourne les events cours et devoir o-pour l'organisation
+ JSONObject result = JSONFactoryUtil.createJSONObject();
+ result.put("success", true);
+
+ result.put("total", evtList.length());
+
+ result.put("results", evtList);
+
+ return result;
+ }
+
+ public static List<EventCDT> calendarEvent_list(Long userId, Integer showFilter, Long calendarIdShow, String filterSubjectIds, Boolean onlyCreatorEvent, Date startDate, Date endDate, long filteredEtabOrgId) throws PortalException, SystemException {
+ return calendarEvent_list(userId, showFilter, calendarIdShow, filterSubjectIds, onlyCreatorEvent, startDate, endDate, true, false, false, false, filteredEtabOrgId);
+ }
+
+ public static List<EventCDT> calendarEvent_list(Long userId, Integer showFilter, Long calendarIdShow, String filterSubjectIds, Boolean onlyCreatorEvent, Date startDate, Date endDate, boolean sortByDate, boolean sortByUser, boolean sortByCalendar, boolean sortBySubject, long filteredEtabOrgId) throws PortalException, SystemException {
+
+ User user = null;
+ try {
+ user = UserLocalServiceUtil.getUser(userId);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+
+ Role enseignant = RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_3);
+ boolean isEnseignant = false;
+ List<Role> lroles = user.getRoles();
+ if(lroles.contains(enseignant)){
+ isEnseignant = true;
+ }
+
+ //final String filterSubjectId = actionRequest.getParameter("filterSubjectId");
+
+ JSONArray folderIdArray = JSONFactoryUtil.createJSONArray(filterSubjectIds);
+ Set<Long> filterSubjectSetIds = new HashSet<Long>();
+ for (int i=0; i<folderIdArray.length(); i++) {
+ filterSubjectSetIds.add(folderIdArray.getLong(i));
+ }
+
+
+ List<EventCDT> listEvt = new ArrayList<EventCDT>();
+
+ // Vue tous mes groupes, pas de filtre matiere, tous mes events
+ if (calendarIdShow.equals(new Long(-1)) && filterSubjectSetIds.contains(new Long(-1)) && onlyCreatorEvent) {
+ listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyCreatorRangeDate(userId, startDate, endDate));
+ }
+ else {
+ // Vue tous mes groupes, pas de filtre matiere
+ if (calendarIdShow.equals(new Long(-1)) && filterSubjectSetIds.contains(new Long(-1))) {
+ // affichage des evt qui concerne la personne
+ try {
+ List<Organization> userOrgs = CDTUserSettings.getOrganizationFiltered(user);
+ for (Organization uorg: userOrgs) {
+ listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(uorg.getGroup().getGroupId(), null, null, startDate, endDate));
+ }
+
+ //Ajout des Events des groupes persos
+ List<Group> groupsPerso = CDTUserSettings.getUserGroups(user);
+ for (Group group : groupsPerso) {
+ listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(group.getGroupId(), null, null, startDate, endDate));
+ }
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ /*
+ // on recupere l'ensemble des annotations associe a la meme periode --> pour optimiser les requetes sur la BD
+ for (AnnotationCDT anno : AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorRangeDate(userId, startDate, endDate)) {
+ mapAnnotation.put(anno.getEvtId(), anno);
+ }
+ */
+
+ }
+ // Vue tous mes groupes, filtre matiere
+ else if (calendarIdShow.equals(new Long(-1)) && !filterSubjectSetIds.contains(new Long(-1))) {
+ try {
+ List<Organization> userOrgs = CDTUserSettings.getOrganizationFiltered(user);
+
+ for (Organization uorg: userOrgs) {
+ List<EventCDT> lstevtCDT = EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(uorg.getGroup().getGroupId(), null, null, startDate, endDate);
+ for (EventCDT evt : lstevtCDT) {
+ Long subjectId = evt.getSubjectCDT();
+ if (filterSubjectSetIds.contains(subjectId)) {
+ listEvt.add(evt);
+ /*
+ List<AnnotationCDT> anno = AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorRangeDate(userId, startDate, endDate);
+ if (anno.size()>0) {
+ mapAnnotation.put(anno.get(0).getEvtId(), anno.get(0));
+ }
+ */
+ }
+ }
+ }
+
+ //Ajout des Events des groupes persos
+ List<Group> groupsPerso = CDTUserSettings.getUserGroups(user);
+ for (Group group : groupsPerso) {
+ List<EventCDT> lstevtCDT = EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(group.getGroupId(), null, null, startDate, endDate);
+ for (EventCDT evt : lstevtCDT) {
+ Long subjectId = evt.getSubjectCDT();
+ if (filterSubjectSetIds.contains(subjectId)) {
+ listEvt.add(evt);
+ /*
+ List<AnnotationCDT> anno = AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorRangeDate(userId, startDate, endDate);
+ if (anno.size()>0) {
+ mapAnnotation.put(anno.get(0).getEvtId(), anno.get(0));
+ }
+ */
+ }
+ }
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ /*
+ // on recupere l'ensemble des annotations associe a la meme periode --> pour optimiser les requetes sur la BD
+ for (AnnotationCDT anno : AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorRangeDate(userId, startDate, endDate)) {
+ mapAnnotation.put(anno.getEvtId(), anno);
+ }
+ */
+ }
+ // Filtre calendrier, pas de filtre matiere
+ else if (!calendarIdShow.equals(new Long(-1)) && filterSubjectSetIds.contains(new Long(-1))) {
+ // Si enseignant alors on n'affiche que les cours
+ //if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
+ // listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(calendarIdShow, new Long(1), null, startDate, endDate));
+ //} else {
+ listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(calendarIdShow, null, null, startDate, endDate));
+ //}
+ /*
+ // on recupere l'ensemble des annotations associe a la meme periode et cette classe
+ for (AnnotationCDT anno : AnnotationCDTLocalServiceUtil.getAnnotationCDTbyCreatorGroupRangeDate(userId, calendarIdShow, startDate, endDate)) {
+ mapAnnotation.put(anno.getEvtId(), anno);
+ }
+ */
+
+ // On ajoute maintenant les evt liee par les eleves
+ // uniquement si il ne s agit pas d un eleve ou un parent
+ /*try {
+ if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),
+ ENTRolesConstants.NATIONAL_1, false) &&
+ !RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),
+ ENTRolesConstants.NATIONAL_2, false) ) {
+ try {
+ List<Long> linkedOrgs = getAllLinkedOrganizations(calendarIdShow);
+ for (Long linkedOrg: linkedOrgs) {
+ listEvt.addAll(EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(linkedOrg, null, null, startDate, endDate));
+
+
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }*/
+ }
+ // Filtre calendrier, Filtre matiere
+ else if (!calendarIdShow.equals(new Long(-1)) && !filterSubjectSetIds.contains(new Long(-1))) {
+ // Si enseignant alors on n'affiche que les cours
+ //List<EventCDT> lstEvt = null;
+ //if (RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
+ // lstEvt = EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(calendarIdShow, new Long(1), null, startDate, endDate);
+ //} else {
+ List<EventCDT> lstEvt = EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(calendarIdShow, null, null, startDate, endDate);
+ //}
+ for (EventCDT evt : lstEvt) {
+ Long subjectId = evt.getSubjectCDT();
+ if (filterSubjectSetIds.contains(subjectId)) {
+ listEvt.add(evt);
+ /*
+ List<AnnotationCDT> anno = AnnotationCDTLocalServiceUtil.getAnnotationCDTbyEvt(evt.getEvtId());
+ if (anno.size()>0) {
+ mapAnnotation.put(anno.get(0).getEvtId(), anno.get(0));
+ }
+ */
+ }
+ }
+
+ // On ajoute maintenant les evt liee par les eleves
+ // uniquement si il ne s agit pas d un eleve ou un parent
+ /*try {
+ if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),
+ ENTRolesConstants.NATIONAL_1, false) &&
+ !RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(),
+ ENTRolesConstants.NATIONAL_2, false) ) {
+ try {
+ List<Long> linkedOrgs = getAllLinkedOrganizations(calendarIdShow);
+ for (Long linkedOrg: linkedOrgs) {
+ List<EventCDT> lstevtCDT = EventCDTLocalServiceUtil.getEventCDTbyGroupRangeDate(linkedOrg, null, null, startDate, endDate);
+ for (EventCDT evt : lstevtCDT) {
+ Long subjectId = evt.getSubjectCDT();
+ if (filterSubjectSetIds.contains(subjectId)) {
+ listEvt.add(evt);
+
+ }
+ }
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }*/
+
+ }
+
+ }
+
+ List<EventCDT> listEvtCDTEns = new ArrayList<EventCDT>();
+
+
+ for (EventCDT evt: listEvt) {
+ // on teste si on doit etre le createur
+ if (onlyCreatorEvent && (evt.getCreator() != userId)) {
+ continue;
+ }
+
+ // on teste le filtre sur l'etab
+ if (filteredEtabOrgId!=0) {
+ try{
+ Group evtGrp = GroupLocalServiceUtil.getGroup(evt.getGroupId());
+ if (evtGrp.isOrganization()) {
+ Organization evtOrg = OrganizationLocalServiceUtil.getOrganization(evtGrp.getOrganizationId());
+ if (evtOrg.getParentOrganizationId()!=filteredEtabOrgId) {
+ continue;
+ }
+ }
+ }
+ catch(Exception exc) {}
+ }
+
+ // On teste ici le type des evt pour savoir si on doit les retourner
+ //kind Evt:
+ // 1 --> cours
+ // 2 --> devoir
+ // showfilter:
+ // 0 --> tout
+ // 1 --> cours
+ // 2 --> devoirs
+ if ((evt.getKindEvt()==1 && showFilter==2) || (evt.getKindEvt()==2 && showFilter==1 && !isEnseignant)) { // || (evt.getKindEvt()==3 && !showNote) ) {
+ continue;
+ }
+
+ // on affiche pas si le mec est pas le createur et si il est pas braodcast!
+ // pour les cours c'est dans les infos retourne que le filtre est fait!
+ if ( !evt.getBroadcast() && (evt.getCreator() != userId) ) {
+ continue;
+ }
+ //Si le user est enseignant
+ //--> on lui affiche les devoirs pour les dates libres
+ //sinon il n'a aucun moyen de les voir
+ if(isEnseignant && evt.getKindEvt()==2){
+ if(evt.getToEvt() != 0){
+ continue;
+ }
+ }
+
+ //On n'affiche pas les cours dont les groupes ne sont pas visibles sur le CDT
+ try{
+ CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(evt.getGroupId());
+ if(!ci.isViewInCDT()){
+ continue;
+ }
+ } catch(Exception e){}
+
+ listEvtCDTEns.add(evt);
+ }
+
+ // on fait un tri sur la date et l heure
+// Collections.sort(listEvtCDTEns, new Comparator<EventCDT>() {
+// @Override
+// public int compare(EventCDT o1, EventCDT o2) {
+// String s1 = testDateFormat.format(o1.getStartDay()) + o1.getStartEvtTime();
+// String s2 = testDateFormat.format(o2.getStartDay()) + o2.getStartEvtTime();
+// return s1.compareTo(s2);
+// //return o1.getStartDay().compareTo(o2.getStartDay());
+// }});
+
+ // Tri par date
+ if(sortByDate) {
+ Collections.sort(listEvtCDTEns);
+ }
+ // Tri par enseignant
+ else if(sortByUser) {
+ Collections.sort(listEvtCDTEns, new Comparator<EventCDT>() {
+ public int compare(EventCDT o1, EventCDT o2) {
+ Long sd1 = o1.getStartDay().getTime();
+ Long sd2 = o2.getStartDay().getTime();
+ String s1 = getUserNameCached(o1.getCreator());
+ String s2 = getUserNameCached(o2.getCreator());
+ if(s1.compareTo(s2) == 0){
+ return sd1.compareTo(sd2);
+ }
+ return s1.compareTo(s2);
+ }
+ });
+ }
+ // Tri par classe
+ else if(sortByCalendar){
+ Collections.sort(listEvtCDTEns, new Comparator<EventCDT>() {
+ public int compare(EventCDT o1, EventCDT o2) {
+ Long sd1 = o1.getStartDay().getTime();
+ Long sd2 = o2.getStartDay().getTime();
+ String s1 = getClasseNameCached(o1.getGroupId());
+ String s2 = getClasseNameCached(o2.getGroupId());
+ if(s1.compareTo(s2) == 0){
+ return sd1.compareTo(sd2);
+ }
+ return s1.compareTo(s2);
+ }
+ });
+ }
+ // Tri par matiere
+ else if(sortBySubject) {
+ Collections.sort(listEvtCDTEns, new Comparator<EventCDT>() {
+ public int compare(EventCDT o1, EventCDT o2) {
+ Long sd1 = o1.getStartDay().getTime();
+ Long sd2 = o2.getStartDay().getTime();
+ String s1 = getMatiereNameCached(o1.getSubjectCDT());
+ String s2 = getMatiereNameCached(o2.getSubjectCDT());
+ if(s1.compareTo(s2) == 0){
+ return sd1.compareTo(sd2);
+ }
+ return s1.compareTo(s2);
+ }
+ });
+ } else {
+ Collections.sort(listEvtCDTEns);
+ }
+
+ return listEvtCDTEns;
+
+ }
+
+ // Methode de cache RAM sur l'export afin d'accelerer un peu le truc!!!
+ private static String getClasseNameCached(long groupId) {
+ String cacheValue = "";
+ String cacheKey="CDT_LIST_EVENT_CN_"+groupId;
+ cacheValue = (String) ENTCacheUtils.getObjectFromCache(cacheKey);
+ if(cacheValue==null){
+ try {
+ Group group = GroupLocalServiceUtil.getGroup(groupId);
+ if (group.isOrganization()) {
+ Organization o = OrganizationLocalServiceUtil.getOrganization(group.getOrganizationId());
+ cacheValue = o.getName().substring(o.getName().lastIndexOf("-")+2);
+ }
+ else {
+ cacheValue = group.getName();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ ENTCacheUtils.storeObjectIntoCache(cacheKey,cacheValue,-1);
+ }
+ return cacheValue;
+ }
+
+ private static String getMatiereNameCached(long subjectCDTId) {
+ String cacheValue = "";
+ String cacheKey="CDT_LIST_EVENT_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;
+ }
+
+ private static String getUserNameCached(long userId) {
+ String cacheValue = "";
+ String cacheKey="CDT_LIST_EVENT_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;
+ }
+
+}