001    /**
002     * Copyright (c) 2000-present 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 = new LinkedHashMap<>();
070    
071                            params.put("usersOrgs", organizationId);
072    
073                            count+= UserLocalServiceUtil.searchCount(
074                                    companyId, null, status, params);
075                    }
076    
077                    return count;
078            }
079    
080            protected int getUserGroupUsersCount(
081                            long companyId, long[] userGroupIds, int status)
082                    throws Exception {
083    
084                    int count = 0;
085    
086                    for (long userGroupId : userGroupIds) {
087                            LinkedHashMap<String, Object> params = new LinkedHashMap<>();
088    
089                            params.put("usersUserGroups", userGroupId);
090    
091                            count+= UserLocalServiceUtil.searchCount(
092                                    companyId, null, status, params);
093                    }
094    
095                    return count;
096            }
097    
098    }