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