001/*
002 *  Copyright 2024 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.pagesubscription.observation;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.Observer;
027import org.ametys.plugins.pagesubscription.notification.PageNotificationsHelper;
028import org.ametys.plugins.pagesubscription.notification.TagNotificationsHelper;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.ObservationConstants;
031import org.ametys.plugins.repository.activities.Activity;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033import org.ametys.web.activities.AbstractPageActivityType;
034import org.ametys.web.activities.PageUpdatedActivityType;
035import org.ametys.web.repository.content.WebContent;
036import org.ametys.web.repository.page.Page;
037
038/**
039 * Observer to clear notifications cache
040 */
041public class ClearNotificationsCacheOnPageActivityObserver extends AbstractLogEnabled implements Observer, Serviceable
042{
043    /** The tag notifications helper */
044    protected TagNotificationsHelper _tagNotificationsHelper;
045
046    /** The page notifications helper */
047    protected PageNotificationsHelper _pageNotificationsHelper;
048
049    /** The ametys object resolver */
050    protected AmetysObjectResolver _resolver;
051    
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        _tagNotificationsHelper = (TagNotificationsHelper) manager.lookup(TagNotificationsHelper.ROLE);
056        _pageNotificationsHelper = (PageNotificationsHelper) manager.lookup(PageNotificationsHelper.ROLE);
057        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
058    }
059    
060    @Override
061    public boolean supports(Event event)
062    {
063        if (ObservationConstants.EVENT_ACTIVITY_CREATED.equals(event.getId()))
064        {
065            Activity activity = (Activity) event.getArguments().get(ObservationConstants.ARGS_ACTIVITY);
066            return activity != null && activity.getActivityType() instanceof AbstractPageActivityType;
067        }
068        
069        return false;
070    }
071
072    @Override
073    public int getPriority(Event event)
074    {
075        return MAX_PRIORITY;
076    }
077
078    @Override
079    public void observe(Event event, Map<String, Object> transientVars) throws Exception
080    {
081        Activity activity = (Activity) event.getArguments().get(ObservationConstants.ARGS_ACTIVITY);
082        
083        String pageId = activity.getValue(AbstractPageActivityType.PAGE_ID);
084        Page page = _resolver.resolveById(pageId);
085        _pageNotificationsHelper.clearCache(page.getSiteName(), page.getId());
086        
087        if (activity.getActivityType() instanceof PageUpdatedActivityType)
088        {
089            String contentId = activity.getValue(PageUpdatedActivityType.CONTENT_ID);
090            Content content = _resolver.resolveById(contentId);
091            String siteName = content instanceof WebContent webContent ? webContent.getSiteName() : null;
092            // Clear cache on each site (site = null) if the content is not a web content
093            for (String tagName : content.getTags())
094            {
095                _tagNotificationsHelper.clearCache(siteName, tagName);
096            }
097        }
098    }
099}