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.cache.service;
017
018import java.util.Arrays;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.right.RightManager;
027import org.ametys.web.ObservationConstants;
028import org.ametys.web.cache.pageelement.AbstractACLPageElementCachePolicy;
029import org.ametys.web.cache.pageelement.PageElementCachePolicy;
030import org.ametys.web.repository.SiteAwareAmetysObject;
031
032/**
033 * {@link PageElementCachePolicy} for the Sitemap service.<br>
034 * This implementation does not handle virtual pages based on Contents, so that it does not remove cache when Content change.
035 */
036public class SitemapServiceCachePolicy extends AbstractACLPageElementCachePolicy
037{
038    
039    /** The sitemap service ID. */
040    public static final String SERVICE_SITEMAP_ID = "org.ametys.web.service.SitemapService";
041    
042    @Override
043    public Set<String> getPageElementTypes()
044    {
045        return Collections.singleton("SERVICE:" + SERVICE_SITEMAP_ID);
046    }
047    
048    @Override
049    protected boolean _supports(Event event, String workspace)
050    {
051        boolean supports = super._supports(event, workspace);
052        
053        if (supports && event.getId().equals(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED))
054        {
055            Map<String, Object> args = event.getArguments();
056            Object object = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
057            @SuppressWarnings("unchecked")
058            Collection<String> profileIds = (Collection<String>) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES);
059            
060            supports = object != null && object instanceof SiteAwareAmetysObject && profileIds.contains(RightManager.READER_PROFILE_ID);
061        }
062        
063        return supports;
064    }
065    
066    @Override
067    protected List<String> _getRemovingCacheEventIds(String workspace)
068    {
069        if ("default".equals(workspace))
070        {
071            return Arrays.asList(ObservationConstants.EVENT_PAGE_ADDED,
072                                 ObservationConstants.EVENT_PAGE_CHANGED,
073                                 ObservationConstants.EVENT_PAGE_DELETED,
074                                 ObservationConstants.EVENT_PAGE_MOVED,
075                                 ObservationConstants.EVENT_PAGE_RENAMED, 
076                                 ObservationConstants.EVENT_PAGE_UPDATED,
077                                 org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
078        }
079        else if ("live".equals(workspace))
080        {
081            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
082                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED,
083                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETED,
084                                 ObservationConstants.EVENT_CONTENT_UNPUBLISHED,
085                                 ObservationConstants.EVENT_ZONEITEM_ADDED,
086                                 ObservationConstants.EVENT_ZONEITEM_DELETED,
087                                 ObservationConstants.EVENT_PAGE_ADDED,
088                                 ObservationConstants.EVENT_PAGE_CHANGED,
089                                 ObservationConstants.EVENT_PAGE_DELETED,
090                                 ObservationConstants.EVENT_PAGE_MOVED,
091                                 ObservationConstants.EVENT_PAGE_RENAMED, 
092                                 ObservationConstants.EVENT_PAGE_UPDATED,
093                                 org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
094        }
095        
096        return Collections.emptyList();
097    }
098    
099}