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.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.excalibur.source.Source;
027import org.xml.sax.SAXException;
028
029import org.ametys.runtime.config.Config;
030import org.ametys.web.repository.site.Site;
031import org.ametys.web.repository.site.SiteManager;
032import org.ametys.web.repository.site.SiteType;
033import org.ametys.web.repository.site.SiteTypesExtensionPoint;
034import org.ametys.web.skin.SkinsManager;
035
036/**
037 * Generates the uitools factories definition using the associated components 
038 */
039public class WorkspaceGenerator extends org.ametys.plugins.core.ui.WorkspaceGenerator
040{
041    /** The site types manager */
042    protected SiteTypesExtensionPoint _siteTypeExtensionPoint;
043    /** The site manager */
044    protected SiteManager _siteManager;
045    /** The site */
046    protected String _siteTypeName;
047    /** The skins manager */
048    protected SkinsManager _skinsManager;
049    
050    @Override
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        super.service(smanager);
054    
055        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
056        _siteTypeExtensionPoint = (SiteTypesExtensionPoint) manager.lookup(SiteTypesExtensionPoint.ROLE);
057        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
058    }
059    
060    @Override
061    public void generate() throws IOException, SAXException, ProcessingException
062    {
063        String siteName = parameters.getParameter("site", null);
064        
065        Request request = ObjectModelHelper.getRequest(objectModel);
066        request.setAttribute("siteName", siteName);
067        
068        Site site = _siteManager.getSite(siteName);
069        if (site != null)
070        {
071            // Checks skin
072            String skinId = site.getSkinId();
073            if (!_skinsManager.getSkins().contains(skinId))
074            {
075                String sysadminMailTo = null;
076                if (Config.getInstance() != null)
077                {
078                    sysadminMailTo = Config.getInstance().getValueAsString("smtp.mail.sysadminto");
079                }
080                throw new IllegalStateException("The site '" + siteName + "' is configured with the unexisting skin '" + skinId + "'. Please contact your system admistrator" 
081                        + (sysadminMailTo != null ? " at " + sysadminMailTo + " " : " ") + "and ask him to fix this by copying this message.");
082            }
083            
084            SiteType siteType = _siteTypeExtensionPoint.getExtension(site.getType());
085            _siteTypeName = siteType != null ? siteType.getName() : null;
086        }
087        
088        doGenerate(getContextualParameters());
089    }
090    
091    @Override
092    protected Map<String, Object> getContextualParameters()
093    {
094        Map<String, Object> contextParameters = super.getContextualParameters();
095        
096        String siteName = parameters.getParameter("site", null);
097        contextParameters.put("siteName", siteName);
098
099        return contextParameters;
100    }
101    
102    
103    @Override
104    protected Source getRibbonConfiguration() throws IOException
105    {
106        if (_siteTypeName != null)
107        {
108            String mode = parameters.getParameter("mode", null);
109            return _resolver.resolveURI("context://WEB-INF/param/cms-ribbon-" + (mode != null ? mode + "-" : "") + _siteTypeName + ".xml");
110        }
111        
112        return super.getRibbonConfiguration();
113    }
114    
115    @Override
116    protected Source getUIToolsConfiguration() throws IOException
117    {
118        if (_siteTypeName != null)
119        {
120            String mode = parameters.getParameter("mode", null);
121            return _resolver.resolveURI("context://WEB-INF/param/cms-uitools-" + (mode != null ? mode + "-" : "") + _siteTypeName + ".xml");
122        }
123        
124        return super.getUIToolsConfiguration();
125    }
126}