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.RightCheckingMode;
051import org.ametys.web.frontoffice.search.instance.model.SearchServiceCriterion;
052import org.ametys.web.frontoffice.search.instance.model.SearchServiceCriterionMode;
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 criterion definitions
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() == SearchServiceCriterionMode.STATIC)
122                                                        .filter(c -> StringUtils.endsWith(c.getCriterionDefinition().getName(), "$org.ametys.plugins.odf.Content.programItem$catalog"))
123                                                        .map(SearchServiceCriterion::getStaticValue)
124                                                        .flatMap(Optional::stream)
125                                                        .map(List.class::cast)
126                                                        .flatMap(List::stream)
127                                                        .map(String.class::cast)
128                                                        .toList();
129            
130            return catalogValues;
131        }
132        else
133        {
134            return List.of();
135        }
136        
137    }
138    
139    /**
140     * Get the skills contents matching the query
141     * @param escapedQuery the query
142     * @param lang the language
143     * @param limit the max number of results
144     * @param rightCheckingMode the mode for checking rights
145     * @return the matching contents
146     * @throws Exception if an error occurred during search
147     */
148    protected AmetysObjectIterable<Content> _getSkills(String escapedQuery, String lang, int limit, RightCheckingMode rightCheckingMode) throws Exception
149    {
150        Searcher searcher = _searcherFactory.create()
151                .withQuery(new StringQuery(SolrFieldNames.TITLE, Operator.SEARCH, escapedQuery, lang, true))
152                .addFilterQuery(new DocumentTypeQuery(SolrFieldNames.TYPE_CONTENT))
153                .addFilterQuery(new ContentTypeQuery(OdfReferenceTableHelper.SKILL))
154                .withLimits(0, limit);
155        
156        _setRightCheckingMode(searcher, rightCheckingMode);
157
158        return searcher.search();
159    }
160    
161    /**
162     * Get the JSON representation of a page hit
163     * @param content the content
164     * @param siteName the current site name
165     * @param odfRootPage the ODF root page. Can be null if search is configured for multiple catalogs
166     * @return the page as json
167     */
168    protected Map<String, Object> _getPageHit(Content content, String siteName, Page odfRootPage)
169    {
170        Page page = null;
171        if (content instanceof Program)
172        {
173            page = odfRootPage != null ? _odfPageResolver.getProgramPage(odfRootPage, (Program) content) : _odfPageResolver.getProgramPage((Program) content, siteName);
174        }
175        else if (content instanceof SubProgram)
176        {
177            page = odfRootPage != null ? _odfPageResolver.getSubProgramPage(odfRootPage, (SubProgram) content, null) : _odfPageResolver.getSubProgramPage((SubProgram) content, null, siteName);
178        }
179        else if (content instanceof Course)
180        {
181            page = odfRootPage != null ? _odfPageResolver.getCoursePage(odfRootPage, (Course) content, (AbstractProgram) null) : _odfPageResolver.getCoursePage((Course) content, (AbstractProgram) null, siteName);
182        }
183        
184        if (page != null)
185        {
186            Map<String, Object> result = new HashMap<>();
187            result.put("title", page.getTitle());
188            
189            RenderingContext context = _renderingContextHandler.getRenderingContext();
190            if (!(context == RenderingContext.BACK))
191            {
192                StringBuilder uri = new StringBuilder();
193                uri.append(_prefixHandler.getUriPrefix(siteName));
194                uri.append("/");
195                uri.append(page.getSitemapName() + "/" + page.getPathInSitemap() + ".html");
196                
197                result.put("url", URIUtils.encodePath(uri.toString()));
198            }
199            else // back
200            {
201                result.put("url", "javascript:(function(){parent.Ametys.tool.ToolsManager.openTool('uitool-page', {id:'" + page.getId() + "'});})()");
202            }
203            
204            return result;
205        }
206        
207        return null;
208    }
209    
210    /**
211     * Get the JSON representation of a content hit
212     * @param content the content
213     * @return the content as json
214     */
215    protected Map<String, Object> _getContentHit(Content content)
216    {
217        Map<String, Object> result = new HashMap<>();
218        
219        result.put("title", content.getTitle());
220        result.put("id", content.getId());
221        
222        return result;
223    }
224
225}