001/*
002 *  Copyright 2022 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.odfweb.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.core.observation.Event;
025import org.ametys.core.observation.Observer;
026import org.ametys.plugins.odfweb.repository.OdfPageHandler;
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.web.ObservationConstants;
029import org.ametys.web.repository.page.Page;
030
031/**
032 * {@link Observer} for invalidating the odf root cache in case of root page deletion
033 */
034public class InvalidateODFRootPageCacheOnPageDeleteObserver implements Observer, Serviceable
035{
036    
037    private OdfPageHandler _odfPageHandler;
038    private AmetysObjectResolver _resolver;
039
040
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        _odfPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
044        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
045    }
046
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(ObservationConstants.EVENT_PAGE_DELETING);
050    }
051
052    public int getPriority(Event event)
053    {
054        return MAX_PRIORITY + 4000;
055    }
056
057
058    public void observe(Event event, Map<String, Object> transientVars) throws Exception
059    {
060        String pageId = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_ID);
061        Page page = _resolver.resolveById(pageId);
062        if (_odfPageHandler.isODFRootPage(page))
063        {
064            _odfPageHandler.clearRootCache();
065        }
066    }
067
068}