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.cms.ObservationConstants; 030import org.ametys.core.observation.Event; 031import org.ametys.odf.ProgramItem; 032import org.ametys.odf.coursepart.CoursePart; 033import org.ametys.odf.orgunit.OrgUnit; 034import org.ametys.odf.person.Person; 035import org.ametys.plugins.odfweb.repository.OdfPageHandler; 036import org.ametys.runtime.plugin.component.AbstractLogEnabled; 037import org.ametys.web.cache.pageelement.PageElementCachePolicy; 038import org.ametys.web.inputdata.SitemapInputData; 039import org.ametys.web.repository.site.Site; 040 041/** 042 * Cache policy for the sitemap, handling ODF virtual pages linked with contents. 043 * Used for the Sitemap InputData as well as for the Sitemap service, even if the page element cache is not the same. 044 */ 045public class OdfVirtualPagesCachePolicy extends AbstractLogEnabled implements Serviceable, PageElementCachePolicy 046{ 047 private static final Set<String> _ODF_PE_TYPES = new HashSet<>(); 048 static 049 { 050 // Used for the Sitemap InputData as well as for the Sitemap service. 051 _ODF_PE_TYPES.add(SitemapInputData.class.getName()); 052 _ODF_PE_TYPES.add("SERVICE:org.ametys.web.service.SitemapService"); 053 _ODF_PE_TYPES.add("CONTENT"); 054 } 055 056 /** The ODF root page handler. */ 057 protected OdfPageHandler _odfRootPageHandler; 058 059 @Override 060 public void service(ServiceManager manager) throws ServiceException 061 { 062 _odfRootPageHandler = (OdfPageHandler) manager.lookup(OdfPageHandler.ROLE); 063 } 064 065 @Override 066 public Set<String> getPageElementTypes() 067 { 068 return _ODF_PE_TYPES; 069 } 070 071 @Override 072 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, Event event) 073 { 074 String id = event.getId(); 075 076 if (_getRemovingCacheEventIds(workspace).contains(id)) 077 { 078 Map<String, Object> args = event.getArguments(); 079 080 if (args.containsKey(org.ametys.cms.ObservationConstants.ARGS_CONTENT)) 081 { 082 Object object = args.get(org.ametys.cms.ObservationConstants.ARGS_CONTENT); 083 if (object instanceof ProgramItem || object instanceof CoursePart || object instanceof OrgUnit || object instanceof Person) 084 { 085 if (_odfRootPageHandler.hasOdfRootPage(site)) 086 { 087 return PolicyResult.REMOVE; 088 } 089 } 090 } 091 } 092 093 return PolicyResult.KEEP; 094 } 095 096 @Override 097 public final PolicyResult shouldClearCache(String workspace, Site site, String pageElementType, String elementId, Event event) 098 { 099 // Never called because the first-level method never returns NEED_INFORMATION. 100 throw new UnsupportedOperationException("Should never be called."); 101 } 102 103 /** 104 * Returns all event ids for which the cache should be removed. 105 * @param workspace the current JCR workspace. 106 * @return all event ids for which the cache should be removed. 107 */ 108 protected List<String> _getRemovingCacheEventIds(String workspace) 109 { 110 if ("default".equals(workspace)) 111 { 112 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 113 ObservationConstants.EVENT_CONTENT_MODIFIED, 114 ObservationConstants.EVENT_CONTENT_DELETED, 115 org.ametys.odf.observation.OdfObservationConstants.ODF_CONTENT_SKILLS_EXCLUSION_CHANGED); 116 } 117 else if ("live".equals(workspace)) 118 { 119 return Arrays.asList(ObservationConstants.EVENT_CONTENT_ADDED, 120 ObservationConstants.EVENT_CONTENT_VALIDATED, 121 ObservationConstants.EVENT_CONTENT_DELETED, 122 ObservationConstants.EVENT_CONTENT_UNTAG_LIVE, 123 org.ametys.odf.observation.OdfObservationConstants.ODF_CONTENT_SKILLS_EXCLUSION_CHANGED); 124 } 125 126 return Collections.emptyList(); 127 } 128}