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