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.Collections;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import javax.jcr.RepositoryException;
024
025import org.apache.avalon.framework.context.Context;
026import org.apache.avalon.framework.context.ContextException;
027import org.apache.avalon.framework.context.Contextualizable;
028import org.apache.avalon.framework.logger.AbstractLogEnabled;
029import org.apache.avalon.framework.service.ServiceException;
030import org.apache.avalon.framework.service.ServiceManager;
031import org.apache.avalon.framework.service.Serviceable;
032import org.apache.cocoon.components.ContextHelper;
033import org.apache.cocoon.environment.Request;
034
035import org.ametys.plugins.repository.AmetysObjectIterable;
036import org.ametys.plugins.repository.AmetysObjectResolver;
037import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
038import org.ametys.plugins.repository.TraversableAmetysObject;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.runtime.model.Enumerator;
041import org.ametys.web.WebConstants;
042import org.ametys.web.repository.page.Page;
043import org.ametys.web.repository.site.SiteManager;
044
045/**
046 * Enumerates existing surveys.
047 */
048public class SurveyEnumerator extends AbstractLogEnabled implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable, Contextualizable
049{
050    
051    /** The ametys object resolver. */
052    protected AmetysObjectResolver _resolver;
053    
054    /** The site manager. */
055    protected SiteManager _siteManager;
056    
057    private Context _context;
058    
059    public void contextualize(Context context) throws ContextException
060    {
061        _context = context;
062    }
063    
064    public void service(ServiceManager manager) throws ServiceException
065    {
066        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
067        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
068    }
069    
070    public Map<String, I18nizableText> getTypedEntries() throws Exception
071    {
072        Request request = ContextHelper.getRequest(_context);
073        
074        Map<String, I18nizableText> entries = new LinkedHashMap<>();
075        
076        String pageId = (String) request.getAttribute(WebConstants.REQUEST_ATTR_PAGE_ID);
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    public I18nizableText getEntry(String value) throws Exception
100    {
101        Survey survey = _resolver.resolveById(value);
102        return new I18nizableText(survey.getLabel());
103    }
104    
105    /**
106     * Get the root node for surveys
107     * @param siteName the site name
108     * @param lang the language
109     * @return the root node
110     * @throws RepositoryException if an error occurs when retrieving the root node of the survey
111     */
112    protected ModifiableTraversableAmetysObject getSurveyRootNode(String siteName, String lang) throws RepositoryException
113    {
114        ModifiableTraversableAmetysObject pluginsNode = _siteManager.getSite(siteName).getRootPlugins();
115        
116        ModifiableTraversableAmetysObject surveyNode = null;
117        if (!pluginsNode.hasChild("survey"))
118        {
119            surveyNode = ((ModifiableTraversableAmetysObject) pluginsNode.createChild("survey", "ametys:unstructured")).createChild("ametys:surveys", "ametys:unstructured");
120            pluginsNode.saveChanges();
121        }
122        else
123        {
124            surveyNode = pluginsNode.getChild("survey/ametys:surveys");
125        }
126        
127        if (!surveyNode.hasChild(lang))
128        {
129            surveyNode.createChild(lang, "ametys:unstructured");
130            pluginsNode.saveChanges();
131        }
132        
133        return pluginsNode.getChild("survey/ametys:surveys/" + lang);
134    }
135    
136    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
137    public Map<Object, I18nizableText> getEntries() throws Exception
138    {
139        Map<Object, I18nizableText> result = new HashMap<>();
140        for (Map.Entry<String, I18nizableText> entry : getTypedEntries().entrySet())
141        {
142            result.put(entry.getKey(), entry.getValue());
143        }
144        return Collections.unmodifiableMap(result);
145    }
146    
147    @Override
148    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
149    public Map<String, Object> getConfiguration()
150    {
151        return Collections.EMPTY_MAP;
152    }
153
154}