--- /dev/null
+<?xml version="1.0" encoding="UTF-8" ?>
+<% /**
+*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>.
+*/ %>
+<%@ include file="./initAjax.jsp" %>
+
+<%@page import="java.text.Collator"%>
+<%@page import="java.util.ArrayList"%>
+<%@page import="java.util.Arrays"%>
+<%@page import="java.util.LinkedHashMap"%>
+<%@page import="java.util.List"%>
+<%@page import="java.util.Locale"%>
+<%@page import="java.util.Map"%>
+<%@page import="java.util.ResourceBundle"%>
+<%@page import="java.util.Set"%>
+<%@page import="java.util.TreeSet"%>
+
+<%@page import="javax.mail.internet.AddressException"%>
+<%@page import="javax.mail.internet.InternetAddress"%>
+
+<%@page import="com.liferay.contacts.util.InstitutionalListsFactory"%>
+<%@page import="com.liferay.contacts.util.ListFicheFactory"%>
+<%@page import="com.liferay.contacts.util.ProxyFiche"%>
+<%@page import="com.liferay.contacts.util.ProxyList"%>
+<%@page import="com.liferay.contacts.util.ProxyListCompletion"%>
+<%@page import="com.liferay.portal.kernel.dao.orm.QueryUtil"%>
+<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
+<%@page import="com.liferay.portal.kernel.util.Validator"%>
+<%@page import="com.liferay.portal.kernel.servlet.HttpHeaders"%>
+<%@page import="com.liferay.portal.model.Group"%>
+<%@page import="com.liferay.portal.service.GroupLocalServiceUtil"%>
+<%@page import="com.liferay.portal.util.comparator.GroupNameComparator"%>
+
+<%@page import="com.pentila.entSavoie.amis.model.FriendFolder"%>
+<%@page import="com.pentila.entSavoie.amis.service.FriendFolderLocalServiceUtil"%>
+<%@page import="com.pentila.entSavoie.carnetDAdresses.model.Liste"%>
+<%@page import="com.pentila.entSavoie.carnetDAdresses.model.ListProperty"%>
+<%@page import="com.pentila.entSavoie.carnetDAdresses.service.ListeLocalServiceUtil"%>
+<%@page import="com.pentila.entSavoie.communityInfos.model.CommunityInfos"%>
+<%@page import="com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil"%>
+<%@page import="com.pentila.entSavoie.internalMessaging.model.Account"%>
+<%@page import="com.pentila.entSavoie.internalMessaging.model.Message"%>
+<%@page import="com.pentila.entSavoie.internalMessaging.service.MessageLocalServiceUtil"%>
+<%@page import="com.pentila.entSavoie.mail.util.MailManager"%>
+<%@page import="com.pentila.entSavoie.mail.util.PortletPropsValues"%>
+<%@page import="com.pentila.entSavoie.utils.ENTCacheUtils"%>
+
+
+<%
+ long time = System.nanoTime();
+ renderResponse.setContentType("text/xml");
+ response.setContentType("text/xml");
+ response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
+
+ ResourceBundle messagesBundle = portletConfig.getResourceBundle(locale);
+
+ String query = ParamUtil.getString(request, "query", "");
+
+ /*Instantiation de l'operateur de comparaison avec Collator*/
+ Collator compareOperator = Collator.getInstance(Locale.FRENCH);
+ compareOperator.setStrength (Collator.PRIMARY);
+
+ int sizeQuery = query.length();
+
+ // Clé du cache
+ String cacheKeyPF="UserMailCompletionPF_"+user.getUserId();
+ String cacheKeyIA="UserMailCompletionIA_"+user.getUserId();
+ String cacheKeyList="UserMailCompletionList_"+user.getUserId();
+
+
+ // Get value from cache instance
+ List<ProxyFiche> cachePF = (List<ProxyFiche>) ENTCacheUtils.getObjectFromCache(cacheKeyPF);
+ List<InternetAddress> cacheIA = (List<InternetAddress>) ENTCacheUtils.getObjectFromCache(cacheKeyIA);
+ List<ProxyListCompletion> cacheList = (List<ProxyListCompletion>) ENTCacheUtils.getObjectFromCache(cacheKeyList);
+
+
+ if(cachePF==null){
+ // calcul du cache PF
+ cachePF = ListFicheFactory.createPersoContacts(user);
+ // on cache pour 10mn
+ ENTCacheUtils.storeObjectIntoCache(cacheKeyPF,cachePF, 600000);
+ }
+ Set<String> mailSet = new TreeSet<String>();
+ for (ProxyFiche f : cachePF) {
+
+ boolean mailValid = true;
+ for (String invalidDomain : PortletPropsValues.getInvalidDomains(user.getCompanyId())) {
+ if (f.getMail().isEmpty() || f.getMail().endsWith(invalidDomain)) {
+ mailValid = false;
+ }
+ }
+ if (mailValid &&
+ ((f.getNom().length()>=sizeQuery && (compareOperator.compare(f.getNom().substring(0,sizeQuery),query)==0)) ||
+ (f.getPrenom().length()>=sizeQuery && (compareOperator.compare(f.getPrenom().substring(0,sizeQuery),query)==0)) ||
+ (f.getMail().length()>=sizeQuery && (compareOperator.compare(f.getMail().substring(0,sizeQuery),query)==0)) ||
+ ((f.getNom() + " " + f.getPrenom()).length()>=sizeQuery && (compareOperator.compare((f.getNom() + " " + f.getPrenom()).substring(0,sizeQuery),query)==0)) ||
+ ((f.getPrenom() + " " + f.getNom()).length()>=sizeQuery && (compareOperator.compare((f.getPrenom() + " " + f.getNom()).substring(0,sizeQuery),query)==0)))) {
+ mailSet.add("\"" + f.getPrenom() + " " + f.getNom() + "\"<" + f.getMail() + ">");
+ }
+ }
+
+ if(cacheIA==null){
+ cacheIA = new ArrayList<InternetAddress>();
+
+ MailManager mailManager = MailManager.getInstance(request);
+ List<Account> accounts = mailManager.getAccounts();
+
+ for (Account acc: accounts) {
+ // on ajoute les mails provenant des inbox
+ long mailFolderId = acc.getInboxFolderId();
+ List<Message> messages = MessageLocalServiceUtil.getFolderMessages(mailFolderId);
+ for (Message mess: messages) {
+ try {
+ cacheIA.addAll(Arrays.asList(InternetAddress.parse(mess.getCc(), true)));
+ cacheIA.addAll(Arrays.asList(InternetAddress.parse(mess.getTo(), true)));
+ cacheIA.addAll(Arrays.asList(InternetAddress.parse(mess.getSender(), true)));
+ }
+ catch (AddressException ae) {
+ continue;
+ }
+ }
+ }
+ // on cache pour 10mn
+ ENTCacheUtils.storeObjectIntoCache(cacheKeyIA,cacheIA, 600000);
+ }
+ for (InternetAddress ia: cacheIA) {
+ if ( Validator.isEmailAddress(ia.getAddress()) &&
+ ((ia.getAddress().length()>=sizeQuery && (compareOperator.compare(ia.getAddress().substring(0,sizeQuery),query)==0)) ||
+ (ia.getPersonal()!=null && ia.getPersonal().length()>=sizeQuery && (compareOperator.compare(ia.getPersonal().substring(0,sizeQuery),query)==0)) )) {
+ mailSet.add(ia.toString());
+ }
+ }
+
+ if(cacheList==null){
+ cacheList = new ArrayList<ProxyListCompletion>();
+
+
+ cacheList.add(new ProxyListCompletion(messagesBundle.getString("tous-mes-contacts-liste"), messagesBundle.getString("tous-mes-contacts-liste"), "all", "", "", ""));
+ cacheList.add(new ProxyListCompletion(messagesBundle.getString("tous-mes-contacts-perso-liste"), messagesBundle.getString("tous-mes-contacts-perso-liste"), "perso", "", "", ""));
+ cacheList.add(new ProxyListCompletion(messagesBundle.getString("tous-mes-amis-liste"), messagesBundle.getString("tous-mes-amis-liste"), "allAmis", "", "", ""));
+ cacheList.add(new ProxyListCompletion(messagesBundle.getString("tous-mes-contacts-inst-liste"), messagesBundle.getString("tous-mes-contacts-inst-liste"), "inst", "", "", ""));
+
+ List<Liste> lesListes = ListeLocalServiceUtil.findByGroupId(user.getGroup().getGroupId());
+ for (Liste list : lesListes) {
+ Map<String, List<ListProperty>> propsMapRes = ListeLocalServiceUtil.getPropertiesMap(list.getListeId());
+ String lName = propsMapRes.get("name").get(0).getValue_();
+ cacheList.add(new ProxyListCompletion(lName + " " + messagesBundle.getString("liste"), lName + " " + messagesBundle.getString("liste"), "local", "", "", String.valueOf(list.getListeId())));
+ }
+
+ List<FriendFolder> listesAmis= FriendFolderLocalServiceUtil.getFriendFolderByUserIDParentFolderId(user.getUserId(),0);
+ for (FriendFolder ff : listesAmis) {
+ cacheList.add(new ProxyListCompletion(ff.getName() + " " + messagesBundle.getString("liste"), ff.getName() + " " + messagesBundle.getString("liste"), "amis", "", "", String.valueOf(ff.getFriendFolderId())));
+ }
+
+ InstitutionalListsFactory ilf = new InstitutionalListsFactory(user);
+ List<ProxyList> pls = ilf.getInstitutionnalProxyLists();
+ for (ProxyList pl : pls) {
+ cacheList.add(new ProxyListCompletion(pl.getName() + " " + messagesBundle.getString("liste"), pl.getName() + " " + messagesBundle.getString("liste"), pl.getType(), pl.getOrgId(), pl.getRoleId(), pl.getListeId()));
+ }
+
+
+
+ //CAS DES COMMUNAUTES
+ // presente l ensemble des groupes de l'utilisateur
+ LinkedHashMap<String, Object> groupParams = new LinkedHashMap<String, Object>();
+ groupParams.put("usersGroups", new Long(user.getUserId()));
+ groupParams.put("active", Boolean.TRUE);
+ GroupNameComparator gnc = new GroupNameComparator();
+
+ List<Group> resultsTmp = GroupLocalServiceUtil.search(user.getCompanyId(), null, null, groupParams, QueryUtil.ALL_POS, QueryUtil.ALL_POS, gnc);
+
+
+
+ for (Group gr : resultsTmp) {
+ try {
+ CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gr.getGroupId());
+ if (ci.getStatus()==0) {
+ cacheList.add(new ProxyListCompletion(gr.getName() + " " + messagesBundle.getString("liste"), gr.getName() + " " + messagesBundle.getString("liste"), "search", "", "", String.valueOf(gr.getGroupId())));
+ }
+ }
+ catch(Exception e) {}
+
+ }
+ // on cache pour 10mn
+ ENTCacheUtils.storeObjectIntoCache(cacheKeyList, cacheList, 600000);
+ }
+
+ List<ProxyListCompletion> plCompResult = new ArrayList<ProxyListCompletion>();
+ for (ProxyListCompletion plc: cacheList) {
+ if (plc.getDisplay().length()>=sizeQuery && plc.getDisplay().contains(query)) {
+ plCompResult.add(plc);
+ }
+ }
+
+ System.out.println("Mail completion : " + (System.nanoTime() - time));
+%>
+
+<completions>
+ <% for (String mail: mailSet) {
+ %>
+ <completion>
+ <value><![CDATA[<%= mail %>]]></value>
+ <display><![CDATA[<%= mail %>]]></display>
+ </completion>
+ <%
+ }
+ %>
+
+ <% for (ProxyListCompletion plc: plCompResult) {
+ %>
+ <%= plc.getXML() %>
+ <%
+ }
+ %>
+
+
+</completions>
+