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