001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.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            public String getText(
039                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
040                            HttpServletResponse response)
041                    throws Exception {
042    
043                    long companyId = PortalUtil.getCompanyId(request);
044    
045                    String className = ParamUtil.getString(request, "className");
046                    long[] ids = StringUtil.split(ParamUtil.getString(request, "ids"), 0L);
047                    boolean active = ParamUtil.getBoolean(request, "active");
048    
049                    int count = 0;
050    
051                    if (className.equals(Organization.class.getName())) {
052                            count = getOrganizationUsersCount(companyId, ids, active);
053                    }
054                    else if (className.equals(UserGroup.class.getName())) {
055                            count = getUserGroupUsersCount(companyId, ids, active);
056                    }
057    
058                    return String.valueOf(count);
059            }
060    
061            protected int getOrganizationUsersCount(
062                            long companyId, long[] organizationIds, boolean active)
063                    throws Exception {
064    
065                    int count = 0;
066    
067                    for (long organizationId : organizationIds) {
068                            LinkedHashMap<String, Object> params =
069                                    new LinkedHashMap<String, Object>();
070    
071                            params.put("usersOrgs", organizationId);
072    
073                            count+= UserLocalServiceUtil.searchCount(
074                                    companyId, null, active, params);
075                    }
076    
077                    return count;
078            }
079    
080            protected int getUserGroupUsersCount(
081                            long companyId, long[] userGroupIds, boolean active)
082                    throws Exception {
083    
084                    int count = 0;
085    
086                    for (long userGroupId : userGroupIds) {
087                            LinkedHashMap<String, Object> params =
088                                    new LinkedHashMap<String, Object>();
089    
090                            params.put("usersUserGroups", userGroupId);
091    
092                            count+= UserLocalServiceUtil.searchCount(
093                                    companyId, null, active, params);
094                    }
095    
096                    return count;
097            }
098    
099    }