001/*
002 *  Copyright 2021 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.userdirectory.observation;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
029import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver;
030import org.ametys.plugins.userdirectory.page.UserZoneItem;
031import org.ametys.runtime.plugin.component.AbstractLogEnabled;
032import org.ametys.web.cache.pageelement.PageElementCache;
033import org.ametys.web.repository.page.Page;
034
035/**
036 * Invalidates the ZoneItem for user contents when the root page properties changed
037 */
038public class InvalidateZoneItemCacheOnRootUpdatedObserver extends AbstractLogEnabled implements Observer, Serviceable
039{
040    private UserDirectoryPageHandler _udPageHandler;
041    private UserDirectoryPageResolver _udPageResolver;
042    private PageElementCache _zoneItemCache;
043
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        _udPageHandler = (UserDirectoryPageHandler) smanager.lookup(UserDirectoryPageHandler.ROLE);
047        _zoneItemCache = (PageElementCache) smanager.lookup(PageElementCache.ROLE + "/zoneItem");
048        _udPageResolver = (UserDirectoryPageResolver) smanager.lookup(UserDirectoryPageResolver.ROLE);
049    }
050    
051    @Override
052    public boolean supports(Event event)
053    {
054        return event.getId().equals(ObservationConstants.EVENT_USER_DIRECTORY_ROOT_UPDATED);
055    }
056    
057    public int getPriority(Event event)
058    {
059        // processed just before front-office cache invalidation
060        return MAX_PRIORITY + 3500;
061    }
062    
063    public void observe(Event event, Map<String, Object> transientVars) throws Exception
064    {
065        Page rootPage = (Page) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE);
066        boolean viewUpdated = (Boolean) event.getArguments().get(ObservationConstants.ARGS_USER_CONTENT_VIEW_UPDATED);
067        
068        if (viewUpdated)
069        {
070            // user content view has been changed, clear zone item cache
071            AmetysObjectIterable<Content> userContents = _udPageHandler.getContentsForRootPage(rootPage);
072            for (Content content : userContents)
073            {
074                _removeZoneItemCache(rootPage, content);
075            }
076        }
077    }
078    
079    private void _removeZoneItemCache(Page rootPage, Content content)
080    {
081        String userPageId = _udPageResolver.getUserPageId(rootPage, content);
082        String zoneItemId = UserZoneItem.getZoneItemId(userPageId);
083        
084        // Remove zone item cache for all workspaces
085        _zoneItemCache.removeItem(null, rootPage.getSiteName(), "CONTENT", zoneItemId);
086    }
087
088}