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.skineditor.generators;
017
018import java.io.IOException;
019import java.util.Set;
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.xml.sax.SAXException;
029import org.xml.sax.helpers.AttributesImpl;
030
031import org.ametys.core.right.RightManager;
032import org.ametys.core.right.RightManager.RightResult;
033import org.ametys.core.user.CurrentUserProvider;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.runtime.i18n.I18nizableText;
036import org.ametys.web.repository.site.Site;
037import org.ametys.web.repository.site.SiteManager;
038import org.ametys.web.skin.Skin;
039import org.ametys.web.skin.SkinsManager;
040
041/**
042 * Generates the list of available skins.
043 */
044public class SkinsGenerator extends ServiceableGenerator
045{
046    private SkinsManager _skinManager;
047    private CurrentUserProvider _userProvider;
048    private RightManager _rightManager;
049    private SiteManager _siteManager;
050    
051    @Override
052    public void service(ServiceManager smanager) throws ServiceException
053    {
054        _skinManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE);
055        _userProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
056        _rightManager = (RightManager) smanager.lookup(RightManager.ROLE);
057        _siteManager = (SiteManager) smanager.lookup(SiteManager.ROLE);
058    }
059    
060    @Override
061    public void generate() throws IOException, SAXException, ProcessingException
062    {
063        Request request = ObjectModelHelper.getRequest(objectModel);
064        String siteName = request.getParameter("siteName");
065        Site site = _siteManager.getSite(siteName);
066        String currentSkinId = site.getSkinId();
067        
068        contentHandler.startDocument();
069        XMLUtils.startElement(contentHandler, "skins");
070        
071        UserIdentity user = _userProvider.getUser();
072        if (_rightManager.hasRight(user, "Plugins_SkinEditor_EditAllSkin", "/cms") == RightResult.RIGHT_ALLOW)
073        {
074            Set<String> skins = _skinManager.getSkins();
075            for (String id : skins)
076            {
077                Skin skin = _skinManager.getSkin(id);
078                _saxSkin(skin, id.equals(currentSkinId));
079            }
080            
081        }
082        else if (_rightManager.hasRight(user, "Plugins_SkinEditor_EditCurrentSkin", "/cms") == RightResult.RIGHT_ALLOW)
083        {
084            Skin skin = _skinManager.getSkin(currentSkinId);
085            _saxSkin(skin, true);
086        }
087        
088        XMLUtils.endElement(contentHandler, "skins");
089        contentHandler.endDocument();
090    }
091    
092    private void _saxSkin (Skin skin, boolean current) throws SAXException
093    {
094        I18nizableText label = skin.getLabel();
095        String icon = skin.getLargeImage();
096        
097        AttributesImpl attr = new AttributesImpl();
098        attr.addAttribute("", "id", "id", "CDATA", skin.getId());
099        if (current)
100        {
101            attr.addAttribute("", "current", "current", "CDATA", "true");
102        }
103        XMLUtils.startElement(contentHandler, "skin", attr);
104        
105        XMLUtils.startElement(contentHandler, "label");
106        label.toSAX(contentHandler);
107        XMLUtils.endElement(contentHandler, "label");
108        
109        XMLUtils.createElement(contentHandler, "icon", icon);
110     
111        XMLUtils.endElement(contentHandler, "skin");
112    }
113
114}