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.portlet.exportimport.lar;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.MapUtil;
020    import com.liferay.portal.kernel.xml.Element;
021    import com.liferay.portal.model.LayoutSet;
022    import com.liferay.portal.model.LayoutSetBranch;
023    import com.liferay.portal.model.Theme;
024    import com.liferay.portal.model.adapter.ModelAdapterUtil;
025    import com.liferay.portal.model.adapter.StagedTheme;
026    
027    /**
028     * @author Mate Thurzo
029     */
030    public class ThemeExporter {
031    
032            public static ThemeExporter getInstance() {
033                    return _instance;
034            }
035    
036            public void exportTheme(
037                            PortletDataContext portletDataContext, LayoutSet layoutSet)
038                    throws Exception {
039    
040                    boolean exportThemeSettings = MapUtil.getBoolean(
041                            portletDataContext.getParameterMap(),
042                            PortletDataHandlerKeys.THEME_REFERENCE);
043    
044                    if (_log.isDebugEnabled()) {
045                            _log.debug("Export theme settings " + exportThemeSettings);
046                    }
047    
048                    if (!exportThemeSettings) {
049                            return;
050                    }
051    
052                    StagedTheme stagedTheme = ModelAdapterUtil.adapt(
053                            layoutSet.getTheme(), Theme.class, StagedTheme.class);
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 = ModelAdapterUtil.adapt(
087                            layoutSetBranch.getTheme(), Theme.class, StagedTheme.class);
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    }