001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
027    /**
028     * @author Mate Thurzo
029     */
030    public class ThemeExporter {
031    
032            public void exportTheme(
033                            PortletDataContext portletDataContext, LayoutSet layoutSet)
034                    throws Exception {
035    
036                    boolean exportThemeSettings = MapUtil.getBoolean(
037                            portletDataContext.getParameterMap(),
038                            PortletDataHandlerKeys.THEME_REFERENCE);
039    
040                    if (_log.isDebugEnabled()) {
041                            _log.debug("Export theme settings " + exportThemeSettings);
042                    }
043    
044                    if (!exportThemeSettings) {
045                            return;
046                    }
047    
048                    StagedTheme stagedTheme = new StagedTheme(layoutSet.getTheme());
049    
050                    if (!portletDataContext.isPerformDirectBinaryImport()) {
051                            Element layoutSetElement = portletDataContext.getExportDataElement(
052                                    layoutSet);
053    
054                            portletDataContext.addReferenceElement(
055                                    layoutSet, layoutSetElement, stagedTheme,
056                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
057                    }
058    
059                    exportThemeSettings(
060                            portletDataContext, stagedTheme.getThemeId(),
061                            layoutSet.getColorSchemeId(), layoutSet.getCss());
062            }
063    
064            public void exportTheme(
065                            PortletDataContext portletDataContext,
066                            LayoutSetBranch layoutSetBranch)
067                    throws Exception {
068    
069                    boolean exportThemeSettings = MapUtil.getBoolean(
070                            portletDataContext.getParameterMap(),
071                            PortletDataHandlerKeys.THEME_REFERENCE);
072    
073                    if (_log.isDebugEnabled()) {
074                            _log.debug("Export theme settings " + exportThemeSettings);
075                    }
076    
077                    if (!exportThemeSettings) {
078                            return;
079                    }
080    
081                    StagedTheme stagedTheme = new StagedTheme(layoutSetBranch.getTheme());
082    
083                    if (!portletDataContext.isPerformDirectBinaryImport()) {
084                            Element layoutSetBranchElement =
085                                    portletDataContext.getExportDataElement(layoutSetBranch);
086    
087                            portletDataContext.addReferenceElement(
088                                    layoutSetBranch, layoutSetBranchElement, stagedTheme,
089                                    PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true);
090                    }
091    
092                    exportThemeSettings(
093                            portletDataContext, stagedTheme.getThemeId(),
094                            layoutSetBranch.getColorSchemeId(), layoutSetBranch.getCss());
095            }
096    
097            protected void exportThemeSettings(
098                            PortletDataContext portletDataContext, String themeId,
099                            String colorSchemeId, String css)
100                    throws Exception {
101    
102                    Element exportDataRootElement =
103                            portletDataContext.getExportDataRootElement();
104    
105                    Element headerElement = exportDataRootElement.element("header");
106    
107                    headerElement.addAttribute("theme-id", themeId);
108                    headerElement.addAttribute("color-scheme-id", colorSchemeId);
109    
110                    Element cssElement = headerElement.addElement("css");
111    
112                    cssElement.addCDATA(css);
113            }
114    
115            private static Log _log = LogFactoryUtil.getLog(ThemeExporter.class);
116    
117    }