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.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.dao.orm.QueryUtil;
019    import com.liferay.portal.kernel.search.Hits;
020    import com.liferay.portal.kernel.search.Sort;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.ArrayUtil;
024    import com.liferay.portal.kernel.util.CSVUtil;
025    import com.liferay.portal.kernel.util.ContentTypes;
026    import com.liferay.portal.kernel.util.OrderByComparator;
027    import com.liferay.portal.kernel.util.ParamUtil;
028    import com.liferay.portal.kernel.util.ProgressTracker;
029    import com.liferay.portal.kernel.util.StringBundler;
030    import com.liferay.portal.kernel.util.StringPool;
031    import com.liferay.portal.kernel.util.Tuple;
032    import com.liferay.portal.model.User;
033    import com.liferay.portal.security.permission.ActionKeys;
034    import com.liferay.portal.security.permission.PermissionChecker;
035    import com.liferay.portal.service.UserLocalServiceUtil;
036    import com.liferay.portal.service.permission.PortalPermissionUtil;
037    import com.liferay.portal.service.permission.PortletPermissionUtil;
038    import com.liferay.portal.struts.ActionConstants;
039    import com.liferay.portal.struts.PortletAction;
040    import com.liferay.portal.theme.ThemeDisplay;
041    import com.liferay.portal.util.PortalUtil;
042    import com.liferay.portal.util.PortletKeys;
043    import com.liferay.portal.util.PropsValues;
044    import com.liferay.portal.util.WebKeys;
045    import com.liferay.portlet.ActionResponseImpl;
046    import com.liferay.portlet.expando.model.ExpandoBridge;
047    import com.liferay.portlet.usersadmin.search.UserSearch;
048    import com.liferay.portlet.usersadmin.search.UserSearchTerms;
049    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
050    
051    import java.util.Collections;
052    import java.util.Iterator;
053    import java.util.LinkedHashMap;
054    import java.util.List;
055    
056    import javax.portlet.ActionRequest;
057    import javax.portlet.ActionResponse;
058    import javax.portlet.PortletConfig;
059    import javax.portlet.PortletURL;
060    
061    import javax.servlet.http.HttpServletRequest;
062    import javax.servlet.http.HttpServletResponse;
063    
064    import org.apache.struts.action.ActionForm;
065    import org.apache.struts.action.ActionMapping;
066    
067    /**
068     * @author Brian Wing Shun Chan
069     * @author Mika Koivisto
070     */
071    public class ExportUsersAction extends PortletAction {
072    
073            @Override
074            public void processAction(
075                            ActionMapping actionMapping, ActionForm actionForm,
076                            PortletConfig portletConfig, ActionRequest actionRequest,
077                            ActionResponse actionResponse)
078                    throws Exception {
079    
080                    try {
081                            String csv = getUsersCSV(actionRequest, actionResponse);
082    
083                            String fileName = "users.csv";
084                            byte[] bytes = csv.getBytes();
085    
086                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
087                                    actionRequest);
088                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
089                                    actionResponse);
090    
091                            ServletResponseUtil.sendFile(
092                                    request, response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
093    
094                            setForward(actionRequest, ActionConstants.COMMON_NULL);
095                    }
096                    catch (Exception e) {
097                            SessionErrors.add(actionRequest, e.getClass());
098    
099                            setForward(actionRequest, "portlet.users_admin.error");
100                    }
101            }
102    
103            protected String getUserCSV(User user) {
104                    StringBundler sb = new StringBundler(
105                            PropsValues.USERS_EXPORT_CSV_FIELDS.length * 2);
106    
107                    for (int i = 0; i < PropsValues.USERS_EXPORT_CSV_FIELDS.length; i++) {
108                            String field = PropsValues.USERS_EXPORT_CSV_FIELDS[i];
109    
110                            if (field.equals("fullName")) {
111                                    sb.append(CSVUtil.encode(user.getFullName()));
112                            }
113                            else if (field.startsWith("expando:")) {
114                                    String attributeName = field.substring(8);
115    
116                                    ExpandoBridge expandoBridge = user.getExpandoBridge();
117    
118                                    sb.append(
119                                            CSVUtil.encode(expandoBridge.getAttribute(attributeName)));
120                            }
121                            else {
122                                    sb.append(
123                                            CSVUtil.encode(BeanPropertiesUtil.getString(user, field)));
124                            }
125    
126                            if ((i + 1) < PropsValues.USERS_EXPORT_CSV_FIELDS.length) {
127                                    sb.append(StringPool.COMMA);
128                            }
129                    }
130    
131                    sb.append(StringPool.NEW_LINE);
132    
133                    return sb.toString();
134            }
135    
136            protected List<User> getUsers(
137                            ActionRequest actionRequest, ActionResponse actionResponse)
138                    throws Exception {
139    
140                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
141                            WebKeys.THEME_DISPLAY);
142    
143                    PermissionChecker permissionChecker =
144                            themeDisplay.getPermissionChecker();
145    
146                    boolean exportAllUsers = PortalPermissionUtil.contains(
147                            permissionChecker, ActionKeys.EXPORT_USER);
148    
149                    if (!exportAllUsers &&
150                            !PortletPermissionUtil.contains(
151                                    permissionChecker, PortletKeys.USERS_ADMIN,
152                                    ActionKeys.EXPORT_USER)) {
153    
154                            return Collections.emptyList();
155                    }
156    
157                    PortletURL portletURL =
158                            ((ActionResponseImpl)actionResponse).createRenderURL(
159                                    PortletKeys.USERS_ADMIN);
160    
161                    UserSearch userSearch = new UserSearch(actionRequest, portletURL);
162    
163                    UserSearchTerms searchTerms =
164                            (UserSearchTerms)userSearch.getSearchTerms();
165    
166                    LinkedHashMap<String, Object> params =
167                            new LinkedHashMap<String, Object>();
168    
169                    long organizationId = searchTerms.getOrganizationId();
170    
171                    if (organizationId > 0) {
172                            params.put("usersOrgs", new Long(organizationId));
173                    }
174                    else if (!exportAllUsers) {
175                            User user = themeDisplay.getUser();
176    
177                            Long[] organizationIds = ArrayUtil.toArray(
178                                    user.getOrganizationIds(true));
179    
180                            if (organizationIds.length > 0) {
181                                    params.put("usersOrgs", organizationIds);
182                            }
183                    }
184    
185                    long roleId = searchTerms.getRoleId();
186    
187                    if (roleId > 0) {
188                            params.put("usersRoles", new Long(roleId));
189                    }
190    
191                    long userGroupId = searchTerms.getUserGroupId();
192    
193                    if (userGroupId > 0) {
194                            params.put("usersUserGroups", new Long(userGroupId));
195                    }
196    
197                    if (PropsValues.USERS_INDEXER_ENABLED &&
198                            PropsValues.USERS_SEARCH_WITH_INDEX) {
199    
200                            params.put("expandoAttributes", searchTerms.getKeywords());
201    
202                            Hits hits = null;
203    
204                            if (searchTerms.isAdvancedSearch()) {
205                                    hits = UserLocalServiceUtil.search(
206                                            themeDisplay.getCompanyId(), searchTerms.getFirstName(),
207                                            searchTerms.getMiddleName(), searchTerms.getLastName(),
208                                            searchTerms.getScreenName(), searchTerms.getEmailAddress(),
209                                            searchTerms.getStatus(), params,
210                                            searchTerms.isAndOperator(), QueryUtil.ALL_POS,
211                                            QueryUtil.ALL_POS, (Sort)null);
212                            }
213                            else {
214                                    hits = UserLocalServiceUtil.search(
215                                            themeDisplay.getCompanyId(), searchTerms.getKeywords(),
216                                            searchTerms.getStatus(), params, QueryUtil.ALL_POS,
217                                            QueryUtil.ALL_POS, (Sort)null);
218                            }
219    
220                            Tuple tuple = UsersAdminUtil.getUsers(hits);
221    
222                            return (List<User>)tuple.getObject(0);
223                    }
224                    else {
225                            if (searchTerms.isAdvancedSearch()) {
226                                    return UserLocalServiceUtil.search(
227                                            themeDisplay.getCompanyId(), searchTerms.getFirstName(),
228                                            searchTerms.getMiddleName(), searchTerms.getLastName(),
229                                            searchTerms.getScreenName(), searchTerms.getEmailAddress(),
230                                            searchTerms.getStatus(), params,
231                                            searchTerms.isAndOperator(), QueryUtil.ALL_POS,
232                                            QueryUtil.ALL_POS, (OrderByComparator)null);
233                            }
234                            else {
235                                    return UserLocalServiceUtil.search(
236                                            themeDisplay.getCompanyId(), searchTerms.getKeywords(),
237                                            searchTerms.getStatus(), params, QueryUtil.ALL_POS,
238                                            QueryUtil.ALL_POS, (OrderByComparator)null);
239                            }
240                    }
241            }
242    
243            protected String getUsersCSV(
244                            ActionRequest actionRequest, ActionResponse actionResponse)
245                    throws Exception {
246    
247                    List<User> users = getUsers(actionRequest, actionResponse);
248    
249                    if (users.isEmpty()) {
250                            return StringPool.BLANK;
251                    }
252    
253                    String exportProgressId = ParamUtil.getString(
254                            actionRequest, "exportProgressId");
255    
256                    ProgressTracker progressTracker = new ProgressTracker(
257                            actionRequest, exportProgressId);
258    
259                    progressTracker.start();
260    
261                    int percentage = 10;
262                    int total = users.size();
263    
264                    progressTracker.updateProgress(percentage);
265    
266                    StringBundler sb = new StringBundler(users.size() * 4);
267    
268                    Iterator<User> itr = users.iterator();
269    
270                    for (int i = 0; itr.hasNext(); i++) {
271                            User user = itr.next();
272    
273                            sb.append(getUserCSV(user));
274    
275                            percentage = Math.min(10 + (i * 90) / total, 99);
276    
277                            progressTracker.updateProgress(percentage);
278                    }
279    
280                    progressTracker.finish();
281    
282                    return sb.toString();
283            }
284    
285    }