001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.workspaces.search.module; 017 018import java.util.Collection; 019import java.util.HashSet; 020import java.util.List; 021 022import org.apache.cocoon.environment.Request; 023import org.apache.cocoon.xml.XMLUtils; 024import org.apache.commons.lang3.StringUtils; 025import org.xml.sax.SAXException; 026 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.search.Sort.Order; 029import org.ametys.cms.search.content.ContentSearcherFactory.SimpleContentSearcher; 030import org.ametys.cms.search.query.AndQuery; 031import org.ametys.cms.search.query.FullTextQuery; 032import org.ametys.cms.search.query.JoinQuery; 033import org.ametys.cms.search.query.MatchAllQuery; 034import org.ametys.cms.search.query.OrQuery; 035import org.ametys.cms.search.query.Query; 036import org.ametys.cms.search.query.Query.Operator; 037import org.ametys.cms.search.query.StringQuery; 038import org.ametys.plugins.userdirectory.transformation.xslt.UserXSLTHelper; 039import org.ametys.plugins.workspaces.WorkspacesConstants; 040import org.ametys.plugins.workspaces.indexing.solr.SolrWorkspacesConstants; 041import org.ametys.plugins.workspaces.project.objects.Project; 042 043/** 044 * Generator for members search module 045 * 046 */ 047public class MemberSearchModuleGenerator extends AbstractContentSolrSearchModuleGenerator 048{ 049 @Override 050 protected SimpleContentSearcher getSearcher() 051 { 052 return _contentSearcherFactory.create(WorkspacesConstants.MEMBER_CONTENT_TYPE_ID); 053 } 054 055 @Override 056 protected String getSortFieldName() 057 { 058 return "lastname"; 059 } 060 061 @Override 062 protected Order getSortOrder() 063 { 064 return Order.ASC; 065 } 066 067 @Override 068 protected Query getQuery(String siteName, String lang, String textfield, Request request) 069 { 070 Collection<Query> queries = new HashSet<>(); 071 072 queries.add(getProjectQuery(request)); 073 074 if (StringUtils.isNotBlank(textfield)) 075 { 076 Query fullTextQuery = new FullTextQuery(textfield, lang, Operator.SEARCH); 077 078 Query skillsQuery = new JoinQuery(fullTextQuery, "skills"); 079 Query keywordsQuery = new JoinQuery(fullTextQuery, "keywords"); 080 081 Query orQuery = new OrQuery(fullTextQuery, skillsQuery, keywordsQuery); 082 queries.add(orQuery); 083 } 084 085 Query fullQuery; 086 087 if (!queries.isEmpty()) 088 { 089 fullQuery = new AndQuery(queries); 090 } 091 else 092 { 093 fullQuery = new MatchAllQuery(); 094 } 095 096 return fullQuery; 097 } 098 099 @Override 100 protected void saxPage(Content content) throws SAXException 101 { 102 String userPageId = UserXSLTHelper.getUserPage(content.getId(), _projectManager.getCatalogSiteName()); 103 if (userPageId != null) 104 { 105 XMLUtils.createElement(contentHandler, "page", userPageId); 106 } 107 } 108 109 /** 110 * Returns the project query 111 * @param request the request 112 * @return the project query 113 */ 114 protected Query getProjectQuery(Request request) 115 { 116 List<Project> projects = getProjects(request, false); 117 118 Query projectQuery = projects.stream() 119 .map(Project::getId) 120 .map(id -> new StringQuery(SolrWorkspacesConstants.PROJECT_ID, Operator.EQ , id, null)) 121 .collect(OrQuery.collector()); 122 123 return projectQuery; 124 } 125}