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