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 public int getPriority() 073 { 074 return MAX_PRIORITY; 075 } 076 077 @Override 078 public void observe(Event event, Map<String, Object> transientVars) throws Exception 079 { 080 Activity activity = (Activity) event.getArguments().get(ObservationConstants.ARGS_ACTIVITY); 081 082 String pageId = activity.getValue(AbstractPageActivityType.PAGE_ID); 083 Page page = _resolver.resolveById(pageId); 084 _pageNotificationsHelper.clearCache(page.getSiteName(), page.getId()); 085 086 if (activity.getActivityType() instanceof PageUpdatedActivityType) 087 { 088 String contentId = activity.getValue(PageUpdatedActivityType.CONTENT_ID); 089 Content content = _resolver.resolveById(contentId); 090 String siteName = content instanceof WebContent webContent ? webContent.getSiteName() : null; 091 // Clear cache on each site (site = null) if the content is not a web content 092 for (String tagName : content.getTags()) 093 { 094 _tagNotificationsHelper.clearCache(siteName, tagName); 095 } 096 } 097 } 098}