001/* 002 * Copyright 2019 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.odfweb.service.search; 017 018import java.util.Collections; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Objects; 023import java.util.Optional; 024import java.util.stream.Collectors; 025 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.commons.lang.StringUtils; 029 030import org.ametys.cms.content.indexing.solr.SolrFieldNames; 031import org.ametys.cms.repository.Content; 032import org.ametys.cms.search.advanced.AbstractTreeNode; 033import org.ametys.cms.search.advanced.TreeLeaf; 034import org.ametys.cms.search.query.ContentTypeQuery; 035import org.ametys.cms.search.query.DocumentTypeQuery; 036import org.ametys.cms.search.query.Query.Operator; 037import org.ametys.cms.search.query.StringQuery; 038import org.ametys.cms.search.solr.SearcherFactory.Searcher; 039import org.ametys.core.util.URIUtils; 040import org.ametys.odf.course.Course; 041import org.ametys.odf.enumeration.OdfReferenceTableHelper; 042import org.ametys.odf.program.AbstractProgram; 043import org.ametys.odf.program.Program; 044import org.ametys.odf.program.SubProgram; 045import org.ametys.plugins.odfweb.repository.OdfPageResolver; 046import org.ametys.plugins.repository.AmetysObject; 047import org.ametys.plugins.repository.AmetysObjectIterable; 048import org.ametys.web.frontoffice.search.fast.AbstractAutocompletionSearchServiceAction; 049import org.ametys.web.frontoffice.search.instance.SearchServiceInstance; 050import org.ametys.web.frontoffice.search.instance.model.FOSearchCriterion; 051import org.ametys.web.frontoffice.search.instance.model.FOSearchCriterionMode; 052import org.ametys.web.frontoffice.search.instance.model.RightCheckingMode; 053import org.ametys.web.renderingcontext.RenderingContext; 054import org.ametys.web.repository.page.Page; 055import org.ametys.web.repository.site.Site; 056 057/** 058 * Get the proposed program's pages and skills for auto-completion while beginning a search 059 */ 060public class ProgramItemAutocompletionSearchServiceAction extends AbstractAutocompletionSearchServiceAction 061{ 062 /** The ODF page resolver */ 063 protected OdfPageResolver _odfPageResolver; 064 065 @Override 066 public void service(ServiceManager serviceManager) throws ServiceException 067 { 068 super.service(serviceManager); 069 _odfPageResolver = (OdfPageResolver) serviceManager.lookup(OdfPageResolver.ROLE); 070 } 071 072 @Override 073 protected Map<String, Object> _searchAmetysObject(Site site, String zoneItemId, String lang, String escapedQuery, int limit, RightCheckingMode rightCheckingMode) throws Exception 074 { 075 Map<String, Object> results = super._searchAmetysObject(site, zoneItemId, lang, escapedQuery, limit, rightCheckingMode); 076 077 // Search on skill's title 078 AmetysObjectIterable<Content> skillResults = _getSkills(escapedQuery, lang, limit, rightCheckingMode); 079 List<Map<String, Object>> skills = skillResults.stream() 080 .map(this::_getContentHit) 081 .collect(Collectors.toList()); 082 083 results.put("skills", skills); 084 085 return results; 086 } 087 088 @Override 089 protected void setHitsInResults(Map<String, Object> results, AmetysObjectIterable<AmetysObject> searchHits, Site site, String zoneItemId, String lang) 090 { 091 List<String> catalogNames = _getCatalogNames(zoneItemId); 092 093 // Compute common ODF root page if search is configured for one catalog 094 Page odfRootPage = catalogNames.size() == 1 ? _odfPageResolver.getOdfRootPage(site.getName(), lang, catalogNames.get(0)) : null; 095 096 List<Map<String, Object>> pages = searchHits.stream() 097 .filter(Content.class::isInstance) 098 .map(Content.class::cast) 099 .map(c -> _getPageHit(c, site.getName(), odfRootPage)) 100 .filter(Objects::nonNull) 101 .collect(Collectors.toList()); 102 results.put("pages", pages); 103 } 104 105 /** 106 * Get the configured catalog in search criteria 107 * @param zoneItemId The id of zone item 108 * @return the catalog's name 109 */ 110 protected List<String> _getCatalogNames(String zoneItemId) 111 { 112 if (StringUtils.isNotEmpty(zoneItemId)) 113 { 114 SearchServiceInstance serviceInstance = _searchServiceInstanceManager.get(zoneItemId); 115 116 List<String> catalogValues = serviceInstance.getCriterionTree() 117 .map(AbstractTreeNode::getFlatLeaves) 118 .orElseGet(Collections::emptyList) 119 .stream() 120 .map(TreeLeaf::getValue) 121 .filter(c -> c.getMode() == FOSearchCriterionMode.STATIC) 122 .filter(c -> StringUtils.endsWith(c.getCriterionDefinition().getId(), "$indexingField$org.ametys.plugins.odf.Content.programItem$catalog")) 123 .map(FOSearchCriterion::getStaticValue) 124 .flatMap(Optional::stream) 125 .map(String.class::cast) 126 .collect(Collectors.toList()); 127 128 return catalogValues; 129 } 130 else 131 { 132 return List.of(); 133 } 134 135 } 136 137 /** 138 * Get the skills contents matching the query 139 * @param escapedQuery the query 140 * @param lang the language 141 * @param limit the max number of results 142 * @param rightCheckingMode the mode for checking rights 143 * @return the matching contents 144 * @throws Exception if an error occurred during search 145 */ 146 protected AmetysObjectIterable<Content> _getSkills(String escapedQuery, String lang, int limit, RightCheckingMode rightCheckingMode) throws Exception 147 { 148 Searcher searcher = _searcherFactory.create() 149 .withQuery(new StringQuery(SolrFieldNames.TITLE, Operator.SEARCH, escapedQuery, lang, true)) 150 .addFilterQuery(new DocumentTypeQuery(SolrFieldNames.TYPE_CONTENT)) 151 .addFilterQuery(new ContentTypeQuery(OdfReferenceTableHelper.SKILL)) 152 .withLimits(0, limit); 153 154 _setRightCheckingMode(searcher, rightCheckingMode); 155 156 return searcher.search(); 157 } 158 159 /** 160 * Get the JSON representation of a page hit 161 * @param content the content 162 * @param siteName the current site name 163 * @param odfRootPage the ODF root page. Can be null if search is configured for multiple catalogs 164 * @return the page as json 165 */ 166 protected Map<String, Object> _getPageHit(Content content, String siteName, Page odfRootPage) 167 { 168 Page page = null; 169 if (content instanceof Program) 170 { 171 page = odfRootPage != null ? _odfPageResolver.getProgramPage(odfRootPage, (Program) content) : _odfPageResolver.getProgramPage((Program) content, siteName); 172 } 173 else if (content instanceof SubProgram) 174 { 175 page = odfRootPage != null ? _odfPageResolver.getSubProgramPage(odfRootPage, (SubProgram) content, null) : _odfPageResolver.getSubProgramPage((SubProgram) content, null, siteName); 176 } 177 else if (content instanceof Course) 178 { 179 page = odfRootPage != null ? _odfPageResolver.getCoursePage(odfRootPage, (Course) content, (AbstractProgram) null) : _odfPageResolver.getCoursePage((Course) content, (AbstractProgram) null, siteName); 180 } 181 182 if (page != null) 183 { 184 Map<String, Object> result = new HashMap<>(); 185 result.put("title", page.getTitle()); 186 187 RenderingContext context = _renderingContextHandler.getRenderingContext(); 188 if (!(context == RenderingContext.BACK)) 189 { 190 StringBuilder uri = new StringBuilder(); 191 uri.append(_prefixHandler.getUriPrefix(siteName)); 192 uri.append("/"); 193 uri.append(page.getSitemapName() + "/" + page.getPathInSitemap() + ".html"); 194 195 result.put("url", URIUtils.encodePath(uri.toString())); 196 } 197 else // back 198 { 199 result.put("url", "javascript:(function(){parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + page.getId() + "'});})()"); 200 } 201 202 return result; 203 } 204 205 return null; 206 } 207 208 /** 209 * Get the JSON representation of a content hit 210 * @param content the content 211 * @return the content as json 212 */ 213 protected Map<String, Object> _getContentHit(Content content) 214 { 215 Map<String, Object> result = new HashMap<>(); 216 217 result.put("title", content.getTitle()); 218 result.put("id", content.getId()); 219 220 return result; 221 } 222 223}