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.web.frontoffice;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.plugins.repository.metadata.CompositeMetadata;
024import org.ametys.web.repository.page.Page;
025import org.ametys.web.repository.page.ZoneItem;
026import org.ametys.web.service.ServiceParameterGroup;
027import org.ametys.web.service.ServiceParameterOrRepeater;
028import org.ametys.web.service.StaticService;
029
030/**
031 * Service which can be cacheable or not, depending on a parameter.
032 */
033public class SearchService extends StaticService
034{
035    /** The additional parameter for FO search extension point */
036    protected AdditionalParameterFOSearchExtensionPoint _additionalParameterFOSearch;
037    
038    @Override
039    public void service(ServiceManager smanager) throws ServiceException
040    {
041        super.service(smanager);
042        _additionalParameterFOSearch = (AdditionalParameterFOSearchExtensionPoint) smanager.lookup(AdditionalParameterFOSearchExtensionPoint.ROLE);
043    }
044    
045    @Override
046    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
047    {
048        CompositeMetadata serviceParameters = zoneItem.getServiceParameters();
049        
050        if (serviceParameters.getString("search-mode", "").equals("criteria-only"))
051        {
052            String resultsPageId = serviceParameters.getString("page-with-search", "");
053            
054            return !resultsPageId.equals(currentPage.getId());
055        }
056        
057        return false;
058    }
059    
060    @Override
061    protected void configureParameters(Configuration configuration, ServiceParameterOrRepeaterParser serviceParameterParser) throws ConfigurationException
062    {
063        super.configureParameters(configuration, serviceParameterParser);
064        
065        for (String additionalParamExtentionId : _additionalParameterFOSearch.getExtensionsIds())
066        {
067            Configuration paramsConfiguration = _additionalParameterFOSearch.getExtension(additionalParamExtentionId);
068            String pluginName = _additionalParameterFOSearch.getPluginName(additionalParamExtentionId);
069            
070            for (Configuration paramConfiguration : paramsConfiguration.getChildren())
071            {
072                String paramName = paramConfiguration.getName();
073                if (paramName.equals("parameter") || paramName.equals("repeater"))
074                {
075                    String insertAfter = paramConfiguration.getAttribute("insertAfter", null);
076                    ServiceParameterGroup group = _getGroup(insertAfter);
077                    ServiceParameterOrRepeater serviceParameter = serviceParameterParser.parse(_manager, pluginName, paramConfiguration);
078                    
079                    group.insertAfter(serviceParameter, insertAfter);
080                    
081                    // Add the param or repeater to the parameter map as well as to his group.
082                    _parameters.put(serviceParameter.getId(), serviceParameter);
083                }
084            }
085        }
086    }
087    
088    private ServiceParameterGroup _getGroup(String paramName)
089    {
090        if (paramName != null)
091        {
092            for (ServiceParameterGroup group : _groups)
093            {
094                for (ServiceParameterOrRepeater param : group.getParameters())
095                {
096                    if (param.getId().equals(paramName))
097                    {
098                        return group;
099                    }
100                }
101            }
102        }
103        
104        // Get the last group by default
105        return _groups.get(_groups.size() - 1);
106    }
107    
108}