001/* 002 * Copyright 2017 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.observation; 017 018import java.util.Map; 019import java.util.Set; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.cms.ObservationConstants; 025import org.ametys.cms.repository.Content; 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.odf.course.Course; 029import org.ametys.odf.program.Program; 030import org.ametys.odf.program.SubProgram; 031import org.ametys.web.WebConstants; 032import org.ametys.web.cache.CacheInvalidationPolicy; 033import org.ametys.web.repository.page.Page; 034import org.ametys.web.repository.site.Site; 035 036/** 037 * {@link Observer} for observing content validation or tagging in order to 038 * invalidate cache on front-office. 039 */ 040public class InvalidateCacheOnContentValidationOrUnpublishingObserver extends AbstractODFObserver 041{ 042 /** Cache invalidation policy */ 043 protected CacheInvalidationPolicy _cachePolicy; 044 045 @Override 046 public void service(ServiceManager serviceManager) throws ServiceException 047 { 048 super.service(serviceManager); 049 _cachePolicy = (CacheInvalidationPolicy) serviceManager.lookup(CacheInvalidationPolicy.class.getName()); 050 } 051 052 @Override 053 public boolean supports(Event event) 054 { 055 return event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED) 056 || event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE); 057 } 058 059 @Override 060 public int getPriority(Event event) 061 { 062 // Will be processed after Solr update 063 return MAX_PRIORITY + 4000; 064 } 065 066 @Override 067 protected String _workspaceToUse() 068 { 069 return WebConstants.LIVE_WORKSPACE; 070 } 071 072 @Override 073 protected void _internalObserve(Event event, Map<String, Object> transientVars, Page odfRootPage, Set<Program> rootPrograms, SubProgram subProgram, Course course) throws Exception 074 { 075 Site site = odfRootPage.getSite(); 076 077 for (Program program : rootPrograms) 078 { 079 _invalidate(site, program); 080 } 081 082 if (subProgram != null) 083 { 084 _invalidate(site, subProgram); 085 } 086 087 if (course != null) 088 { 089 _invalidate(site, course); 090 } 091 092 } 093 094 /** 095 * Invalidate content cache 096 * @param site the site 097 * @param content the content 098 */ 099 protected void _invalidate(Site site, Content content) 100 { 101 try 102 { 103 _cachePolicy.invalidateCacheOnContentModification(site, content); 104 } 105 catch (Exception e) 106 { 107 getLogger().error("Unable to invalidate cache for progam {}", content, e); 108 } 109 } 110}