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