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