001/*
002 *  Copyright 2012 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.web.cache.pageelement;
017
018import java.util.List;
019
020import org.ametys.core.observation.Event;
021import org.ametys.web.repository.site.Site;
022
023/**
024 * Base class for simple {@link PageElementCachePolicy} only based on event ids.<br>
025 * Implementations should provide a list of event ids for which the cache should be removed.
026 * Be aware that for all other events, the returned value will be KEEP. 
027 */
028public abstract class AbstractSimplePageElementCachePolicy implements PageElementCachePolicy
029{
030    @Override
031    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event)
032    {
033        if (_supports(event, workspace))
034        {
035            return PolicyResult.REMOVE;
036        }
037        
038        return PolicyResult.KEEP;
039    }
040    
041    /**
042     * Checks if the event is supported. 
043     * @param event The event
044     * @param workspace The JCR workspace.
045     * @return If true, the cache should be clear
046     */
047    protected boolean _supports (Event event, String workspace)
048    {
049        String id = event.getId();
050        return _getRemovingCacheEventIds(workspace).contains(id);
051    }
052    
053    /**
054     * Returns all event ids for which the cache should be removed.
055     * @param workspace the current JCR workspace.
056     * @return all event ids for which the cache should be removed.
057     */
058    protected abstract List<String> _getRemovingCacheEventIds(String workspace);
059
060    /**
061     * Never called because the first-level method never returns NEED_INFORMATION.
062     */
063    @Override
064    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event)
065    {
066        throw new UnsupportedOperationException("Should never be called.");
067    }
068}