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.web.filter; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.configuration.Configurable; 024import org.apache.avalon.framework.configuration.Configuration; 025import org.apache.avalon.framework.configuration.ConfigurationException; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.plugins.repository.AmetysObjectResolver; 031import org.ametys.plugins.repository.RepositoryConstants; 032import org.ametys.plugins.repository.query.SortCriteria; 033import org.ametys.runtime.plugin.component.PluginAware; 034 035/** 036 * This class represents a static filter for pages 037 * 038 */ 039public class StaticPageFilter extends DefaultPageFilter implements Configurable, PluginAware, Serviceable 040{ 041 /** The plugin name */ 042 protected String _pluginName; 043 /** The feature name */ 044 protected String _featureName; 045 046 /** 047 * Empty constructor 048 */ 049 public StaticPageFilter() 050 { 051 // Empty 052 } 053 054 /** 055 * Constructor 056 * @param id The filter id 057 * @param resolver The ametys object resolver 058 */ 059 public StaticPageFilter(String id, AmetysObjectResolver resolver) 060 { 061 super(id, resolver); 062 } 063 064 @Override 065 public void service(ServiceManager smanager) throws ServiceException 066 { 067 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 068 } 069 070 @Override 071 public void setPluginInfo(String pluginName, String featureName, String id) 072 { 073 _pluginName = pluginName; 074 _featureName = featureName; 075 _id = id; 076 } 077 078 @Override 079 public void configure(Configuration configuration) throws ConfigurationException 080 { 081 _tags = _configureTags(configuration.getChild("tags", true)); 082 _length = configuration.getChild("max-result", true).getValueAsInteger(Integer.MAX_VALUE); 083 _metadata = _configureMetadata(configuration.getChild("metadata", true)); 084 _context = _configureContext(configuration.getChild("context", true)); 085 _contextLang = _configureContextLanguage(configuration.getChild("context", true)); 086 _depth = _configureDepth(configuration.getChild("context", true)); 087 _sortCriteria = _configureSortCriteria(configuration.getChild("sort-information")); 088 } 089 090 /** 091 * Configure the tag keys 092 * @param configuration The tags configuration 093 * @return The set of tag keys 094 * @throws ConfigurationException If an error occurs 095 */ 096 protected List<String> _configureTags(Configuration configuration) throws ConfigurationException 097 { 098 List<String> tags = new ArrayList<>(); 099 for (Configuration tag : configuration.getChildren("tag")) 100 { 101 tags.add(tag.getAttribute("key")); 102 } 103 return tags; 104 } 105 106 /** 107 * Configure the metadata 108 * @param configuration The metadata configuration 109 * @return The metadata to filter by 110 * @throws ConfigurationException If an error occurs 111 */ 112 protected Map<String, String> _configureMetadata(Configuration configuration) throws ConfigurationException 113 { 114 Map<String, String> metadata = new HashMap<>(); 115 for (Configuration elt : configuration.getChildren("metadata")) 116 { 117 metadata.put(elt.getAttribute("id"), elt.getValue("")); 118 } 119 return metadata; 120 } 121 122 /** 123 * Configure the sort criteria 124 * @param configuration The sort criteria configuration 125 * @return The sort criteria 126 * @throws ConfigurationException If an error occurs 127 */ 128 protected SortCriteria _configureSortCriteria(Configuration configuration) throws ConfigurationException 129 { 130 SortCriteria sortCriteria = null; 131 if (configuration != null) 132 { 133 sortCriteria = new SortCriteria(); 134 for (Configuration sort : configuration.getChildren("sort")) 135 { 136 String metadataId = sort.getAttribute("metadataId"); 137 if ("title".equals(metadataId)) 138 { 139 sortCriteria.addJCRPropertyCriterion(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":" + metadataId, sort.getAttributeAsBoolean("ascending", false), sort.getAttributeAsBoolean("lower-case", false)); 140 } 141 else 142 { 143 sortCriteria.addCriterion(metadataId, sort.getAttributeAsBoolean("ascending", false), sort.getAttributeAsBoolean("lower-case", false)); 144 } 145 146 } 147 } 148 return sortCriteria; 149 } 150 151 /** 152 * Configure the context search 153 * @param configuration The context configuration 154 * @return The search context 155 * @throws ConfigurationException If an error occurs 156 */ 157 protected Context _configureContext (Configuration configuration) throws ConfigurationException 158 { 159 String context = configuration.getAttribute("type", "current-site"); 160 if (context.equals(Context.OTHER_SITES.toString())) 161 { 162 return Context.OTHER_SITES; 163 } 164 else if (context.equals(Context.SITES.toString())) 165 { 166 return Context.SITES; 167 } 168 else if (context.equals(Context.CHILD_PAGES.toString())) 169 { 170 return Context.CHILD_PAGES; 171 } 172 173 return Context.CURRENT_SITE; 174 } 175 176 /** 177 * Configure the context language 178 * @param configuration The context configuration 179 * @return The context language 180 * @throws ConfigurationException If an error occurs 181 */ 182 protected ContextLanguage _configureContextLanguage (Configuration configuration) throws ConfigurationException 183 { 184 String context = configuration.getAttribute("lang", "current"); 185 if (context.equals(ContextLanguage.OTHERS.toString())) 186 { 187 return ContextLanguage.OTHERS; 188 } 189 else if (context.equals(ContextLanguage.ALL.toString())) 190 { 191 return ContextLanguage.ALL; 192 } 193 194 return ContextLanguage.CURRENT; 195 } 196 197 /** 198 * Configure the depth search 199 * @param configuration The depth configuration 200 * @return The depth 201 * @throws ConfigurationException If an error occurs 202 */ 203 protected int _configureDepth (Configuration configuration) throws ConfigurationException 204 { 205 return configuration.getAttributeAsInteger("depth", 0); 206 } 207 208}