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.web.repository.site;
017
018import java.io.IOException;
019import java.util.Collection;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.generation.ServiceableGenerator;
027import org.apache.cocoon.xml.XMLUtils;
028import org.apache.commons.lang.StringUtils;
029import org.xml.sax.SAXException;
030import org.xml.sax.helpers.AttributesImpl;
031
032import org.ametys.plugins.repository.AmetysObjectIterable;
033import org.ametys.plugins.repository.AmetysObjectResolver;
034import org.ametys.runtime.model.ElementDefinition;
035import org.ametys.runtime.model.ModelItem;
036import org.ametys.runtime.model.type.ElementType;
037import org.ametys.web.repository.sitemap.Sitemap;
038
039/**
040 * Generate sax events representing a {@link Site}.
041 */
042public class SiteGenerator extends ServiceableGenerator
043{
044    /** The site manager. */
045    protected SiteManager _siteManager;
046    
047    private AmetysObjectResolver _resolver;
048    
049    @Override
050    public void service(ServiceManager sManager) throws ServiceException
051    {
052        super.service(sManager);
053        _resolver = (AmetysObjectResolver) sManager.lookup(AmetysObjectResolver.ROLE);
054        _siteManager = (SiteManager) sManager.lookup(SiteManager.ROLE);
055    }
056    
057    @Override
058    public void generate() throws IOException, SAXException, ProcessingException
059    {
060        Request request = ObjectModelHelper.getRequest(objectModel);
061        String id = request.getParameter("id");
062        String siteName = parameters.getParameter("name", request.getParameter("name"));
063        
064        Site site = null;
065        if (StringUtils.isBlank(id) && StringUtils.isNotBlank(siteName))
066        {
067            site = _siteManager.getSite(siteName);
068        }
069        else
070        {
071            site = _resolver.resolveById(id);
072        }
073        
074        contentHandler.startDocument();
075        
076        AttributesImpl attr = new AttributesImpl();
077        attr.addAttribute("", "id", "id", "CDATA", site.getId());
078        attr.addAttribute("", "name", "name", "CDATA", site.getName());
079        attr.addAttribute("", "title", "title", "CDATA", StringUtils.defaultString(site.getTitle()));
080        attr.addAttribute("", "path", "path", "CDATA", site.getSitePath());
081        XMLUtils.startElement(contentHandler, "site", attr);
082        _saxSite (site);
083        XMLUtils.endElement(contentHandler, "site");
084        
085        contentHandler.endDocument();
086    }
087    
088    private <T> void _saxSite (Site site) throws SAXException
089    {
090        XMLUtils.startElement(contentHandler, "metadata");
091        Collection<String> dataNames = site.getDataNames();
092        for (String dataName : dataNames)
093        {
094            ModelItem modelItem = site.getDefinition(dataName);
095            if (modelItem instanceof ElementDefinition)
096            {
097                T value = site.getValue(dataName);
098                ElementType<T> type = ((ElementDefinition) modelItem).getType();
099                XMLUtils.createElement(contentHandler, dataName,  type.toString(value));
100            }
101        }
102        
103        AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps();
104        for (Sitemap sitemap : sitemaps)
105        {
106            XMLUtils.createElement(contentHandler, "lang", sitemap.getName());
107        }
108        
109        XMLUtils.endElement(contentHandler, "metadata");
110    }
111
112}