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.List;
019import java.util.stream.Collectors;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.Request;
025import org.xml.sax.SAXException;
026
027import org.ametys.cms.search.SearchResults;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030
031/**
032 * Abstract generator for search module of type content
033 *
034 */
035public abstract class AbstractSolrSearchModuleGenerator extends AbstractSearchModuleGenerator
036{
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042    }
043    
044    @Override
045    protected void saxHits(String siteName, String lang, String textfield, Request request, int offset, int limit, int minLimit) throws SAXException, ProcessingException
046    {
047        try
048        {
049            SearchResults<? extends AmetysObject> searchResults = getSearchResults(siteName, lang, textfield, request, offset, limit);
050            
051            long totalCount = searchResults.getTotalCount();
052            AmetysObjectIterable<? extends AmetysObject> results = searchResults.getObjects();
053            List< ? extends AmetysObject> resultList = results.stream().collect(Collectors.toList());
054            
055            saxHits(resultList, lang, offset, limit, minLimit, totalCount);
056        }
057        catch (Exception e)
058        {
059            throw new ProcessingException("Unable to get search results", e);
060        }
061    }
062
063    /**
064     * Get the search results
065     * @param siteName the current site name
066     * @param lang the current language
067     * @param textfield the search input
068     * @param request the request
069     * @param offset the search offset
070     * @param limit the max number of results
071     * @return the search results
072     * @throws Exception if the search failed
073     */
074    protected abstract SearchResults<? extends AmetysObject> getSearchResults(String siteName, String lang, String textfield, Request request, int offset, int limit) throws Exception;
075    
076}