001/*
002 *  Copyright 2012 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.web.site;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Map;
021import java.util.Map.Entry;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.cocoon.ProcessingException;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.AttributesImpl;
028import org.apache.cocoon.xml.XMLUtils;
029import org.xml.sax.SAXException;
030
031import org.ametys.cms.languages.Language;
032import org.ametys.cms.languages.LanguagesManager;
033import org.ametys.plugins.repository.UnknownAmetysObjectException;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.repository.site.Site;
036import org.ametys.web.repository.site.SiteManager;
037import org.ametys.web.repository.sitemap.Sitemap;
038
039/**
040 * Generates the site parameters (sorted by category and group) and their values, for a given site.
041 */
042public class SiteParametersGenerator extends ServiceableGenerator
043{
044    /** The site manager. */
045    protected SiteManager _siteManager;
046    
047    /** The site configuration. */
048    protected SiteConfigurationExtensionPoint _siteConfiguration;
049    
050    /** The available languages manager */
051    protected LanguagesManager _languagesManager;
052    
053    @Override
054    public void service(ServiceManager serviceManager) throws ServiceException
055    {
056        super.service(serviceManager);
057        _siteManager = (SiteManager) serviceManager.lookup(SiteManager.ROLE);
058        _siteConfiguration = (SiteConfigurationExtensionPoint) serviceManager.lookup(SiteConfigurationExtensionPoint.ROLE);
059        _languagesManager = (LanguagesManager) serviceManager.lookup(LanguagesManager.ROLE);
060    }
061    
062    @Override
063    public void generate() throws IOException, SAXException, ProcessingException
064    {
065        String siteName = parameters.getParameter("siteName", "");
066        
067        try
068        {
069            contentHandler.startDocument();
070            
071            Site site = _siteManager.getSite(siteName);
072            
073            AttributesImpl atts = new AttributesImpl();
074            atts.addCDATAAttribute("id", site.getId());
075            atts.addCDATAAttribute("path", site.getSitePath());
076            atts.addCDATAAttribute("name", siteName);
077            atts.addCDATAAttribute("title", site.getTitle());
078            XMLUtils.startElement(contentHandler, "site", atts);
079            
080            _saxSitemaps(site);
081            
082            _saxParameters(siteName);
083            
084            XMLUtils.endElement(contentHandler, "site");
085            contentHandler.endDocument();
086        }
087        catch (UnknownAmetysObjectException e)
088        {
089            String message = "The site '" + siteName + "' does not exist.";
090            getLogger().error(message, e);
091            throw new ProcessingException(message, e);
092        }
093        
094    }
095
096    /**
097     * Generate the site's parameters.
098     * @param siteName the site name.
099     * @throws ProcessingException if an error occurs processing the request.
100     * @throws SAXException if an error occurs SAXing the data.
101     */
102    protected void _saxParameters(String siteName) throws ProcessingException, SAXException
103    {
104        Map<I18nizableText, Map<I18nizableText, List<SiteParameter>>> siteParameters = _siteConfiguration.getCategorizedParameters(siteName);
105        Site site = _siteManager.getSite(siteName);
106        
107        XMLUtils.startElement(contentHandler, "parameters");
108        
109        XMLUtils.startElement(contentHandler, "categories");
110        
111        for (Entry<I18nizableText, Map<I18nizableText, List<SiteParameter>>> categoryEntry : siteParameters.entrySet())
112        {
113            I18nizableText categoryLabel = categoryEntry.getKey();
114            Map<I18nizableText, List<SiteParameter>> groups = categoryEntry.getValue();
115            
116            XMLUtils.startElement(contentHandler, "category");
117            categoryLabel.toSAX(contentHandler, "label");
118            
119            XMLUtils.startElement(contentHandler, "groups");
120            
121            for (Entry<I18nizableText, List<SiteParameter>> groupEntry : groups.entrySet())
122            {
123                I18nizableText groupLabel = groupEntry.getKey();
124                List<SiteParameter> groupParams = groupEntry.getValue();
125                
126                XMLUtils.startElement(contentHandler, "group");
127                groupLabel.toSAX(contentHandler, "label");
128                
129                XMLUtils.startElement(contentHandler, "parameters");
130                for (SiteParameter param : groupParams)
131                {
132                    site.dataToSAX(contentHandler, param.getName());
133                }
134                XMLUtils.endElement(contentHandler, "parameters");
135                
136                XMLUtils.endElement(contentHandler, "group");
137            }
138            
139            XMLUtils.endElement(contentHandler, "groups");
140            
141            XMLUtils.endElement(contentHandler, "category");
142        }
143        
144        XMLUtils.endElement(contentHandler, "categories");
145        
146        XMLUtils.endElement(contentHandler, "parameters");
147    }
148
149    /**
150     * Generate the sitemaps of a site.
151     * @param site the site.
152     * @throws SAXException if an error occurs.
153     * @throws ProcessingException if an error occurs.
154     */
155    protected void _saxSitemaps(Site site) throws ProcessingException, SAXException
156    {
157        _saxSelectedSitemaps(site);
158
159        // Now saxing possible
160        _saxPossibleSitemaps();
161    }
162
163    private void _saxPossibleSitemaps() throws SAXException
164    {
165        XMLUtils.startElement(contentHandler, "possible-sitemaps");
166        
167        Map<String, Language> languages = _languagesManager.getAvailableLanguages();
168        for (String language : languages.keySet())
169        {
170            AttributesImpl atts = new AttributesImpl();
171            atts.addCDATAAttribute("name", language);
172            XMLUtils.startElement(contentHandler, "sitemap", atts);
173            languages.get(language).getLabel().toSAX(contentHandler);
174            XMLUtils.endElement(contentHandler, "sitemap");
175        }
176        
177        XMLUtils.endElement(contentHandler, "possible-sitemaps");
178    }
179
180    private void _saxSelectedSitemaps(Site site) throws SAXException
181    {
182        XMLUtils.startElement(contentHandler, "sitemaps");
183        for (Sitemap sitemap : site.getSitemaps())
184        {
185            XMLUtils.createElement(contentHandler, "sitemap", sitemap.getName());
186        }
187        XMLUtils.endElement(contentHandler, "sitemaps");
188    }
189}