001/*
002 *  Copyright 2012 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.calendar.events;
017
018import java.util.HashSet;
019import java.util.Set;
020
021import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
022import org.ametys.cms.tag.TagProviderExtensionPoint;
023import org.ametys.plugins.repository.AmetysObjectResolver;
024import org.ametys.plugins.repository.query.expression.AndExpression;
025import org.ametys.plugins.repository.query.expression.Expression;
026import org.ametys.plugins.repository.query.expression.Expression.Operator;
027import org.ametys.plugins.repository.query.expression.OrExpression;
028import org.ametys.web.repository.site.SiteManager;
029import org.ametys.web.tags.TagExpression;
030
031/**
032 * Content filter which allows to directly provide:
033 * - a custom metadata expression to filter the contents
034 * - a list of tags to match of only one is required for the content to match ("OR" tags).
035 */
036public class EventsFilter extends org.ametys.web.filter.StaticWebContentFilter
037{
038    /** The list of tags to match. */
039    protected Set<String> _orTags;
040    
041    /** The metadata expression. */
042    protected Expression _metadataExpression;
043    
044    /**
045     * Build an events filter.
046     */
047    public EventsFilter()
048    {
049        super();
050        _orTags = new HashSet<>();
051    }
052    
053    /**
054     * Build an events filter.
055     * @param id The filter id
056     * @param resolver The ametys object resolver
057     * @param contentTypeExtensionPoint The extension point for content types
058     * @param siteManager The site manager
059     * @param tagProviderEP The tag provider
060     */
061    public EventsFilter(String id, AmetysObjectResolver resolver, ContentTypeExtensionPoint contentTypeExtensionPoint, SiteManager siteManager, TagProviderExtensionPoint tagProviderEP)
062    {
063        super(id, resolver, contentTypeExtensionPoint, siteManager, tagProviderEP);
064        _orTags = new HashSet<>();
065    }
066    
067    /**
068     * Build a events filter from copy a another
069     * @param id The filter id
070     * @param originalFilter The filter to copy
071     * @param resolver The ametys object resolver
072     * @param contentTypeExtensionPoint The extension point for content types
073     * @param siteManager The site manager
074     * @param tagProviderEP The tag provider
075     */
076    public EventsFilter(String id, EventsFilter originalFilter, AmetysObjectResolver resolver, ContentTypeExtensionPoint contentTypeExtensionPoint, SiteManager siteManager, TagProviderExtensionPoint tagProviderEP)
077    {
078        super(id, originalFilter, resolver, contentTypeExtensionPoint, siteManager, tagProviderEP);
079        _orTags = new HashSet<>(originalFilter._orTags);
080    }
081    
082    /**
083     * Get the OR tags.
084     * @return the OR tags.
085     */
086    public Set<String> getOrTags()
087    {
088        return _orTags;
089    }
090    
091    /**
092     * Set the OR tags.
093     * @param tags the OR tags to set.
094     */
095    public void setOrTags(Set<String> tags)
096    {
097        _orTags = new HashSet<>(tags);
098    }
099    
100    /**
101     * Add a tag to the OR list.
102     * @param tagName the name of the tag to add.
103     */
104    public void addOrTag(String tagName)
105    {
106        _orTags.add(tagName);
107    }
108    
109    /**
110     * Set the metadata expression.
111     * @param expression the metadata expression.
112     */
113    public void setMetadataExpression(Expression expression)
114    {
115        _metadataExpression = expression;
116    }
117    
118    @Override
119    public Expression getMetadataExpression()
120    {
121        if (_metadataExpression == null)
122        {
123            return super.getMetadataExpression();
124        }
125        else
126        {
127            return _metadataExpression;
128        }
129    }
130    
131    @Override
132    public FilterSearchContext createSeachContext()
133    {
134        return new EventFilterSearchContext(_siteManager);
135    }
136    
137    /**
138     * FilterSearchContext specific to the events filter.
139     */
140    public class EventFilterSearchContext extends DefaultFilterSearchContext
141    {
142        
143        /**
144         * Build an EventFilterSearchContext.
145         * @param siteManager the site manager.
146         */
147        protected EventFilterSearchContext(SiteManager siteManager)
148        {
149            super(siteManager);
150        }
151        
152        /**
153         * Get the expression corresponding to the filter's tags
154         * @param siteName The current site name
155         * @return The expression corresponding to the filter's tags
156         */
157        @Override
158        public Expression getTagsExpression(String siteName)
159        {
160            Expression tagExpr = super.getTagsExpression(siteName);
161            Expression expr = null;
162            
163            if (!_orTags.isEmpty())
164            {
165                Expression[] tagExprArray = new Expression[_orTags.size()];
166                int i = 0;
167                for (String tag : _orTags)
168                {
169                    tagExprArray[i] = new TagExpression(Operator.EQ, tag);
170                    i++;
171                }
172                expr = tagExpr != null ? new AndExpression(tagExpr, new OrExpression(tagExprArray)) : new OrExpression(tagExprArray);
173            }
174            else
175            {
176                expr = tagExpr;
177            }
178            
179            return expr;
180        }
181    }
182    
183    
184}