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.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.
068                                    getLayoutSetPrototypeByUuidAndCompanyId(
069                                            layoutSetPrototypeUuid, getCompanyId());
070    
071                    return layoutSetPrototype.getLayoutSetPrototypeId();
072            }
073    
074            public long getLiveLogoId() {
075                    long logoId = 0;
076    
077                    Group group = null;
078    
079                    try {
080                            group = getGroup();
081    
082                            if (!group.isStagingGroup()) {
083                                    return logoId;
084                            }
085                    }
086                    catch (Exception e) {
087                            return logoId;
088                    }
089    
090                    Group liveGroup = group.getLiveGroup();
091    
092                    LayoutSet liveLayoutSet = null;
093    
094                    if (isPrivateLayout()) {
095                            liveLayoutSet = liveGroup.getPrivateLayoutSet();
096                    }
097                    else {
098                            liveLayoutSet = liveGroup.getPublicLayoutSet();
099                    }
100    
101                    return liveLayoutSet.getLogoId();
102            }
103    
104            @Override
105            public String getSettings() {
106                    if (_settingsProperties == null) {
107                            return super.getSettings();
108                    }
109                    else {
110                            return _settingsProperties.toString();
111                    }
112            }
113    
114            public UnicodeProperties getSettingsProperties() {
115                    if (_settingsProperties == null) {
116                            _settingsProperties = new UnicodeProperties(true);
117    
118                            try {
119                                    _settingsProperties.load(super.getSettings());
120                            }
121                            catch (IOException ioe) {
122                                    _log.error(ioe, ioe);
123                            }
124                    }
125    
126                    return _settingsProperties;
127            }
128    
129            public String getSettingsProperty(String key) {
130                    UnicodeProperties settingsProperties = getSettingsProperties();
131    
132                    return settingsProperties.getProperty(key);
133            }
134    
135            public Theme getTheme() throws SystemException {
136                    return ThemeLocalServiceUtil.getTheme(
137                            getCompanyId(), getThemeId(), false);
138            }
139    
140            public String getThemeSetting(String key, String device)
141                    throws SystemException {
142    
143                    UnicodeProperties settingsProperties = getSettingsProperties();
144    
145                    String value = settingsProperties.getProperty(
146                            ThemeSettingImpl.namespaceProperty(device, key));
147    
148                    if (value != null) {
149                            return value;
150                    }
151    
152                    Theme theme = getTheme(device);
153    
154                    value = theme.getSetting(key);
155    
156                    return value;
157            }
158    
159            public String getVirtualHostname() {
160                    try {
161                            VirtualHost virtualHost =
162                                    VirtualHostLocalServiceUtil.fetchVirtualHost(
163                                            getCompanyId(), getLayoutSetId());
164    
165                            if (virtualHost == null) {
166                                    return StringPool.BLANK;
167                            }
168                            else {
169                                    return virtualHost.getHostname();
170                            }
171                    }
172                    catch (Exception e) {
173                            return StringPool.BLANK;
174                    }
175            }
176    
177            public ColorScheme getWapColorScheme() throws SystemException {
178                    return ThemeLocalServiceUtil.getColorScheme(
179                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
180                            true);
181            }
182    
183            public Theme getWapTheme() throws SystemException {
184                    return ThemeLocalServiceUtil.getTheme(
185                            getCompanyId(), getWapThemeId(), true);
186            }
187    
188            public boolean isLayoutSetPrototypeLinkActive() {
189                    if (isLayoutSetPrototypeLinkEnabled() &&
190                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
191    
192                            return true;
193                    }
194    
195                    return false;
196            }
197    
198            @Override
199            public void setSettings(String settings) {
200                    _settingsProperties = null;
201    
202                    super.setSettings(settings);
203            }
204    
205            public void setSettingsProperties(UnicodeProperties settingsProperties) {
206                    _settingsProperties = settingsProperties;
207    
208                    super.setSettings(_settingsProperties.toString());
209            }
210    
211            protected Theme getTheme(String device) throws SystemException {
212                    boolean controlPanel = false;
213    
214                    try {
215                            Group group = getGroup();
216    
217                            controlPanel = group.isControlPanel();
218                    }
219                    catch (Exception e) {
220                    }
221    
222                    if (controlPanel) {
223                            String themeId = PrefsPropsUtil.getString(
224                                    getCompanyId(),
225                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
226    
227                            return ThemeLocalServiceUtil.getTheme(
228                                    getCompanyId(), themeId, !device.equals("regular"));
229                    }
230                    else if (device.equals("regular")) {
231                            return getTheme();
232                    }
233                    else {
234                            return getWapTheme();
235                    }
236            }
237    
238            private static Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
239    
240            private UnicodeProperties _settingsProperties;
241    
242    }