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                if (insertAfterViewItem != null)
103                {
104                    ViewHelper.insertViewItemAfter(insertAfterViewItem.getParent(), viewItemToInsert, insertAfter.get());
105                    
106                    // Insert the corresponding model item in the right model item container
107                    ModelItem modelItemToInsert = viewItemToInsert.getDefinition();
108                    ModelItemGroup parent = insertAfterViewItem.getDefinition().getParent();
109                    if (parent != null)
110                    {
111                        parent.addChild(modelItemToInsert);
112                    }
113                    else
114                    {
115                        _modelItems.put(modelItemToInsert.getName(), modelItemToInsert);
116                    }
117                }
118                else
119                {
120                    getLogger().warn("For service {}, could not find the item with name '{}' to insert after. The attribute {} will be added at the end of the view.", this.getId(), insertAfter.get(), viewItemToInsert.getName());
121                    _insertItemAtTheEnd(viewItemToInsert);
122                }
123            }
124            catch (UndefinedItemPathException | IllegalArgumentException ex)
125            {
126                throw new ConfigurationException("Could not add the attribute " + viewItemToInsert.getName() + " after " + insertAfter.get(), ex);
127            }
128        }
129        else
130        {
131            _insertItemAtTheEnd(viewItemToInsert);
132        }
133    }
134
135    private void _insertItemAtTheEnd(ModelViewItem viewItemToInsert)
136    {
137        // Insert the item at the end of the last group
138        List<ViewItem> firstLevelGroups = _view.getViewItems();
139        
140        ViewItem lastElement = firstLevelGroups.get(firstLevelGroups.size() - 1);
141        if (lastElement instanceof ViewItemGroup lastGroup)
142        {
143            // There are only groups in the view, so we can cast without checking
144            lastGroup.addViewItem(viewItemToInsert);
145        }
146        else
147        {
148            // There are no groups in the view, just add the item at the end of the view
149            _view.addViewItem(viewItemToInsert);
150        }
151        
152        // Insert the corresponding model item in the service
153        ModelItem itemToInsertDefinition = viewItemToInsert.getDefinition();
154        _modelItems.put(itemToInsertDefinition.getName(), itemToInsertDefinition);
155    }
156}