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