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.util;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutConstants;
026    import com.liferay.portal.security.permission.ActionKeys;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.security.permission.PermissionThreadLocal;
029    import com.liferay.portal.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.service.permission.LayoutPermissionUtil;
031    
032    import java.util.HashMap;
033    import java.util.Map;
034    
035    /**
036     * @author Shuyang Zhou
037     * @author Brian Wing Shun Chan
038     */
039    public class LayoutSettings {
040    
041            public static void addLayoutSetting(String type) {
042                    _layoutSettingsMap.put(type, new LayoutSettings(type));
043            }
044    
045            public static LayoutSettings getInstance(Layout layout) {
046                    return getInstance(layout.getType());
047            }
048    
049            public static LayoutSettings getInstance(String type) {
050                    return _layoutSettingsMap.get(type);
051            }
052    
053            public static Map<String, LayoutSettings> getLayoutSettingsMap() {
054                    return _layoutSettingsMap;
055            }
056    
057            public String[] getConfigurationActionDelete() {
058                    return _configurationActionDelete;
059            }
060    
061            public String[] getConfigurationActionUpdate() {
062                    return _configurationActionUpdate;
063            }
064    
065            public String getEditPage() {
066                    return _editPage;
067            }
068    
069            public String getType() {
070                    return _type;
071            }
072    
073            public String getURL() {
074                    return _url;
075            }
076    
077            public String getURL(Map<String, String> variables) {
078                    long plid = GetterUtil.getLong(variables.get("liferay:plid"));
079    
080                    String url = getDefaultURL();
081    
082                    try {
083                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
084    
085                            if (hasViewPermission(layout)) {
086                                    url = getURL();
087                            }
088                    }
089                    catch (Exception e) {
090                            _log.error(e);
091                    }
092    
093                    return replaceVariables(url, variables);
094            }
095    
096            public String getViewPage() {
097                    return _viewPage;
098            }
099    
100            public boolean isFirstPageable() {
101                    return _firstPageable;
102            }
103    
104            public boolean isParentable() {
105                    return _parentable;
106            }
107    
108            public boolean isSitemapable() {
109                    return _sitemapable;
110            }
111    
112            public boolean isURLFriendliable() {
113                    return _urlFriendliable;
114            }
115    
116            protected boolean hasViewPermission(Layout layout) {
117                    if (layout.isTypeControlPanel()) {
118                            return false;
119                    }
120    
121                    PermissionChecker permissionChecker =
122                            PermissionThreadLocal.getPermissionChecker();
123    
124                    if (permissionChecker == null) {
125                            return false;
126                    }
127    
128                    try {
129                            return LayoutPermissionUtil.contains(
130                                    permissionChecker, layout, ActionKeys.VIEW);
131                    }
132                    catch (Exception e) {
133                            _log.error(e);
134    
135                            return false;
136                    }
137            }
138    
139            protected String replaceVariables(
140                    String url, Map<String, String> variables) {
141    
142                    return StringUtil.replace(
143                            url, StringPool.DOLLAR_AND_OPEN_CURLY_BRACE,
144                            StringPool.CLOSE_CURLY_BRACE, variables);
145            }
146    
147            protected String getDefaultURL() {
148                    return _URL;
149            }
150    
151            private LayoutSettings(String type) {
152                    _type = type;
153    
154                    Filter filter = new Filter(type);
155    
156                    _configurationActionDelete = StringUtil.split(
157                            GetterUtil.getString(
158                                    PropsUtil.get(
159                                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE, filter)));
160                    _configurationActionUpdate = StringUtil.split(
161                            GetterUtil.getString(
162                                    PropsUtil.get(
163                                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE, filter)));
164                    _editPage = GetterUtil.getString(
165                            PropsUtil.get(PropsKeys.LAYOUT_EDIT_PAGE, filter));
166                    _firstPageable = GetterUtil.getBoolean(
167                            PropsUtil.get(PropsKeys.LAYOUT_FIRST_PAGEABLE, filter));
168                    _parentable = GetterUtil.getBoolean(
169                            PropsUtil.get(PropsKeys.LAYOUT_PARENTABLE, filter), true);
170                    _sitemapable = GetterUtil.getBoolean(
171                            PropsUtil.get(PropsKeys.LAYOUT_SITEMAPABLE, filter), true);
172                    _url = GetterUtil.getString(
173                            PropsUtil.get(PropsKeys.LAYOUT_URL, filter));
174                    _urlFriendliable = GetterUtil.getBoolean(
175                            PropsUtil.get(PropsKeys.LAYOUT_URL_FRIENDLIABLE, filter), true);
176                    _viewPage = GetterUtil.getString(
177                            PropsUtil.get(PropsKeys.LAYOUT_VIEW_PAGE, filter));
178            }
179    
180            private static Log _log = LogFactoryUtil.getLog(LayoutSettings.class);
181    
182            private static final String _URL =
183                    "${liferay:mainPath}/portal/layout?p_l_id=${liferay:plid}&" +
184                            "p_v_l_s_g_id=${liferay:pvlsgid}";
185    
186            private static Map<String, LayoutSettings> _layoutSettingsMap =
187                    new HashMap<String, LayoutSettings>();
188    
189            static {
190                    _layoutSettingsMap.put(
191                            LayoutConstants.TYPE_CONTROL_PANEL,
192                            new LayoutSettings(LayoutConstants.TYPE_CONTROL_PANEL));
193    
194                    for (String type : PropsValues.LAYOUT_TYPES) {
195                            _layoutSettingsMap.put(type, new LayoutSettings(type));
196                    }
197            }
198    
199            private String[] _configurationActionDelete;
200            private String[] _configurationActionUpdate;
201            private String _editPage;
202            private boolean _firstPageable;
203            private boolean _parentable;
204            private boolean _sitemapable;
205            private String _type;
206            private String _url;
207            private boolean _urlFriendliable;
208            private String _viewPage;
209    
210    }