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.ObjectModelHelper;
025import org.apache.cocoon.environment.Redirector;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.SourceResolver;
028import org.slf4j.Logger;
029
030import org.ametys.core.util.AvalonLoggerAdapter;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.web.WebConstants;
033import org.ametys.web.repository.page.SitemapElement;
034import org.ametys.web.repository.page.ZoneItem;
035
036/**
037 * Set some request attibutes before {@link SearchServiceGenerator} is processed.
038 */
039public class SetSearchServiceRequestAttributesAction extends ServiceableAction
040{
041    /** The SL4j logger */
042    protected Logger _logger;
043    /** The resolver for Ametys objects */
044    protected AmetysObjectResolver _ametysObjectResolver;
045    
046    @Override
047    public void enableLogging(org.apache.avalon.framework.logger.Logger logger)
048    {
049        super.enableLogging(logger);
050        _logger = new AvalonLoggerAdapter(logger);
051    }
052    
053    @Override
054    public void service(ServiceManager smanager) throws ServiceException
055    {
056        super.service(smanager);
057        _ametysObjectResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
058    }
059    
060    @Override
061    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
062    {
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        ZoneItem zoneItem = _setZoneItem(request, parameters);
065        SitemapElement page = _getPage(zoneItem);
066        _setSite(request, page);
067        _setSkin(request, page);
068        _setSitemap(request, page);
069        return EMPTY_MAP;
070    }
071    
072    private ZoneItem _setZoneItem(Request request, Parameters parameters)
073    {
074        ZoneItem zoneItem = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
075        if (zoneItem == null)
076        {
077            String zoneItemId = parameters.getParameter(SearchServiceGenerator.__ZONE_ITEM_GENERATOR_PARAM_NAME, "");
078            zoneItem = _ametysObjectResolver.resolveById(zoneItemId);
079            request.setAttribute(WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem);
080            _logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_ZONEITEM, zoneItem);
081        }
082        return zoneItem;
083    }
084    
085    private SitemapElement _getPage(ZoneItem zoneItem)
086    {
087        return zoneItem.getZone().getSitemapElement();
088    }
089    
090    private void _setSite(Request request, SitemapElement page)
091    {
092        String siteName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITE_NAME);
093        if (siteName == null)
094        { 
095            siteName = page.getSite().getName();
096            request.setAttribute(WebConstants.REQUEST_ATTR_SITE_NAME, siteName);
097            _logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_SITE_NAME, siteName);
098        }
099    }
100    
101    private void _setSkin(Request request, SitemapElement page)
102    {
103        String skinName = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SKIN_ID);
104        if (skinName == null)
105        {
106            skinName = page.getSite().getSkinId();
107            request.setAttribute(WebConstants.REQUEST_ATTR_SKIN_ID, skinName);
108            _logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_SKIN_ID, skinName);
109        }
110    }
111    
112    private void _setSitemap(Request request, SitemapElement page)
113    {
114        String sitemap = (String) request.getAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME);
115        if (sitemap == null)
116        {
117            sitemap = page.getSitemapName();
118            request.setAttribute(WebConstants.REQUEST_ATTR_SITEMAP_NAME, sitemap);
119            _logger.info("Request attribute '{}' was set to '{}'", WebConstants.REQUEST_ATTR_SITEMAP_NAME, sitemap);
120        }
121    }
122}
123