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.util.CookieKeys;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.ColorScheme;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutBranch;
026    import com.liferay.portal.model.LayoutRevision;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.model.LayoutTypePortletConstants;
029    import com.liferay.portal.model.Theme;
030    import com.liferay.portal.service.LayoutBranchLocalServiceUtil;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
033    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
034    import com.liferay.portal.service.ThemeLocalServiceUtil;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portal.util.WebKeys;
038    
039    import java.util.List;
040    import java.util.Locale;
041    
042    import javax.servlet.http.HttpServletRequest;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class LayoutRevisionImpl extends LayoutRevisionBaseImpl {
048    
049            public LayoutRevisionImpl() {
050            }
051    
052            @Override
053            public List<LayoutRevision> getChildren() {
054                    return LayoutRevisionLocalServiceUtil.getChildLayoutRevisions(
055                            getLayoutSetBranchId(), getLayoutRevisionId(), getPlid());
056            }
057    
058            @Override
059            public ColorScheme getColorScheme() throws PortalException {
060                    if (isInheritLookAndFeel()) {
061                            return getLayoutSet().getColorScheme();
062                    }
063                    else {
064                            return ThemeLocalServiceUtil.getColorScheme(
065                                    getCompanyId(), getTheme().getThemeId(), getColorSchemeId(),
066                                    false);
067                    }
068            }
069    
070            @Override
071            public String getCssText() throws PortalException {
072                    if (isInheritLookAndFeel()) {
073                            return getLayoutSet().getCss();
074                    }
075                    else {
076                            return getCss();
077                    }
078            }
079    
080            @Override
081            public String getHTMLTitle(Locale locale) {
082                    String localeLanguageId = LocaleUtil.toLanguageId(locale);
083    
084                    return getHTMLTitle(localeLanguageId);
085            }
086    
087            @Override
088            public String getHTMLTitle(String localeLanguageId) {
089                    String htmlTitle = getTitle(localeLanguageId);
090    
091                    if (Validator.isNull(htmlTitle)) {
092                            htmlTitle = getName(localeLanguageId);
093                    }
094    
095                    return htmlTitle;
096            }
097    
098            @Override
099            public boolean getIconImage() {
100                    if (getIconImageId() > 0) {
101                            return true;
102                    }
103    
104                    return false;
105            }
106    
107            @Override
108            public LayoutBranch getLayoutBranch() throws PortalException {
109                    return LayoutBranchLocalServiceUtil.getLayoutBranch(
110                            getLayoutBranchId());
111            }
112    
113            @Override
114            public LayoutSet getLayoutSet() throws PortalException {
115                    return LayoutSetLocalServiceUtil.getLayoutSet(
116                            getGroupId(), isPrivateLayout());
117            }
118    
119            @Override
120            public String getRegularURL(HttpServletRequest request)
121                    throws PortalException {
122    
123                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
124                            WebKeys.THEME_DISPLAY);
125    
126                    String portalURL = PortalUtil.getPortalURL(request);
127    
128                    Layout layout = LayoutLocalServiceUtil.getLayout(getPlid());
129    
130                    String url = PortalUtil.getLayoutURL(layout, themeDisplay);
131    
132                    if (!CookieKeys.hasSessionId(request) &&
133                            (url.startsWith(portalURL) || url.startsWith(StringPool.SLASH))) {
134    
135                            url = PortalUtil.getURLWithSessionId(
136                                    url, request.getSession().getId());
137                    }
138    
139                    return url;
140            }
141    
142            @Override
143            public Theme getTheme() throws PortalException {
144                    if (isInheritLookAndFeel()) {
145                            return getLayoutSet().getTheme();
146                    }
147                    else {
148                            return ThemeLocalServiceUtil.getTheme(
149                                    getCompanyId(), getThemeId(), false);
150                    }
151            }
152    
153            @Override
154            public String getThemeSetting(String key, String device) {
155                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
156    
157                    String value = typeSettingsProperties.getProperty(
158                            ThemeSettingImpl.namespaceProperty(device, key));
159    
160                    if (value != null) {
161                            return value;
162                    }
163    
164                    if (!isInheritLookAndFeel()) {
165                            try {
166                                    Theme theme = getTheme(device);
167    
168                                    return theme.getSetting(key);
169                            }
170                            catch (Exception e) {
171                            }
172                    }
173    
174                    try {
175                            LayoutSet layoutSet = getLayoutSet();
176    
177                            value = layoutSet.getThemeSetting(key, device);
178                    }
179                    catch (Exception e) {
180                    }
181    
182                    return value;
183            }
184    
185            @Override
186            public String getTypeSettings() {
187                    if (_typeSettingsProperties == null) {
188                            return super.getTypeSettings();
189                    }
190                    else {
191                            return _typeSettingsProperties.toString();
192                    }
193            }
194    
195            @Override
196            public UnicodeProperties getTypeSettingsProperties() {
197                    if (_typeSettingsProperties == null) {
198                            _typeSettingsProperties = new UnicodeProperties(true);
199    
200                            _typeSettingsProperties.fastLoad(super.getTypeSettings());
201                    }
202    
203                    return _typeSettingsProperties;
204            }
205    
206            @Override
207            public String getTypeSettingsProperty(String key) {
208                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
209    
210                    return typeSettingsProperties.getProperty(key);
211            }
212    
213            @Override
214            public String getTypeSettingsProperty(String key, String defaultValue) {
215                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
216    
217                    return typeSettingsProperties.getProperty(key, defaultValue);
218            }
219    
220            @Override
221            public ColorScheme getWapColorScheme() throws PortalException {
222                    if (isInheritLookAndFeel()) {
223                            return getLayoutSet().getWapColorScheme();
224                    }
225                    else {
226                            return ThemeLocalServiceUtil.getColorScheme(
227                                    getCompanyId(), getWapTheme().getThemeId(),
228                                    getWapColorSchemeId(), true);
229                    }
230            }
231    
232            @Override
233            public Theme getWapTheme() throws PortalException {
234                    if (isInheritWapLookAndFeel()) {
235                            return getLayoutSet().getWapTheme();
236                    }
237                    else {
238                            return ThemeLocalServiceUtil.getTheme(
239                                    getCompanyId(), getWapThemeId(), true);
240                    }
241            }
242    
243            @Override
244            public boolean hasChildren() {
245                    if (!getChildren().isEmpty()) {
246                            return true;
247                    }
248    
249                    return false;
250            }
251    
252            @Override
253            public boolean isContentDisplayPage() {
254                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
255    
256                    String defaultAssetPublisherPortletId =
257                            typeSettingsProperties.getProperty(
258                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
259    
260                    if (Validator.isNotNull(defaultAssetPublisherPortletId)) {
261                            return true;
262                    }
263                    else {
264                            return false;
265                    }
266            }
267    
268            @Override
269            public boolean isIconImage() {
270                    return getIconImage();
271            }
272    
273            @Override
274            public boolean isInheritLookAndFeel() {
275                    if (Validator.isNull(getThemeId()) ||
276                            Validator.isNull(getColorSchemeId())) {
277    
278                            return true;
279                    }
280                    else {
281                            return false;
282                    }
283            }
284    
285            @Override
286            public boolean isInheritWapLookAndFeel() {
287                    if (Validator.isNull(getWapThemeId()) ||
288                            Validator.isNull(getWapColorSchemeId())) {
289    
290                            return true;
291                    }
292                    else {
293                            return false;
294                    }
295            }
296    
297            @Override
298            public void setTypeSettings(String typeSettings) {
299                    _typeSettingsProperties = null;
300    
301                    super.setTypeSettings(typeSettings);
302            }
303    
304            @Override
305            public void setTypeSettingsProperties(
306                    UnicodeProperties typeSettingsProperties) {
307    
308                    _typeSettingsProperties = typeSettingsProperties;
309    
310                    super.setTypeSettings(_typeSettingsProperties.toString());
311            }
312    
313            protected Theme getTheme(String device) throws PortalException {
314                    if (device.equals("regular")) {
315                            return getTheme();
316                    }
317                    else {
318                            return getWapTheme();
319                    }
320            }
321    
322            private UnicodeProperties _typeSettingsProperties;
323    
324    }