001/*
002 *  Copyright 2015 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.plugins.syndication;
017
018import org.apache.avalon.framework.component.Component;
019import org.apache.avalon.framework.service.ServiceException;
020import org.apache.avalon.framework.service.ServiceManager;
021import org.apache.avalon.framework.service.Serviceable;
022import org.apache.commons.lang3.ArrayUtils;
023
024import org.ametys.core.right.RightManager;
025import org.ametys.plugins.repository.metadata.CompositeMetadata;
026import org.ametys.plugins.repository.metadata.CompositeMetadata.MetadataType;
027import org.ametys.web.renderingcontext.RenderingContextHandler;
028import org.ametys.web.repository.page.Page;
029
030
031/**
032 *  Component for access to the user preferences for feeds
033 */
034public class RssFeedUserPrefsComponent implements Component, Serviceable
035{
036    /** Avalon ROLE. */
037    public static final String ROLE = RssFeedUserPrefsComponent.class.getName();
038 
039    /** The right manager */
040    protected RightManager _rightManager;
041    
042    /** The rendering context handler. */
043    protected RenderingContextHandler _renderingContext;
044    
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
048        _renderingContext = (RenderingContextHandler) manager.lookup(RenderingContextHandler.ROLE);
049    }
050    
051    /**
052     * Determines if the preference forms for RSS Feeds must be displayed
053     * @param page The page
054     * @param nbCustomFeeds The number of personal feed
055     * @param nbFeeds The number of feeds
056     * @param maxFeeds The max number of feeds
057     * @return true if the preference forms for RSS Feeds must be displayed
058     */
059    public boolean showPreferenceForm(Page page, int nbCustomFeeds, long nbFeeds, long maxFeeds)
060    {
061        if (page == null)
062        {
063            return false;
064        }
065        
066        return !_rightManager.hasAnonymousReadAccess(page) && _hasCustomsFeeds(nbCustomFeeds, nbFeeds, maxFeeds);
067    }
068
069    /**
070     * Determines if the service for feeds is cacheable
071     * @param repeater The repeater feeds
072     * @param nbCustomFeeds The number of personal feed
073     * @param nbFeeds The number of feeds
074     * @param maxFeeds The max number of feeds
075     * @return true if the service is cacheable
076     */
077    public boolean isServiceCacheable(CompositeMetadata repeater, int nbCustomFeeds, int nbFeeds, int maxFeeds)
078    {
079        for (String name : repeater.getMetadataNames())
080        {
081            if (repeater.getType(name) == MetadataType.COMPOSITE)
082            {
083                CompositeMetadata feed = repeater.getCompositeMetadata(name);
084                boolean limited = feed.getBoolean("limited");
085                String[] foUsers = feed.getStringArray("fo-user", ArrayUtils.EMPTY_STRING_ARRAY);
086                String[] foGroups = feed.getStringArray("fo-group", ArrayUtils.EMPTY_STRING_ARRAY);
087                
088                if (limited || ArrayUtils.isNotEmpty(foUsers) || ArrayUtils.isNotEmpty(foGroups))
089                {
090                    return false;
091                }
092            }
093        }
094        
095        return !_hasCustomsFeeds(nbCustomFeeds, nbFeeds, maxFeeds);
096    }
097    
098    private boolean _hasCustomsFeeds(int nbCustomFeeds, long nbFeeds, long maxFeeds)
099    {
100        if (nbCustomFeeds != 0)
101        {
102            return true;
103        }
104        
105        if (maxFeeds != 0 && maxFeeds < nbFeeds)
106        {
107            return true;
108        }
109        
110        return false;
111    }
112}
113