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.plugins.repository.jcr.JCRAmetysObject;
028import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
029import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver;
030import org.ametys.plugins.userdirectory.page.UserZoneItem;
031import org.ametys.plugins.workspaces.WorkspacesConstants;
032import org.ametys.web.cache.pageelement.PageElementCache;
033import org.ametys.web.repository.page.Page;
034
035/**
036 * {@link Observer} to invalidate the zoneitem containing the member' content when a member was added or removed from a project
037 *
038 */
039public class InvalidateZoneItemCacheOnMemberUpdatedObserver extends AbstractMemberObserver
040{
041    private PageElementCache _zoneItemCache;
042    private UserDirectoryPageHandler _udPageHandler;
043    private UserDirectoryPageResolver _udPageResolver;
044
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _zoneItemCache = (PageElementCache) smanager.lookup(PageElementCache.ROLE + "/zoneItem");
050        _udPageHandler = (UserDirectoryPageHandler) smanager.lookup(UserDirectoryPageHandler.ROLE);
051        _udPageResolver = (UserDirectoryPageResolver) smanager.lookup(UserDirectoryPageResolver.ROLE);
052    }
053    
054    @Override
055    protected void _internalObserve(Event event, List<Content> userContents) throws Exception
056    {
057        for (Content content : userContents)
058        {
059            if (content instanceof JCRAmetysObject)
060            {
061                Set<Page> rootPages = _udPageHandler.getUserDirectoryRootPages(WorkspacesConstants.MEMBER_CONTENT_TYPE_ID);
062                
063                for (Page rootPage : rootPages)
064                {
065                    String userPageId = _udPageResolver.getUserPageId(rootPage, content);
066                    if (userPageId != null)
067                    {
068                        String zoneItemId = UserZoneItem.getZoneItemId(userPageId);
069                        // Invalidate cache for all workspaces
070                        _zoneItemCache.removeItem(null, rootPage.getSiteName(), "CONTENT", zoneItemId);
071                    }
072                    
073                }
074            }
075        }
076    }
077}