001    /**
002     * Copyright (c) 2000-present 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.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.CacheField;
025    import com.liferay.portal.model.ColorScheme;
026    import com.liferay.portal.model.Company;
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.CompanyLocalServiceUtil;
033    import com.liferay.portal.service.GroupLocalServiceUtil;
034    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
035    import com.liferay.portal.service.ThemeLocalServiceUtil;
036    import com.liferay.portal.service.VirtualHostLocalServiceUtil;
037    import com.liferay.portal.util.PrefsPropsUtil;
038    import com.liferay.portal.util.PropsValues;
039    
040    import java.io.IOException;
041    
042    /**
043     * Represents a portal layout set, providing access to the layout set's color
044     * schemes, groups, prototypes, themes, and more.
045     *
046     * <p>
047     * Each {@link Group} in Liferay can have a public and a private layout set.
048     * This keeps information common to all layouts (pages) in the layout set.
049     * </p>
050     *
051     * @author Brian Wing Shun Chan
052     * @author Jorge Ferrer
053     */
054    public class LayoutSetImpl extends LayoutSetBaseImpl {
055    
056            /**
057             * Returns the layout set's color scheme.
058             *
059             * <p>
060             * Just like themes, color schemes can be configured on the layout set
061             * level. The layout set's color scheme can be overridden on the layout
062             * level.
063             * </p>
064             *
065             * @return the layout set's color scheme
066             */
067            @Override
068            public ColorScheme getColorScheme() {
069                    return ThemeLocalServiceUtil.getColorScheme(
070                            getCompanyId(), getTheme().getThemeId(), getColorSchemeId(), false);
071            }
072    
073            @Override
074            public String getCompanyFallbackVirtualHostname() {
075                    if (_companyFallbackVirtualHostname != null) {
076                            return _companyFallbackVirtualHostname;
077                    }
078    
079                    _companyFallbackVirtualHostname = StringPool.BLANK;
080    
081                    if (Validator.isNotNull(
082                                    PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME) &&
083                            !isPrivateLayout()) {
084    
085                            Group group = GroupLocalServiceUtil.fetchGroup(
086                                    getCompanyId(), PropsValues.VIRTUAL_HOSTS_DEFAULT_SITE_NAME);
087    
088                            if ((group != null) && (getGroupId() == group.getGroupId())) {
089                                    Company company = CompanyLocalServiceUtil.fetchCompany(
090                                            getCompanyId());
091    
092                                    if (company != null) {
093                                            _companyFallbackVirtualHostname =
094                                                    company.getVirtualHostname();
095                                    }
096                            }
097                    }
098    
099                    return _companyFallbackVirtualHostname;
100            }
101    
102            /**
103             * Returns the layout set's group.
104             *
105             * @return the layout set's group
106             * @throws PortalException if a group with the primary key could not be
107             *         found
108             */
109            @Override
110            public Group getGroup() throws PortalException {
111                    return GroupLocalServiceUtil.getGroup(getGroupId());
112            }
113    
114            /**
115             * Returns the layout set prototype's ID, or <code>0</code> if it has no
116             * layout set prototype.
117             *
118             * <p>
119             * Prototype is Liferay's technical name for a site template.
120             * </p>
121             *
122             * @return the layout set prototype's ID, or <code>0</code> if it has no
123             *         layout set prototype
124             * @throws PortalException if a matching layout set prototype could not be
125             *         found
126             */
127            @Override
128            public long getLayoutSetPrototypeId() throws PortalException {
129                    String layoutSetPrototypeUuid = getLayoutSetPrototypeUuid();
130    
131                    if (Validator.isNull(layoutSetPrototypeUuid)) {
132                            return 0;
133                    }
134    
135                    LayoutSetPrototype layoutSetPrototype =
136                            LayoutSetPrototypeLocalServiceUtil.
137                                    getLayoutSetPrototypeByUuidAndCompanyId(
138                                            layoutSetPrototypeUuid, getCompanyId());
139    
140                    return layoutSetPrototype.getLayoutSetPrototypeId();
141            }
142    
143            @Override
144            public long getLiveLogoId() {
145                    long logoId = 0;
146    
147                    Group group = null;
148    
149                    try {
150                            group = getGroup();
151    
152                            if (!group.isStagingGroup()) {
153                                    return logoId;
154                            }
155                    }
156                    catch (Exception e) {
157                            return logoId;
158                    }
159    
160                    Group liveGroup = group.getLiveGroup();
161    
162                    LayoutSet liveLayoutSet = null;
163    
164                    if (isPrivateLayout()) {
165                            liveLayoutSet = liveGroup.getPrivateLayoutSet();
166                    }
167                    else {
168                            liveLayoutSet = liveGroup.getPublicLayoutSet();
169                    }
170    
171                    return liveLayoutSet.getLogoId();
172            }
173    
174            @Override
175            public boolean getLogo() {
176                    if (getLogoId() > 0) {
177                            return true;
178                    }
179    
180                    return false;
181            }
182    
183            @Override
184            public String getSettings() {
185                    if (_settingsProperties == null) {
186                            return super.getSettings();
187                    }
188                    else {
189                            return _settingsProperties.toString();
190                    }
191            }
192    
193            @Override
194            public UnicodeProperties getSettingsProperties() {
195                    if (_settingsProperties == null) {
196                            _settingsProperties = new UnicodeProperties(true);
197    
198                            try {
199                                    _settingsProperties.load(super.getSettings());
200                            }
201                            catch (IOException ioe) {
202                                    _log.error(ioe, ioe);
203                            }
204                    }
205    
206                    return _settingsProperties;
207            }
208    
209            @Override
210            public String getSettingsProperty(String key) {
211                    UnicodeProperties settingsProperties = getSettingsProperties();
212    
213                    return settingsProperties.getProperty(key);
214            }
215    
216            @Override
217            public Theme getTheme() {
218                    return ThemeLocalServiceUtil.getTheme(
219                            getCompanyId(), getThemeId(), false);
220            }
221    
222            @Override
223            public String getThemeSetting(String key, String device) {
224                    UnicodeProperties settingsProperties = getSettingsProperties();
225    
226                    String value = settingsProperties.getProperty(
227                            ThemeSettingImpl.namespaceProperty(device, key));
228    
229                    if (value != null) {
230                            return value;
231                    }
232    
233                    Theme theme = getTheme(device);
234    
235                    value = theme.getSetting(key);
236    
237                    return value;
238            }
239    
240            /**
241             * Returns the name of the layout set's virtual host.
242             *
243             * <p>
244             * When accessing a layout set that has a the virtual host, the URL elements
245             * "/web/sitename" or "/group/sitename" can be omitted.
246             * </p>
247             *
248             * @return the layout set's virtual host name, or an empty string if the
249             *         layout set has no virtual host configured
250             */
251            @Override
252            public String getVirtualHostname() {
253                    if (_virtualHostname != null) {
254                            return _virtualHostname;
255                    }
256    
257                    VirtualHost virtualHost = VirtualHostLocalServiceUtil.fetchVirtualHost(
258                            getCompanyId(), getLayoutSetId());
259    
260                    if (virtualHost == null) {
261                            _virtualHostname = StringPool.BLANK;
262                    }
263                    else {
264                            _virtualHostname = virtualHost.getHostname();
265                    }
266    
267                    return _virtualHostname;
268            }
269    
270            @Override
271            public ColorScheme getWapColorScheme() {
272                    return ThemeLocalServiceUtil.getColorScheme(
273                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
274                            true);
275            }
276    
277            @Override
278            public Theme getWapTheme() {
279                    return ThemeLocalServiceUtil.getTheme(
280                            getCompanyId(), getWapThemeId(), true);
281            }
282    
283            @Override
284            public boolean hasSetModifiedDate() {
285                    return true;
286            }
287    
288            @Override
289            public boolean isLayoutSetPrototypeLinkActive() {
290                    if (isLayoutSetPrototypeLinkEnabled() &&
291                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
292    
293                            return true;
294                    }
295    
296                    return false;
297            }
298    
299            @Override
300            public boolean isLogo() {
301                    return getLogo();
302            }
303    
304            @Override
305            public void setCompanyFallbackVirtualHostname(
306                    String companyFallbackVirtualHostname) {
307    
308                    _companyFallbackVirtualHostname = companyFallbackVirtualHostname;
309            }
310    
311            @Override
312            public void setSettings(String settings) {
313                    _settingsProperties = null;
314    
315                    super.setSettings(settings);
316            }
317    
318            @Override
319            public void setSettingsProperties(UnicodeProperties settingsProperties) {
320                    _settingsProperties = settingsProperties;
321    
322                    super.setSettings(_settingsProperties.toString());
323            }
324    
325            /**
326             * Sets the name of the layout set's virtual host.
327             *
328             * @param virtualHostname the name of the layout set's virtual host
329             * @see   #getVirtualHostname()
330             */
331            @Override
332            public void setVirtualHostname(String virtualHostname) {
333                    _virtualHostname = virtualHostname;
334            }
335    
336            protected Theme getTheme(String device) {
337                    boolean controlPanel = false;
338    
339                    try {
340                            Group group = getGroup();
341    
342                            controlPanel = group.isControlPanel();
343                    }
344                    catch (Exception e) {
345                    }
346    
347                    if (controlPanel) {
348                            String themeId = PrefsPropsUtil.getString(
349                                    getCompanyId(),
350                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
351    
352                            return ThemeLocalServiceUtil.getTheme(
353                                    getCompanyId(), themeId, !device.equals("regular"));
354                    }
355                    else if (device.equals("regular")) {
356                            return getTheme();
357                    }
358                    else {
359                            return getWapTheme();
360                    }
361            }
362    
363            private static final Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
364    
365            @CacheField
366            private String _companyFallbackVirtualHostname;
367    
368            private UnicodeProperties _settingsProperties;
369    
370            @CacheField
371            private String _virtualHostname;
372    
373    }