--- /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.search.util;
+
+import java.text.Normalizer;
+import java.text.Normalizer.Form;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.regex.Pattern;
+
+import javax.naming.NamingException;
+
+import com.liferay.contacts.util.OrganizationFilter;
+import com.liferay.portal.kernel.dao.orm.QueryUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+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.communityInfos.model.CommunityInfos;
+import com.pentila.entSavoie.communityInfos.service.CommunityInfosLocalServiceUtil;
+
+public class IndexerUtil {
+ private static Log logger = LogFactoryUtil.getLog(IndexerUtil.class);
+ // constant to use for method IndexerUtil.getSearchGroupsQuery
+ /**
+ * variable to include the user community INCLUDE_USER_AND_COMMUNITY = true
+ */
+ public static boolean INCLUDE_USER_AND_COMMUNITY = true;
+ /**
+ * variable to don't include the user community ONT_INCLUDE_USER_AND_COMMUNITY = false
+ */
+ public static boolean DONT_INCLUDE_USER_AND_COMMUNITY = false;
+ /**
+ * variable to include the user global orgs INCLUDE_GLOBAL_ORG = true
+ */
+ public static boolean INCLUDE_GLOBAL_ORG = true;
+ /**
+ * variable to don't include the user global orgs INCLUDE_GLOBAL_ORG = false
+ */
+ public static boolean DONT_INCLUDE_GLOBAL_ORG = false;
+ /**
+ * variable to include the user personnal group INCLUDE_USER_GROUP = true
+ */
+ public static boolean INCLUDE_USER_GROUP = true;
+ /**
+ * variable dont include the user personnal group INCLUDE_USER_GROUP = falses
+ */
+ public static boolean DONT_INCLUDE_USER_GROUP = false;
+ /**
+ * variable to use in the indexers to store/search the formatted date (wrong value "modified")
+ */
+ public static String MODIFIED_DATE_FORMATTED = "modifiedDate";
+ /**
+ * variable to use in the indexers to store/search the etabId
+ */
+ public static String ETAB_ID_PARAMETER = "etabId";
+
+
+ public static String getSearchGroupsQuery(User user, boolean includeUserAndCommunity, boolean includeGlobalOrgs, boolean includeUserGroup) throws NamingException, Exception {
+ List<Organization> userOrgs = user.getOrganizations();
+ String listGroupId = "(";
+
+ for (Organization o: userOrgs) {
+ if(includeGlobalOrgs || (
+ !o.getName().contains("- Parent") &&
+ !o.getName().contains("- Enseignants") &&
+ !o.getName().contains("- Personnels"))) {
+ listGroupId += o.getGroup().getGroupId() + " or ";
+ }
+ }
+
+ if(includeUserAndCommunity){
+ List<Long> communitiesGroupID = getUserCommunity(user);
+ for(Long aGroupId : communitiesGroupID){
+ listGroupId += aGroupId + " or ";
+ }
+ if(includeUserGroup){
+ listGroupId += user.getGroup().getGroupId() + " or ";
+ }
+ }
+
+ if(listGroupId.contains(" or ")){
+ listGroupId = listGroupId.substring(0, listGroupId.lastIndexOf(" or ")) + ")";
+ }
+
+ return listGroupId;
+
+ }
+
+ /**
+ * 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!
+ * A PART OF THIS METHOD IS COMMENT AT THE MOMENT BECAUSE OF LIFERAY BUG CONCERNING LUCENE CLAUSE LIMIT.
+ * Sometimes (for profile with high visibility) the number of group are > 1024 this is replace by
+ * the method {@link #getEtabVisibility(User) getEtabVisibility}
+ * @param pUser the user you want to get the group visibility
+ * @param pIncludeCommunity boolean to define if we add the user communities to the list of result
+ * @param pIncludeUserGroup boolean to define if we add the user personnal group to the list of result
+ * @return the list of user group for SOLR, return null in case of error during
+ */
+ public static String getExtendedVisibility(User pUser, boolean pIncludeCommunity, boolean pIncludeUserGroup){
+ StringBuilder listGroupBuilder = new StringBuilder().append("(");
+ String listGroup = "";
+ // get all user etab
+ try {
+// List<Organization> etabsWithSchoolarComplex = OrganizationFilter.getAllUserSchoolsIncludingSchoolComplex(pUser);
+
+ // get all user organization from all etab.
+// List<Organization> allUserOrgVisibility = new ArrayList<Organization>(etabsWithSchoolarComplex);
+// for(Organization etab : etabsWithSchoolarComplex){
+// allUserOrgVisibility.addAll(OrganizationLocalServiceUtil.getOrganizations(pUser.getCompanyId(), etab.getOrganizationId()));
+// }
+
+ // Build the string for all orgd
+ if(pIncludeCommunity){
+ for(Long aCommunityGroupId : getUserCommunity(pUser)){
+ listGroupBuilder.append(aCommunityGroupId).append(" or ");
+ }
+ }
+
+// for(Organization anUserOrgVisibility : allUserOrgVisibility){
+// listGroupBuilder.append(anUserOrgVisibility.getGroupId()).append(" or ");
+// System.out.println(GroupLocalServiceUtil.getGroup(anUserOrgVisibility.getGroupId()).getName());
+// }
+
+ if(pIncludeUserGroup){
+ listGroupBuilder.append(pUser.getGroup().getGroupId()).append(" or ");
+ }
+
+ // finalize the string
+ listGroup = listGroupBuilder.toString();
+ if(listGroup.contains(" or ")){
+ listGroup = listGroup.substring(0, listGroup.lastIndexOf(" or ")) + ")";
+ }
+ else if (listGroup.equals("(")) {
+ return "";
+ }
+
+ } catch (Exception e) {
+ logger.error(e);
+ return null;
+ }
+
+ return listGroup;
+ }
+
+ public static long getEtabId(long organizationId) throws PortalException, SystemException{
+ Organization o = OrganizationLocalServiceUtil.getOrganization(organizationId);
+ Organization etab = o;
+ //Si on est pas sur l'organisation "Cartable de Savoie"
+ if(etab.getParentOrganizationId() != 0){
+ while(o.getParentOrganizationId() != 0){
+ etab = o;
+ o = o.getParentOrganization();
+ }
+ }
+ return etab.getGroupId();
+ }
+
+
+ // Recupere les parametres entres dans la request et appelle la fonction permettant de desarchiver l'archive
+ public static String normalizeStringEscaped(String keywords) {
+
+ keywords = Normalizer.normalize(keywords, Form.NFD);
+
+ Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
+ keywords = pattern.matcher(keywords).replaceAll("");
+
+ String LUCENE_ESCAPE_CHARS = "[\\\\+\\-\\!\\(\\)\\:\\^\\]\\{\\}\\~\\*\\?\\\"]";
+ Pattern LUCENE_PATTERN = Pattern.compile(LUCENE_ESCAPE_CHARS);
+ String REPLACEMENT_STRING = "\\\\\\\\\\\\$0";
+ keywords = LUCENE_PATTERN.matcher(keywords).replaceAll(REPLACEMENT_STRING);
+ return keywords;
+
+ }
+
+ public static String normalizeString(String keywords) {
+
+ keywords = Normalizer.normalize(keywords, Form.NFD);
+
+ Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
+ keywords = pattern.matcher(keywords).replaceAll("");
+
+
+ return keywords;
+
+ }
+
+ /**
+ * This method return the list of user community groupID
+ * @param pUser the user you want the community group id
+ * @return List<Long> representing the list of groupID for the community of user return empty array in case of error.
+ */
+ private static List<Long> getUserCommunity(User pUser){
+ ArrayList<Long> userCommunitiesId = new ArrayList<Long>();
+ LinkedHashMap<String, Object> groupParams = new LinkedHashMap<String, Object>();
+ groupParams.put("usersGroups", new Long(pUser.getUserId()));
+ groupParams.put("active", Boolean.TRUE);
+ GroupNameComparator gnc = new GroupNameComparator();
+
+ try {
+ List<Group> listUserGrp = pUser.getGroups();
+ for (Group gTmp: listUserGrp) {
+ try {
+ CommunityInfos ci = CommunityInfosLocalServiceUtil.getCommunityInfosByGroupId(gTmp.getGroupId());
+ if (ci.getStatus()==0) {
+ userCommunitiesId.add(gTmp.getGroupId());
+ }
+ }
+ catch(Exception e) {
+ // aucune info pour ce groupe --> surement le groupe Guest
+ }
+ }
+ }
+ catch(Exception e){
+ logger.error(e);
+ return userCommunitiesId;
+ }
+
+ return userCommunitiesId;
+ }
+
+ /**
+ * Method temporary used to simplify the solr call and be sure there is less than 1024 clauses
+ * This method return the list user etab formated for solar request. Use it only for extended profile
+ * @param pUser the user you want to get the group visibility
+ * @return the list of user group for SOLR, return null in case of error during
+ */
+ public static String getEtabVisibility(User pUser){
+ StringBuilder listGroupBuilder = new StringBuilder().append("(");
+ String listGroup = "";
+ // get all user etab
+ try {
+ List<Organization> etabsWithSchoolarComplex = OrganizationFilter.getAllUserSchoolsIncludingSchoolComplex(pUser);
+
+ for(Organization etab : etabsWithSchoolarComplex){
+ listGroupBuilder.append(etab.getGroup().getGroupId()).append(" or ");
+ }
+
+ listGroup = listGroupBuilder.toString();
+ if(listGroup.contains(" or ")){
+ listGroup = listGroup.substring(0, listGroup.lastIndexOf(" or ")) + ")";
+ }
+ }
+ catch(Exception e){
+ logger.error(e);
+ return null;
+ }
+ return listGroup;
+ }
+
+ /**
+ * From list of string it build a SOLR search criteria. Add it to search query via addTermeMethod.
+ * It set OR operator between each value of list.
+ * @param pCriteria List<String> - list of criteria
+ * @return String - List of creteria with OR oprator
+ */
+ public static String getSearchCriteriaOR(List<String> pCriteria){
+ StringBuilder listCriteriaBuilder = new StringBuilder("(");
+ String listCriteria = "";
+ for(String aCriteria : pCriteria){
+ listCriteriaBuilder.append(aCriteria).append(" or ");
+ }
+
+ listCriteria = listCriteriaBuilder.toString();
+ if(listCriteria.contains(" or ")){
+ listCriteria = listCriteria.substring(0, listCriteria.lastIndexOf(" or "));
+ }
+ listCriteria = listCriteria + ")";
+ return listCriteria;
+ }
+
+
+}