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.cocoon;
017
018import java.util.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Map;
023
024import org.ametys.cms.search.SearchResult;
025import org.ametys.cms.search.SearchResults;
026import org.ametys.cms.search.SearchResultsIterable;
027import org.ametys.cms.search.SearchResultsIterator;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectIterator;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.plugins.repository.IdCollectionIterable;
033
034/**
035 * Search results based on a list of AmetysObject IDs (lazy resolved).
036 * @param <A> the actual type of {@link AmetysObject}s. 
037 */
038@Deprecated
039public class ContentSearchResults<A extends AmetysObject> implements SearchResults<A>
040{
041    
042    /** The AmetysObject resolver. */
043    private AmetysObjectResolver _resolver;
044    
045    /** The list of object ids. */
046    private List<String> _objectIds;
047    
048    /**
049     * Build a local SearchResults.
050     * @param resolver The AmetysObject resolver.
051     * @param ids the object IDs.
052     */
053    public ContentSearchResults(AmetysObjectResolver resolver, Collection<String> ids)
054    {
055        _resolver = resolver;
056        _objectIds = new ArrayList<>(ids);
057    }
058    
059    @Override
060    public SearchResultsIterable<SearchResult<A>> getResults()
061    {
062        return new IdSearchResultsIterable(_resolver, _objectIds);
063    }
064    
065    @Override
066    public AmetysObjectIterable<A> getObjects()
067    {
068        return new IdCollectionIterable<>(_resolver, _objectIds);
069    }
070    
071    @Override
072    public Iterable<String> getObjectIds()
073    {
074        return _objectIds;
075    }
076    
077    @Override
078    public Map<String, Map<String, Integer>> getFacetResults()
079    {
080        return Collections.emptyMap();
081    }
082    
083    @Override
084    public long getTotalCount()
085    {
086        return _objectIds.size();
087    }
088    
089    @Override
090    public float getMaxScore()
091    {
092        return 0;
093    }
094    
095    class IdSearchResultsIterable implements SearchResultsIterable<SearchResult<A>>
096    {
097        private IdCollectionIterable<A> _iterable;
098
099        IdSearchResultsIterable(AmetysObjectResolver resolver, List<String> contentIds)
100        {
101            this._iterable = new IdCollectionIterable<>(resolver, contentIds);
102        }
103        
104        @Override
105        public SearchResultsIterator<SearchResult<A>> iterator()
106        {
107            return new SearchResultsIteratorWrapper(_iterable.iterator());
108        }
109        
110        @Override
111        public long getSize()
112        {
113            return _iterable.iterator().getSize();
114        }
115    }
116    
117    class SearchResultsIteratorWrapper implements SearchResultsIterator<SearchResult<A>>
118    {
119        private AmetysObjectIterator<A> _iterator;
120        
121        SearchResultsIteratorWrapper(AmetysObjectIterator<A> iterator)
122        {
123            this._iterator = iterator;
124        }
125        
126        @Override
127        public boolean hasNext()
128        {
129            return _iterator.hasNext();
130        }
131        
132        @Override
133        public SearchResult<A> next()
134        {
135            return new SearchResultWrapper(_iterator.next());
136        }
137        
138        @Override
139        public void skip(long skipNum)
140        {
141            _iterator.skip(skipNum);
142        }
143    }
144    
145    class SearchResultWrapper implements SearchResult<A>
146    {
147        private A _object;
148        
149        SearchResultWrapper(A object)
150        {
151            this._object = object;
152        }
153
154        @Override
155        public A getObject()
156        {
157            return _object;
158        }
159        
160        @Override
161        public float getScore()
162        {
163            return 0;
164        }
165    }
166
167}