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.containsKey(ObservationConstants.ARGS_PAGE)) 089 { 090 Object object = args.get(ObservationConstants.ARGS_PAGE); 091 if (object instanceof Page) 092 { 093 if (_odfRootPageHandler.isODFRootPage((Page) object)) 094 { 095 return PolicyResult.REMOVE; 096 } 097 } 098 } 099 } 100 101 return PolicyResult.KEEP; 102 } 103 104 @Override 105 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event) 106 { 107 // Never called because the first-level method never returns NEED_INFORMATION. 108 throw new UnsupportedOperationException("Should never be called."); 109 } 110 111 /** 112 * Returns all event ids for which the cache should be removed. 113 * @param workspace the current JCR workspace. 114 * @return all event ids for which the cache should be removed. 115 */ 116 protected List<String> _getRemovingCacheEventIds(String workspace) 117 { 118 if ("default".equals(workspace)) 119 { 120 return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED, 121 org.ametys.cms.ObservationConstants.EVENT_CONTENT_MODIFIED, 122 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING, 123 OdfWebObservationConstants.ODF_ROOT_UPDATED, 124 OdfWebObservationConstants.ODF_ROOT_DELETED); 125 } 126 else if ("live".equals(workspace)) 127 { 128 return Arrays.asList(org.ametys.cms.ObservationConstants.EVENT_CONTENT_ADDED, 129 org.ametys.cms.ObservationConstants.EVENT_CONTENT_VALIDATED, 130 org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING, 131 OdfWebObservationConstants.ODF_ROOT_UPDATED, 132 OdfWebObservationConstants.ODF_ROOT_DELETED); 133 } 134 135 return Collections.emptyList(); 136 } 137}