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;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
026import org.ametys.runtime.model.ModelItem;
027import org.ametys.runtime.model.ModelViewItem;
028import org.ametys.runtime.model.ViewItem;
029import org.ametys.runtime.model.ViewItemContainer;
030import org.ametys.runtime.model.ViewItemGroup;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.repository.page.ZoneItem;
033import org.ametys.web.service.StaticService;
034
035/**
036 * Service which can be cacheable or not, depending on a parameter.
037 */
038public class SearchPagesService extends StaticService
039{
040    /** The additional parameter for FO search extension point */
041    protected AdditionalParameterFOSearchExtensionPoint _additionalParameterFOSearch;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _additionalParameterFOSearch = (AdditionalParameterFOSearchExtensionPoint) smanager.lookup(AdditionalParameterFOSearchExtensionPoint.ROLE);
048    }
049    
050    @Override
051    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
052    {
053        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
054        
055        if (serviceParameters.getValue("search-mode", false, "").equals("criteria-only"))
056        {
057            String resultsPageId = serviceParameters.getValue("page-with-search", false, "");
058            
059            return !resultsPageId.equals(currentPage.getId());
060        }
061        
062        return false;
063    }
064    
065    @Override
066    protected void configureParameters(Configuration parametersConfiguration) throws ConfigurationException
067    {
068        super.configureParameters(parametersConfiguration);
069        
070        for (String additionalParamExtentionId : _additionalParameterFOSearch.getExtensionsIds())
071        {
072            Configuration paramsConfiguration = _additionalParameterFOSearch.getExtension(additionalParamExtentionId);
073            String pluginName = _additionalParameterFOSearch.getPluginName(additionalParamExtentionId);
074            
075            for (Configuration paramConfiguration : paramsConfiguration.getChildren())
076            {
077                // Parse the item to insert
078                String paramName = paramConfiguration.getName();
079                ModelViewItem itemToInsert = paramName.equals("repeater") ? _parseRepeater(paramConfiguration, null, pluginName) : _parseParameter(paramConfiguration, null, pluginName);
080
081                if (itemToInsert != null)
082                {
083                    String insertAfter = paramConfiguration.getAttribute("insertAfter", null);
084                    
085                    // Add the item to model items if it has no parent in the model ( unlike a parameter in a repeater for example)
086                    if (!_hasParentModelGroup(insertAfter))
087                    {
088                        _modelItems.put(itemToInsert.getDefinition().getName(), itemToInsert.getDefinition());
089                    }
090                    
091                    // Insert the item after the item with the given name
092                    _insertViewItemAfter(itemToInsert, insertAfter);
093                }
094            }
095        }
096    }
097    
098    private boolean _hasParentModelGroup(String paramName)
099    {
100        if (paramName != null)
101        {
102            ModelItem modelItem = getModelItem(paramName);
103            if (modelItem != null)
104            {
105                return modelItem.getParent() != null;
106            }
107        }
108        
109        return false;
110    }
111    
112    private void _insertViewItemAfter(ViewItem itemToInsert, String insertAfter)
113    {
114        boolean inserted = false;
115        
116        // Try to insert the item after the item with the given name
117        if (insertAfter != null)
118        {
119            inserted = _insertViewItemAfter(itemToInsert, insertAfter, _view);
120        }
121        
122        if (!inserted)
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(itemToInsert);
131        }
132    }
133    
134    private boolean _insertViewItemAfter(ViewItem itemToInsert, String paramName, ViewItemContainer currentContainer)
135    {
136        int indexInGroup = 0;
137        for (ViewItem viewItem : currentContainer.getViewItems())
138        {
139            if (viewItem instanceof ModelViewItem && paramName.equals(((ModelViewItem) viewItem).getDefinition().getName()))
140            {
141                currentContainer.insertViewItem(itemToInsert, indexInGroup + 1);
142                return true;
143            }
144            
145            if (viewItem instanceof ViewItemGroup)
146            {
147                boolean inserted = _insertViewItemAfter(itemToInsert, paramName, (ViewItemGroup) viewItem);
148                if (inserted)
149                {
150                    return true;
151                }
152            }
153            
154            indexInGroup++;
155        }
156        
157        return false;
158    }
159    
160}