001/*
002 *  Copyright 2010 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.newsletter.category;
017
018import java.io.IOException;
019import java.util.List;
020import java.util.Set;
021
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.xml.XMLUtils;
026import org.apache.commons.lang.StringUtils;
027import org.xml.sax.SAXException;
028import org.xml.sax.helpers.AttributesImpl;
029
030import org.ametys.runtime.i18n.I18nizableText;
031
032/**
033 * SAX events for categories stored in JCR repository
034 * 
035 */
036public class NewslettersTreeGenerator extends CategoryGenerator
037{
038    @Override
039    public void generate() throws IOException, SAXException, ProcessingException
040    {
041        Request request = ObjectModelHelper.getRequest(objectModel);
042        String categoryID = request.getParameter("categoryID");
043        String siteName = request.getParameter("sitename");
044        String lang = request.getParameter("lang");
045        boolean categoriesOnly = parameters.getParameterAsBoolean("categoriesOnly", false);
046        
047        contentHandler.startDocument();
048        if (StringUtils.isEmpty(categoryID) || "root".equals(categoryID))
049        {
050            AttributesImpl attr = new AttributesImpl();
051            attr.addAttribute("", "id", "id", "CDATA", "root");
052            attr.addAttribute("", "hasChild", "hasChild", "CDATA", "true");
053            
054            XMLUtils.startElement(contentHandler, "provider", attr);
055            
056            Set<String> ids = _categoryProviderEP.getExtensionsIds();
057            for (String id : ids)
058            {
059                CategoryProvider provider = _categoryProviderEP.getExtension(id); 
060                saxProvider(provider, siteName, lang, false);
061            }
062            
063            XMLUtils.endElement(contentHandler, "provider");
064        }
065        else if (categoryID.startsWith("provider_"))
066        {
067            String id = categoryID.substring("provider_".length());
068            CategoryProvider provider = _categoryProviderEP.getExtension(id); 
069            
070            saxProvider(provider, siteName, lang, true);
071        }
072        else
073        {
074            Set<String> ids = _categoryProviderEP.getExtensionsIds();
075            for (String id : ids)
076            {
077                CategoryProvider provider = _categoryProviderEP.getExtension(id);
078                if (provider.hasCategory(categoryID))
079                {
080                    Category category = provider.getCategory(categoryID);
081                    saxCategory(provider, category, true, !categoriesOnly);
082                }
083            }
084        }
085        contentHandler.endDocument();
086    }
087    
088    /**
089     * SAX a category provider
090     * @param provider the category provider
091     * @param siteName the site name
092     * @param lang the language name
093     * @param children true to SAX children
094     * @throws SAXException If the error occurred while SAXing
095     */
096    protected void saxProvider (CategoryProvider provider, String siteName, String lang, boolean children) throws SAXException
097    {
098        String id = provider.getId();
099        I18nizableText title = provider.getLabel();
100        I18nizableText description = provider.getDescription();
101        
102        AttributesImpl attr = new AttributesImpl();
103        attr.addAttribute("", "id", "id", "CDATA", "provider_" + id);
104        
105        List<Category> childCategories = provider.getCategories(siteName, lang);
106        if (childCategories.size() > 0)
107        {
108            attr.addAttribute("", "hasChild", "hasChild", "CDATA", "true");
109        }
110        attr.addAttribute("", "mode", "mode", "CDATA", provider.isWritable() ? "write" : "read");
111        
112        XMLUtils.startElement(contentHandler, "provider", attr);
113        
114        XMLUtils.startElement(contentHandler, "title");
115        title.toSAX(contentHandler);
116        XMLUtils.endElement(contentHandler, "title");
117
118        XMLUtils.startElement(contentHandler, "description");
119        description.toSAX(contentHandler);
120        XMLUtils.endElement(contentHandler, "description");
121        
122        if (children)
123        {
124            for (Category child : childCategories)
125            {
126                saxCategory(provider, child, false, false);
127            }
128        }
129        
130        XMLUtils.endElement(contentHandler, "provider");
131    }
132}