/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.site.teams.web.internal.display.context;
import com.liferay.portal.kernel.dao.search.EmptyOnClickRowChecker;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Organization;
import com.liferay.portal.kernel.portlet.PortalPreferences;
import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.service.OrganizationLocalServiceUtil;
import com.liferay.portal.kernel.service.TeamServiceUtil;
import com.liferay.portal.kernel.service.permission.GroupPermissionUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.site.teams.web.internal.constants.SiteTeamsPortletKeys;
import com.liferay.site.teams.web.internal.search.TeamSearch;
import com.liferay.users.admin.kernel.util.UsersAdminUtil;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.servlet.http.HttpServletRequest;
/**
* @author Eudaldo Alonso
*/
public class SiteTeamsDisplayContext {
public SiteTeamsDisplayContext(
RenderRequest renderRequest, RenderResponse renderResponse,
HttpServletRequest request)
throws Exception {
_renderRequest = renderRequest;
_renderResponse = renderResponse;
_request = request;
addBreadcrumbEntries();
}
public String getDisplayStyle() {
if (Validator.isNotNull(_displayStyle)) {
return _displayStyle;
}
PortalPreferences portalPreferences =
PortletPreferencesFactoryUtil.getPortalPreferences(_request);
_displayStyle = portalPreferences.getValue(
SiteTeamsPortletKeys.SITE_TEAMS, "display-style", "icon");
return _displayStyle;
}
public String getOrderByCol() {
if (Validator.isNotNull(_orderByCol)) {
return _orderByCol;
}
_orderByCol = ParamUtil.getString(_request, "orderByCol", "name");
return _orderByCol;
}
public String getOrderByType() {
if (Validator.isNotNull(_orderByType)) {
return _orderByType;
}
_orderByType = ParamUtil.getString(_request, "orderByType", "asc");
return _orderByType;
}
public PortletURL getPortletURL() {
PortletURL portletURL = _renderResponse.createRenderURL();
portletURL.setParameter("displayStyle", getDisplayStyle());
return portletURL;
}
public SearchContainer getSearchContainer() throws PortalException {
ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
WebKeys.THEME_DISPLAY);
SearchContainer searchContainer = new TeamSearch(
_renderRequest, getPortletURL());
searchContainer.setEmptyResultsMessage("there-are-no-site-teams");
if (Validator.isNull(getKeywords())) {
if (isShowAddButton()) {
searchContainer.setEmptyResultsMessage(
"there-are-no-site-teams.-you-can-add-a-site-team-by-" +
"clicking-the-plus-button-on-the-bottom-right-corner");
searchContainer.setEmptyResultsMessageCssClass(
"taglib-empty-result-message-header-has-plus-btn");
}
}
else {
searchContainer.setSearch(true);
}
searchContainer.setId("teams");
searchContainer.setRowChecker(
new EmptyOnClickRowChecker(_renderResponse));
searchContainer.setTotal(getTotal());
List results = TeamServiceUtil.search(
themeDisplay.getScopeGroupId(), getKeywords(), getKeywords(),
new LinkedHashMap<String, Object>(), searchContainer.getStart(),
searchContainer.getEnd(), searchContainer.getOrderByComparator());
searchContainer.setResults(results);
return searchContainer;
}
public boolean isDescriptiveView() {
if (Objects.equals(getDisplayStyle(), "descriptive")) {
return true;
}
return false;
}
public boolean isDisabledManagementBar() {
if (getTotal() > 0) {
return false;
}
if (Validator.isNotNull(getKeywords())) {
return false;
}
return true;
}
public boolean isIconView() {
if (Objects.equals(getDisplayStyle(), "icon")) {
return true;
}
return false;
}
public boolean isListView() {
if (Objects.equals(getDisplayStyle(), "list")) {
return true;
}
return false;
}
public boolean isSearchEnabled() {
if (getTotal() > 0) {
return true;
}
if (Validator.isNotNull(getKeywords())) {
return true;
}
return false;
}
public boolean isShowAddButton() throws PortalException {
ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
WebKeys.THEME_DISPLAY);
if (GroupPermissionUtil.contains(
themeDisplay.getPermissionChecker(),
themeDisplay.getScopeGroup(), ActionKeys.MANAGE_TEAMS)) {
return true;
}
return false;
}
protected void addBreadcrumbEntries() throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
WebKeys.THEME_DISPLAY);
Group group = themeDisplay.getScopeGroup();
if (group.isOrganization()) {
Organization organization =
OrganizationLocalServiceUtil.getOrganization(
group.getOrganizationId());
UsersAdminUtil.addPortletBreadcrumbEntries(
organization, _request, _renderResponse);
}
else {
PortalUtil.addPortletBreadcrumbEntry(
_request, group.getDescriptiveName(themeDisplay.getLocale()),
null);
}
PortalUtil.addPortletBreadcrumbEntry(
_request, LanguageUtil.get(_request, "manage-teams"),
themeDisplay.getURLCurrent());
}
protected String getKeywords() {
if (_keywords != null) {
return _keywords;
}
_keywords = ParamUtil.getString(_request, "keywords");
return _keywords;
}
protected int getTotal() {
ThemeDisplay themeDisplay = (ThemeDisplay)_request.getAttribute(
WebKeys.THEME_DISPLAY);
return TeamServiceUtil.searchCount(
themeDisplay.getScopeGroupId(), getKeywords(), getKeywords(),
new LinkedHashMap<String, Object>());
}
private String _displayStyle;
private String _keywords;
private String _orderByCol;
private String _orderByType;
private final RenderRequest _renderRequest;
private final RenderResponse _renderResponse;
private final HttpServletRequest _request;
}