1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.ProgressTracker;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.impl.RoleImpl;
32  import com.liferay.portal.service.RoleLocalServiceUtil;
33  import com.liferay.portal.service.UserLocalServiceUtil;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="ExportUsersAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class ExportUsersAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              String csv = getUsersCSV(request);
65  
66              String fileName = "users.csv";
67              byte[] bytes = csv.getBytes();
68  
69              ServletResponseUtil.sendFile(
70                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
71  
72              return null;
73          }
74          catch (Exception e) {
75              PortalUtil.sendError(e, request, response);
76  
77              return null;
78          }
79      }
80  
81      protected String getUsersCSV(HttpServletRequest request) throws Exception {
82          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
83              WebKeys.THEME_DISPLAY);
84  
85          if (!RoleLocalServiceUtil.hasUserRole(
86                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
87                  RoleImpl.ADMINISTRATOR, true)) {
88  
89              return StringPool.BLANK;
90          }
91  
92          String exportProgressId = ParamUtil.getString(
93              request, "exportProgressId");
94  
95          ProgressTracker progressTracker = new ProgressTracker(
96              request, exportProgressId);
97  
98          progressTracker.start();
99  
100         List<User> users = UserLocalServiceUtil.search(
101             themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
102             QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
103 
104         int percentage = 10;
105         int total = users.size();
106 
107         progressTracker.updateProgress(percentage);
108 
109         StringBuilder sb = new StringBuilder(users.size() * 50);
110 
111         Iterator<User> itr = users.iterator();
112 
113         for (int i = 0; itr.hasNext(); i++) {
114             User user = itr.next();
115 
116             sb.append(user.getFullName());
117             sb.append(StringPool.COMMA);
118             sb.append(user.getEmailAddress());
119             sb.append(StringPool.NEW_LINE);
120 
121             percentage = Math.min(10 + (i * 90) / total, 99);
122 
123             progressTracker.updateProgress(percentage);
124         }
125 
126         progressTracker.finish();
127 
128         return sb.toString();
129     }
130 
131 }