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