001    /**
002     * Copyright (c) 2000-2012 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.model.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.model.ColorScheme;
026    import com.liferay.portal.model.Group;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.model.LayoutSetStagingHandler;
029    import com.liferay.portal.model.Theme;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
032    import com.liferay.portal.service.ThemeLocalServiceUtil;
033    import com.liferay.portal.util.PrefsPropsUtil;
034    
035    import java.io.IOException;
036    
037    /**
038     * @author Raymond Augé
039     * @author Julio Camarero
040     */
041    public class LayoutSetBranchImpl extends LayoutSetBranchBaseImpl {
042    
043            public LayoutSetBranchImpl() {
044            }
045    
046            public ColorScheme getColorScheme() throws SystemException {
047                    return ThemeLocalServiceUtil.getColorScheme(
048                            getCompanyId(), getTheme().getThemeId(), getColorSchemeId(), false);
049            }
050    
051            public Group getGroup() throws PortalException, SystemException {
052                    return GroupLocalServiceUtil.getGroup(getGroupId());
053            }
054    
055            public LayoutSet getLayoutSet() {
056                    if (_layoutSet != null) {
057                            return _layoutSet;
058                    }
059    
060                    try {
061                            _layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
062                                    getGroupId(), getPrivateLayout());
063    
064                            LayoutSetStagingHandler layoutSetStagingHandler =
065                                    LayoutStagingUtil.getLayoutSetStagingHandler(_layoutSet);
066    
067                            if (layoutSetStagingHandler == null) {
068                                    return _layoutSet;
069                            }
070    
071                            _layoutSet = layoutSetStagingHandler.getLayoutSet();
072    
073                            return _layoutSet;
074                    }
075                    catch (SystemException se) {
076                    }
077                    catch (PortalException pe) {
078                    }
079    
080                    return _layoutSet;
081            }
082    
083            public long getLiveLogoId() {
084                    long logoId = getLayoutSet().getLogoId();
085    
086                    if (logoId == 0) {
087                            logoId = getLayoutSet().getLiveLogoId();
088                    }
089    
090                    return logoId;
091            }
092    
093            @Override
094            public String getSettings() {
095                    if (_settingsProperties == null) {
096                            return super.getSettings();
097                    }
098                    else {
099                            return _settingsProperties.toString();
100                    }
101            }
102    
103            public UnicodeProperties getSettingsProperties() {
104                    if (_settingsProperties == null) {
105                            _settingsProperties = new UnicodeProperties(true);
106    
107                            try {
108                                    _settingsProperties.load(super.getSettings());
109                            }
110                            catch (IOException ioe) {
111                                    _log.error(ioe, ioe);
112                            }
113                    }
114    
115                    return _settingsProperties;
116            }
117    
118            public String getSettingsProperty(String key) {
119                    UnicodeProperties settingsProperties = getSettingsProperties();
120    
121                    return settingsProperties.getProperty(key);
122            }
123    
124            public Theme getTheme() throws SystemException {
125                    return ThemeLocalServiceUtil.getTheme(
126                            getCompanyId(), getThemeId(), false);
127            }
128    
129            public String getThemeSetting(String key, String device)
130                    throws SystemException {
131    
132                    UnicodeProperties settingsProperties = getSettingsProperties();
133    
134                    String value = settingsProperties.getProperty(
135                            ThemeSettingImpl.namespaceProperty(device, key));
136    
137                    if (value != null) {
138                            return value;
139                    }
140    
141                    Theme theme = null;
142    
143                    boolean controlPanel = false;
144    
145                    try {
146                            Group group = getGroup();
147    
148                            controlPanel = group.isControlPanel();
149                    }
150                    catch (Exception e) {
151                    }
152    
153                    if (controlPanel) {
154                            String themeId = PrefsPropsUtil.getString(
155                                    getCompanyId(),
156                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
157    
158                            theme = ThemeLocalServiceUtil.getTheme(
159                                    getCompanyId(), themeId, !device.equals("regular"));
160                    }
161                    else if (device.equals("regular")) {
162                            theme = getTheme();
163                    }
164                    else {
165                            theme = getWapTheme();
166                    }
167    
168                    value = theme.getSetting(key);
169    
170                    return value;
171            }
172    
173            public ColorScheme getWapColorScheme() throws SystemException {
174                    return ThemeLocalServiceUtil.getColorScheme(
175                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
176                            true);
177            }
178    
179            public Theme getWapTheme() throws SystemException {
180                    return ThemeLocalServiceUtil.getTheme(
181                            getCompanyId(), getWapThemeId(), true);
182            }
183    
184            public boolean isLayoutSetPrototypeLinkActive() {
185                    if (isLayoutSetPrototypeLinkEnabled() &&
186                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
187    
188                            return true;
189                    }
190    
191                    return false;
192            }
193    
194            @Override
195            public void setSettings(String settings) {
196                    _settingsProperties = null;
197    
198                    super.setSettings(settings);
199            }
200    
201            public void setSettingsProperties(UnicodeProperties settingsProperties) {
202                    _settingsProperties = settingsProperties;
203    
204                    super.setSettings(_settingsProperties.toString());
205            }
206    
207            private static Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
208    
209            private LayoutSet _layoutSet;
210            private UnicodeProperties _settingsProperties;
211    
212    }