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 java.util.List;
019import java.util.Optional;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.ConfigurationException;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
027import org.ametys.runtime.model.ModelItem;
028import org.ametys.runtime.model.ModelItemGroup;
029import org.ametys.runtime.model.ModelViewItem;
030import org.ametys.runtime.model.ViewHelper;
031import org.ametys.runtime.model.ViewItem;
032import org.ametys.runtime.model.ViewItemGroup;
033import org.ametys.runtime.model.exception.UndefinedItemPathException;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.page.ZoneItem;
036import org.ametys.web.service.StaticService;
037
038/**
039 * Service which can be cacheable or not, depending on a parameter.
040 */
041public class SearchPagesService extends StaticService
042{
043    /** The additional parameter for FO search extension point */
044    protected AdditionalParameterFOSearchExtensionPoint _additionalParameterFOSearch;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _additionalParameterFOSearch = (AdditionalParameterFOSearchExtensionPoint) smanager.lookup(AdditionalParameterFOSearchExtensionPoint.ROLE);
051    }
052    
053    @Override
054    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
055    {
056        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
057        
058        if (serviceParameters.getValue("search-mode", false, "").equals("criteria-only"))
059        {
060            String resultsPageId = serviceParameters.getValue("page-with-search", false, "");
061            
062            return !resultsPageId.equals(currentPage.getId());
063        }
064        
065        return false;
066    }
067    
068    @Override
069    protected void configureParameters(Configuration parametersConfiguration) throws ConfigurationException
070    {
071        super.configureParameters(parametersConfiguration);
072        
073        for (String additionalParamExtentionId : _additionalParameterFOSearch.getExtensionsIds())
074        {
075            Configuration paramsConfiguration = _additionalParameterFOSearch.getExtension(additionalParamExtentionId);
076            String pluginName = _additionalParameterFOSearch.getPluginName(additionalParamExtentionId);
077            
078            for (Configuration paramConfiguration : paramsConfiguration.getChildren())
079            {
080                // Parse the item to insert
081                String paramName = paramConfiguration.getName();
082                ModelViewItem itemToInsert = paramName.equals("repeater") ? _viewAndParametersParser.parseRepeater(paramConfiguration, null, pluginName, "plugin." + pluginName, _view, _modelItems, this, _serviceParameterDefinitionParser, _repeaterDefinitionParser) : _viewAndParametersParser.parseParameter(paramConfiguration, null, pluginName, "plugin." + pluginName, _view, _modelItems, this, _serviceParameterDefinitionParser, _repeaterDefinitionParser);
083
084                if (itemToInsert != null)
085                {
086                    String insertAfter = paramConfiguration.getAttribute("insertAfter", null);
087                    
088                    // Insert the item after the item with the given name
089                    _insertItemAfter(itemToInsert, Optional.ofNullable(insertAfter));
090                }
091            }
092        }
093    }
094    
095    private void _insertItemAfter(ModelViewItem viewItemToInsert, Optional<String> insertAfter) throws ConfigurationException
096    {
097        if (insertAfter.isPresent())
098        {
099            try
100            {
101                ModelViewItem insertAfterViewItem = _view.getModelViewItem(insertAfter.get());
102                ViewHelper.insertViewItemAfter(insertAfterViewItem.getParent(), viewItemToInsert, insertAfter.get());
103                
104                // Insert the corresponding model item in the right model item container
105                ModelItem modelItemToInsert = viewItemToInsert.getDefinition();
106                ModelItemGroup parent = insertAfterViewItem.getDefinition().getParent();
107                if (parent != null)
108                {
109                    parent.addChild(modelItemToInsert);
110                }
111                else
112                {
113                    _modelItems.put(modelItemToInsert.getName(), modelItemToInsert);
114                }
115            }
116            catch (UndefinedItemPathException | IllegalArgumentException ex)
117            {
118                throw new ConfigurationException("Could not add the attribute " + viewItemToInsert.getName() + " after " + insertAfter.get(), ex);
119            }
120           
121        }
122        else
123        {
124           // Insert the item at the end of the last group
125            List<ViewItem> firstLevelGroups = _view.getViewItems();
126            
127            // There are only groups in the view, so we can cast without checking
128            ViewItemGroup lastGroup = (ViewItemGroup) firstLevelGroups.get(firstLevelGroups.size() - 1);
129            
130            lastGroup.addViewItem(viewItemToInsert);
131            
132            // Insert the corresponding model item in the service
133            ModelItem itemToInsertDefinition = viewItemToInsert.getDefinition();
134            _modelItems.put(itemToInsertDefinition.getName(), itemToInsertDefinition);
135        }
136    }
137}