001/*
002 *  Copyright 2011 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.actions;
017
018import java.util.HashMap;
019import java.util.Iterator;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.environment.SourceResolver;
027
028import org.ametys.plugins.repository.AmetysObjectIterable;
029import org.ametys.plugins.survey.repository.Survey;
030import org.ametys.web.repository.page.SitemapElement;
031import org.ametys.web.repository.page.ZoneItem;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * Get the url of survey form
036 */
037public class GetSurveyActionUrl extends AbstractSurveyAction
038{
039    @Override
040    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
041    {
042        Map<String, String> result = new HashMap<>();
043        
044        Request request = ObjectModelHelper.getRequest(objectModel);
045        String surveyId = request.getParameter("id");
046        
047        Survey survey = _resolver.resolveById(surveyId);
048        
049        String xpathQuery = "//element(" + survey.getSiteName() + ", ametys:site)/ametys-internal:sitemaps/" + survey.getLanguage()
050                + "//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.survey.service.Display' and ametys:service_parameters/@ametys:surveyId = '" + surveyId + "']";
051
052        AmetysObjectIterable<ZoneItem> zoneItems = _resolver.query(xpathQuery);
053        Iterator<ZoneItem> it = zoneItems.iterator();
054        if (it.hasNext())
055        {
056            SitemapElement sitemapElement = it.next().getZone().getSitemapElement();
057            
058            Site site = _siteManager.getSite(sitemapElement.getSiteName());
059            result.put("actionUrl", site.getUrl() + "/" + sitemapElement.getSitemapName() + "/_plugins/survey/" + sitemapElement.getTemplate() + "/finish.html");
060        }
061        else
062        {
063            Site site = _siteManager.getSite(survey.getSiteName());
064            result.put("actionUrl", site.getUrl() + "/" + survey.getLanguage() + "/_plugins/survey/page/finish.html");
065        }
066        
067        return result;
068    }
069}