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