1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.model.Organization;
25  import com.liferay.portal.model.UserGroup;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  import com.liferay.portal.struts.AJAXAction;
28  import com.liferay.portal.util.PortalUtil;
29  
30  import java.util.LinkedHashMap;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionMapping;
37  
38  /**
39   * <a href="GetUsersCountAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Gavin Wan
42   *
43   */
44  public class GetUsersCountAction extends AJAXAction {
45  
46      public String getText(
47              ActionMapping mapping, ActionForm form, HttpServletRequest request,
48              HttpServletResponse response)
49          throws Exception {
50  
51          long companyId = PortalUtil.getCompanyId(request);
52  
53          String className = ParamUtil.getString(request, "className");
54          long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
55          boolean active = ParamUtil.getBoolean(request, "active");
56  
57          int count = 0;
58  
59          if (className.equals(Organization.class.getName())) {
60              count = getOrganizationUsersCount(companyId, ids, active);
61          }
62          else if (className.equals(UserGroup.class.getName())) {
63              count = getUserGroupUsersCount(companyId, ids, active);
64          }
65  
66          return String.valueOf(count);
67      }
68  
69      protected int getOrganizationUsersCount(
70              long companyId, long[] organizationIds, boolean active)
71          throws Exception {
72  
73          int count = 0;
74  
75          for (long organizationId : organizationIds) {
76              LinkedHashMap<String, Object> params =
77                  new LinkedHashMap<String, Object>();
78  
79              params.put("usersOrgs", organizationId);
80  
81              count+= UserLocalServiceUtil.searchCount(
82                  companyId, null, active, params);
83          }
84  
85          return count;
86      }
87  
88      protected int getUserGroupUsersCount(
89              long companyId, long[] userGroupIds, boolean active)
90          throws Exception {
91  
92          int count = 0;
93  
94          for (long userGroupId : userGroupIds) {
95              LinkedHashMap<String, Object> params =
96                  new LinkedHashMap<String, Object>();
97  
98              params.put("usersUserGroups", userGroupId);
99  
100             count+= UserLocalServiceUtil.searchCount(
101                 companyId, null, active, params);
102         }
103 
104         return count;
105     }
106 
107 }