--- /dev/null
+<%@page import="com.pentila.entSavoie.utils.PermissionsUtils"%>
+<%@page import="com.liferay.portal.service.persistence.PermissionUtil"%>
+<% /**
+*Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+*
+*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>.
+*/ %>
+<%@page import="com.liferay.portal.kernel.workflow.WorkflowConstants"%>
+<%@page import="com.pentila.entSavoie.messageBoards.service.MBMessageInfosLocalServiceUtil"%>
+<%@page import="com.pentila.entSavoie.messageBoards.model.MBMessageInfos"%>
+<%@page import="com.pentila.entSavoie.forum.util.NbMessagesNewComparator"%>
+<%@page import="com.pentila.entSavoie.forum.util.NameComparator"%>
+<%@page import="java.util.Collection"%>
+<%@page import="java.util.TreeMap"%>
+<%@page import="java.util.HashMap"%>
+<%@page import="java.util.Date"%>
+<%@page import="java.util.ArrayList"%>
+<%@page import="com.liferay.portal.util.PortalUtil"%>
+<%@page import="com.liferay.portal.kernel.util.HttpUtil"%>
+<%@page import="com.liferay.portal.model.CompanyConstants"%>
+<%@page import="com.pentila.entSavoie.portlets.forum.JSPPortlet"%>
+<%@page import="com.pentila.entSavoie.messageBoards.service.MBLastAccessLocalServiceUtil"%>
+<%@page import="java.text.SimpleDateFormat"%>
+<%@page import="java.text.DateFormat"%>
+<%@page import="com.pentila.entSavoie.utils.PortletConstant"%>
+<%@page import="com.pentila.entSavoie.utils.JSPPortletUtil"%>
+
+<%@page import="java.util.List"%>
+
+<%@page import="com.liferay.portal.kernel.dao.orm.QueryUtil"%>
+<%@page import="com.liferay.portal.model.User"%>
+<%@page import="com.liferay.portal.service.ExtUserLocalServiceUtil"%>
+<%@page import="com.liferay.portlet.messageboards.model.MBMessage"%>
+<%@page import="com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil"%>
+<%@page import="com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil"%>
+<%@page import="com.liferay.portlet.messageboards.model.MBThread"%>
+<%@page import="com.liferay.portlet.messageboards.model.MBCategory"%>
+<%@page import="com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil"%>
+<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
+
+<%@page import="com.pentila.entSavoie.messageBoards.MBUtils"%>
+
+<%@ include file="./initAjax.jsp" %>
+
+<%
+final long groupId = ParamUtil.getLong(renderRequest, "groupId");
+
+final long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
+
+final JSONObject ret = JSONFactoryUtil.createJSONObject();
+final JSONArray entities = JSONFactoryUtil.createJSONArray();
+
+DateFormat dfCategory = new SimpleDateFormat("dd/MM/yyyy");
+DateFormat dfThread = new SimpleDateFormat("dd/MM/yyyy à HH:mm");
+
+final String dir = ParamUtil.getString(renderRequest, "dir");
+boolean asc = dir.equals("ASC") || dir.equals("");
+final String sort = ParamUtil.getString(renderRequest, "sort");
+
+int start = ParamUtil.getInteger(renderRequest, "start");
+int limit = ParamUtil.getInteger(renderRequest, "limit", 10);
+int stop = start + limit;
+
+int nbCategory = MBCategoryLocalServiceUtil.getCategoriesCount(groupId, categoryId);
+
+List<MBCategory> listCategoryTmp = null;
+List<MBCategory> listCategory = new ArrayList<MBCategory>();
+List<MBThread> listThreadTmp = null;
+List<MBThread> listThread = new ArrayList<MBThread>();
+
+int total = 0;
+
+listCategoryTmp = MBCategoryLocalServiceUtil.getCategories(groupId, categoryId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+if (listCategoryTmp != null) {
+ for (MBCategory cat : listCategoryTmp) {
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, cat, "VIEW")) {
+ listCategory.add(cat);
+ }
+ }
+
+ total += listCategory.size();
+}
+
+if (categoryId!=0) {
+ listThreadTmp = MBThreadLocalServiceUtil.getThreads(groupId, categoryId, WorkflowConstants.STATUS_ANY, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+ if (listThreadTmp != null) {
+ for (MBThread thread : listThreadTmp) {
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker,
+ MBMessageLocalServiceUtil.getMBMessage(thread.getRootMessageId()), "VIEW")) {
+ listThread.add(thread);
+ }
+ }
+
+ total += listThread.size();
+ }
+}
+
+
+// !-- A revoir pour calculer le total proprement --!
+ret.put("total", total);
+
+int loaded = 0;
+
+/*
+ * On trie les categories si besoin
+ */
+List<MBCategory> categories = new ArrayList<MBCategory>();
+if(sort.equals("name")){
+ TreeMap<String, List<MBCategory>> mapCategory = new TreeMap<String, List<MBCategory>>(new NameComparator(asc));
+ //Si il existe des catégories
+ if (listCategory!=null){
+ for(MBCategory category: listCategory){
+ List<MBCategory> categs = new ArrayList<MBCategory>();
+ if(mapCategory.containsKey(category.getName())){
+ categs.addAll(mapCategory.get(category.getName()));
+ }
+ categs.add(category);
+ mapCategory.put(category.getName(), categs);
+ }
+ }
+ for(String key: mapCategory.keySet()){
+ categories.addAll(mapCategory.get(key));
+ }
+} else if(sort.equals("nbMessageNew")){
+ TreeMap<Integer, List<MBCategory>> mapCategory = new TreeMap<Integer, List<MBCategory>>(new NbMessagesNewComparator(asc));
+ //Si il existe des catégories
+ if (listCategory!=null){
+ for(MBCategory category: listCategory){
+ int nbMessageNew = JSPPortlet.countNewMessageByCategory(user.getUserId(), groupId, category.getCategoryId(), permissionChecker);
+ List<MBCategory> categs = new ArrayList<MBCategory>();
+ if(mapCategory.containsKey(nbMessageNew)){
+ categs.addAll(mapCategory.get(nbMessageNew));
+ }
+ categs.add(category);
+ mapCategory.put(nbMessageNew, categs);
+ }
+ }
+ for(Integer key: mapCategory.keySet()){
+ categories.addAll(mapCategory.get(key));
+ }
+} else {
+ if (listCategory!=null){
+ categories.addAll(listCategory);
+ }
+}
+
+int j = start;
+if (start < nbCategory){
+ while ((j < categories.size()) && (loaded < limit)) {
+ JSONObject curr = JSONFactoryUtil.createJSONObject();
+ MBCategory category = categories.get(j);
+ curr.put("id", category.getCategoryId());
+ curr.put("type", "Category");
+ curr.put("creator", category.getUserName());
+ curr.put("createdDate", dfCategory.format(category.getCreateDate()));
+ curr.put("name", category.getName());
+ curr.put("description", category.getDescription());
+ curr.put("rootMessageId", 0);
+ // Nombre de sous-categorie de la categorie
+ int nbMBSubCategory = MBCategoryLocalServiceUtil.getCategoriesCount(groupId, category.getCategoryId());
+ curr.put("subcategoryCount", nbMBSubCategory);
+ curr.put("threadCount", category.getThreadCount());
+ curr.put("messageCount", category.getMessageCount());
+ // Si l'utilisateur a la permission d'editer la categorie
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, category, "EDIT")) {
+ curr.put("hideEdit", false);
+ } else {
+ curr.put("hideEdit", true);
+ }
+ // Si l'utilisateur a la permission de supprimer la categorie
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, category, "DELETE")) {
+ curr.put("hideDelete", false);
+ } else {
+ curr.put("hideDelete", true);
+ }
+ // Pas de souscription a une categorie
+ curr.put("hideSubscribe", true);
+ // Pas de deplacement d'une categorie
+ curr.put("hideMoveThread", true);
+ // Si l'utilisateur a la permission de gérer les permissions
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, category, "MANAGE_PERMISSIONS")) {
+ curr.put("hideManagePermissions", false);
+ } else {
+ curr.put("hideManagePermissions", true);
+ }
+ int nbMessageNew = JSPPortlet.countNewMessageByCategory(user.getUserId(), groupId, category.getCategoryId(), permissionChecker);
+ curr.put("nbMessageNew", nbMessageNew);
+ curr.put("groupId", groupId);
+ entities.put(curr);
+ j++;
+ loaded++;
+ }
+}
+
+
+//Une fois les catégories ajoutées, on regarde si il reste de la place pour les thread
+if (loaded < limit) {
+
+ /*
+ * On trie les threads si besoin
+ */
+ List<MBThread> threads = new ArrayList<MBThread>();
+ if(sort.equals("name")){
+ TreeMap<String, List<MBThread>> mapThread = new TreeMap<String, List<MBThread>>(new NameComparator(asc));
+ if(listThread != null){
+ for(MBThread thread: listThread){
+ MBMessage rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());
+ List<MBThread> thds = new ArrayList<MBThread>();
+ if(mapThread.containsKey(rootMessage.getSubject())){
+ thds.addAll(mapThread.get(rootMessage.getSubject()));
+ }
+ thds.add(thread);
+ mapThread.put(rootMessage.getSubject(), thds);
+ }
+ }
+ for(String key: mapThread.keySet()){
+ threads.addAll(mapThread.get(key));
+ }
+ } else if(sort.equals("nbMessageNew")){
+ TreeMap<Integer, List<MBThread>> mapThread = new TreeMap<Integer, List<MBThread>>(new NbMessagesNewComparator(asc));
+ if(listThread != null){
+ for(MBThread thread: listThread){
+ int nbMessageNew = JSPPortlet.countNewMessageByThread(user.getUserId(), thread);
+ List<MBThread> thds = new ArrayList<MBThread>();
+ if(mapThread.containsKey(nbMessageNew)){
+ thds.addAll(mapThread.get(nbMessageNew));
+ }
+ thds.add(thread);
+ mapThread.put(nbMessageNew, thds);
+ }
+ }
+ for(Integer key: mapThread.keySet()){
+ threads.addAll(mapThread.get(key));
+ }
+ } else {
+ if(listThread != null){
+ threads.addAll(listThread);
+ }
+ }
+
+ int k = 0;
+ //Si on a deja chargé des categories
+ if (start > nbCategory) {
+ k = start - nbCategory;
+ }
+ List<MBThread> listThreadSuscribed = MBThreadLocalServiceUtil.getGroupThreads(groupId, user.getUserId(), WorkflowConstants.STATUS_ANY, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
+ // Tant qu'on a la place de charger des threads et qu'il en reste dans la liste, on les charge
+ while ((k < threads.size()) && (loaded < limit)) {
+ MBThread thread = threads.get(k);
+ JSONObject curr = JSONFactoryUtil.createJSONObject();
+ curr.put("id", thread.getThreadId());
+ curr.put("type", "Thread");
+ curr.put("messageCount", thread.getMessageCount());
+ curr.put("viewCount", thread.getViewCount());
+ // Premier message du fil de discussion
+ MBMessage rootMessage = null;
+ try{
+ rootMessage = MBMessageLocalServiceUtil.getMessage(thread.getRootMessageId());
+ } catch(Exception e){
+ k++;
+ continue;
+ }
+
+ MBMessageInfos mbmi = null;
+ try{
+ mbmi = MBMessageInfosLocalServiceUtil.getMessageInfosByMessageId(rootMessage.getMessageId());
+ } catch(Exception e){
+ k++;
+ continue;
+ }
+
+ if(mbmi == null){
+ k++;
+ continue;
+ }
+
+ // Titre du premier message
+ curr.put("name", rootMessage.getSubject());
+ curr.put("rootMessageId", rootMessage.getMessageId());
+ // Corps du message
+ curr.put("description", rootMessage.getBody());
+ // Nom du createur du fil de discussion et date de creation
+ curr.put("creator", rootMessage.getUserName());
+ curr.put("createdDate", dfThread.format(rootMessage.getCreateDate()));
+ // Dernier utilisateur ayant posté
+ curr.put("lastPostUser", PortalUtil.getUserName(thread.getLastPostByUserId(), messages.getString("Utilisateur-supprime")));
+ curr.put("lastPostDate", dfThread.format(thread.getLastPostDate()));
+ curr.put("priority", thread.getPriority());
+
+
+ // Pieces jointes
+ final JSONArray attachements = JSONFactoryUtil.createJSONArray();
+ String[] attachementsFiles = rootMessage.getAttachmentsFiles();
+ for (int i = 0; i < attachementsFiles.length; i++) {
+ final JSONObject fileDescriptor = JSPPortletUtil.formatAttachment(themeDisplay, attachementsFiles[i], rootMessage.getMessageId(), PortletConstant.FORUM);
+ attachements.put(fileDescriptor);
+ }
+ curr.put("attachements", attachements.toString());
+
+ // Si le thread est contenu dans la liste des thread auxquels l'utilisateur est abonné
+ if (listThreadSuscribed.contains(thread)) {
+ curr.put("isSubscribed", true);
+ } else {
+ curr.put("isSubscribed", false);
+ }
+ // Si l'utilisateur a la permission d'editer le fil de discussion
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, rootMessage, "EDIT")) {
+ curr.put("hideEdit", false);
+ } else {
+ curr.put("hideEdit", true);
+ }
+ // Si l'utilisateur a la permission de supprimer le fil de discussion
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, rootMessage, "DELETE")) {
+ curr.put("hideDelete", false);
+ } else {
+ curr.put("hideDelete", true);
+ }
+ // Si l'utilisateur a la permission de souscrire a un fil de discussion
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, rootMessage, "SUBSCRIBE")) {
+ curr.put("hideSubscribe", false);
+ } else {
+ curr.put("hideSubscribe", true);
+ }
+ // Si l'utilisateur a la permission de deplacer un fil de discussion
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, rootMessage, "MOVE_THREAD")) {
+ curr.put("hideMoveThread", false);
+ } else {
+ curr.put("hideMoveThread", true);
+ }
+ // Si l'utilisateur a la permission de gérer les permissions
+ if (PermissionsUtils.hasUserPermissionForRessourceInGroup(permissionChecker, rootMessage, "MANAGE_PERMISSIONS")) {
+ curr.put("hideManagePermissions", false);
+ } else {
+ curr.put("hideManagePermissions", true);
+ }
+ int nbMessageNew = JSPPortlet.countNewMessageByThread(user.getUserId(), thread);
+ curr.put("nbMessageNew", nbMessageNew);
+ curr.put("groupId", groupId);
+
+ curr.put("anonymous", mbmi.getIsAnonymous());
+
+ entities.put(curr);
+ loaded++;
+ k++;
+ }
+}
+
+ret.put("entities", entities);
+%>
+<%= ret.toString() %>