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.survey.observation;
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.repository.jcr.JCRAmetysObject;
029import org.ametys.plugins.survey.SurveyEvents;
030import org.ametys.plugins.survey.dao.SurveyDAO;
031import org.ametys.plugins.survey.repository.Survey;
032import org.ametys.web.cache.AbstractSiteCacheObserver;
033import org.ametys.web.repository.page.ModifiablePage;
034import org.ametys.web.repository.page.ModifiableZoneItem;
035import org.ametys.web.repository.page.SitemapElement;
036import org.ametys.web.repository.site.Site;
037
038/**
039 * {@link Observer} for observing survey re-initialization in order to
040 * invalidate cache on front-office.
041 */
042public class InvalidateCacheOnSurveyReInitializedObserver extends AbstractSiteCacheObserver
043{
044    /** The survey DAO */
045    protected SurveyDAO _surveyDAO;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _surveyDAO = (SurveyDAO) manager.lookup(SurveyDAO.ROLE);
052    }
053    
054    @Override
055    public boolean supports(Event event)
056    {
057        return event.getId().equals(SurveyEvents.SURVEY_REINITIALIZED);
058    }
059    
060    @Override
061    protected void _internalObserve(Event event, Site site, Session liveSession) throws Exception
062    {
063        Map<String, Object> args = event.getArguments();
064        if (args.containsKey("survey"))
065        {
066            Survey survey = (Survey) args.get("survey");
067            for (ModifiableZoneItem zoneItem : _surveyDAO.getSurveyZoneItems(survey.getSiteName(), survey.getLanguage(), survey.getId()))
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("Survey re-initialized: " + 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("survey"))
095        {
096            Survey survey = (Survey) args.get("survey");
097            return survey.getSite();
098        }
099        
100        return null;
101    }
102}