public Properties getThemeSettings() { Theme theme = getTheme(); Properties properties = new Properties(); Map<String, ThemeSetting> themeSettings = theme.getSettings(); for (String key : themeSettings.keySet()) { ThemeSetting themeSetting = themeSettings.get(key); String value = null; if (themeSetting.isConfigurable()) { value = getThemeSetting(key); } else { value = themeSetting.getValue(); } if (value != null) { properties.put(key, value); } } return properties; }
public String getThemeSetting(String key) { Theme theme = getTheme(); String device = theme.getDevice(); Layout layout = getLayout(); return layout.getThemeSetting(key, device); }
private void _setSpriteImages(ServletContext servletContext, Theme theme, String resourcePath) throws Exception { Set<String> resourcePaths = servletContext.getResourcePaths(resourcePath); if (resourcePaths == null) { return; } List<File> images = new ArrayList<File>(resourcePaths.size()); for (String curResourcePath : resourcePaths) { if (curResourcePath.endsWith(StringPool.SLASH)) { _setSpriteImages(servletContext, theme, curResourcePath); } else if (curResourcePath.endsWith(".png")) { String realPath = ServletContextUtil.getRealPath(servletContext, curResourcePath); if (realPath != null) { images.add(new File(realPath)); } else { if (ServerDetector.isTomcat()) { if (_log.isInfoEnabled()) { _log.info(ServletContextUtil.LOG_INFO_SPRITES); } } else { _log.error("Real path for " + curResourcePath + " is null"); } } } } String spriteFileName = ".sprite.png"; String spritePropertiesFileName = ".sprite.properties"; String spritePropertiesRootPath = ServletContextUtil.getRealPath(servletContext, theme.getImagesPath()); Properties spriteProperties = SpriteProcessorUtil.generate( images, spriteFileName, spritePropertiesFileName, spritePropertiesRootPath, 16, 16, 10240); if (spriteProperties == null) { return; } spriteFileName = resourcePath.substring(theme.getImagesPath().length(), resourcePath.length()) + spriteFileName; theme.setSpriteImages(spriteFileName, spriteProperties); }
public ColorScheme fetchColorScheme(long companyId, String themeId, String colorSchemeId) { colorSchemeId = GetterUtil.getString(colorSchemeId); Theme theme = fetchTheme(companyId, themeId); if (theme == null) { return null; } Map<String, ColorScheme> colorSchemesMap = theme.getColorSchemesMap(); return colorSchemesMap.get(colorSchemeId); }
public List<Theme> getWARThemes() { List<Theme> themes = ListUtil.fromMapValues(_themes); Iterator<Theme> itr = themes.iterator(); while (itr.hasNext()) { Theme theme = itr.next(); if (!theme.isWARFile()) { itr.remove(); } } return themes; }
private void _setSpriteImages(ServletContext servletContext, Theme theme, String resourcePath) throws Exception { if (!resourcePath.startsWith(StringPool.SLASH)) { resourcePath = StringPool.SLASH.concat(resourcePath); } Set<String> resourcePaths = servletContext.getResourcePaths(resourcePath); if (resourcePaths == null) { return; } List<URL> imageURLs = new ArrayList<URL>(resourcePaths.size()); for (String curResourcePath : resourcePaths) { if (curResourcePath.endsWith(StringPool.SLASH)) { _setSpriteImages(servletContext, theme, curResourcePath); } else if (curResourcePath.endsWith(".png")) { URL imageURL = servletContext.getResource(curResourcePath); if (imageURL != null) { imageURLs.add(imageURL); } else { if (ServerDetector.isTomcat()) { if (_log.isInfoEnabled()) { _log.info(ServletContextUtil.LOG_INFO_SPRITES); } } else { _log.error("Real path for " + curResourcePath + " is null"); } } } } String spriteFileName = resourcePath.concat(PropsValues.SPRITE_FILE_NAME); String spritePropertiesFileName = resourcePath.concat(PropsValues.SPRITE_PROPERTIES_FILE_NAME); URL spritePropertiesRootURL = servletContext.getResource(StringPool.SLASH); Properties spriteProperties = SpriteProcessorUtil.generate( servletContext, imageURLs, spriteFileName, spritePropertiesFileName, spritePropertiesRootURL, 16, 16, 10240); if (spriteProperties == null) { return; } String contextPath = ContextPathUtil.getContextPath(servletContext); spriteFileName = contextPath.concat(SpriteProcessor.PATH).concat(spriteFileName); theme.setSpriteImages(spriteFileName, spriteProperties); }
private void _setSpriteImages(ServletContext servletContext, Theme theme, String resourcePath) throws Exception { if (!resourcePath.startsWith(StringPool.SLASH)) { resourcePath = StringPool.SLASH.concat(resourcePath); } Set<String> resourcePaths = servletContext.getResourcePaths(resourcePath); if ((resourcePaths == null) || resourcePaths.isEmpty()) { return; } List<URL> imageURLs = new ArrayList<URL>(resourcePaths.size()); for (String curResourcePath : resourcePaths) { if (curResourcePath.endsWith(StringPool.SLASH)) { _setSpriteImages(servletContext, theme, curResourcePath); } else if (curResourcePath.endsWith(".png")) { URL imageURL = servletContext.getResource(curResourcePath); if (imageURL != null) { imageURLs.add(imageURL); } else { _log.error("Resource URL for " + curResourcePath + " is null"); } } } String spriteRootDirName = PropsValues.SPRITE_ROOT_DIR; String spriteFileName = resourcePath.concat(PropsValues.SPRITE_FILE_NAME); String spritePropertiesFileName = resourcePath.concat(PropsValues.SPRITE_PROPERTIES_FILE_NAME); String rootPath = ServletContextUtil.getRootPath(servletContext); Properties spriteProperties = SpriteProcessorUtil.generate( servletContext, imageURLs, spriteRootDirName, spriteFileName, spritePropertiesFileName, rootPath, 16, 16, 10240); if (spriteProperties == null) { return; } String contextPath = ContextPathUtil.getContextPath(servletContext); spriteFileName = contextPath.concat(SpriteProcessor.PATH).concat(spriteFileName); theme.setSpriteImages(spriteFileName, spriteProperties); }
protected String getThemeId() { String themeId = null; try { Layout layout = getLayout(); Theme theme = layout.getTheme(); if (theme != null) { themeId = theme.getThemeId(); } else { themeId = layout.getThemeId(); } } catch (Exception e) { _log.error(e, e); } return themeId; }
@Override public String getDefaultThemeSetting(String key, String device, boolean inheritLookAndFeel) { if (!inheritLookAndFeel) { try { Theme theme = getTheme(device); return theme.getSetting(key); } catch (Exception e) { } } try { LayoutSet layoutSet = getLayoutSet(); return layoutSet.getThemeSetting(key, device); } catch (Exception e) { } return StringPool.BLANK; }
public ColorScheme getColorScheme( long companyId, String themeId, String colorSchemeId, boolean wapTheme) throws SystemException { colorSchemeId = GetterUtil.getString(colorSchemeId); Theme theme = getTheme(companyId, themeId, wapTheme); Map<String, ColorScheme> colorSchemesMap = theme.getColorSchemesMap(); ColorScheme colorScheme = colorSchemesMap.get(colorSchemeId); if (colorScheme == null) { List<ColorScheme> colorSchemes = theme.getColorSchemes(); if (colorSchemes.size() > 0) { for (int i = (colorSchemes.size() - 1); i >= 0; i--) { colorScheme = colorSchemes.get(i); if (colorScheme.isDefaultCs()) { break; } } } } if (colorScheme == null) { if (wapTheme) { colorSchemeId = ColorSchemeImpl.getDefaultWapColorSchemeId(); } else { colorSchemeId = ColorSchemeImpl.getDefaultRegularColorSchemeId(); } } if (colorScheme == null) { colorScheme = ColorSchemeImpl.getNullColorScheme(); } return colorScheme; }
public List<Theme> getThemes(long companyId, long groupId, long userId, boolean wapTheme) throws SystemException { List<Theme> themes = getThemes(companyId); themes = PluginUtil.restrictPlugins(themes, companyId, userId); Iterator<Theme> itr = themes.iterator(); while (itr.hasNext()) { Theme theme = itr.next(); if (theme.getThemeId().equals("controlpanel") || !theme.isGroupAvailable(groupId) || (theme.isWapTheme() != wapTheme)) { itr.remove(); } } return themes; }
private Map<String, Theme> _getThemes(long companyId) { Map<String, Theme> themes = _themesPool.get(companyId); if (themes != null) { return themes; } themes = new ConcurrentHashMap<String, Theme>(); for (Map.Entry<String, Theme> entry : _themes.entrySet()) { String themeId = entry.getKey(); Theme theme = entry.getValue(); if (theme.isCompanyAvailable(companyId)) { themes.put(themeId, theme); } } _themesPool.put(companyId, themes); return themes; }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } Theme theme = null; try { theme = (Theme) obj; } catch (ClassCastException cce) { return false; } String themeId = theme.getThemeId(); if (getThemeId().equals(themeId)) { return true; } else { return false; } }
@Override public void prepare(Template template, HttpServletRequest request) { super.prepare(template, request); // Theme display ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); if (themeDisplay != null) { // Init template.put( "init", themeDisplay.getPathContext() + TemplateConstants.SERVLET_SEPARATOR + "/html/themes/_unstyled/templates/init.vm"); } // Theme Theme theme = (Theme) request.getAttribute(WebKeys.THEME); if ((theme == null) && (themeDisplay != null)) { theme = themeDisplay.getTheme(); } if (theme != null) { // Full css and templates path String servletContextName = GetterUtil.getString(theme.getServletContextName()); template.put( "fullCssPath", servletContextName + theme.getVelocityResourceListener() + theme.getCssPath()); template.put( "fullTemplatesPath", servletContextName + theme.getVelocityResourceListener() + theme.getTemplatesPath()); } // Insert custom vm variables Map<String, Object> vmVariables = (Map<String, Object>) request.getAttribute(WebKeys.VM_VARIABLES); if (vmVariables != null) { for (Map.Entry<String, Object> entry : vmVariables.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); if (Validator.isNotNull(key)) { template.put(key, value); } } } }
private Map<String, Theme> _getThemes(long companyId) { Map<String, Theme> themes = _themesPool.get(companyId); if (themes == null) { themes = new ConcurrentHashMap<String, Theme>(); Iterator<Map.Entry<String, Theme>> itr = _themes.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<String, Theme> entry = itr.next(); String themeId = entry.getKey(); Theme theme = entry.getValue(); if (theme.isCompanyAvailable(companyId)) { themes.put(themeId, theme); } } _themesPool.put(companyId, themes); } return themes; }
public void setLookAndFeel(Theme theme, ColorScheme colorScheme) { _theme = theme; _colorScheme = colorScheme; if ((theme != null) && (colorScheme != null)) { String themeStaticResourcePath = theme.getStaticResourcePath(); String cdnBaseURL = getCDNBaseURL(); setPathColorSchemeImages( cdnBaseURL + themeStaticResourcePath + colorScheme.getColorSchemeImagesPath()); String dynamicResourcesHost = getCDNDynamicResourcesHost(); if (Validator.isNull(dynamicResourcesHost)) { String portalURL = getPortalURL(); if (getServerName() != null) { try { portalURL = PortalUtil.getPortalURL(getLayout(), this); } catch (Exception e) { _log.error(e, e); } } dynamicResourcesHost = portalURL; } setPathThemeCss(dynamicResourcesHost + themeStaticResourcePath + theme.getCssPath()); setPathThemeImages(cdnBaseURL + themeStaticResourcePath + theme.getImagesPath()); setPathThemeJavaScript(cdnBaseURL + themeStaticResourcePath + theme.getJavaScriptPath()); setPathThemeRoot(themeStaticResourcePath + theme.getRootPath()); setPathThemeTemplates(cdnBaseURL + themeStaticResourcePath + theme.getTemplatesPath()); } }
@Override public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException { try { // Do not randomize look and feel unless the user is logged in ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); if (!themeDisplay.isSignedIn()) { return; } // Do not randomize look and feel unless the user is accessing the // portal String requestURI = GetterUtil.getString(request.getRequestURI()); if (!requestURI.endsWith("/portal/layout")) { return; } // Do not randomize look and feel unless the user is accessing a // personal layout Layout layout = themeDisplay.getLayout(); if (layout == null) { return; } boolean wapTheme = BrowserSnifferUtil.isWap(request); List<Theme> themes = ThemeLocalServiceUtil.getPageThemes( themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), wapTheme); if (!themes.isEmpty()) { Theme theme = themes.get(RandomUtil.nextInt(themes.size())); List<ColorScheme> colorSchemes = theme.getColorSchemes(); ColorScheme colorScheme = colorSchemes.get(RandomUtil.nextInt(colorSchemes.size())); LayoutServiceUtil.updateLookAndFeel( layout.getGroupId(), layout.isPrivateLayout(), layout.getPlid(), theme.getThemeId(), colorScheme.getColorSchemeId(), layout.getCss(), wapTheme); themeDisplay.setLookAndFeel(theme, colorScheme); request.setAttribute(WebKeys.THEME, theme); request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme); } } catch (Exception e) { _log.error(e, e); throw new ActionException(e); } }
private Set<String> _readThemes( String servletContextName, ServletContext servletContext, String themesPath, boolean loadFromServletContext, String xml, PluginPackage pluginPackage) throws Exception { Set<String> themeIds = new HashSet<String>(); if (xml == null) { return themeIds; } Document document = SAXReaderUtil.read(xml, true); Element rootElement = document.getRootElement(); Version portalVersion = _getVersion(ReleaseInfo.getVersion()); boolean compatible = false; Element compatibilityElement = rootElement.element("compatibility"); if (compatibilityElement != null) { List<Element> versionElements = compatibilityElement.elements("version"); for (Element versionElement : versionElements) { Version version = _getVersion(versionElement.getTextTrim()); if (version.includes(portalVersion)) { compatible = true; break; } } } if (!compatible) { _log.error("Themes in this WAR are not compatible with " + ReleaseInfo.getServerInfo()); return themeIds; } ThemeCompanyLimit companyLimit = null; Element companyLimitElement = rootElement.element("company-limit"); if (companyLimitElement != null) { companyLimit = new ThemeCompanyLimit(); Element companyIncludesElement = companyLimitElement.element("company-includes"); if (companyIncludesElement != null) { companyLimit.setIncludes(_getCompanyLimitIncludes(companyIncludesElement)); } Element companyExcludesElement = companyLimitElement.element("company-excludes"); if (companyExcludesElement != null) { companyLimit.setExcludes(_getCompanyLimitExcludes(companyExcludesElement)); } } ThemeGroupLimit groupLimit = null; Element groupLimitElement = rootElement.element("group-limit"); if (groupLimitElement != null) { groupLimit = new ThemeGroupLimit(); Element groupIncludesElement = groupLimitElement.element("group-includes"); if (groupIncludesElement != null) { groupLimit.setIncludes(_getGroupLimitIncludes(groupIncludesElement)); } Element groupExcludesElement = groupLimitElement.element("group-excludes"); if (groupExcludesElement != null) { groupLimit.setExcludes(_getGroupLimitExcludes(groupExcludesElement)); } } long timestamp = ServletContextUtil.getLastModified(servletContext); List<Element> themeElements = rootElement.elements("theme"); for (Element themeElement : themeElements) { ContextReplace themeContextReplace = new ContextReplace(); themeContextReplace.addValue("themes-path", themesPath); String themeId = themeElement.attributeValue("id"); if (servletContextName != null) { themeId = themeId + PortletConstants.WAR_SEPARATOR + servletContextName; } themeId = PortalUtil.getJsSafePortletId(themeId); themeContextReplace.addValue("theme-id", themeId); themeIds.add(themeId); Theme theme = _themes.get(themeId); if (theme == null) { theme = new ThemeImpl(themeId); } theme.setTimestamp(timestamp); PluginSetting pluginSetting = pluginSettingLocalService.getDefaultPluginSetting(); theme.setPluginPackage(pluginPackage); theme.setDefaultPluginSetting(pluginSetting); theme.setThemeCompanyLimit(companyLimit); theme.setThemeGroupLimit(groupLimit); if (servletContextName != null) { theme.setServletContextName(servletContextName); } theme.setLoadFromServletContext(loadFromServletContext); String name = GetterUtil.getString(themeElement.attributeValue("name"), theme.getName()); String rootPath = GetterUtil.getString(themeElement.elementText("root-path"), theme.getRootPath()); rootPath = themeContextReplace.replace(rootPath); themeContextReplace.addValue("root-path", rootPath); String templatesPath = GetterUtil.getString( themeElement.elementText("templates-path"), theme.getTemplatesPath()); templatesPath = themeContextReplace.replace(templatesPath); templatesPath = StringUtil.safePath(templatesPath); themeContextReplace.addValue("templates-path", templatesPath); String cssPath = GetterUtil.getString(themeElement.elementText("css-path"), theme.getCssPath()); cssPath = themeContextReplace.replace(cssPath); cssPath = StringUtil.safePath(cssPath); themeContextReplace.addValue("css-path", cssPath); String imagesPath = GetterUtil.getString(themeElement.elementText("images-path"), theme.getImagesPath()); imagesPath = themeContextReplace.replace(imagesPath); imagesPath = StringUtil.safePath(imagesPath); themeContextReplace.addValue("images-path", imagesPath); String javaScriptPath = GetterUtil.getString( themeElement.elementText("javascript-path"), theme.getJavaScriptPath()); javaScriptPath = themeContextReplace.replace(javaScriptPath); javaScriptPath = StringUtil.safePath(javaScriptPath); themeContextReplace.addValue("javascript-path", javaScriptPath); String virtualPath = GetterUtil.getString(themeElement.elementText("virtual-path"), theme.getVirtualPath()); String templateExtension = GetterUtil.getString( themeElement.elementText("template-extension"), theme.getTemplateExtension()); theme.setName(name); theme.setRootPath(rootPath); theme.setTemplatesPath(templatesPath); theme.setCssPath(cssPath); theme.setImagesPath(imagesPath); theme.setJavaScriptPath(javaScriptPath); theme.setVirtualPath(virtualPath); theme.setTemplateExtension(templateExtension); Element settingsElement = themeElement.element("settings"); if (settingsElement != null) { List<Element> settingElements = settingsElement.elements("setting"); for (Element settingElement : settingElements) { boolean configurable = GetterUtil.getBoolean(settingElement.attributeValue("configurable")); String key = settingElement.attributeValue("key"); String[] options = StringUtil.split(settingElement.attributeValue("options")); String type = settingElement.attributeValue("type"); String value = settingElement.attributeValue("value"); String script = settingElement.getTextTrim(); theme.addSetting(key, value, configurable, type, options, script); } } theme.setWapTheme( GetterUtil.getBoolean(themeElement.elementText("wap-theme"), theme.isWapTheme())); Element rolesElement = themeElement.element("roles"); if (rolesElement != null) { List<Element> roleNameElements = rolesElement.elements("role-name"); for (Element roleNameElement : roleNameElements) { pluginSetting.addRole(roleNameElement.getText()); } } _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace); _readColorSchemes(themeElement, theme.getColorSchemesMap(), themeContextReplace); Element layoutTemplatesElement = themeElement.element("layout-templates"); if (layoutTemplatesElement != null) { Element standardElement = layoutTemplatesElement.element("standard"); if (standardElement != null) { layoutTemplateLocalService.readLayoutTemplate( servletContextName, servletContext, null, standardElement, true, themeId, pluginPackage); } Element customElement = layoutTemplatesElement.element("custom"); if (customElement != null) { layoutTemplateLocalService.readLayoutTemplate( servletContextName, servletContext, null, customElement, false, themeId, pluginPackage); } } if (!theme.isWapTheme()) { _setSpriteImages(servletContext, theme, imagesPath); } if (!_themes.containsKey(themeId)) { _themes.put(themeId, theme); } } return themeIds; }
protected void exportTheme(LayoutSet layoutSet, ZipWriter zipWriter) throws Exception { Theme theme = layoutSet.getTheme(); String lookAndFeelXML = ContentUtil.get("com/liferay/portal/dependencies/liferay-look-and-feel.xml.tmpl"); lookAndFeelXML = StringUtil.replace( lookAndFeelXML, new String[] {"[$TEMPLATE_EXTENSION$]", "[$VIRTUAL_PATH$]"}, new String[] {theme.getTemplateExtension(), theme.getVirtualPath()}); String servletContextName = theme.getServletContextName(); ServletContext servletContext = ServletContextPool.get(servletContextName); if (servletContext == null) { if (_log.isWarnEnabled()) { _log.warn("Servlet context not found for theme " + theme.getThemeId()); } return; } File themeZip = new File(zipWriter.getPath() + "/theme.zip"); ZipWriter themeZipWriter = ZipWriterFactoryUtil.getZipWriter(themeZip); themeZipWriter.addEntry("liferay-look-and-feel.xml", lookAndFeelXML); File cssPath = null; File imagesPath = null; File javaScriptPath = null; File templatesPath = null; if (!theme.isLoadFromServletContext()) { ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(servletContextName); if (themeLoader == null) { _log.error(servletContextName + " does not map to a theme loader"); } else { String realPath = themeLoader.getFileStorage().getPath() + StringPool.SLASH + theme.getName(); cssPath = new File(realPath + "/css"); imagesPath = new File(realPath + "/images"); javaScriptPath = new File(realPath + "/javascript"); templatesPath = new File(realPath + "/templates"); } } else { cssPath = new File(servletContext.getRealPath(theme.getCssPath())); imagesPath = new File(servletContext.getRealPath(theme.getImagesPath())); javaScriptPath = new File(servletContext.getRealPath(theme.getJavaScriptPath())); templatesPath = new File(servletContext.getRealPath(theme.getTemplatesPath())); } exportThemeFiles("css", cssPath, themeZipWriter); exportThemeFiles("images", imagesPath, themeZipWriter); exportThemeFiles("javascript", javaScriptPath, themeZipWriter); exportThemeFiles("templates", templatesPath, themeZipWriter); }
public int compareTo(Theme theme) { return getName().compareTo(theme.getName()); }