1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class GetUsersCountAction extends AJAXAction {
47  
48      public String getText(
49              ActionMapping mapping, ActionForm form, HttpServletRequest request,
50              HttpServletResponse response)
51          throws Exception {
52  
53          long companyId = PortalUtil.getCompanyId(request);
54  
55          String className = ParamUtil.getString(request, "className");
56          long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
57          boolean active = ParamUtil.getBoolean(request, "active");
58  
59          int count = 0;
60  
61          if (className.equals(Organization.class.getName())) {
62              count = getOrganizationUsersCount(companyId, ids, active);
63          }
64          else if (className.equals(UserGroup.class.getName())) {
65              count = getUserGroupUsersCount(companyId, ids, active);
66          }
67  
68          return String.valueOf(count);
69      }
70  
71      protected int getOrganizationUsersCount(
72              long companyId, long[] organizationIds, boolean active)
73          throws Exception {
74  
75          int count = 0;
76  
77          for (long organizationId : organizationIds) {
78              LinkedHashMap<String, Object> params =
79                  new LinkedHashMap<String, Object>();
80  
81              params.put("usersOrgs", organizationId);
82  
83              count+= UserLocalServiceUtil.searchCount(
84                  companyId, null, active, params);
85          }
86  
87          return count;
88      }
89  
90      protected int getUserGroupUsersCount(
91              long companyId, long[] userGroupIds, boolean active)
92          throws Exception {
93  
94          int count = 0;
95  
96          for (long userGroupId : userGroupIds) {
97              LinkedHashMap<String, Object> params =
98                  new LinkedHashMap<String, Object>();
99  
100             params.put("usersUserGroups", userGroupId);
101 
102             count+= UserLocalServiceUtil.searchCount(
103                 companyId, null, active, params);
104         }
105 
106         return count;
107     }
108 
109 }