001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar;
016    
017    import com.liferay.portal.kernel.lar.PortletDataContext;
018    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.LayoutSet;
024    import com.liferay.portal.model.LayoutSetBranch;
025    import com.liferay.portlet.layoutsadmin.lar.StagedTheme;
026    import com.liferay.portlet.layoutsadmin.lar.StagedThemeImpl;
027    
028    /**
029     * @author Mate Thurzo
030     */
031    public class ThemeExporter {
032    
033            public static ThemeExporter getInstance() {
034                    return _instance;
035            }
036    
037            public void exportTheme(
038                            PortletDataContext portletDataContext, LayoutSet layoutSet)
039                    throws Exception {
040    
041                    boolean exportThemeSettings = MapUtil.getBoolean(
042                            portletDataContext.getParameterMap(),
043                            PortletDataHandlerKeys.THEME_REFERENCE);
044    
045                    if (_log.isDebugEnabled()) {
046                            _log.debug("Export theme settings " + exportThemeSettings);
047                    }
048    
049                    if (!exportThemeSettings) {
050                            return;
051                    }
052    
053                    StagedTheme stagedTheme = new StagedThemeImpl(layoutSet.getTheme());
054    
055                    if (!portletDataContext.isPerformDirectBinaryImport()) {
056                            Element layoutSetElement = portletDataContext.getExportDataElement(
057                                    layoutSet);
058    
059                            portletDataContext.addReferenceElement(
060                                    layoutSet, layoutSetElement, stagedTheme,
061                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
062                    }
063    
064                    exportThemeSettings(
065                            portletDataContext, stagedTheme.getThemeId(),
066                            layoutSet.getColorSchemeId(), layoutSet.getCss());
067            }
068    
069            public void exportTheme(
070                            PortletDataContext portletDataContext,
071                            LayoutSetBranch layoutSetBranch)
072                    throws Exception {
073    
074                    boolean exportThemeSettings = MapUtil.getBoolean(
075                            portletDataContext.getParameterMap(),
076                            PortletDataHandlerKeys.THEME_REFERENCE);
077    
078                    if (_log.isDebugEnabled()) {
079                            _log.debug("Export theme settings " + exportThemeSettings);
080                    }
081    
082                    if (!exportThemeSettings) {
083                            return;
084                    }
085    
086                    StagedTheme stagedTheme = new StagedThemeImpl(
087                            layoutSetBranch.getTheme());
088    
089                    if (!portletDataContext.isPerformDirectBinaryImport()) {
090                            Element layoutSetBranchElement =
091                                    portletDataContext.getExportDataElement(layoutSetBranch);
092    
093                            portletDataContext.addReferenceElement(
094                                    layoutSetBranch, layoutSetBranchElement, stagedTheme,
095                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
096                    }
097    
098                    exportThemeSettings(
099                            portletDataContext, stagedTheme.getThemeId(),
100                            layoutSetBranch.getColorSchemeId(), layoutSetBranch.getCss());
101            }
102    
103            protected void exportThemeSettings(
104                            PortletDataContext portletDataContext, String themeId,
105                            String colorSchemeId, String css)
106                    throws Exception {
107    
108                    Element exportDataRootElement =
109                            portletDataContext.getExportDataRootElement();
110    
111                    Element headerElement = exportDataRootElement.element("header");
112    
113                    headerElement.addAttribute("theme-id", themeId);
114                    headerElement.addAttribute("color-scheme-id", colorSchemeId);
115    
116                    Element cssElement = headerElement.addElement("css");
117    
118                    cssElement.addCDATA(css);
119            }
120    
121            private ThemeExporter() {
122            }
123    
124            private static final Log _log = LogFactoryUtil.getLog(ThemeExporter.class);
125    
126            private static final ThemeExporter _instance = new ThemeExporter();
127    
128    }