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.plugins.odfweb.cachepolicy;
017
018import java.util.Arrays;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.avalon.framework.service.Serviceable;
028
029import org.ametys.core.observation.Event;
030import org.ametys.odf.orgunit.OrgUnit;
031import org.ametys.odf.program.Program;
032import org.ametys.plugins.odfweb.OdfWebObservationConstants;
033import org.ametys.plugins.odfweb.repository.OdfPageHandler;
034import org.ametys.runtime.plugin.component.AbstractLogEnabled;
035import org.ametys.web.ObservationConstants;
036import org.ametys.web.cache.pageelement.PageElementCachePolicy;
037import org.ametys.web.repository.page.Page;
038import org.ametys.web.repository.site.Site;
039
040/**
041 * Cache policy for the program list service.
042 */
043public class OdfProgramListServiceCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy
044{
045    
046    private static final Set<String> _TYPES = new HashSet<>();
047    static
048    {
049        _TYPES.add("SERVICE:org.ametys.odf.service.ProgramList");
050    }
051    
052    /** The ODF root page handler. */
053    protected OdfPageHandler _odfRootPageHandler;
054    
055    @Override
056    public void service(ServiceManager manager) throws ServiceException
057    {
058        _odfRootPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE);
059    }
060    
061    @Override
062    public Set<String> getPageElementTypes()
063    {
064        return _TYPES;
065    }
066    
067    @Override
068    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event)
069    {
070        String id = event.getId();
071        
072        if (_getRemovingCacheEventIds(workspace).contains(id))
073        {
074            Map<String, Object> args = event.getArguments();
075            
076            if (args.containsKey(org.ametys.cms.ObservationConstants.ARGS_CONTENT))
077            {
078                Object object = args.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT);
079                if (object instanceof Program || object instanceof OrgUnit)
080                {
081                    if (_odfRootPageHandler.hasOdfRootPage(site))
082                    {
083                        return PolicyResult.REMOVE;
084                    }
085                }
086            }
087            
088            if (args.get(ObservationConstants.ARGS_PAGE) instanceof Page page1 && _odfRootPageHandler.isODFRootPage(page1) 
089                || args.get(ObservationConstants.ARGS_SITEMAP_ELEMENT) instanceof Page page2 && _odfRootPageHandler.isODFRootPage(page2))
090            {
091                return PolicyResult.REMOVE;
092            }
093        }
094        
095        return PolicyResult.KEEP;
096    }
097    
098    @Override
099    public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event)
100    {
101        // Never called because the first-level method never returns NEED_INFORMATION.
102        throw new UnsupportedOperationException("Should never be called.");
103    }
104    
105    /**
106     * Returns all event ids for which the cache should be removed.
107     * @param workspace the current JCR workspace.
108     * @return all event ids for which the cache should be removed.
109     */
110    protected List<String> _getRemovingCacheEventIds(String workspace)
111    {
112        if ("default".equals(workspace))
113        {
114            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
115                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_MODIFIED,
116                                 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING,
117                                 OdfWebObservationConstants.ODF_ROOT_UPDATED,
118                                 OdfWebObservationConstants.ODF_ROOT_DELETED);
119        }
120        else if ("live".equals(workspace))
121        {
122            return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED,
123                    org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED,
124                    org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING,
125                    OdfWebObservationConstants.ODF_ROOT_UPDATED,
126                    OdfWebObservationConstants.ODF_ROOT_DELETED);
127        }
128        
129        return Collections.emptyList();
130    }
131}