001    /**
002     * Copyright (c) 2000-2012 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.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.LayoutSet;
028    import com.liferay.portal.model.LayoutSetPrototype;
029    import com.liferay.portal.model.Theme;
030    import com.liferay.portal.model.VirtualHost;
031    import com.liferay.portal.service.GroupLocalServiceUtil;
032    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
033    import com.liferay.portal.service.ThemeLocalServiceUtil;
034    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
035    import com.liferay.portal.util.PrefsPropsUtil;
036    
037    import java.io.IOException;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Jorge Ferrer
042     */
043    public class LayoutSetImpl extends LayoutSetBaseImpl {
044    
045            public LayoutSetImpl() {
046            }
047    
048            public ColorScheme getColorScheme() throws SystemException {
049                    return ThemeLocalServiceUtil.getColorScheme(
050                            getCompanyId(), getTheme().getThemeId(), getColorSchemeId(), false);
051            }
052    
053            public Group getGroup() throws PortalException, SystemException {
054                    return GroupLocalServiceUtil.getGroup(getGroupId());
055            }
056    
057            public long getLayoutSetPrototypeId()
058                    throws PortalException, SystemException {
059    
060                    String layoutSetPrototypeUuid = getLayoutSetPrototypeUuid();
061    
062                    if (Validator.isNull(layoutSetPrototypeUuid)) {
063                            return 0;
064                    }
065    
066                    LayoutSetPrototype layoutSetPrototype =
067                            LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuid(
068                                    layoutSetPrototypeUuid);
069    
070                    return layoutSetPrototype.getLayoutSetPrototypeId();
071            }
072    
073            public long getLiveLogoId() {
074                    long logoId = 0;
075    
076                    Group group = null;
077    
078                    try {
079                            group = getGroup();
080    
081                            if (!group.isStagingGroup()) {
082                                    return logoId;
083                            }
084                    }
085                    catch (Exception e) {
086                            return logoId;
087                    }
088    
089                    Group liveGroup = group.getLiveGroup();
090    
091                    LayoutSet liveLayoutSet = null;
092    
093                    if (isPrivateLayout()) {
094                            liveLayoutSet = liveGroup.getPrivateLayoutSet();
095                    }
096                    else {
097                            liveLayoutSet = liveGroup.getPublicLayoutSet();
098                    }
099    
100                    return liveLayoutSet.getLogoId();
101            }
102    
103            @Override
104            public String getSettings() {
105                    if (_settingsProperties == null) {
106                            return super.getSettings();
107                    }
108                    else {
109                            return _settingsProperties.toString();
110                    }
111            }
112    
113            public UnicodeProperties getSettingsProperties() {
114                    if (_settingsProperties == null) {
115                            _settingsProperties = new UnicodeProperties(true);
116    
117                            try {
118                                    _settingsProperties.load(super.getSettings());
119                            }
120                            catch (IOException ioe) {
121                                    _log.error(ioe, ioe);
122                            }
123                    }
124    
125                    return _settingsProperties;
126            }
127    
128            public String getSettingsProperty(String key) {
129                    UnicodeProperties settingsProperties = getSettingsProperties();
130    
131                    return settingsProperties.getProperty(key);
132            }
133    
134            public Theme getTheme() throws SystemException {
135                    return ThemeLocalServiceUtil.getTheme(
136                            getCompanyId(), getThemeId(), false);
137            }
138    
139            public String getThemeSetting(String key, String device)
140                    throws SystemException {
141    
142                    UnicodeProperties settingsProperties = getSettingsProperties();
143    
144                    String value = settingsProperties.getProperty(
145                            ThemeSettingImpl.namespaceProperty(device, key));
146    
147                    if (value != null) {
148                            return value;
149                    }
150    
151                    Theme theme = getTheme(device);
152    
153                    value = theme.getSetting(key);
154    
155                    return value;
156            }
157    
158            public String getVirtualHostname() {
159                    try {
160                            VirtualHost virtualHost =
161                                    VirtualHostLocalServiceUtil.fetchVirtualHost(
162                                            getCompanyId(), getLayoutSetId());
163    
164                            if (virtualHost == null) {
165                                    return StringPool.BLANK;
166                            }
167                            else {
168                                    return virtualHost.getHostname();
169                            }
170                    }
171                    catch (Exception e) {
172                            return StringPool.BLANK;
173                    }
174            }
175    
176            public ColorScheme getWapColorScheme() throws SystemException {
177                    return ThemeLocalServiceUtil.getColorScheme(
178                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
179                            true);
180            }
181    
182            public Theme getWapTheme() throws SystemException {
183                    return ThemeLocalServiceUtil.getTheme(
184                            getCompanyId(), getWapThemeId(), true);
185            }
186    
187            public boolean isLayoutSetPrototypeLinkActive() {
188                    if (isLayoutSetPrototypeLinkEnabled() &&
189                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
190    
191                            return true;
192                    }
193    
194                    return false;
195            }
196    
197            @Override
198            public void setSettings(String settings) {
199                    _settingsProperties = null;
200    
201                    super.setSettings(settings);
202            }
203    
204            public void setSettingsProperties(UnicodeProperties settingsProperties) {
205                    _settingsProperties = settingsProperties;
206    
207                    super.setSettings(_settingsProperties.toString());
208            }
209    
210            protected Theme getTheme(String device) throws SystemException {
211                    boolean controlPanel = false;
212    
213                    try {
214                            Group group = getGroup();
215    
216                            controlPanel = group.isControlPanel();
217                    }
218                    catch (Exception e) {
219                    }
220    
221                    if (controlPanel) {
222                            String themeId = PrefsPropsUtil.getString(
223                                    getCompanyId(),
224                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
225    
226                            return ThemeLocalServiceUtil.getTheme(
227                                    getCompanyId(), themeId, !device.equals("regular"));
228                    }
229                    else if (device.equals("regular")) {
230                            return getTheme();
231                    }
232                    else {
233                            return getWapTheme();
234                    }
235            }
236    
237            private static Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
238    
239            private UnicodeProperties _settingsProperties;
240    
241    }