001/*
002 *  Copyright 2016 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.cms.search.solr;
017
018import java.util.Iterator;
019
020import org.apache.solr.client.solrj.response.QueryResponse;
021import org.apache.solr.common.SolrDocument;
022import org.apache.solr.common.SolrDocumentList;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026import org.ametys.cms.search.SearchResult;
027import org.ametys.cms.search.SearchResultsIterable;
028import org.ametys.cms.search.SearchResultsIterator;
029import org.ametys.plugins.repository.AmetysObject;
030import org.ametys.plugins.repository.AmetysObjectIterable;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032
033/**
034 * {@link AmetysObjectIterable} backed by a solr {@link QueryResponse}.
035 * @param <A> the actual type of {@link AmetysObject}s.
036 */
037public class SolrResponseIterable<A extends AmetysObject> implements SearchResultsIterable<SearchResult<A>>
038{
039    static final Logger __LOGGER = LoggerFactory.getLogger(SolrResponseIdIterable.class);
040
041    AmetysObjectResolver _resolver;
042    private SolrDocumentList _docList;
043    private SolrResponseIterator _lastIterator;
044    
045    /**
046     * Build a SolrResponseIterable.
047     * @param docList The solr response document list.
048     * @param resolver The Ametys object resolver.
049     */
050    public SolrResponseIterable(SolrDocumentList docList, AmetysObjectResolver resolver)
051    {
052        _docList = docList;
053        _resolver = resolver;
054    }
055    
056    @Override
057    public SearchResultsIterator<SearchResult<A>> iterator()
058    {
059        _lastIterator = new SolrResponseIterator<>(_docList.iterator(), _docList.size());
060        return _lastIterator;
061    }
062    
063    @Override
064    public long getSize()
065    {
066        if (_lastIterator == null)
067        {
068            return _docList.size();
069        }
070        else
071        {
072            // Some documents may represent unexisting ametys objects (due to indexation issues, etc.) so we need to subtract the number of errors found
073            return _lastIterator.getSize();
074        }
075    }
076    
077    class SolrResponseIterator<AO extends AmetysObject> extends AbstractResponseIterator<SearchResult<AO>>
078    {
079        public SolrResponseIterator(Iterator<SolrDocument> it, long size)
080        {
081            super(it, size, _resolver, __LOGGER);
082        }
083        
084        @Override
085        protected SearchResult<AO> _setNextObject(SolrDocument document, String id)
086        {
087            return new SolrDocumentResult<>(document, _resolver);
088        }
089    }
090}