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.userdirectory.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.repository.AmetysObjectIterable; 028import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 029import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver; 030import org.ametys.plugins.userdirectory.page.UserPage; 031import org.ametys.runtime.plugin.component.AbstractLogEnabled; 032import org.ametys.web.cache.pageelement.PageElementCache; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.repository.page.Zone; 035import org.ametys.web.repository.page.ZoneItem; 036import org.ametys.web.repository.page.ZoneItem.ZoneType; 037 038/** 039 * Invalidates the ZoneItem for user contents when the root page properties changed 040 */ 041public class InvalidateZoneItemCacheOnRootUpdatedObserver extends AbstractLogEnabled implements Observer, Serviceable 042{ 043 private UserDirectoryPageHandler _udPageHandler; 044 private UserDirectoryPageResolver _udPageResolver; 045 private PageElementCache _zoneItemCache; 046 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 _udPageHandler = (UserDirectoryPageHandler) smanager.lookup(UserDirectoryPageHandler.ROLE); 050 _zoneItemCache = (PageElementCache) smanager.lookup(PageElementCache.ROLE + "/zoneItem"); 051 _udPageResolver = (UserDirectoryPageResolver) smanager.lookup(UserDirectoryPageResolver.ROLE); 052 } 053 054 @Override 055 public boolean supports(Event event) 056 { 057 return event.getId().equals(ObservationConstants.EVENT_USER_DIRECTORY_ROOT_UPDATED); 058 } 059 060 public int getPriority(Event event) 061 { 062 // processed just before front-office cache invalidation 063 return MAX_PRIORITY + 3500; 064 } 065 066 public void observe(Event event, Map<String, Object> transientVars) throws Exception 067 { 068 Page rootPage = (Page) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_PAGE); 069 boolean viewUpdated = (Boolean) event.getArguments().get(ObservationConstants.ARGS_USER_CONTENT_VIEW_UPDATED); 070 071 if (viewUpdated) 072 { 073 // user content view has been changed, clear zone item cache 074 AmetysObjectIterable<Content> userContents = _udPageHandler.getContentsForRootPage(rootPage); 075 for (Content content : userContents) 076 { 077 _removeZoneItemCache(rootPage, content); 078 } 079 } 080 } 081 082 private void _removeZoneItemCache(Page rootPage, Content content) 083 { 084 UserPage userPage = _udPageResolver.getUserPage(rootPage, content); 085 if (userPage != null) 086 { 087 AmetysObjectIterable< ? extends Zone> zones = userPage.getZones(); 088 for (Zone zone : zones) 089 { 090 for (ZoneItem zoneItem : zone.getZoneItems()) 091 { 092 if (zoneItem.getType().equals(ZoneType.CONTENT)) 093 { 094 // Remove zone item cache for all workspaces 095 _zoneItemCache.removeItem(null, rootPage.getSiteName(), "CONTENT", zoneItem.getId()); 096 } 097 } 098 } 099 } 100 } 101 102}