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.model.CacheField;
021    import com.liferay.portal.kernel.model.ColorScheme;
022    import com.liferay.portal.kernel.model.Company;
023    import com.liferay.portal.kernel.model.Group;
024    import com.liferay.portal.kernel.model.LayoutSet;
025    import com.liferay.portal.kernel.model.LayoutSetPrototype;
026    import com.liferay.portal.kernel.model.Theme;
027    import com.liferay.portal.kernel.model.VirtualHost;
028    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
029    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
030    import com.liferay.portal.kernel.service.LayoutSetPrototypeLocalServiceUtil;
031    import com.liferay.portal.kernel.service.ThemeLocalServiceUtil;
032    import com.liferay.portal.kernel.service.VirtualHostLocalServiceUtil;
033    import com.liferay.portal.kernel.util.PropsKeys;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.UnicodeProperties;
036    import com.liferay.portal.kernel.util.Validator;
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());
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(getCompanyId(), getThemeId());
215            }
216    
217            @Override
218            public String getThemeSetting(String key, String device) {
219                    UnicodeProperties settingsProperties = getSettingsProperties();
220    
221                    String value = settingsProperties.getProperty(
222                            ThemeSettingImpl.namespaceProperty(device, key));
223    
224                    if (value != null) {
225                            return value;
226                    }
227    
228                    Theme theme = getTheme(device);
229    
230                    value = theme.getSetting(key);
231    
232                    return value;
233            }
234    
235            /**
236             * Returns the name of the layout set's virtual host.
237             *
238             * <p>
239             * When accessing a layout set that has a the virtual host, the URL elements
240             * "/web/sitename" or "/group/sitename" can be omitted.
241             * </p>
242             *
243             * @return the layout set's virtual host name, or an empty string if the
244             *         layout set has no virtual host configured
245             */
246            @Override
247            public String getVirtualHostname() {
248                    if (_virtualHostname != null) {
249                            return _virtualHostname;
250                    }
251    
252                    VirtualHost virtualHost = VirtualHostLocalServiceUtil.fetchVirtualHost(
253                            getCompanyId(), getLayoutSetId());
254    
255                    if (virtualHost == null) {
256                            _virtualHostname = StringPool.BLANK;
257                    }
258                    else {
259                            _virtualHostname = virtualHost.getHostname();
260                    }
261    
262                    return _virtualHostname;
263            }
264    
265            @Override
266            public boolean hasSetModifiedDate() {
267                    return true;
268            }
269    
270            @Override
271            public boolean isLayoutSetPrototypeLinkActive() {
272                    if (isLayoutSetPrototypeLinkEnabled() &&
273                            Validator.isNotNull(getLayoutSetPrototypeUuid())) {
274    
275                            return true;
276                    }
277    
278                    return false;
279            }
280    
281            @Override
282            public boolean isLogo() {
283                    return getLogo();
284            }
285    
286            @Override
287            public void setCompanyFallbackVirtualHostname(
288                    String companyFallbackVirtualHostname) {
289    
290                    _companyFallbackVirtualHostname = companyFallbackVirtualHostname;
291            }
292    
293            @Override
294            public void setSettings(String settings) {
295                    _settingsProperties = null;
296    
297                    super.setSettings(settings);
298            }
299    
300            @Override
301            public void setSettingsProperties(UnicodeProperties settingsProperties) {
302                    _settingsProperties = settingsProperties;
303    
304                    super.setSettings(_settingsProperties.toString());
305            }
306    
307            /**
308             * Sets the name of the layout set's virtual host.
309             *
310             * @param virtualHostname the name of the layout set's virtual host
311             * @see   #getVirtualHostname()
312             */
313            @Override
314            public void setVirtualHostname(String virtualHostname) {
315                    _virtualHostname = virtualHostname;
316            }
317    
318            protected Theme getTheme(String device) {
319                    boolean controlPanel = false;
320    
321                    try {
322                            Group group = getGroup();
323    
324                            controlPanel = group.isControlPanel();
325                    }
326                    catch (Exception e) {
327                    }
328    
329                    if (controlPanel) {
330                            String themeId = PrefsPropsUtil.getString(
331                                    getCompanyId(),
332                                    PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
333    
334                            return ThemeLocalServiceUtil.getTheme(getCompanyId(), themeId);
335                    }
336                    else {
337                            return getTheme();
338                    }
339            }
340    
341            private static final Log _log = LogFactoryUtil.getLog(LayoutSetImpl.class);
342    
343            @CacheField
344            private String _companyFallbackVirtualHostname;
345    
346            private UnicodeProperties _settingsProperties;
347    
348            @CacheField
349            private String _virtualHostname;
350    
351    }