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