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.repository.page.actions;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.ParameterException;
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.acting.ServiceableAction;
026import org.apache.cocoon.environment.ObjectModelHelper;
027import org.apache.cocoon.environment.Redirector;
028import org.apache.cocoon.environment.Request;
029import org.apache.cocoon.environment.SourceResolver;
030import org.apache.commons.lang.StringUtils;
031
032import org.ametys.core.util.JSONUtils;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.WebConstants;
036import org.ametys.web.filter.DefaultPageFilter;
037import org.ametys.web.filter.PageFilter;
038import org.ametys.web.filter.PageFilter.Context;
039import org.ametys.web.repository.page.ZoneItem;
040
041/**
042 * This action creates a filter from the sitemap parameters or request parameter 'filterId' and set the filter in request attributes.
043 *
044 */
045public class SetPageFilterInRequestAttributesAction extends ServiceableAction
046{
047    private AmetysObjectResolver _resolver;
048    private JSONUtils _jsonUtils;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
055        _jsonUtils = (JSONUtils) smanager.lookup(JSONUtils.ROLE);
056    }
057    
058    @Override
059    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
060    {
061        Map<String, String> result = new HashMap<>();
062        
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        
065        @SuppressWarnings("unchecked")
066        Map<String, Object> parentContextAttributes = (Map<String, Object>) objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
067        ZoneItem zi = (ZoneItem) request.getAttribute(WebConstants.REQUEST_ATTR_ZONEITEM);
068        
069        PageFilter filter = _getFilterFromParams(parameters, zi, parentContextAttributes);
070        
071        // Set the filter in request attributes
072        if (filter != null)
073        {
074            request.setAttribute("filter", filter);
075        }
076       
077        return result;
078    }
079    
080    /**
081     * Get the filter from the action parameters.
082     * @param parameters The parameters to filter
083     * @param zoneItem the zone item
084     * @param parentContextAttributes The parent context attributes.
085     * @return the page filter.
086     */
087    protected PageFilter _getFilterFromParams(Parameters parameters, ZoneItem zoneItem, Map<String, Object> parentContextAttributes)
088    {
089        // Creates filter from sitemap parameters
090        PageFilter filter = new DefaultPageFilter(null, _resolver);
091        
092        try
093        {
094            // The filter inputs
095            filter.setLength(parameters.getParameterAsInteger("length", Integer.MAX_VALUE));
096            
097            Map<String, Object> sitesData = _jsonUtils.convertJsonToMap(parameters.getParameter("sites"));
098            String searchContext = (String) sitesData.get("context");
099            Map<String, Object> searchContextMap = _jsonUtils.convertJsonToMap(parameters.getParameter("searchContext"));
100            String subSearchContext = (String) searchContextMap.get("context");
101            
102            if (StringUtils.isEmpty(searchContext)
103                || subSearchContext != null && !Context.CURRENT_SITE.name().equals(subSearchContext))
104            {
105                // Delegate to the search-context enumeration context
106                searchContext = subSearchContext;
107            }
108            
109            if ("CHILD_PAGES".equals(searchContext) || "CHILD_PAGES_OF".equals(searchContext))
110            {
111                filter.setContext(Context.CHILD_PAGES);
112            }
113            else if ("DIRECT_CHILD_PAGES".equals(searchContext) || "DIRECT_CHILD_PAGES_OF".equals(searchContext))
114            {
115                // Direct child pages are child pages context with depth 1.
116                filter.setContext(Context.CHILD_PAGES);
117                filter.setDepth(1);
118            }
119            else
120            {
121                // Else, parse the context.
122                filter.setContext(Context.valueOf(searchContext));
123            }
124
125            if (searchContextMap.get("page") != null)
126            {
127                filter.setPageId((String) searchContextMap.get("page"));
128            }
129            
130            if (!"".equals(parameters.getParameter("contextLang")))
131            {
132                filter.setContextLanguage(PageFilter.ContextLanguage.valueOf(parameters.getParameter("contextLang")));
133            }
134            filter.setTitle(new I18nizableText(parameters.getParameter("service-title")));
135            
136            String[] tags;
137            String tagsStr = parameters.getParameter("tags", null);
138            if (tagsStr == null)
139            {
140                tags = (String[]) parentContextAttributes.get("tags");
141            }
142            else
143            {
144                tags = StringUtils.split(tagsStr, ',');
145            }
146            
147            if (tags.length > 0 && tags[0].length() > 0)
148            {
149                for (String tag : tags)
150                {
151                    if (StringUtils.isNotEmpty(tag))
152                    {
153                        filter.addTag(tag);
154                    }
155                }
156            }
157        }
158        catch (ParameterException e)
159        {
160            getLogger().error("Missing at least one parameter", e);
161        }
162        
163        return filter;
164    }
165}