001/*
002 *  Copyright 2023 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.flipbook;
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.explorer.ObservationConstants;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029/**
030 * The observer target the file created by {@link ConvertResource2ImagesComponent} and {@link ConvertExternalResource2ImagesComponent} and {@link ConvertContentAttachment2ImagesComponent}
031 * 
032 * A flipbook resource can be stored in the site resource, the shared resource or an attachment
033 */
034public class CleanFlipbookCacheOnResourceDeletedObserver extends AbstractLogEnabled implements Observer, Serviceable
035{
036    private ConvertResource2ImagesComponent _resourceImageComponent;
037    private ConvertContentAttachment2ImagesComponent _attachmentImageComponent;
038    
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        _resourceImageComponent = (ConvertResource2ImagesComponent) manager.lookup(ConvertResource2ImagesComponent.ROLE);
042        _attachmentImageComponent = (ConvertContentAttachment2ImagesComponent) manager.lookup(ConvertContentAttachment2ImagesComponent.ROLE);
043    }
044    
045    public boolean supports(Event event)
046    {
047        // handle the deletion of a resource or a collection containing a resource
048        return ObservationConstants.EVENT_RESOURCE_DELETED.equals(event.getId())
049            || ObservationConstants.EVENT_COLLECTION_DELETED.equals(event.getId())
050            || ObservationConstants.EVENT_RESOURCE_MOVED.equals(event.getId())
051            || ObservationConstants.EVENT_COLLECTION_MOVED.equals(event.getId())
052            || ObservationConstants.EVENT_RESOURCE_RENAMED.equals(event.getId())
053            || ObservationConstants.EVENT_COLLECTION_RENAMED.equals(event.getId());
054    }
055
056    public int getPriority(Event event)
057    {
058        return Observer.MIN_PRIORITY;
059    }
060
061    public void observe(Event event, Map<String, Object> transientVars) throws Exception
062    {
063        Map<String, Object> args = event.getArguments();
064        String path = (String) args.get(ObservationConstants.ARGS_PATH);
065        String resourcePath = (String) args.get(ObservationConstants.ARGS_RESOURCE_PATH);
066        if (resourcePath == null)
067        {
068            resourcePath = (String) args.get(ObservationConstants.ARGS_EXPLORER_PATH);
069        }
070        if (ObservationConstants.EVENT_RESOURCE_MOVED.equals(event.getId())
071            || ObservationConstants.EVENT_COLLECTION_MOVED.equals(event.getId())
072            || ObservationConstants.EVENT_RESOURCE_RENAMED.equals(event.getId())
073            || ObservationConstants.EVENT_COLLECTION_RENAMED.equals(event.getId()))
074        {
075            path = (String) args.get("object.old.path");
076            resourcePath = (String) args.get("resource.old.path");
077            if (resourcePath == null)
078            {
079                resourcePath = (String) args.get("explorer.old.path");
080            }
081        }
082        
083        _resourceImageComponent.doCleanCache(path, resourcePath);
084        _attachmentImageComponent.doCleanCache(path);
085    }
086}