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.wall.observers;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.contenttype.ContentTypesHelper;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.plugins.workspaces.project.ProjectManager;
031import org.ametys.plugins.workspaces.project.modules.WorkspaceModule;
032import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
033import org.ametys.plugins.workspaces.project.objects.Project;
034import org.ametys.plugins.workspaces.wall.WallContentModule;
035import org.ametys.runtime.plugin.component.AbstractLogEnabled;
036import org.ametys.web.cache.pageelement.PageElementCache;
037import org.ametys.web.repository.content.WebContent;
038import org.ametys.web.repository.page.Page;
039import org.ametys.web.repository.page.ZoneItem;
040import org.ametys.web.repository.page.ZoneItem.ZoneType;
041
042/**
043 * {@link Observer} to invalidate zone item cache of wall content service when a wall content is commented
044 *
045 */
046public abstract class AbstractInvalidateZoneItemCacheOnWallObserver extends AbstractLogEnabled implements Observer, Serviceable
047{
048    /** The content type helper. */
049    protected ContentTypesHelper _contentTypeHelper;
050
051    /** The page element cache */
052    protected PageElementCache _zoneItemCache;
053    
054    /** Workspaces project manager */
055    protected ProjectManager _projectManager;
056    
057    /** Workspaces module manager */
058    protected WorkspaceModuleExtensionPoint _moduleManagerEP;
059
060    @Override
061    public void service(ServiceManager manager) throws ServiceException
062    {
063        _contentTypeHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
064        _zoneItemCache = (PageElementCache) manager.lookup(PageElementCache.ROLE + "/zoneItem");
065        _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE);
066        _moduleManagerEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE);
067    }
068    
069    public int getPriority(Event event)
070    {
071        // processed just before front-office cache invalidation
072        return MAX_PRIORITY + 3500;
073    }
074    
075    public void observe(Event event, Map<String, Object> transientVars) throws Exception
076    {
077        Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
078        
079        if (content instanceof WebContent)
080        {
081            String siteName = ((WebContent) content).getSiteName();
082            
083            Optional<Project> optProject = _projectManager.getProjectsForSite(siteName)
084                .stream()
085                .findFirst()
086                .map(p -> _projectManager.getProject(p));
087            
088            if (optProject.isPresent())
089            {
090                Project project = optProject.get();
091                
092                WorkspaceModule module = _moduleManagerEP.getModule(WallContentModule.WALLCONTENT_MODULE_ID);
093                if (module != null && _projectManager.isModuleActivated(project, WallContentModule.WALLCONTENT_MODULE_ID))
094                {
095                    Optional<Page> optPage = _projectManager.getModulePages(project, module)
096                        .stream()
097                        .filter(p -> p.getSitemapName().equals(content.getLanguage()))
098                        .findFirst();
099                    
100                    if (optPage.isPresent())
101                    {
102                        Page page = optPage.get();
103                        Optional< ? extends ZoneItem> optZoneItem = page.getZone("default").getZoneItems()
104                                .stream()
105                                .filter(z -> z.getType() == ZoneType.SERVICE && z.getServiceId().equals(WallContentModule.WALLCONTENT_SERVICE_ID))
106                                .findFirst();
107                        
108                        if (optZoneItem.isPresent())
109                        {
110                            ZoneItem zoneItem = optZoneItem.get();
111                            _zoneItemCache.removeItem(null, siteName, "SERVICE:" + zoneItem.getServiceId(), zoneItem.getId());
112                        }
113                    }
114                }
115                
116            }
117        }
118        
119    }
120
121}