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 ColorScheme getWapColorScheme()
204                    throws PortalException, SystemException {
205    
206                    if (isInheritLookAndFeel()) {
207                            return getLayoutSet().getWapColorScheme();
208                    }
209                    else {
210                            return ThemeLocalServiceUtil.getColorScheme(
211                                    getCompanyId(), getWapTheme().getThemeId(),
212                                    getWapColorSchemeId(), true);
213                    }
214            }
215    
216            @Override
217            public Theme getWapTheme() throws PortalException, SystemException {
218                    if (isInheritWapLookAndFeel()) {
219                            return getLayoutSet().getWapTheme();
220                    }
221                    else {
222                            return ThemeLocalServiceUtil.getTheme(
223                                    getCompanyId(), getWapThemeId(), true);
224                    }
225            }
226    
227            @Override
228            public boolean hasChildren() throws SystemException {
229                    if (!getChildren().isEmpty()) {
230                            return true;
231                    }
232    
233                    return false;
234            }
235    
236            @Override
237            public boolean isContentDisplayPage() {
238                    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();
239    
240                    String defaultAssetPublisherPortletId =
241                            typeSettingsProperties.getProperty(
242                                    LayoutTypePortletConstants.DEFAULT_ASSET_PUBLISHER_PORTLET_ID);
243    
244                    if (Validator.isNotNull(defaultAssetPublisherPortletId)) {
245                            return true;
246                    }
247                    else {
248                            return false;
249                    }
250            }
251    
252            @Override
253            public boolean isInheritLookAndFeel() {
254                    if (Validator.isNull(getThemeId()) ||
255                            Validator.isNull(getColorSchemeId())) {
256    
257                            return true;
258                    }
259                    else {
260                            return false;
261                    }
262            }
263    
264            @Override
265            public boolean isInheritWapLookAndFeel() {
266                    if (Validator.isNull(getWapThemeId()) ||
267                            Validator.isNull(getWapColorSchemeId())) {
268    
269                            return true;
270                    }
271                    else {
272                            return false;
273                    }
274            }
275    
276            @Override
277            public void setTypeSettings(String typeSettings) {
278                    _typeSettingsProperties = null;
279    
280                    super.setTypeSettings(typeSettings);
281            }
282    
283            @Override
284            public void setTypeSettingsProperties(
285                    UnicodeProperties typeSettingsProperties) {
286    
287                    _typeSettingsProperties = typeSettingsProperties;
288    
289                    super.setTypeSettings(_typeSettingsProperties.toString());
290            }
291    
292            protected Theme getTheme(String device)
293                    throws PortalException, SystemException {
294    
295                    if (device.equals("regular")) {
296                            return getTheme();
297                    }
298                    else {
299                            return getWapTheme();
300                    }
301            }
302    
303            private UnicodeProperties _typeSettingsProperties;
304    
305    }