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.site.Site;
036
037/**
038 * {@link Observer} for observing survey re-initialization in order to
039 * invalidate cache on front-office.
040 */
041public class InvalidateCacheOnSurveyReInitializedObserver extends AbstractSiteCacheObserver
042{
043    /** The survey DAO */
044    protected SurveyDAO _surveyDAO;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _surveyDAO = (SurveyDAO) manager.lookup(SurveyDAO.ROLE);
051    }
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        return event.getId().equals(SurveyEvents.SURVEY_REINITIALIZED);
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("survey"))
064        {
065            Survey survey = (Survey) args.get("survey");
066            for (ModifiableZoneItem zoneItem : _surveyDAO.getSurveyZoneItems(survey.getSiteName(), survey.getLanguage(), survey.getId()))
067            {
068                ModifiablePage page = (ModifiablePage) zoneItem.getZone().getPage();
069                if (page instanceof JCRAmetysObject)
070                {
071                    JCRAmetysObject jcrPage = (JCRAmetysObject) page;
072                    Node pageNode = jcrPage.getNode();
073                    
074                    if (liveSession.itemExists(pageNode.getPath()))
075                    {
076                        if (getLogger().isInfoEnabled())
077                        {
078                            getLogger().info("Survey re-initialized: " + event + ", invalidating cache");
079                        }
080                        
081                        _cachePolicy.invalidateCacheOnPageModification(page);
082                    }
083                }
084            }
085        }
086    }
087
088    @Override
089    protected Site _getSite(Event event)
090    {
091        Map<String, Object> args = event.getArguments();
092        if (args.containsKey("survey"))
093        {
094            Survey survey = (Survey) args.get("survey");
095            return survey.getSite();
096        }
097        
098        return null;
099    }
100}