001/*
002 *  Copyright 2019 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.search.requesttime;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.acting.ServiceableAction;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.SourceResolver;
026
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.web.frontoffice.search.SearchService;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.page.SitemapElement;
031import org.ametys.web.repository.page.ZoneItem;
032import org.ametys.web.service.Service;
033import org.ametys.web.service.ServiceExtensionPoint;
034
035/**
036 * Action to determine if the result search page of the {@link SearchService} instance corresponding to the given {@link ZoneItem} id is cacheable.
037 */
038public class IsSearchResultCacheableAction extends ServiceableAction
039{
040    /** The search {@link Service} */
041    protected SearchService _searchService;
042    
043    /** The resolver for Ametys objects */
044    protected AmetysObjectResolver _ametysObjectResolver;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        ServiceExtensionPoint serviceEP = (ServiceExtensionPoint) smanager.lookup(ServiceExtensionPoint.ROLE);
051        _searchService = (SearchService) serviceEP.getExtension(SearchService.ROLE);
052        _ametysObjectResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
053    }
054    
055    @Override
056    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
057    {
058        String zoneItemId = parameters.getParameter(SearchServiceGenerator.__ZONE_ITEM_GENERATOR_PARAM_NAME);
059        ZoneItem zoneItem = _ametysObjectResolver.resolveById(zoneItemId);
060        SitemapElement currentSitemapElement = zoneItem.getZone().getSitemapElement();
061        return currentSitemapElement instanceof Page currentPage && _searchService.isCacheable(currentPage, zoneItem) ? EMPTY_MAP : null;
062    }
063}
064