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.AmetysObjectIterable;
028import org.ametys.plugins.repository.jcr.JCRAmetysObject;
029import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
030import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver;
031import org.ametys.plugins.userdirectory.page.UserPage;
032import org.ametys.plugins.workspaces.WorkspacesConstants;
033import org.ametys.web.cache.pageelement.PageElementCache;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.page.Zone;
036import org.ametys.web.repository.page.ZoneItem;
037import org.ametys.web.repository.page.ZoneItem.ZoneType;
038
039/**
040 * {@link Observer} to invalidate the zoneitem containing the member' content when a member was added or removed from a project
041 *
042 */
043public class InvalidateZoneItemCacheOnMemberUpdatedObserver extends AbstractMemberObserver
044{
045    private PageElementCache _zoneItemCache;
046    private UserDirectoryPageHandler _udPageHandler;
047    private UserDirectoryPageResolver _udPageResolver;
048
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(smanager);
053        _zoneItemCache = (PageElementCache) smanager.lookup(PageElementCache.ROLE + "/zoneItem");
054        _udPageHandler = (UserDirectoryPageHandler) smanager.lookup(UserDirectoryPageHandler.ROLE);
055        _udPageResolver = (UserDirectoryPageResolver) smanager.lookup(UserDirectoryPageResolver.ROLE);
056    }
057    
058    @Override
059    protected void _internalObserve(Event event, List<Content> userContents) throws Exception
060    {
061        for (Content content : userContents)
062        {
063            if (content instanceof JCRAmetysObject)
064            {
065                Set<Page> rootPages = _udPageHandler.getUserDirectoryRootPages(WorkspacesConstants.MEMBER_CONTENT_TYPE_ID);
066                
067                for (Page rootPage : rootPages)
068                {
069                    UserPage userPage = _udPageResolver.getUserPage(rootPage, content);
070                    if (userPage != null)
071                    {
072                        // Get the zones of the page
073                        AmetysObjectIterable< ? extends Zone> zones = userPage.getZones();
074                        for (Zone zone : zones)
075                        {
076                            // For every zone item of every zone
077                            for (ZoneItem zoneItem : zone.getZoneItems())
078                            {
079                                if (zoneItem.getType().equals(ZoneType.CONTENT)) 
080                                {
081                                    // Invalidate cache for all workspaces
082                                    _zoneItemCache.removeItem(null, rootPage.getSiteName(), "CONTENT", zoneItem.getId());
083                                }
084                            }
085                        }
086                    }
087                    
088                }
089            }
090        }
091    }
092}