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.repository;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import javax.jcr.RepositoryException;
022
023import org.apache.avalon.framework.context.Context;
024import org.apache.avalon.framework.context.ContextException;
025import org.apache.avalon.framework.context.Contextualizable;
026import org.apache.avalon.framework.logger.AbstractLogEnabled;
027import org.apache.avalon.framework.service.ServiceException;
028import org.apache.avalon.framework.service.ServiceManager;
029import org.apache.avalon.framework.service.Serviceable;
030import org.apache.cocoon.components.ContextHelper;
031import org.apache.cocoon.environment.Request;
032
033import org.ametys.plugins.repository.AmetysObjectIterable;
034import org.ametys.plugins.repository.AmetysObjectResolver;
035import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
036import org.ametys.plugins.repository.TraversableAmetysObject;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.parameter.Enumerator;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.repository.site.SiteManager;
041
042/**
043 * Enumerates existing surveys.
044 */
045public class SurveyEnumerator extends AbstractLogEnabled implements Enumerator, Serviceable, Contextualizable
046{
047    
048    /** The ametys object resolver. */
049    protected AmetysObjectResolver _resolver;
050    
051    /** The site manager. */
052    protected SiteManager _siteManager;
053    
054    private Context _context;
055    
056    @Override
057    public void contextualize(Context context) throws ContextException
058    {
059        _context = context;
060    }
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
066        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
067    }
068    
069    @Override
070    public Map<Object, I18nizableText> getEntries() throws Exception
071    {
072        Request request = ContextHelper.getRequest(_context);
073        
074        Map<Object, I18nizableText> entries = new LinkedHashMap<>();
075        
076        String pageId = request.getParameter("pageId");
077        Page page = _resolver.resolveById(pageId);
078        
079        String siteName = (String) request.getAttribute("siteName");
080        String language = page.getSitemapName();
081        
082        TraversableAmetysObject rootNode = getSurveyRootNode(siteName, language);
083        
084        try (AmetysObjectIterable<Survey> surveys = rootNode.getChildren())
085        {
086            for (Survey survey : surveys)
087            {
088                if (survey.isValidated())
089                {
090                    I18nizableText label = new I18nizableText(survey.getLabel());
091                    entries.put(survey.getId(), label);
092                }
093            }
094        }
095        
096        return entries;
097    }
098    
099    @Override
100    public I18nizableText getEntry(String value) throws Exception
101    {
102        Survey survey = _resolver.resolveById(value);
103        return new I18nizableText(survey.getLabel());
104    }
105    
106    /**
107     * Get the root node for surveys
108     * @param siteName the site name
109     * @param lang the language
110     * @return the root node
111     * @throws RepositoryException if an error occurs when retrieving the root node of the survey
112     */
113    protected ModifiableTraversableAmetysObject getSurveyRootNode(String siteName, String lang) throws RepositoryException
114    {
115        ModifiableTraversableAmetysObject pluginsNode = _siteManager.getSite(siteName).getRootPlugins();
116        
117        ModifiableTraversableAmetysObject surveyNode = null;
118        if (!pluginsNode.hasChild("survey"))
119        {
120            surveyNode = ((ModifiableTraversableAmetysObject) pluginsNode.createChild("survey", "ametys:unstructured")).createChild("ametys:surveys", "ametys:unstructured");
121            pluginsNode.saveChanges();
122        }
123        else
124        {
125            surveyNode = pluginsNode.getChild("survey/ametys:surveys");
126        }
127        
128        if (!surveyNode.hasChild(lang))
129        {
130            surveyNode.createChild(lang, "ametys:unstructured");
131            pluginsNode.saveChanges();
132        }
133        
134        return pluginsNode.getChild("survey/ametys:surveys/" + lang);
135    }
136
137}