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