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