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