001/* 002 * Copyright 2019 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.forms; 017 018import java.util.Map; 019 020import javax.jcr.Node; 021import javax.jcr.Session; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.core.observation.Event; 027import org.ametys.core.observation.Observer; 028import org.ametys.plugins.forms.dao.FormDAO; 029import org.ametys.plugins.forms.repository.Form; 030import org.ametys.plugins.repository.jcr.JCRAmetysObject; 031import org.ametys.web.cache.AbstractSiteCacheObserver; 032import org.ametys.web.repository.page.ModifiablePage; 033import org.ametys.web.repository.page.ModifiableZoneItem; 034import org.ametys.web.repository.page.SitemapElement; 035import org.ametys.web.repository.site.Site; 036 037/** 038 * {@link Observer} for observing form modification in order to 039 * invalidate cache on front-office. 040 */ 041public class InvalidateCacheOnFormModifiedObserver extends AbstractSiteCacheObserver 042{ 043 /** The form DAO */ 044 protected FormDAO _formDAO; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 super.service(manager); 050 _formDAO = (FormDAO) manager.lookup(FormDAO.ROLE); 051 } 052 053 @Override 054 public boolean supports(Event event) 055 { 056 return event.getId().equals(FormEvents.FORM_MODIFIED); 057 } 058 059 @Override 060 protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception 061 { 062 Map<String, Object> args = event.getArguments(); 063 if (args.containsKey("form")) 064 { 065 Form form = (Form) args.get("form"); 066 067 for (ModifiableZoneItem zoneItem : _formDAO.getFormZoneItems(form.getId(), form.getSiteName())) 068 { 069 SitemapElement sitemapElement = zoneItem.getZone().getSitemapElement(); 070 if (sitemapElement instanceof JCRAmetysObject 071 && sitemapElement instanceof ModifiablePage page) 072 { 073 JCRAmetysObject jcrPage = (JCRAmetysObject) page; 074 Node pageNode = jcrPage.getNode(); 075 076 if (liveSession.itemExists(pageNode.getPath())) 077 { 078 if (getLogger().isInfoEnabled()) 079 { 080 getLogger().info("Form modified: " + event + ", invalidating cache"); 081 } 082 083 _cachePolicy.invalidateCacheOnPageModification(page); 084 } 085 } 086 } 087 } 088 } 089 090 @Override 091 protected Site _getSite(Event event) 092 { 093 Map<String, Object> args = event.getArguments(); 094 if (args.containsKey("form")) 095 { 096 Form form = (Form) args.get("form"); 097 return form.getSite(); 098 } 099 100 return null; 101 } 102 103}