001/*
002 *  Copyright 2016 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;
017
018import java.util.Collection;
019import java.util.Map;
020
021import org.ametys.core.observation.Event;
022import org.ametys.core.observation.Observer;
023import org.ametys.core.right.RightManager;
024import org.ametys.web.repository.page.Page;
025import org.ametys.web.repository.page.PagesContainer;
026import org.ametys.web.repository.sitemap.Sitemap;
027
028/**
029 * {@link Observer} for observing page or sitemap ACL update in order to
030 * invalidate cache on front-office.
031 */
032public class InvalidateCacheOnSitemapACLUpdateObserver extends AbstractCacheObserver
033{
034    @Override
035    public boolean supports(Event event)
036    {
037        return event.getId().equals(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED);
038    }
039    
040    public void observe(Event event, Map<String, Object> transientVars) throws Exception
041    {
042        PagesContainer pageCt = _getPageContainer (event);
043        
044        if (pageCt != null)
045        {
046            if (pageCt instanceof Sitemap)
047            {
048                // Invalidate the whole sitemap
049                CacheHelper.invalidateCache((Sitemap) pageCt, getLogger());
050            }
051            else if (pageCt instanceof Page)
052            {
053                // Invalidate the whole sitemap (menu can changed)
054                CacheHelper.invalidateCache(((Page) pageCt).getSitemap(), getLogger());
055            }
056        }
057    }
058    
059    private PagesContainer _getPageContainer (Event event)
060    {
061        Map<String, Object> args = event.getArguments();
062        Object aclContext = args.get(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT);
063        @SuppressWarnings("unchecked")
064        Collection<String> profileIds = (Collection<String>) args.get(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES);
065        
066        // Observer event only if the ACL context is a page and the reading profile has been updated
067        return aclContext instanceof PagesContainer &&  profileIds.contains(RightManager.READER_PROFILE_ID) ? (PagesContainer) aclContext : null;
068    }
069
070    
071}