/** * 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.item.selector.taglib.servlet.taglib; import com.liferay.item.selector.taglib.internal.servlet.ServletContextUtil; import com.liferay.item.selector.taglib.internal.servlet.item.selector.ItemSelectorUtil; import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.dao.search.SearchContainer; import com.liferay.portal.kernel.dao.search.SearchPaginationUtil; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.model.Company; import com.liferay.portal.kernel.model.Group; import com.liferay.portal.kernel.model.Organization; import com.liferay.portal.kernel.model.User; import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil; import com.liferay.portal.kernel.service.GroupLocalServiceUtil; import com.liferay.portal.kernel.theme.ThemeDisplay; import com.liferay.portal.kernel.util.ListUtil; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.util.WebKeys; import com.liferay.taglib.util.IncludeTag; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.PageContext; /** * @author Roberto Díaz */ public class GroupSelectorTag extends IncludeTag { public void setGroups(List<Group> groups) { _groups = groups; } public void setGroupsCount(int groupsCount) { _groupsCount = groupsCount; } @Override public void setPageContext(PageContext pageContext) { super.setPageContext(pageContext); servletContext = ServletContextUtil.getServletContext(); } @Override protected void cleanUp() { super.cleanUp(); _groups = null; _groupsCount = 0; } protected List<Group> getGroups(HttpServletRequest request) { if (_groups != null) { return _groups; } ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY); User user = themeDisplay.getUser(); String keywords = ParamUtil.getString(request, "keywords"); int cur = ParamUtil.getInteger( request, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_CUR); int delta = ParamUtil.getInteger( request, SearchContainer.DEFAULT_DELTA_PARAM, SearchContainer.DEFAULT_DELTA); int[] startAndEnd = SearchPaginationUtil.calculateStartAndEnd( cur, delta); if (Validator.isNotNull(keywords)) { LinkedHashMap<String, Object> groupParams = new LinkedHashMap<>(); groupParams.put("site", Boolean.TRUE); groupParams.put("usersGroups", Long.valueOf(user.getUserId())); return GroupLocalServiceUtil.search( themeDisplay.getCompanyId(), _CLASSNAME_IDS, keywords, groupParams, startAndEnd[0], startAndEnd[1], null); } try { List<Group> groups = user.getMySiteGroups(null, startAndEnd[1]); return ListUtil.subList(groups, startAndEnd[0], startAndEnd[1]); } catch (Exception e) { _log.error(e, e); return new ArrayList<>(); } } protected int getGroupsCount(HttpServletRequest request) { if ((_groups != null) || (_groupsCount > 0)) { return _groupsCount; } ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY); User user = themeDisplay.getUser(); String keywords = ParamUtil.getString(request, "keywords"); if (Validator.isNotNull(keywords)) { LinkedHashMap<String, Object> groupParams = new LinkedHashMap<>(); groupParams.put("site", Boolean.TRUE); groupParams.put("usersGroups", Long.valueOf(user.getUserId())); return GroupLocalServiceUtil.searchCount( themeDisplay.getCompanyId(), _CLASSNAME_IDS, keywords, groupParams); } try { List<Group> groups = user.getMySiteGroups(null, QueryUtil.ALL_POS); return groups.size(); } catch (Exception e) { _log.error(e, e); return 0; } } @Override protected String getPage() { return "/group_selector/page.jsp"; } @Override protected void setAttributes(HttpServletRequest request) { request.setAttribute( "liferay-item-selector:group-selector:groups", getGroups(request)); request.setAttribute( "liferay-item-selector:group-selector:groupsCount", getGroupsCount(request)); request.setAttribute( "liferay-item-selector:group-selector:itemSelector", ItemSelectorUtil.getItemSelector()); } private static final long[] _CLASSNAME_IDS = { ClassNameLocalServiceUtil.getClassNameId(Company.class), ClassNameLocalServiceUtil.getClassNameId(Group.class), ClassNameLocalServiceUtil.getClassNameId(Organization.class) }; private static final Log _log = LogFactoryUtil.getLog( GroupSelectorTag.class); private List<Group> _groups; private int _groupsCount; }