1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.OrderByComparator;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.ProgressTracker;
22  import com.liferay.portal.kernel.util.StringBundler;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.model.RoleConstants;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.service.RoleLocalServiceUtil;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.struts.action.Action;
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="ExportUsersAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   */
49  public class ExportUsersAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          try {
57              String csv = getUsersCSV(request);
58  
59              String fileName = "users.csv";
60              byte[] bytes = csv.getBytes();
61  
62              ServletResponseUtil.sendFile(
63                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
64  
65              return null;
66          }
67          catch (Exception e) {
68              PortalUtil.sendError(e, request, response);
69  
70              return null;
71          }
72      }
73  
74      protected String getUsersCSV(HttpServletRequest request) throws Exception {
75          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
76              WebKeys.THEME_DISPLAY);
77  
78          if (!RoleLocalServiceUtil.hasUserRole(
79                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
80                  RoleConstants.ADMINISTRATOR, true)) {
81  
82              return StringPool.BLANK;
83          }
84  
85          String exportProgressId = ParamUtil.getString(
86              request, "exportProgressId");
87  
88          ProgressTracker progressTracker = new ProgressTracker(
89              request, exportProgressId);
90  
91          progressTracker.start();
92  
93          List<User> users = UserLocalServiceUtil.search(
94              themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
95              QueryUtil.ALL_POS, QueryUtil.ALL_POS, (OrderByComparator)null);
96  
97          int percentage = 10;
98          int total = users.size();
99  
100         progressTracker.updateProgress(percentage);
101 
102         if (total == 0) {
103             return StringPool.BLANK;
104         }
105 
106         StringBundler sb = new StringBundler(users.size() * 4);
107 
108         Iterator<User> itr = users.iterator();
109 
110         for (int i = 0; itr.hasNext(); i++) {
111             User user = itr.next();
112 
113             sb.append(user.getFullName());
114             sb.append(StringPool.COMMA);
115             sb.append(user.getEmailAddress());
116             sb.append(StringPool.NEW_LINE);
117 
118             percentage = Math.min(10 + (i * 90) / total, 99);
119 
120             progressTracker.updateProgress(percentage);
121         }
122 
123         progressTracker.finish();
124 
125         return sb.toString();
126     }
127 
128 }