1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie.search.util;
37 import java.text.Normalizer;
38 import java.text.Normalizer.Form;
39 import java.util.ArrayList;
40 import java.util.LinkedHashMap;
41 import java.util.List;
42 import java.util.regex.Pattern;
44 import javax.naming.NamingException;
46 import com.liferay.contacts.util.OrganizationFilter;
47 import com.liferay.portal.kernel.dao.orm.QueryUtil;
48 import com.liferay.portal.kernel.exception.PortalException;
49 import com.liferay.portal.kernel.exception.SystemException;
50 import com.liferay.portal.kernel.log.Log;
51 import com.liferay.portal.kernel.log.LogFactoryUtil;
52 import com.liferay.portal.model.Group;
53 import com.liferay.portal.model.Organization;
54 import com.liferay.portal.model.User;
55 import com.liferay.portal.service.GroupLocalServiceUtil;
56 import com.liferay.portal.service.OrganizationLocalServiceUtil;
57 import com.liferay.portal.util.comparator.GroupNameComparator;
58 import com.pentila.entSavoie.communityInfos.model.CommunityInfos;
59 import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
61 public class IndexerUtil {
62 private static Log logger = LogFactoryUtil.getLog(IndexerUtil.class);
63 // constant to use for method IndexerUtil.getSearchGroupsQuery
65 * variable to include the user community INCLUDE_USER_AND_COMMUNITY = true
67 public static boolean INCLUDE_USER_AND_COMMUNITY = true;
69 * variable to don't include the user community ONT_INCLUDE_USER_AND_COMMUNITY = false
71 public static boolean DONT_INCLUDE_USER_AND_COMMUNITY = false;
73 * variable to include the user global orgs INCLUDE_GLOBAL_ORG = true
75 public static boolean INCLUDE_GLOBAL_ORG = true;
77 * variable to don't include the user global orgs INCLUDE_GLOBAL_ORG = false
79 public static boolean DONT_INCLUDE_GLOBAL_ORG = false;
81 * variable to include the user personnal group INCLUDE_USER_GROUP = true
83 public static boolean INCLUDE_USER_GROUP = true;
85 * variable dont include the user personnal group INCLUDE_USER_GROUP = falses
87 public static boolean DONT_INCLUDE_USER_GROUP = false;
89 * variable to use in the indexers to store/search the formatted date (wrong value "modified")
91 public static String MODIFIED_DATE_FORMATTED = "modifiedDate";
93 * variable to use in the indexers to store/search the etabId
95 public static String ETAB_ID_PARAMETER = "etabId";
98 public static String getSearchGroupsQuery(User user, boolean includeUserAndCommunity, boolean includeGlobalOrgs, boolean includeUserGroup) throws NamingException, Exception {
99 List<Organization> userOrgs = user.getOrganizations();
100 String listGroupId = "(";
102 for (Organization o: userOrgs) {
103 if(includeGlobalOrgs || (
104 !o.getName().contains("- Parent") &&
105 !o.getName().contains("- Enseignants") &&
106 !o.getName().contains("- Personnels"))) {
107 listGroupId += o.getGroup().getGroupId() + " or ";
111 if(includeUserAndCommunity){
112 List<Long> communitiesGroupID = getUserCommunity(user);
113 for(Long aGroupId : communitiesGroupID){
114 listGroupId += aGroupId + " or ";
116 if(includeUserGroup){
117 listGroupId += user.getGroup().getGroupId() + " or ";
121 if(listGroupId.contains(" or ")){
122 listGroupId = listGroupId.substring(0, listGroupId.lastIndexOf(" or ")) + ")";
130 * This method return the list of group which the user with extended right can search on. This method <b>don't verify</b> the user rights!
131 * A PART OF THIS METHOD IS COMMENT AT THE MOMENT BECAUSE OF LIFERAY BUG CONCERNING LUCENE CLAUSE LIMIT.
132 * Sometimes (for profile with high visibility) the number of group are > 1024 this is replace by
133 * the method {@link #getEtabVisibility(User) getEtabVisibility}
134 * @param pUser the user you want to get the group visibility
135 * @param pIncludeCommunity boolean to define if we add the user communities to the list of result
136 * @param pIncludeUserGroup boolean to define if we add the user personnal group to the list of result
137 * @return the list of user group for SOLR, return null in case of error during
139 public static String getExtendedVisibility(User pUser, boolean pIncludeCommunity, boolean pIncludeUserGroup){
140 StringBuilder listGroupBuilder = new StringBuilder().append("(");
141 String listGroup = "";
144 // List<Organization> etabsWithSchoolarComplex = OrganizationFilter.getAllUserSchoolsIncludingSchoolComplex(pUser);
146 // get all user organization from all etab.
147 // List<Organization> allUserOrgVisibility = new ArrayList<Organization>(etabsWithSchoolarComplex);
148 // for(Organization etab : etabsWithSchoolarComplex){
149 // allUserOrgVisibility.addAll(OrganizationLocalServiceUtil.getOrganizations(pUser.getCompanyId(), etab.getOrganizationId()));
152 // Build the string for all orgd
153 if(pIncludeCommunity){
154 for(Long aCommunityGroupId : getUserCommunity(pUser)){
155 listGroupBuilder.append(aCommunityGroupId).append(" or ");
159 // for(Organization anUserOrgVisibility : allUserOrgVisibility){
160 // listGroupBuilder.append(anUserOrgVisibility.getGroupId()).append(" or ");
161 // System.out.println(GroupLocalServiceUtil.getGroup(anUserOrgVisibility.getGroupId()).getName());
164 if(pIncludeUserGroup){
165 listGroupBuilder.append(pUser.getGroup().getGroupId()).append(" or ");
168 // finalize the string
169 listGroup = listGroupBuilder.toString();
170 if(listGroup.contains(" or ")){
171 listGroup = listGroup.substring(0, listGroup.lastIndexOf(" or ")) + ")";
173 else if (listGroup.equals("(")) {
177 } catch (Exception e) {
185 public static long getEtabId(long organizationId) throws PortalException, SystemException{
186 Organization o = OrganizationLocalServiceUtil.getOrganization(organizationId);
187 Organization etab = o;
188 //Si on est pas sur l'organisation "Cartable de Savoie"
189 if(etab.getParentOrganizationId() != 0){
190 while(o.getParentOrganizationId() != 0){
192 o = o.getParentOrganization();
195 return etab.getGroupId();
199 // Recupere les parametres entres dans la request et appelle la fonction permettant de desarchiver l'archive
200 public static String normalizeStringEscaped(String keywords) {
202 keywords = Normalizer.normalize(keywords, Form.NFD);
204 Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
205 keywords = pattern.matcher(keywords).replaceAll("");
207 String LUCENE_ESCAPE_CHARS = "[\\\\+\\-\\!\\(\\)\\:\\^\\]\\{\\}\\~\\*\\?\\\"]";
208 Pattern LUCENE_PATTERN = Pattern.compile(LUCENE_ESCAPE_CHARS);
209 String REPLACEMENT_STRING = "\\\\\\\\\\\\$0";
210 keywords = LUCENE_PATTERN.matcher(keywords).replaceAll(REPLACEMENT_STRING);
215 public static String normalizeString(String keywords) {
217 keywords = Normalizer.normalize(keywords, Form.NFD);
219 Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
220 keywords = pattern.matcher(keywords).replaceAll("");
228 * This method return the list of user community groupID
229 * @param pUser the user you want the community group id
230 * @return List<Long> representing the list of groupID for the community of user return empty array in case of error.
232 private static List<Long> getUserCommunity(User pUser){
233 ArrayList<Long> userCommunitiesId = new ArrayList<Long>();
234 LinkedHashMap<String, Object> groupParams = new LinkedHashMap<String, Object>();
235 groupParams.put("usersGroups", new Long(pUser.getUserId()));
236 groupParams.put("active", Boolean.TRUE);
237 GroupNameComparator gnc = new GroupNameComparator();
240 List<Group> listUserGrp = pUser.getGroups();
241 for (Group gTmp: listUserGrp) {
243 CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
244 if (ci.getStatus()==0) {
245 userCommunitiesId.add(gTmp.getGroupId());
249 // aucune info pour ce groupe --> surement le groupe Guest
255 return userCommunitiesId;
258 return userCommunitiesId;
262 * Method temporary used to simplify the solr call and be sure there is less than 1024 clauses
263 * This method return the list user etab formated for solar request. Use it only for extended profile
264 * @param pUser the user you want to get the group visibility
265 * @return the list of user group for SOLR, return null in case of error during
267 public static String getEtabVisibility(User pUser){
268 StringBuilder listGroupBuilder = new StringBuilder().append("(");
269 String listGroup = "";
272 List<Organization> etabsWithSchoolarComplex = OrganizationFilter.getAllUserSchoolsIncludingSchoolComplex(pUser);
274 for(Organization etab : etabsWithSchoolarComplex){
275 listGroupBuilder.append(etab.getGroup().getGroupId()).append(" or ");
278 listGroup = listGroupBuilder.toString();
279 if(listGroup.contains(" or ")){
280 listGroup = listGroup.substring(0, listGroup.lastIndexOf(" or ")) + ")";
291 * From list of string it build a SOLR search criteria. Add it to search query via addTermeMethod.
292 * It set OR operator between each value of list.
293 * @param pCriteria List<String> - list of criteria
294 * @return String - List of creteria with OR oprator
296 public static String getSearchCriteriaOR(List<String> pCriteria){
297 StringBuilder listCriteriaBuilder = new StringBuilder("(");
298 String listCriteria = "";
299 for(String aCriteria : pCriteria){
300 listCriteriaBuilder.append(aCriteria).append(" or ");
303 listCriteria = listCriteriaBuilder.toString();
304 if(listCriteria.contains(" or ")){
305 listCriteria = listCriteria.substring(0, listCriteria.lastIndexOf(" or "));
307 listCriteria = listCriteria + ")";