--- /dev/null
+package com.pentila.entSavoie.utils;
+
+import java.text.Collator;
+import java.text.Normalizer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import com.liferay.contacts.util.InstitutionalListsFactory;
+import com.liferay.contacts.util.ListFicheFactory;
+import com.liferay.contacts.util.ProxyFiche;
+import com.liferay.contacts.util.ProxyListCompletion;
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.language.LanguageUtil;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.model.Group;
+import com.liferay.portal.model.Organization;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.OrganizationLocalServiceUtil;
+import com.liferay.portal.util.comparator.GroupNameComparator;
+import com.pentila.entSavoie.amis.model.FriendFolder;
+import com.pentila.entSavoie.amis.service.FriendFolderLocalServiceUtil;
+import com.pentila.entSavoie.carnetDAdresses.model.ListProperty;
+import com.pentila.entSavoie.carnetDAdresses.model.Liste;
+import com.pentila.entSavoie.carnetDAdresses.service.ListeLocalServiceUtil;
+import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
+import com.pentila.entSavoie.institutionalList.model.ListeInstDetails;
+import com.pentila.entSavoie.ENTDisplayUtil;
+
+public class ENTAutoCompletionUtil {
+
+ public static final String CONTACT_LIST_CACHE_KEY = "UserMailCompletionPF_";
+ public static final String GROUP_LIST_CACHE_KEY = "UserMailCompletionList_";
+
+ private static Log logger = LogFactoryUtil.getLog(ENTAutoCompletionUtil.class);
+
+ /**
+ * Return a list of users matching the query to display for auto completion
+ * @param query The String used to find result
+ * @param user The user currently searching
+ * @return a ProxyFiche Set which contains all the results
+ */
+ public static List<ProxyFiche> getContactCompletionForUser(String query, User user) {
+
+ List<ProxyFiche> cachePF = getContactsCache(user);
+
+ return getFilteredListFromProxyFicheList(cachePF, query);
+ }
+
+ /**
+ * Take a users list and a query and return a filtered list with user matching the query
+ * @param usersProxyFicheList Full users List that we want to filtered
+ * @param query Query from user search to match
+ * @return a ProxyFiche List which contains matching users
+ */
+ public static List<ProxyFiche> getFilteredListFromProxyFicheList(List<ProxyFiche> usersProxyFicheList, String query) {
+ List<ProxyFiche> filteredPFList = new ArrayList<ProxyFiche>();
+
+ //Instantiation de l'operateur de comparaison avec Collator
+ Collator compareOperator = Collator.getInstance(Locale.FRENCH);
+ compareOperator.setStrength (Collator.PRIMARY);
+
+ if (query.equals("")) {
+ return usersProxyFicheList;
+ }
+ else {
+ int sizeQuery = query.length();
+ for (ProxyFiche f : usersProxyFicheList) {
+ if (f.getType().equals("fiche") && f.getRelUserId().equals("")) {
+ continue;
+ }
+ if ((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.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)) ||
+ (f.getStatut().length()>=sizeQuery && (compareOperator.compare(f.getStatut().substring(0,sizeQuery),query)==0))) {
+ filteredPFList.add(f);
+ }
+ }
+ }
+
+ return filteredPFList;
+ }
+
+ /**
+ * Return a list of groups matching the query to display for auto completion
+ * @param query The String used to find result
+ * @param user The user currently searching
+ * @return a ProxyListCompletion List which contains all the results
+ */
+ public static List<ProxyListCompletion> getGroupsCompletionForUser(String query, User user) {
+
+ List<ProxyListCompletion> cacheList = getGroupsCache(user);
+
+ int sizeQuery = query.length();
+
+ //Search if groups contain the query
+ List<ProxyListCompletion> plCompResult = new ArrayList<ProxyListCompletion>();
+ query = Normalizer.normalize(query, Normalizer.Form.NFD).toLowerCase();
+ query = query.replaceAll("[^\\p{ASCII}]", "");
+
+ for (ProxyListCompletion plc: cacheList) {
+ String name = Normalizer.normalize(plc.getDisplay(), Normalizer.Form.NFD).toLowerCase();
+ name = name.replaceAll("[^\\p{ASCII}]", "");
+ if (plc.getDisplay().length()>=sizeQuery && name.contains(query)) {
+ plCompResult.add(plc);
+ }
+ }
+
+ return plCompResult;
+ }
+
+ /**
+ * Returns all user's contacts after getting them in cache
+ * @param The user currently searching
+ * @return a ProxyFiche list
+ */
+ @SuppressWarnings("unchecked")
+ private static List<ProxyFiche> getContactsCache(User user) {
+ // Create cache key
+ String contactListCacheKey = ENTAutoCompletionUtil.CONTACT_LIST_CACHE_KEY + user.getUserId();
+
+ // Try to get values from cache : if we can't we generate them
+ List<ProxyFiche> contactListCache = (List<ProxyFiche>) ENTCacheUtils.getObjectFromCache(contactListCacheKey);
+
+ if (contactListCache == null) {
+ generateAutoCompletionCaches(user);
+ contactListCache = (List<ProxyFiche>) ENTCacheUtils.getObjectFromCache(contactListCacheKey);
+ }
+
+ return contactListCache;
+ }
+
+ /**
+ * Returns all user's groups/lists after getting them in cache
+ * @param user
+ * @return a ProxyListCompletion list
+ */
+ @SuppressWarnings("unchecked")
+ private static List<ProxyListCompletion> getGroupsCache(User user) {
+ // Create cache key
+ String groupListCacheKey = ENTAutoCompletionUtil.GROUP_LIST_CACHE_KEY + user.getUserId();
+
+ //Try to get values from cache : if we can't we generate them
+ List<ProxyListCompletion> groupListCache = (List<ProxyListCompletion>) ENTCacheUtils.getObjectFromCache(groupListCacheKey);
+
+ if (groupListCache == null) {
+ generateAutoCompletionCaches(user);
+ groupListCache = (List<ProxyListCompletion>) ENTCacheUtils.getObjectFromCache(groupListCacheKey);
+ }
+
+ return groupListCache;
+ }
+
+ /**
+ * Generate cache for auto-completion. We get all the user's groups, lists and contacts to cache them
+ * @param user The user currently searching for his contacts
+ */
+ private static void generateAutoCompletionCaches(User user) {
+ LanguageUtil.get(user.getLocale(), "tous-mes-contacts-liste");
+
+ // Cache keys
+ String cacheKeyPF = ENTAutoCompletionUtil.CONTACT_LIST_CACHE_KEY + user.getUserId();
+ String cacheKeyList = ENTAutoCompletionUtil.GROUP_LIST_CACHE_KEY + user.getUserId();
+
+ List<ProxyFiche> cachePF = new ArrayList<ProxyFiche>();
+ List<ProxyListCompletion> cacheList = new ArrayList<ProxyListCompletion>();
+
+ // First we manage personal user list
+ cacheList = new ArrayList<ProxyListCompletion>();
+ cacheList.add(new ProxyListCompletion(LanguageUtil.get(user.getLocale(), "tous-mes-contacts-liste"),LanguageUtil.get(user.getLocale(), "tous-mes-contacts-liste"), "all", "", "", ""));
+ cacheList.add(new ProxyListCompletion(LanguageUtil.get(user.getLocale(), "tous-mes-contacts-perso-liste"), LanguageUtil.get(user.getLocale(), "tous-mes-contacts-perso-liste"), "perso", "", "", ""));
+ cacheList.add(new ProxyListCompletion(LanguageUtil.get(user.getLocale(), "tous-mes-amis-liste"), LanguageUtil.get(user.getLocale(), "tous-mes-amis-liste"), "allAmis", "", "", ""));
+ cacheList.add(new ProxyListCompletion(LanguageUtil.get(user.getLocale(), "tous-mes-contacts-inst-liste"), LanguageUtil.get(user.getLocale(), "tous-mes-contacts-inst-liste"), "inst", "", "", ""));
+
+ List<Liste> lesListes;
+ try {
+ 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 + " " + LanguageUtil.get(user.getLocale(), "liste"), lName + " " + LanguageUtil.get(user.getLocale(), "liste"), "local", "", "", String.valueOf(list.getListeId())));
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+
+ List<FriendFolder> friendLists;
+ try {
+ friendLists = FriendFolderLocalServiceUtil.getFriendFolderByUserIDParentFolderId(user.getUserId(),0);
+
+ for (FriendFolder ff : friendLists) {
+ cacheList.add(new ProxyListCompletion(ff.getName() + " " + LanguageUtil.get(user.getLocale(), "liste"), ff.getName() + " " + LanguageUtil.get(user.getLocale(), "liste"), "amis", "", "", String.valueOf(ff.getFriendFolderId())));
+ }
+ } catch (SystemException e) {
+ logger.error(e);
+ }
+
+ // Second, we add all the institutional user lists
+ HashMap<String, List<ListeInstDetails> > userInstitutionalLists = ENTAddressBookListsManager.getUserInstitutionalLists(user, "");
+ List<ListeInstDetails> lids = userInstitutionalLists.get(ENTAddressBookListsManager.USER_CURRENT_LISTS);
+ List<ListeInstDetails> listFilter = new ArrayList<ListeInstDetails>();
+ for (ListeInstDetails lid : lids) {
+ if (lid.getOrgId() == Long.parseLong(lid.getSubOrgId())) {
+ try {
+ Organization org = OrganizationLocalServiceUtil.getOrganization(lid.getOrgId());
+ cacheList.add(new ProxyListCompletion(lid.getListeInstDetailsName() + " " + LanguageUtil.get(user.getLocale(), "liste"),
+ lid.getListeInstDetailsName() + " - " + ENTDisplayUtil.formatSCONETName(user, org.getName()) + " " + LanguageUtil.get(user.getLocale(), "liste"),
+ "search", lid.getSubOrgId(), String.valueOf(lid.getRoleFilter()), String.valueOf(lid.getListeInstId())));
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ listFilter.add(lid);
+ }
+ else {
+ cacheList.add(new ProxyListCompletion(lid.getListeInstDetailsName() + " " + LanguageUtil.get(user.getLocale(), "liste"),
+ lid.getListeInstDetailsName() + " " + LanguageUtil.get(user.getLocale(), "liste"), "search",
+ lid.getSubOrgId(), String.valueOf(lid.getRoleFilter()), String.valueOf(lid.getListeInstId())));
+ }
+ }
+
+ // Then we handle user's groups
+ List<Group> groupListResult = ENTAddressBookListsManager.getUserGroupsLists(user, "");
+ for(Group grp : groupListResult) {
+ cacheList.add(new ProxyListCompletion(grp.getName() + " " + LanguageUtil.get(user.getLocale(), "liste"),
+ grp.getName() + " " + LanguageUtil.get(user.getLocale(), "liste"),
+ "search", "", "", String.valueOf(grp.getGroupId())));
+ }
+
+ // We store all the lists in cache for half an hour (30 minutes)
+ ENTCacheUtils.storeObjectIntoCache(cacheKeyList, cacheList, 1800000);
+
+ // We get all contacts from lists and store them in cache too (30 minutes)
+ cachePF = ListFicheFactory.createAllContactsWithLists(user, lids, listFilter, groupListResult);
+ ENTCacheUtils.storeObjectIntoCache(cacheKeyPF, cachePF, 1800000);
+ }
+}