001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PermissionThreadLocal;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.permission.LayoutPermissionUtil;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    /**
035     * @author Shuyang Zhou
036     * @author Brian Wing Shun Chan
037     */
038    public class LayoutSettings {
039    
040            public static void addLayoutSetting(String type) {
041                    _layoutSettingsMap.put(type, new LayoutSettings(type));
042            }
043    
044            public static LayoutSettings getInstance(Layout layout) {
045                    return getInstance(layout.getType());
046            }
047    
048            public static LayoutSettings getInstance(String type) {
049                    return _layoutSettingsMap.get(type);
050            }
051    
052            public static Map<String, LayoutSettings> getLayoutSettingsMap() {
053                    return _layoutSettingsMap;
054            }
055    
056            public String[] getConfigurationActionDelete() {
057                    return _configurationActionDelete;
058            }
059    
060            public String[] getConfigurationActionUpdate() {
061                    return _configurationActionUpdate;
062            }
063    
064            public String getEditPage() {
065                    return _editPage;
066            }
067    
068            public String getType() {
069                    return _type;
070            }
071    
072            public String getURL() {
073                    return _url;
074            }
075    
076            public String getURL(Map<String, String> variables) {
077                    long plid = GetterUtil.getLong(variables.get("liferay:plid"));
078    
079                    String url = getDefaultURL();
080    
081                    try {
082                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
083    
084                            if (hasViewPermission(layout)) {
085                                    url = getURL();
086                            }
087                    }
088                    catch (Exception e) {
089                            _log.error(e);
090                    }
091    
092                    return replaceVariables(url, variables);
093            }
094    
095            public String getViewPage() {
096                    return _viewPage;
097            }
098    
099            public boolean isFirstPageable() {
100                    return _firstPageable;
101            }
102    
103            public boolean isParentable() {
104                    return _parentable;
105            }
106    
107            public boolean isSitemapable() {
108                    return _sitemapable;
109            }
110    
111            public boolean isURLFriendliable() {
112                    return _urlFriendliable;
113            }
114    
115            protected boolean hasViewPermission(Layout layout) {
116                    if (layout.isTypeControlPanel()) {
117                            return false;
118                    }
119    
120                    try {
121                            return LayoutPermissionUtil.contains(
122                                    PermissionThreadLocal.getPermissionChecker(), layout,
123                                    ActionKeys.VIEW);
124                    }
125                    catch (Exception e) {
126                            _log.error(e);
127    
128                            return false;
129                    }
130            }
131    
132            protected String replaceVariables(
133                    String url, Map<String, String> variables) {
134    
135                    return StringUtil.replace(
136                            url, StringPool.DOLLAR_AND_OPEN_CURLY_BRACE,
137                            StringPool.CLOSE_CURLY_BRACE, variables);
138            }
139    
140            protected String getDefaultURL() {
141                    return _URL;
142            }
143    
144            private LayoutSettings(String type) {
145                    _type = type;
146    
147                    Filter filter = new Filter(type);
148    
149                    _configurationActionDelete = StringUtil.split(
150                            GetterUtil.getString(
151                                    PropsUtil.get(
152                                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE, filter)));
153                    _configurationActionUpdate = StringUtil.split(
154                            GetterUtil.getString(
155                                    PropsUtil.get(
156                                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE, filter)));
157                    _editPage = GetterUtil.getString(
158                            PropsUtil.get(PropsKeys.LAYOUT_EDIT_PAGE, filter));
159                    _firstPageable = GetterUtil.getBoolean(
160                            PropsUtil.get(PropsKeys.LAYOUT_FIRST_PAGEABLE, filter));
161                    _parentable = GetterUtil.getBoolean(
162                            PropsUtil.get(PropsKeys.LAYOUT_PARENTABLE, filter), true);
163                    _sitemapable = GetterUtil.getBoolean(
164                            PropsUtil.get(PropsKeys.LAYOUT_SITEMAPABLE, filter), true);
165                    _url = GetterUtil.getString(
166                            PropsUtil.get(PropsKeys.LAYOUT_URL, filter));
167                    _urlFriendliable = GetterUtil.getBoolean(
168                            PropsUtil.get(PropsKeys.LAYOUT_URL_FRIENDLIABLE, filter), true);
169                    _viewPage = GetterUtil.getString(
170                            PropsUtil.get(PropsKeys.LAYOUT_VIEW_PAGE, filter));
171            }
172    
173            private static Log _log = LogFactoryUtil.getLog(LayoutSettings.class);
174    
175            private static final String _URL =
176                    "${liferay:mainPath}/portal/layout?p_l_id=${liferay:plid}&" +
177                            "p_v_l_s_g_id=${liferay:pvlsgid}";
178    
179            private static Map<String, LayoutSettings> _layoutSettingsMap =
180                    new HashMap<String, LayoutSettings>();
181    
182            static {
183                    _layoutSettingsMap.put(
184                            LayoutConstants.TYPE_CONTROL_PANEL,
185                            new LayoutSettings(LayoutConstants.TYPE_CONTROL_PANEL));
186    
187                    for (String type : PropsValues.LAYOUT_TYPES) {
188                            _layoutSettingsMap.put(type, new LayoutSettings(type));
189                    }
190            }
191    
192            private String[] _configurationActionDelete;
193            private String[] _configurationActionUpdate;
194            private String _editPage;
195            private boolean _firstPageable;
196            private boolean _parentable;
197            private boolean _sitemapable;
198            private String _type;
199            private String _url;
200            private boolean _urlFriendliable;
201            private String _viewPage;
202    
203    }