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.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
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.Theme;
028    import com.liferay.portal.model.VirtualHost;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.ThemeLocalServiceUtil;
031    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
032    import com.liferay.portal.util.PrefsPropsUtil;
033    
034    import java.io.IOException;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Jorge Ferrer
039     */
040    public class LayoutSetImpl extends LayoutSetBaseImpl {
041    
042            public LayoutSetImpl() {
043            }
044    
045            public Theme getTheme() throws SystemException {
046                    return ThemeLocalServiceUtil.getTheme(
047                            getCompanyId(), getThemeId(), false);
048            }
049    
050            public ColorScheme getColorScheme() throws SystemException {
051                    return ThemeLocalServiceUtil.getColorScheme(
052                            getCompanyId(), getTheme().getThemeId(), getColorSchemeId(), false);
053            }
054    
055            public Group getGroup() throws PortalException, SystemException {
056                    return GroupLocalServiceUtil.getGroup(getGroupId());
057            }
058    
059            @Override
060            public String getSettings() {
061                    if (_settingsProperties == null) {
062                            return super.getSettings();
063                    }
064                    else {
065                            return _settingsProperties.toString();
066                    }
067            }
068    
069            public UnicodeProperties getSettingsProperties() {
070                    if (_settingsProperties == null) {
071                            _settingsProperties = new UnicodeProperties(true);
072    
073                            try {
074                                    _settingsProperties.load(super.getSettings());
075                            }
076                            catch (IOException ioe) {
077                                    _log.error(ioe, ioe);
078                            }
079                    }
080    
081                    return _settingsProperties;
082            }
083    
084            public String getSettingsProperty(String key) {
085                    UnicodeProperties settingsProperties = getSettingsProperties();
086    
087                    return settingsProperties.getProperty(key);
088            }
089    
090            public String getThemeSetting(String key, String device)
091                    throws SystemException {
092    
093                    UnicodeProperties settingsProperties = getSettingsProperties();
094    
095                    String value = settingsProperties.getProperty(
096                            ThemeSettingImpl.namespaceProperty(device, key));
097    
098                    if (value != null) {
099                            return value;
100                    }
101    
102                    Theme theme = null;
103    
104                    boolean controlPanel = false;
105    
106                    try {
107                            Group group = getGroup();
108    
109                            controlPanel = group.isControlPanel();
110                    }
111                    catch (Exception e) {
112                    }
113    
114                    if (controlPanel) {
115                            String themeId = PrefsPropsUtil.getString(
116                                    getCompanyId(),
117                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
118    
119                            theme = ThemeLocalServiceUtil.getTheme(
120                                    getCompanyId(), themeId, !device.equals("regular"));
121                    }
122                    else if (device.equals("regular")) {
123                            theme = getTheme();
124                    }
125                    else {
126                            theme = getWapTheme();
127                    }
128    
129                    value = theme.getSetting(key);
130    
131                    return value;
132            }
133    
134            public String getVirtualHostname() {
135                    try {
136                            VirtualHost virtualHost =
137                                    VirtualHostLocalServiceUtil.fetchVirtualHost(
138                                            getCompanyId(), getLayoutSetId());
139    
140                            if (virtualHost == null) {
141                                    return StringPool.BLANK;
142                            }
143                            else {
144                                    return virtualHost.getHostname();
145                            }
146                    }
147                    catch (Exception e) {
148                            return StringPool.BLANK;
149                    }
150            }
151    
152            public Theme getWapTheme() throws SystemException {
153                    return ThemeLocalServiceUtil.getTheme(
154                            getCompanyId(), getWapThemeId(), true);
155            }
156    
157            public ColorScheme getWapColorScheme() throws SystemException {
158                    return ThemeLocalServiceUtil.getColorScheme(
159                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
160                            true);
161            }
162    
163            public boolean isLayoutSetPrototypeLinkActive() {
164                    if (isLayoutSetPrototypeLinkEnabled() &&
165                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
166    
167                            return true;
168                    }
169    
170                    return false;
171            }
172    
173            @Override
174            public void setSettings(String settings) {
175                    _settingsProperties = null;
176    
177                    super.setSettings(settings);
178            }
179    
180            public void setSettingsProperties(UnicodeProperties settingsProperties) {
181                    _settingsProperties = settingsProperties;
182    
183                    super.setSettings(_settingsProperties.toString());
184            }
185    
186            private static Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
187    
188            private UnicodeProperties _settingsProperties;
189    
190    }