001/*
002 *  Copyright 2010 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.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.repository.UnknownAmetysObjectException;
029import org.ametys.plugins.survey.repository.Survey;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.core.ui.Callable;
032import org.ametys.core.ui.StaticClientSideElement;
033import org.ametys.web.repository.page.Page;
034
035/**
036 * This element creates a button for page redirection of a survey
037 */
038public class RedirectPageClientSideElement extends StaticClientSideElement
039{
040    /** Repository content */
041    protected AmetysObjectResolver _resolver;
042    
043    @Override
044    public void service(ServiceManager smanager) throws ServiceException
045    {
046        super.service(smanager);
047        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
048    }
049    
050    /**
051     * Get the Redirect page status for a given survey
052     * @param surveyId The survey id
053     * @return The redirect page status
054     */
055    @Callable
056    public Map<String, Object> getStatus(String surveyId)
057    {
058        Map<String, Object> results = new HashMap<>();
059        
060        Survey survey = _resolver.resolveById(surveyId);
061        
062        String redirectPageId = survey.getRedirection();
063        if (StringUtils.isNotEmpty(redirectPageId))
064        {
065            try
066            {
067                Page page = _resolver.resolveById(redirectPageId);
068                
069                List<String> i18nParameters = new ArrayList<>();
070                i18nParameters.add(page.getTitle());
071                i18nParameters.add(page.getSitemapName() + "/" + page.getPathInSitemap() + ".html");
072                
073                I18nizableText ed = (I18nizableText) this._script.getParameters().get("redirect-page-description");
074                I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
075                
076                results.put("page-redirection", redirectPageId);
077                results.put("page-redirection-description", msg);
078            }
079            catch (UnknownAmetysObjectException e)
080            {
081                getLogger().warn("The redirect page of id '" + redirectPageId + "' does not exist anymore", e);
082            }
083        }
084        
085        return results;
086    }
087
088}