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.workspaces.members.observers;
017
018import java.util.List;
019import java.util.Set;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
029import org.ametys.plugins.workspaces.WorkspacesConstants;
030import org.ametys.web.cache.CacheInvalidationPolicy;
031import org.ametys.web.repository.page.Page;
032
033/**
034 * {@link Observer} to invalidate cache on front-office of a user content when a member was added or removed from a project
035 *
036 */
037public class InvalidateCacheOnMemberUpdatedObserver extends AbstractMemberObserver
038{
039    private CacheInvalidationPolicy _cachePolicy;
040    private UserDirectoryPageHandler _udPageHandler;
041    
042    @Override
043    public void service(ServiceManager smanager) throws ServiceException
044    {
045        super.service(smanager);
046        _cachePolicy = (CacheInvalidationPolicy) smanager.lookup(CacheInvalidationPolicy.class.getName());
047        _udPageHandler = (UserDirectoryPageHandler) smanager.lookup(UserDirectoryPageHandler.ROLE);
048    }
049    
050    @Override
051    public int getPriority()
052    {
053        // Will be processed after indexation
054        return MAX_PRIORITY + 4000;
055    }
056
057    @Override
058    protected void _internalObserve(Event event, Set<UserIdentity> users) throws Exception
059    {
060        List<Content> userContents = getUserContents(users);
061        Set<Page> rootPages = _udPageHandler.getUserDirectoryRootPages(WorkspacesConstants.MEMBER_CONTENT_TYPE_ID);
062        
063        for (Content userContent : userContents)
064        {
065            for (Page rootPage : rootPages)
066            {
067                try
068                {
069                    _cachePolicy.invalidateCacheOnContentModification(rootPage.getSite(), userContent);
070                }
071                catch (Exception e)
072                {
073                    getLogger().error("Unable to invalidate cache for user content " + userContent, e);
074                }
075            }
076        }
077    }
078}