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.cache;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.core.right.RightManager;
022import org.ametys.plugins.repository.AmetysObjectResolver;
023import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
024import org.ametys.plugins.survey.dao.SurveyDAO;
025import org.ametys.plugins.survey.repository.Survey;
026import org.ametys.web.repository.page.Page;
027import org.ametys.web.repository.page.ZoneItem;
028import org.ametys.web.service.StaticService;
029
030/**
031 * Service for displaying a survey, override StaticService to make it cacheable only if survey is public.
032 */
033public class SurveyService extends StaticService
034{
035
036    /** The Ametys object resolver */
037    protected AmetysObjectResolver _resolver;
038
039    /** The right manager */
040    protected RightManager _rightManager;
041
042    /** The survey DAO */
043    protected SurveyDAO _surveyDAO;
044
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        super.service(manager);
049        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
050        _rightManager = (RightManager) manager.lookup(RightManager.ROLE);
051        _surveyDAO = (SurveyDAO) manager.lookup(SurveyDAO.ROLE);
052    }
053
054    @Override
055    public boolean isCacheable(Page currentPage, ZoneItem zoneItem)
056    {
057        ModelAwareDataHolder serviceParameters = zoneItem.getServiceParameters();
058        String surveyId = serviceParameters.getValue("surveyId");
059        Survey survey = _resolver.resolveById(surveyId);
060
061        // The survey must be public for the service to be cacheable
062        return !_surveyDAO.isPrivate(survey);
063    }
064
065}