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