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.context.Context;
021import org.apache.avalon.framework.context.ContextException;
022import org.apache.avalon.framework.context.Contextualizable;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.cocoon.components.ContextHelper;
027import org.apache.cocoon.environment.Request;
028
029import org.ametys.cms.ObservationConstants;
030import org.ametys.cms.repository.Content;
031import org.ametys.core.observation.Event;
032import org.ametys.core.observation.Observer;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035import org.ametys.web.WebHelper;
036
037/**
038 * Remove the file stored by the flipbook cache in ametys home when a content is deleted or modified.
039 * The observer target the file created by {@link ConvertContentAttachment2ImagesComponent} and {@link ConvertMetadata2ImagesComponent}.
040 * 
041 * In case of deletion, the cache could store outdated attachments or metadata.
042 * In case of modification, the cache could store outdated metadata.
043 * The modification of the attachment is handled by {@link CleanFlipbookCacheOnResourceDeletedObserver}
044 */
045public class CleanFlipbookCacheOnContentDeletedObserver extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable
046{
047
048    private AmetysObjectResolver _resolver;
049    private ConvertMetadata2ImagesComponent _attributeImageComponent;
050    private Context _context;
051    private ConvertContentAttachment2ImagesComponent _attachmentImageComponent;
052
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
056        _attributeImageComponent = (ConvertMetadata2ImagesComponent) manager.lookup(ConvertMetadata2ImagesComponent.ROLE);
057        _attachmentImageComponent = (ConvertContentAttachment2ImagesComponent) manager.lookup(ConvertContentAttachment2ImagesComponent.ROLE);
058    }
059    
060    public void contextualize(Context context) throws ContextException
061    {
062        _context = context;
063    }
064
065    public boolean supports(Event event)
066    {
067        return ObservationConstants.EVENT_CONTENT_DELETING.equals(event.getId())
068            || ObservationConstants.EVENT_CONTENT_MODIFIED.equals(event.getId());
069    }
070
071    public int getPriority(Event event)
072    {
073        return Observer.MIN_PRIORITY;
074    }
075
076    public void observe(Event event, Map<String, Object> transientVars) throws Exception
077    {
078        Map<String, Object> arguments = event.getArguments();
079        String contentId = (String) arguments.get(ObservationConstants.ARGS_CONTENT_ID);
080        Content content = _resolver.resolveById(contentId);
081        
082        Request request = ContextHelper.getRequest(_context);
083        String siteName = WebHelper.getSiteName(request, content);
084        
085        _attributeImageComponent.doCleanCache(content, siteName);
086        
087        // only delete the attachment cache if the content was deleted
088        if (ObservationConstants.EVENT_CONTENT_DELETING.equals(event.getId()))
089        {
090            _attachmentImageComponent.doCleanCache(content, siteName);
091        }
092    }
093
094}