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.portlet.layoutsadmin.display.context;
016    
017    import com.liferay.exportimport.kernel.staging.StagingUtil;
018    import com.liferay.portal.kernel.model.Group;
019    import com.liferay.portal.kernel.service.GroupLocalServiceUtil;
020    import com.liferay.portal.kernel.theme.ThemeDisplay;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.util.PropsValues;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Mate Thurzo
030     */
031    public class GroupDisplayContextHelper {
032    
033            public GroupDisplayContextHelper(HttpServletRequest request) {
034                    _request = request;
035            }
036    
037            public Group getGroup() {
038                    if (_group != null) {
039                            return _group;
040                    }
041    
042                    if (!PropsValues.STAGING_LIVE_GROUP_LOCKING_ENABLED &&
043                            (getSelGroup() != null)) {
044    
045                            _group = getSelGroup();
046    
047                            return _group;
048                    }
049    
050                    if (getStagingGroup() != null) {
051                            _group = getStagingGroup();
052                    }
053                    else {
054                            _group = getLiveGroup();
055                    }
056    
057                    if (_group == null) {
058                            _group = getSelGroup();
059                    }
060    
061                    return _group;
062            }
063    
064            public Long getGroupId() {
065                    if (_groupId != null) {
066                            return _groupId;
067                    }
068    
069                    Group group = getGroup();
070    
071                    if (group != null) {
072                            _groupId = group.getGroupId();
073                    }
074    
075                    return _groupId;
076            }
077    
078            public UnicodeProperties getGroupTypeSettings() {
079                    if (_groupTypeSettings != null) {
080                            return _groupTypeSettings;
081                    }
082    
083                    Group group = getGroup();
084    
085                    if (group != null) {
086                            _groupTypeSettings = group.getTypeSettingsProperties();
087                    }
088                    else {
089                            _groupTypeSettings = new UnicodeProperties();
090                    }
091    
092                    return _groupTypeSettings;
093            }
094    
095            public Group getLiveGroup() {
096                    if (_liveGroup != null) {
097                            return _liveGroup;
098                    }
099    
100                    Group group = getSelGroup();
101    
102                    if (group == null) {
103                            return null;
104                    }
105    
106                    _liveGroup = StagingUtil.getLiveGroup(group.getGroupId());
107    
108                    if (_liveGroup == null) {
109                            _liveGroup = group;
110                    }
111    
112                    return _liveGroup;
113            }
114    
115            public Long getLiveGroupId() {
116                    if (_liveGroupId != null) {
117                            return _liveGroupId;
118                    }
119    
120                    Group liveGroup = getLiveGroup();
121    
122                    if (liveGroup != null) {
123                            _liveGroupId = liveGroup.getGroupId();
124                    }
125    
126                    return _liveGroupId;
127            }
128    
129            public Group getSelGroup() {
130                    if (_selGroup != null) {
131                            return _selGroup;
132                    }
133    
134                    long groupId = ParamUtil.getLong(_request, "groupId");
135    
136                    _selGroup = GroupLocalServiceUtil.fetchGroup(groupId);
137    
138                    if (_selGroup == null) {
139                            _selGroup = (Group)_request.getAttribute(WebKeys.GROUP);
140                    }
141    
142                    if (_selGroup == null) {
143                            ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
144                                    WebKeys.THEME_DISPLAY);
145    
146                            _selGroup = themeDisplay.getScopeGroup();
147                    }
148    
149                    return _selGroup;
150            }
151    
152            public Group getStagingGroup() {
153                    if (_stagingGroup != null) {
154                            return _stagingGroup;
155                    }
156    
157                    Group group = getSelGroup();
158    
159                    if (group == null) {
160                            return null;
161                    }
162    
163                    _stagingGroup = StagingUtil.getStagingGroup(group.getGroupId());
164    
165                    return _stagingGroup;
166            }
167    
168            public Long getStagingGroupId() {
169                    if (_stagingGroupId != null) {
170                            return _stagingGroupId;
171                    }
172    
173                    Group stagingGroup = getStagingGroup();
174    
175                    if (stagingGroup != null) {
176                            _stagingGroupId = stagingGroup.getGroupId();
177                    }
178    
179                    return _stagingGroupId;
180            }
181    
182            private Group _group;
183            private Long _groupId;
184            private UnicodeProperties _groupTypeSettings;
185            private Group _liveGroup;
186            private Long _liveGroupId;
187            private final HttpServletRequest _request;
188            private Group _selGroup;
189            private Group _stagingGroup;
190            private Long _stagingGroupId;
191    
192    }