001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.usersadmin.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.model.Organization;
020    import com.liferay.portal.model.UserGroup;
021    import com.liferay.portal.service.UserLocalServiceUtil;
022    import com.liferay.portal.struts.AJAXAction;
023    import com.liferay.portal.util.PortalUtil;
024    
025    import java.util.LinkedHashMap;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionMapping;
032    
033    /**
034     * @author Gavin Wan
035     */
036    public class GetUsersCountAction extends AJAXAction {
037    
038            @Override
039            public String getText(
040                            ActionMapping actionMapping, ActionForm actionForm,
041                            HttpServletRequest request, HttpServletResponse response)
042                    throws Exception {
043    
044                    long companyId = PortalUtil.getCompanyId(request);
045    
046                    String className = ParamUtil.getString(request, "className");
047                    long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
048                    int status = ParamUtil.getInteger(request, "status");
049    
050                    int count = 0;
051    
052                    if (className.equals(Organization.class.getName())) {
053                            count = getOrganizationUsersCount(companyId, ids, status);
054                    }
055                    else if (className.equals(UserGroup.class.getName())) {
056                            count = getUserGroupUsersCount(companyId, ids, status);
057                    }
058    
059                    return String.valueOf(count);
060            }
061    
062            protected int getOrganizationUsersCount(
063                            long companyId, long[] organizationIds, int status)
064                    throws Exception {
065    
066                    int count = 0;
067    
068                    for (long organizationId : organizationIds) {
069                            LinkedHashMap<String, Object> params =
070                                    new LinkedHashMap<String, Object>();
071    
072                            params.put("usersOrgs", organizationId);
073    
074                            count+= UserLocalServiceUtil.searchCount(
075                                    companyId, null, status, params);
076                    }
077    
078                    return count;
079            }
080    
081            protected int getUserGroupUsersCount(
082                            long companyId, long[] userGroupIds, int status)
083                    throws Exception {
084    
085                    int count = 0;
086    
087                    for (long userGroupId : userGroupIds) {
088                            LinkedHashMap<String, Object> params =
089                                    new LinkedHashMap<String, Object>();
090    
091                            params.put("usersUserGroups", userGroupId);
092    
093                            count+= UserLocalServiceUtil.searchCount(
094                                    companyId, null, status, params);
095                    }
096    
097                    return count;
098            }
099    
100    }