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