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;
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.core.user.CurrentUserProvider;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.core.util.I18nUtils;
033import org.ametys.plugins.repository.AmetysObject;
034import org.ametys.plugins.repository.AmetysObjectIterable;
035import org.ametys.plugins.repository.AmetysObjectResolver;
036import org.ametys.runtime.i18n.I18nizableText;
037import org.ametys.web.site.SiteConfigurationManager;
038
039/**
040 * Generates the list of existing {@link Site}.
041 */
042public class SitesGenerator extends ServiceableGenerator
043{
044    private SiteManager _siteManager;
045    private SiteTypesExtensionPoint _siteTypeExtensionPoint;
046    private SiteConfigurationManager _siteConfigurationManager;
047    private CurrentUserProvider _userProvider;
048    private AmetysObjectResolver _ametysResolver;
049    private I18nUtils _i18nUtils;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        super.service(smanager);
055        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
056        _siteTypeExtensionPoint = (SiteTypesExtensionPoint) smanager.lookup(SiteTypesExtensionPoint.ROLE);
057        _siteConfigurationManager = (SiteConfigurationManager) smanager.lookup(SiteConfigurationManager.ROLE);
058        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
059        _ametysResolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
060        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
061    }
062    
063    @Override
064    public void generate() throws IOException, SAXException, ProcessingException
065    {
066        UserIdentity user = _userProvider.getUser();
067        
068        Request request = ObjectModelHelper.getRequest(objectModel);
069        String node = request.getParameter("node");
070        
071        contentHandler.startDocument();
072        
073        XMLUtils.startElement(contentHandler, "sites");
074        
075        if (node != null && (node.startsWith("collection://") || node.startsWith("site://")))
076        {
077            AmetysObject aObject = _ametysResolver.resolveById(node);
078            
079            try (AmetysObjectIterable<Site> rootSites = aObject instanceof Site ? ((Site) aObject).getChildrenSites() : _siteManager.getRootSites())
080            {
081                for (Site site : rootSites)
082                {
083                    if (_siteManager.isGrantedSite(user, site.getName()))
084                    {
085                        _saxSite (site);
086                    }
087                }
088            }
089        }
090        else
091        {
092            _saxRoot ();
093        }
094        
095        XMLUtils.endElement(contentHandler, "sites");
096        
097        contentHandler.endDocument();
098    }
099    
100    private void _saxRoot () throws SAXException
101    {
102        AttributesImpl attr = new AttributesImpl();
103        
104        attr.addAttribute("", "id", "id", "CDATA", _siteManager.getRoot().getId());
105        attr.addAttribute("", "type", "type", "CDATA", "root");
106        XMLUtils.startElement(contentHandler, "site", attr);
107        
108        XMLUtils.createElement(contentHandler, "name", "root");
109        XMLUtils.createElement(contentHandler, "title", _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_ROOT_TITLE")));
110        
111        XMLUtils.createElement(contentHandler, "description", _i18nUtils.translate(new I18nizableText("plugin.web", "PLUGINS_WEB_ADMINISTRATOR_SITES_ROOT_DESC"))); 
112         
113        XMLUtils.createElement(contentHandler, "url", "");
114        
115        XMLUtils.createElement(contentHandler, "iconSmall", "/plugins/web/resources/img/sitetype/sites_16.png");
116        XMLUtils.createElement(contentHandler, "iconLarge", "/plugins/web/resources/img/sitetype/sites_48.png");
117        
118        XMLUtils.endElement(contentHandler, "site");
119    }
120    
121    private void _saxSite (Site site) throws SAXException
122    {
123        boolean valid = _siteConfigurationManager.isSiteConfigurationValid(site);
124        
125        AttributesImpl attr = new AttributesImpl();
126        attr.addAttribute("", "id", "id", "CDATA", site.getId());
127        attr.addAttribute("", "type", "type", "CDATA", site.getType());
128        attr.addCDATAAttribute("configuration-valid", Boolean.toString(valid));
129        XMLUtils.startElement(contentHandler, "site", attr);
130        
131        XMLUtils.createElement(contentHandler, "name", site.getName());
132        String title = site.getTitle() != null ? site.getTitle() : site.getName();
133        XMLUtils.createElement(contentHandler, "title", title);
134        
135        String desc = site.getDescription() != null ? site.getDescription() : "";
136        XMLUtils.createElement(contentHandler, "description", desc);
137        
138        String url = site.getUrl() != null ? site.getUrl() : "";
139        XMLUtils.createElement(contentHandler, "url", url);
140        
141        SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType());
142        
143        String iconGlyph = siteType.getIconGlyph();
144        if (iconGlyph != null)
145        {
146            XMLUtils.createElement(contentHandler, "iconGlyph", siteType.getIconGlyph());
147        }
148        XMLUtils.createElement(contentHandler, "iconSmall", siteType.getSmallIcon());
149        XMLUtils.createElement(contentHandler, "iconLarge", siteType.getLargeIcon());
150        siteType.getLabel().toSAX(contentHandler, "typeLabel");
151        
152        XMLUtils.endElement(contentHandler, "site");
153    }
154}