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