001/*
002 *  Copyright 2010 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.filter;
017
018import java.util.List;
019import java.util.Map;
020
021import org.ametys.cms.repository.Content;
022import org.ametys.plugins.repository.AmetysObjectIterable;
023import org.ametys.plugins.repository.AmetysObjectResolver;
024import org.ametys.plugins.repository.query.SortCriteria;
025import org.ametys.plugins.repository.query.expression.Expression;
026import org.ametys.runtime.model.View;
027
028/**
029 * This interface represents a filter for {@link Content}
030 */
031public interface ContentFilter
032{
033    /**
034     * The context language
035     */
036    public enum ContextLanguage 
037    {
038        /** Constant to search only in the current sitemap */
039        CURRENT
040        {
041            @Override
042            public String toString ()
043            {
044                return "current";
045            }
046        },
047        /** Constant to search in all sitemap */
048        ALL
049        {
050            @Override
051            public String toString ()
052            {
053                return "all";
054            }
055        },
056        /** Constant to search in other sitemaps */
057        OTHERS
058        {
059            @Override
060            public String toString ()
061            {
062                return "others";
063            }
064        }
065    }
066    
067    /** The condition. */
068    public enum Condition
069    {
070        /** Or condition */
071        OR,
072        /** And condition. */
073        AND
074    }
075    
076    /**
077     * Get the filter id
078     * @return The filter id
079     */
080    public String getId ();
081    
082    /**
083     * Set the filter id.
084     * @param id The id to set.
085     */
086    public void setId (String id);
087    
088    /**
089     * Get the content types to match contents
090     * @return The content types to match contents
091     */
092    public List<String> getContentTypes ();
093    
094    /**
095     * Add a content type to the filter
096     * @param cTypeId The content type id to add
097     */
098    public void addContentType (String cTypeId);
099    
100    /**
101     * Get the content types to match contents
102     * @return The content types to match contents
103     */
104    public Map<String, String> getMetadataValues ();
105    
106    /**
107     * The metadata condition
108     * @return The metadata condition
109     */
110    public Condition getMetadataCondition ();
111    
112    /**
113     * Add a metadata filter
114     * @param metadataId The metadata id.
115     * @param value The value to test. Set null to test existence
116     */
117    public void addMetadata (String metadataId, String value);
118    
119    /**
120     * Set the metadata condition
121     * @param condition The condition to set
122     */
123    public void setMetadataCondition (Condition condition);
124    
125    /**
126     * Get the current filter expression
127     * @return The expression. Can be null.
128     */
129    public Expression getAdditionalFilterExpression();
130    
131    /**
132     * Set the filter expression that should complete metadata filtering (by doing an AND between both)
133     * @param expression The expression to set. Can be null to remove curren expression
134     */
135    public void setAdditionalFilterExpression (Expression expression);
136    
137
138    /**
139     * Get the context language
140     * @return The context language
141     */
142    public ContextLanguage getContextLanguage ();
143    
144    /**
145     * Set the context language
146     * @param context The context language to set
147     */
148    public void setContextLanguage (ContextLanguage context);
149    
150    /**
151     * Get the name of {@link View} used for matching contents 
152     * @return The name of {@link View} used for matching contents 
153     */
154    public String getView ();
155    
156    /**
157     * Set the filter view
158     * @param viewName The name of {@link View}
159     */
160    public void setView (String viewName);
161    
162    /**
163     * Get the max number of results
164     * @return The max number of results
165     */
166    public int getLength ();
167    
168    /**
169     * Set the max number o results
170     * @param length The max number of results
171     */
172    public void setLength (int length);
173    
174    /**
175     * Get the sort criteria allows to sort query results.
176     * @return the sort criteria
177     */
178    public SortCriteria getSortCriteria ();
179    
180    /**
181     * Add a sort criteria
182     * @param metadataId The metadata id to sort
183     * @param ascending true to ascending sort
184     * @param useLowerCase <code>true</code> for case insensitive order on a string property
185     */
186    public void addSortCriteria (String metadataId, boolean ascending, boolean useLowerCase);
187    
188    /**
189     * Set the ametys object resolver to execute query
190     * @param resolver The ametys object resolver to set
191     */
192    public void setResolver (AmetysObjectResolver resolver);
193    
194    /**
195     * Get the contents matching this filter.
196     * @return The matching contents.
197     */
198    public AmetysObjectIterable<Content> getMatchingContents ();
199    
200    /**
201     * Get the contents matching this filter.
202     * @param lang The current language.
203     * @return The matching contents.
204     */
205    public AmetysObjectIterable<Content> getMatchingContents (String lang);
206    
207}