001/*
002 *  Copyright 2020 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.zoneitem;
017
018import java.util.List;
019import java.util.Map;
020import java.util.stream.Collectors;
021
022import org.ametys.core.observation.Event;
023import org.ametys.plugins.repository.AmetysRepositoryException;
024import org.ametys.web.ObservationConstants;
025import org.ametys.web.repository.page.Page;
026import org.ametys.web.repository.page.SitemapElement;
027import org.ametys.web.repository.page.ZoneItem;
028import org.ametys.web.repository.page.ZoneItem.ZoneType;
029
030/**
031 * Invalidates the ZoneItem containing a Content which has just been commented.
032 */
033public class InvalidateZoneItemCacheOnViewParametersModifiedObserver extends AbstractZoneItemCacheOnContentObserver
034{
035    @Override
036    public boolean supports(Event event)
037    {
038        return event.getId().equals(ObservationConstants.EVENT_VIEW_PARAMETERS_MODIFIED);
039    }
040    
041    @Override
042    public void observe(Event event, Map<String, Object> transientVars) throws Exception
043    {
044        Map<String, Object> args = event.getArguments();
045        SitemapElement sitemapElement = (SitemapElement) args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT);
046        
047        try
048        {
049            _removeZoneItemsCache(sitemapElement);
050            
051        }
052        catch (AmetysRepositoryException e)
053        {
054            getLogger().error("Unable to get site name from page '" + sitemapElement.getId() + "' while processing event " + event, e);
055        }
056        
057    }
058
059    /**
060     * Remove zone items cache of the page and page child
061     * @param sitemapElement the page
062     */
063    protected void _removeZoneItemsCache(SitemapElement sitemapElement)
064    {
065        List< ? extends ZoneItem> collect = sitemapElement.getZones()
066            .stream()
067            .flatMap(z -> z.getZoneItems().stream())
068            .collect(Collectors.toList());
069        
070        for (ZoneItem zoneItem : collect)
071        {
072            // Compute the page element type.
073            String pageElementType = zoneItem.getType().equals(ZoneType.CONTENT) ? "CONTENT" : "SERVICE:" + zoneItem.getServiceId();
074            
075            // Remove the item.
076            _zoneItemCache.removeItem(null, sitemapElement.getSiteName(), pageElementType, zoneItem.getId());
077        }
078        
079        for (Page childPage : sitemapElement.getChildrenPages())
080        {
081            _removeZoneItemsCache(childPage);
082        }
083    }
084}