001/* 002 * Copyright 2024 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.survey.observation; 017 018import javax.jcr.Node; 019import javax.jcr.Session; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.core.observation.Event; 025import org.ametys.core.observation.Observer; 026import org.ametys.plugins.repository.jcr.JCRAmetysObject; 027import org.ametys.plugins.survey.dao.SurveyDAO; 028import org.ametys.plugins.survey.repository.Survey; 029import org.ametys.web.cache.AbstractSiteCacheObserver; 030import org.ametys.web.repository.page.ModifiablePage; 031import org.ametys.web.repository.page.ModifiableZoneItem; 032import org.ametys.web.repository.page.SitemapElement; 033import org.ametys.web.repository.site.Site; 034 035/** 036 * Abstract {@link Observer} for observing survey changes in order to invalidate cache on front-office. 037 */ 038public abstract class AbstractSurveyCacheObserver extends AbstractSiteCacheObserver 039{ 040 /** The survey DAO */ 041 protected SurveyDAO _surveyDAO; 042 043 @Override 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 super.service(manager); 047 _surveyDAO = (SurveyDAO) manager.lookup(SurveyDAO.ROLE); 048 } 049 050 @Override 051 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 052 { 053 Survey survey = getSurveyFromEvent(event); 054 if (survey != null) 055 { 056 for (ModifiableZoneItem zoneItem : _surveyDAO.getSurveyZoneItems(survey.getSiteName(), survey.getLanguage(), survey.getId())) 057 { 058 SitemapElement sitemapElement = zoneItem.getZone().getSitemapElement(); 059 if (sitemapElement instanceof JCRAmetysObject 060 && sitemapElement instanceof ModifiablePage page) 061 { 062 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 063 Node pageNode = jcrPage.getNode(); 064 065 if (liveSession.itemExists(pageNode.getPath())) 066 { 067 if (getLogger().isInfoEnabled()) 068 { 069 getLogger().info("Survey re-initialized: " + event + ", invalidating cache"); 070 } 071 072 _cachePolicy.invalidateCacheOnPageModification(page); 073 } 074 } 075 } 076 } 077 } 078 079 @Override 080 protected Site _getSite(Event event) 081 { 082 Survey survey = getSurveyFromEvent(event); 083 if (survey != null) 084 { 085 return survey.getSite(); 086 } 087 088 return null; 089 } 090 091 /** 092 * Get the survey from the Event 093 * @param event the event 094 * @return the survey 095 */ 096 protected abstract Survey getSurveyFromEvent(Event event); 097}