1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.kernel.util.ParamUtil;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.model.Organization;
20  import com.liferay.portal.model.UserGroup;
21  import com.liferay.portal.service.UserLocalServiceUtil;
22  import com.liferay.portal.struts.AJAXAction;
23  import com.liferay.portal.util.PortalUtil;
24  
25  import java.util.LinkedHashMap;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionMapping;
32  
33  /**
34   * <a href="GetUsersCountAction.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Gavin Wan
37   */
38  public class GetUsersCountAction extends AJAXAction {
39  
40      public String getText(
41              ActionMapping mapping, ActionForm form, HttpServletRequest request,
42              HttpServletResponse response)
43          throws Exception {
44  
45          long companyId = PortalUtil.getCompanyId(request);
46  
47          String className = ParamUtil.getString(request, "className");
48          long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
49          boolean active = ParamUtil.getBoolean(request, "active");
50  
51          int count = 0;
52  
53          if (className.equals(Organization.class.getName())) {
54              count = getOrganizationUsersCount(companyId, ids, active);
55          }
56          else if (className.equals(UserGroup.class.getName())) {
57              count = getUserGroupUsersCount(companyId, ids, active);
58          }
59  
60          return String.valueOf(count);
61      }
62  
63      protected int getOrganizationUsersCount(
64              long companyId, long[] organizationIds, boolean active)
65          throws Exception {
66  
67          int count = 0;
68  
69          for (long organizationId : organizationIds) {
70              LinkedHashMap<String, Object> params =
71                  new LinkedHashMap<String, Object>();
72  
73              params.put("usersOrgs", organizationId);
74  
75              count+= UserLocalServiceUtil.searchCount(
76                  companyId, null, active, params);
77          }
78  
79          return count;
80      }
81  
82      protected int getUserGroupUsersCount(
83              long companyId, long[] userGroupIds, boolean active)
84          throws Exception {
85  
86          int count = 0;
87  
88          for (long userGroupId : userGroupIds) {
89              LinkedHashMap<String, Object> params =
90                  new LinkedHashMap<String, Object>();
91  
92              params.put("usersUserGroups", userGroupId);
93  
94              count+= UserLocalServiceUtil.searchCount(
95                  companyId, null, active, params);
96          }
97  
98          return count;
99      }
100 
101 }