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.blog;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.cocoon.ProcessingException;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.generation.ServiceableGenerator;
026import org.apache.cocoon.xml.AttributesImpl;
027import org.apache.cocoon.xml.XMLUtils;
028import org.xml.sax.SAXException;
029
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.web.repository.page.PagesContainer;
032
033/**
034 * Blog root page generator.
035 */
036public class BlogRootPageGenerator extends ServiceableGenerator
037{
038    
039    /** The ametys object resolver. */
040    protected AmetysObjectResolver _ametysResolver;
041    
042    /** The blog cache manager. */
043    protected BlogPageHandler _blogPageHandler;
044    
045    @Override
046    public void service(ServiceManager serviceManager) throws ServiceException
047    {
048        super.service(serviceManager);
049        _ametysResolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
050        _blogPageHandler = (BlogPageHandler) serviceManager.lookup(BlogPageHandler.ROLE);
051    }
052    
053    @Override
054    public void generate() throws IOException, SAXException, ProcessingException
055    {
056        Request request = ObjectModelHelper.getRequest(objectModel);
057        
058        String siteName = parameters.getParameter("siteName", (String) request.getAttribute("siteName"));
059        String language = parameters.getParameter("lang", (String) request.getAttribute("renderingLanguage"));
060        
061        PagesContainer page = _blogPageHandler.getBlogRootPage(siteName, language);
062        
063        AttributesImpl attrs = new AttributesImpl();
064        if (page != null)
065        {
066            attrs.addCDATAAttribute("name", page.getName());
067            attrs.addCDATAAttribute("site", page.getSiteName());
068            attrs.addCDATAAttribute("sitemap", page.getSitemapName());
069            attrs.addCDATAAttribute("path", page.getPathInSitemap());
070            attrs.addCDATAAttribute("id", page.getId());
071        }
072        
073        contentHandler.startDocument();
074        
075        XMLUtils.createElement(contentHandler, "blog-root-page", attrs);
076        
077        contentHandler.endDocument();
078    }
079    
080}