1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.model.Organization;
28  import com.liferay.portal.model.UserGroup;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portal.struts.AJAXAction;
31  import com.liferay.portal.util.PortalUtil;
32  
33  import java.util.LinkedHashMap;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="GetUsersCountAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Gavin Wan
45   *
46   */
47  public class GetUsersCountAction extends AJAXAction {
48  
49      public String getText(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          long companyId = PortalUtil.getCompanyId(request);
55  
56          String className = ParamUtil.getString(request, "className");
57          long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
58          boolean active = ParamUtil.getBoolean(request, "active");
59  
60          int count = 0;
61  
62          if (className.equals(Organization.class.getName())) {
63              count = getOrganizationUsersCount(companyId, ids, active);
64          }
65          else if (className.equals(UserGroup.class.getName())) {
66              count = getUserGroupUsersCount(companyId, ids, active);
67          }
68  
69          return String.valueOf(count);
70      }
71  
72      protected int getOrganizationUsersCount(
73              long companyId, long[] organizationIds, boolean active)
74          throws Exception {
75  
76          int count = 0;
77  
78          for (long organizationId : organizationIds) {
79              LinkedHashMap<String, Object> params =
80                  new LinkedHashMap<String, Object>();
81  
82              params.put("usersOrgs", organizationId);
83  
84              count+= UserLocalServiceUtil.searchCount(
85                  companyId, null, active, params);
86          }
87  
88          return count;
89      }
90  
91      protected int getUserGroupUsersCount(
92              long companyId, long[] userGroupIds, boolean active)
93          throws Exception {
94  
95          int count = 0;
96  
97          for (long userGroupId : userGroupIds) {
98              LinkedHashMap<String, Object> params =
99                  new LinkedHashMap<String, Object>();
100 
101             params.put("usersUserGroups", userGroupId);
102 
103             count+= UserLocalServiceUtil.searchCount(
104                 companyId, null, active, params);
105         }
106 
107         return count;
108     }
109 
110 }