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.kernel.display.context.util;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.kernel.util.JavaConstants;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.theme.PortletDisplay;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.PortletURLUtil;
030    
031    import java.util.Locale;
032    
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletResponse;
035    import javax.portlet.PortletURL;
036    
037    import javax.servlet.http.HttpServletRequest;
038    
039    /**
040     * @author Iv??n Zaera
041     */
042    public abstract class BaseRequestHelper {
043    
044            public BaseRequestHelper(HttpServletRequest request) {
045                    _request = request;
046            }
047    
048            public Company getCompany() {
049                    if (_company == null) {
050                            ThemeDisplay themeDisplay = getThemeDisplay();
051    
052                            _company = themeDisplay.getCompany();
053                    }
054    
055                    return _company;
056            }
057    
058            public long getCompanyId() {
059                    if (_companyId == null) {
060                            ThemeDisplay themeDisplay = getThemeDisplay();
061    
062                            _companyId = themeDisplay.getCompanyId();
063                    }
064    
065                    return _companyId;
066            }
067    
068            public String getCurrentURL() {
069                    if (_currentURL == null) {
070                            PortletURL portletURL = PortletURLUtil.getCurrent(
071                                    getLiferayPortletRequest(), getLiferayPortletResponse());
072    
073                            _currentURL = portletURL.toString();
074                    }
075    
076                    return _currentURL;
077            }
078    
079            public Layout getLayout() {
080                    if (_layout == null) {
081                            ThemeDisplay themeDisplay = getThemeDisplay();
082    
083                            _layout = themeDisplay.getLayout();
084                    }
085    
086                    return _layout;
087            }
088    
089            public LiferayPortletRequest getLiferayPortletRequest() {
090                    if (_liferayPortletRequest == null) {
091                            PortletRequest portletRequest =
092                                    (PortletRequest)_request.getAttribute(
093                                            JavaConstants.JAVAX_PORTLET_REQUEST);
094    
095                            _liferayPortletRequest = PortalUtil.getLiferayPortletRequest(
096                                    portletRequest);
097                    }
098    
099                    return _liferayPortletRequest;
100            }
101    
102            public LiferayPortletResponse getLiferayPortletResponse() {
103                    if (_liferayPortletResponse == null) {
104                            PortletResponse portletResponse =
105                                    (PortletResponse)_request.getAttribute(
106                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
107    
108                            _liferayPortletResponse = PortalUtil.getLiferayPortletResponse(
109                                    portletResponse);
110                    }
111    
112                    return _liferayPortletResponse;
113            }
114    
115            public Locale getLocale() {
116                    if (_locale == null) {
117                            ThemeDisplay themeDisplay = getThemeDisplay();
118    
119                            _locale = themeDisplay.getLocale();
120                    }
121    
122                    return _locale;
123            }
124    
125            public PermissionChecker getPermissionChecker() {
126                    if (_permissionChecker == null) {
127                            ThemeDisplay themeDisplay = getThemeDisplay();
128    
129                            _permissionChecker = themeDisplay.getPermissionChecker();
130                    }
131    
132                    return _permissionChecker;
133            }
134    
135            public PortletDisplay getPortletDisplay() {
136                    if (_portletDisplay == null) {
137                            ThemeDisplay themeDisplay = getThemeDisplay();
138    
139                            _portletDisplay = themeDisplay.getPortletDisplay();
140                    }
141    
142                    return _portletDisplay;
143            }
144    
145            public String getPortletId() {
146                    if (_portletId == null) {
147                            PortletDisplay portletDisplay = getPortletDisplay();
148    
149                            _portletId = portletDisplay.getId();
150                    }
151    
152                    return _portletId;
153            }
154    
155            public String getPortletName() {
156                    if (_portletName == null) {
157                            PortletDisplay portletDisplay = getPortletDisplay();
158    
159                            _portletName = portletDisplay.getPortletName();
160                    }
161    
162                    return _portletName;
163            }
164    
165            public String getPortletResource() {
166                    if (_portletResource == null) {
167                            PortletDisplay portletDisplay = getPortletDisplay();
168    
169                            _portletResource = portletDisplay.getPortletResource();
170                    }
171    
172                    return _portletResource;
173            }
174    
175            public String getPortletTitle() {
176                    if (_portletTitle == null) {
177                            PortletDisplay portletDisplay = getPortletDisplay();
178    
179                            _portletTitle = portletDisplay.getTitle();
180                    }
181    
182                    return _portletTitle;
183            }
184    
185            public HttpServletRequest getRequest() {
186                    return _request;
187            }
188    
189            public String getResourcePortletId() {
190                    if (_resourcePortletId == null) {
191                            String portletResource = getPortletResource();
192    
193                            if (Validator.isNotNull(portletResource)) {
194                                    _resourcePortletId = getPortletResource();
195                            }
196                            else {
197                                    _resourcePortletId = getPortletId();
198                            }
199                    }
200    
201                    return _resourcePortletId;
202            }
203    
204            public String getResourcePortletName() {
205                    if (_resourcePortletName == null) {
206                            String portletResource = getPortletResource();
207    
208                            if (Validator.isNotNull(portletResource)) {
209                                    _resourcePortletName = portletResource;
210                            }
211                            else {
212                                    _resourcePortletName = getPortletName();
213                            }
214                    }
215    
216                    return _resourcePortletName;
217            }
218    
219            public long getScopeGroupId() {
220                    if (_scopeGroupId == null) {
221                            ThemeDisplay themeDisplay = getThemeDisplay();
222    
223                            _scopeGroupId = themeDisplay.getScopeGroupId();
224                    }
225    
226                    return _scopeGroupId;
227            }
228    
229            public long getSiteGroupId() {
230                    if (_siteGroupId == null) {
231                            ThemeDisplay themeDisplay = getThemeDisplay();
232    
233                            _siteGroupId = themeDisplay.getSiteGroupId();
234                    }
235    
236                    return _siteGroupId;
237            }
238    
239            public ThemeDisplay getThemeDisplay() {
240                    if (_themeDisplay == null) {
241                            _themeDisplay = (ThemeDisplay)_request.getAttribute(
242                                    WebKeys.THEME_DISPLAY);
243                    }
244    
245                    return _themeDisplay;
246            }
247    
248            public User getUser() {
249                    if (_user == null) {
250                            ThemeDisplay themeDisplay = getThemeDisplay();
251    
252                            _user = themeDisplay.getUser();
253                    }
254    
255                    return _user;
256            }
257    
258            public long getUserId() {
259                    if (_user == null) {
260                            ThemeDisplay themeDisplay = getThemeDisplay();
261    
262                            _user = themeDisplay.getUser();
263                    }
264    
265                    return _user.getUserId();
266            }
267    
268            private Company _company;
269            private Long _companyId;
270            private String _currentURL;
271            private Layout _layout;
272            private LiferayPortletRequest _liferayPortletRequest;
273            private LiferayPortletResponse _liferayPortletResponse;
274            private Locale _locale;
275            private PermissionChecker _permissionChecker;
276            private PortletDisplay _portletDisplay;
277            private String _portletId;
278            private String _portletName;
279            private String _portletResource;
280            private String _portletTitle;
281            private final HttpServletRequest _request;
282            private String _resourcePortletId;
283            private String _resourcePortletName;
284            private Long _scopeGroupId;
285            private Long _siteGroupId;
286            private ThemeDisplay _themeDisplay;
287            private User _user;
288    
289    }