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             */
107            @Override
108            public Group getGroup() throws PortalException {
109                    return GroupLocalServiceUtil.getGroup(getGroupId());
110            }
111    
112            /**
113             * Returns the layout set prototype's ID, or <code>0</code> if it has no
114             * layout set prototype.
115             *
116             * <p>
117             * Prototype is Liferay's technical name for a site template.
118             * </p>
119             *
120             * @return the layout set prototype's ID, or <code>0</code> if it has no
121             *         layout set prototype
122             */
123            @Override
124            public long getLayoutSetPrototypeId() throws PortalException {
125                    String layoutSetPrototypeUuid = getLayoutSetPrototypeUuid();
126    
127                    if (Validator.isNull(layoutSetPrototypeUuid)) {
128                            return 0;
129                    }
130    
131                    LayoutSetPrototype layoutSetPrototype =
132                            LayoutSetPrototypeLocalServiceUtil.
133                                    getLayoutSetPrototypeByUuidAndCompanyId(
134                                            layoutSetPrototypeUuid, getCompanyId());
135    
136                    return layoutSetPrototype.getLayoutSetPrototypeId();
137            }
138    
139            @Override
140            public long getLiveLogoId() {
141                    long logoId = 0;
142    
143                    Group group = null;
144    
145                    try {
146                            group = getGroup();
147    
148                            if (!group.isStagingGroup()) {
149                                    return logoId;
150                            }
151                    }
152                    catch (Exception e) {
153                            return logoId;
154                    }
155    
156                    Group liveGroup = group.getLiveGroup();
157    
158                    LayoutSet liveLayoutSet = null;
159    
160                    if (isPrivateLayout()) {
161                            liveLayoutSet = liveGroup.getPrivateLayoutSet();
162                    }
163                    else {
164                            liveLayoutSet = liveGroup.getPublicLayoutSet();
165                    }
166    
167                    return liveLayoutSet.getLogoId();
168            }
169    
170            @Override
171            public boolean getLogo() {
172                    if (getLogoId() > 0) {
173                            return true;
174                    }
175    
176                    return false;
177            }
178    
179            @Override
180            public String getSettings() {
181                    if (_settingsProperties == null) {
182                            return super.getSettings();
183                    }
184                    else {
185                            return _settingsProperties.toString();
186                    }
187            }
188    
189            @Override
190            public UnicodeProperties getSettingsProperties() {
191                    if (_settingsProperties == null) {
192                            _settingsProperties = new UnicodeProperties(true);
193    
194                            try {
195                                    _settingsProperties.load(super.getSettings());
196                            }
197                            catch (IOException ioe) {
198                                    _log.error(ioe, ioe);
199                            }
200                    }
201    
202                    return _settingsProperties;
203            }
204    
205            @Override
206            public String getSettingsProperty(String key) {
207                    UnicodeProperties settingsProperties = getSettingsProperties();
208    
209                    return settingsProperties.getProperty(key);
210            }
211    
212            @Override
213            public Theme getTheme() {
214                    return ThemeLocalServiceUtil.getTheme(
215                            getCompanyId(), getThemeId(), false);
216            }
217    
218            @Override
219            public String getThemeSetting(String key, String device) {
220                    UnicodeProperties settingsProperties = getSettingsProperties();
221    
222                    String value = settingsProperties.getProperty(
223                            ThemeSettingImpl.namespaceProperty(device, key));
224    
225                    if (value != null) {
226                            return value;
227                    }
228    
229                    Theme theme = getTheme(device);
230    
231                    value = theme.getSetting(key);
232    
233                    return value;
234            }
235    
236            /**
237             * Returns the name of the layout set's virtual host.
238             *
239             * <p>
240             * When accessing a layout set that has a the virtual host, the URL elements
241             * "/web/sitename" or "/group/sitename" can be omitted.
242             * </p>
243             *
244             * @return the layout set's virtual host name, or an empty string if the
245             *         layout set has no virtual host configured
246             */
247            @Override
248            public String getVirtualHostname() {
249                    if (_virtualHostname != null) {
250                            return _virtualHostname;
251                    }
252    
253                    VirtualHost virtualHost = VirtualHostLocalServiceUtil.fetchVirtualHost(
254                            getCompanyId(), getLayoutSetId());
255    
256                    if (virtualHost == null) {
257                            _virtualHostname = StringPool.BLANK;
258                    }
259                    else {
260                            _virtualHostname = virtualHost.getHostname();
261                    }
262    
263                    return _virtualHostname;
264            }
265    
266            @Override
267            public ColorScheme getWapColorScheme() {
268                    return ThemeLocalServiceUtil.getColorScheme(
269                            getCompanyId(), getWapTheme().getThemeId(), getWapColorSchemeId(),
270                            true);
271            }
272    
273            @Override
274            public Theme getWapTheme() {
275                    return ThemeLocalServiceUtil.getTheme(
276                            getCompanyId(), getWapThemeId(), true);
277            }
278    
279            @Override
280            public boolean hasSetModifiedDate() {
281                    return true;
282            }
283    
284            @Override
285            public boolean isLayoutSetPrototypeLinkActive() {
286                    if (isLayoutSetPrototypeLinkEnabled() &&
287                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
288    
289                            return true;
290                    }
291    
292                    return false;
293            }
294    
295            @Override
296            public boolean isLogo() {
297                    return getLogo();
298            }
299    
300            @Override
301            public void setCompanyFallbackVirtualHostname(
302                    String companyFallbackVirtualHostname) {
303    
304                    _companyFallbackVirtualHostname = companyFallbackVirtualHostname;
305            }
306    
307            @Override
308            public void setSettings(String settings) {
309                    _settingsProperties = null;
310    
311                    super.setSettings(settings);
312            }
313    
314            @Override
315            public void setSettingsProperties(UnicodeProperties settingsProperties) {
316                    _settingsProperties = settingsProperties;
317    
318                    super.setSettings(_settingsProperties.toString());
319            }
320    
321            /**
322             * Sets the name of the layout set's virtual host.
323             *
324             * @param virtualHostname the name of the layout set's virtual host
325             * @see   #getVirtualHostname()
326             */
327            @Override
328            public void setVirtualHostname(String virtualHostname) {
329                    _virtualHostname = virtualHostname;
330            }
331    
332            protected Theme getTheme(String device) {
333                    boolean controlPanel = false;
334    
335                    try {
336                            Group group = getGroup();
337    
338                            controlPanel = group.isControlPanel();
339                    }
340                    catch (Exception e) {
341                    }
342    
343                    if (controlPanel) {
344                            String themeId = PrefsPropsUtil.getString(
345                                    getCompanyId(),
346                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
347    
348                            return ThemeLocalServiceUtil.getTheme(
349                                    getCompanyId(), themeId, !device.equals("regular"));
350                    }
351                    else if (device.equals("regular")) {
352                            return getTheme();
353                    }
354                    else {
355                            return getWapTheme();
356                    }
357            }
358    
359            private static final Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
360    
361            @CacheField
362            private String _companyFallbackVirtualHostname;
363    
364            private UnicodeProperties _settingsProperties;
365    
366            @CacheField
367            private String _virtualHostname;
368    
369    }