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.workspace;
017
018import java.io.IOException;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.ProcessingException;
024import org.apache.cocoon.xml.AttributesImpl;
025import org.apache.cocoon.xml.XMLUtils;
026import org.apache.commons.lang3.StringUtils;
027import org.apache.excalibur.source.Source;
028import org.xml.sax.SAXException;
029
030import org.ametys.runtime.config.Config;
031import org.ametys.web.repository.site.Site;
032import org.ametys.web.repository.site.SiteManager;
033import org.ametys.web.repository.site.SiteType;
034import org.ametys.web.repository.site.SiteTypesExtensionPoint;
035import org.ametys.web.site.SiteColorsComponent;
036import org.ametys.web.skin.SkinsManager;
037
038/**
039 * Generates the uitools factories definition using the associated components 
040 */
041public class WorkspaceGenerator extends org.ametys.plugins.core.ui.WorkspaceGenerator
042{
043    /** The site types manager */
044    protected SiteTypesExtensionPoint _siteTypeExtensionPoint;
045    /** The site manager */
046    protected SiteManager _siteManager;
047    /** The site */
048    protected String _siteTypeName;
049    /** The skins manager */
050    protected SkinsManager _skinsManager;
051    /** The site colors */
052    protected SiteColorsComponent _siteColors;
053    
054    @Override
055    public void service(ServiceManager smanager) throws ServiceException
056    {
057        super.service(smanager);
058    
059        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
060        _siteTypeExtensionPoint = (SiteTypesExtensionPoint) manager.lookup(SiteTypesExtensionPoint.ROLE);
061        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
062        _siteColors = (SiteColorsComponent) manager.lookup(SiteColorsComponent.ROLE);
063    }
064    
065    @Override
066    public void generate() throws IOException, SAXException, ProcessingException
067    {
068        String siteName = parameters.getParameter("site", null);
069        
070        Site site = _siteManager.getSite(siteName);
071        if (site != null)
072        {
073            // Checks skin
074            String skinId = site.getSkinId();
075            if (!_skinsManager.getSkins().contains(skinId))
076            {
077                String sysadminMailTo = null;
078                if (Config.getInstance() != null)
079                {
080                    sysadminMailTo = Config.getInstance().getValueAsString("smtp.mail.sysadminto");
081                }
082                throw new IllegalStateException("The site '" + siteName + "' is configured with the unexisting skin '" + skinId + "'. Please contact your system admistrator" 
083                        + (StringUtils.isNotBlank(sysadminMailTo) ? " at " + sysadminMailTo + " " : " ") + "and ask him to fix this by copying this message.");
084            }
085            
086            SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType());
087            _siteTypeName = siteType != null ? siteType.getName() : null;
088        }
089        
090        doGenerate(getContextualParameters());
091    }
092    
093    @Override
094    protected Map<String, Object> getContextualParameters()
095    {
096        Map<String, Object> contextParameters = super.getContextualParameters();
097        
098        String siteName = parameters.getParameter("site", null);
099        contextParameters.put("siteName", siteName);
100
101        return contextParameters;
102    }
103    
104    
105    @Override
106    protected Source getRibbonConfiguration() throws IOException
107    {
108        if (_siteTypeName != null)
109        {
110            String mode = parameters.getParameter("mode", null);
111            return _resolver.resolveURI("context://WEB-INF/param/cms-ribbon-" + (mode != null ? mode + "-" : "") + _siteTypeName + ".xml");
112        }
113        
114        return super.getRibbonConfiguration();
115    }
116    
117    @Override
118    protected Source getUIToolsConfiguration() throws IOException
119    {
120        if (_siteTypeName != null)
121        {
122            String mode = parameters.getParameter("mode", null);
123            return _resolver.resolveURI("context://WEB-INF/param/cms-uitools-" + (mode != null ? mode + "-" : "") + _siteTypeName + ".xml");
124        }
125        
126        return super.getUIToolsConfiguration();
127    }
128
129    @Override
130    protected void saxAdditionnalInfo(Map<String, Object> contextParameters) throws SAXException
131    {
132        super.saxAdditionnalInfo(contextParameters);
133        
134        String siteName = (String) contextParameters.get("siteName");
135        Site site = _siteManager.getSite(siteName);
136
137        _saxColorInformations(site);
138        _saxGlyph(site);
139    }
140    
141    private void _saxGlyph(Site site) throws SAXException
142    {
143        SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType());
144        
145        String iconGlyph = siteType.getIconGlyph();
146        if (StringUtils.isNotBlank(iconGlyph))
147        {
148            XMLUtils.createElement(contentHandler, "site-glyph", iconGlyph);
149        }
150        
151        String siteIcon = siteType.getSmallIcon();
152        if (StringUtils.isNotBlank(siteIcon))
153        {
154            XMLUtils.createElement(contentHandler, "site-icon", siteIcon);
155        }
156    }
157
158    private void _saxColorInformations(Site site) throws SAXException
159    {
160        String colorIndex = site.getColor();
161        if (!_siteColors.getColors().containsKey(colorIndex))
162        {
163            colorIndex = _siteColors.getDefaultKey();
164        }
165        Map<String, String> colorInfos = _siteColors.getColors().get(colorIndex);
166        if (colorInfos == null)
167        {
168            colorInfos = _siteColors.getColors().get(_siteColors.getDefaultKey());
169        }
170        
171        AttributesImpl attrs = new AttributesImpl();
172        for (String name : colorInfos.keySet())
173        {
174            attrs.addCDATAAttribute(name, colorInfos.get(name));
175        }
176        XMLUtils.createElement(contentHandler, "site-color", attrs);
177    }
178}