1   /**
2    * Copyright (c) 2000-2009 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.OrderByComparator;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.ProgressTracker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.model.RoleConstants;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.RoleLocalServiceUtil;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.util.Iterator;
41  import java.util.List;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.struts.action.Action;
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="ExportUsersAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ExportUsersAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          try {
65              String csv = getUsersCSV(request);
66  
67              String fileName = "users.csv";
68              byte[] bytes = csv.getBytes();
69  
70              ServletResponseUtil.sendFile(
71                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
72  
73              return null;
74          }
75          catch (Exception e) {
76              PortalUtil.sendError(e, request, response);
77  
78              return null;
79          }
80      }
81  
82      protected String getUsersCSV(HttpServletRequest request) throws Exception {
83          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84              WebKeys.THEME_DISPLAY);
85  
86          if (!RoleLocalServiceUtil.hasUserRole(
87                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
88                  RoleConstants.ADMINISTRATOR, true)) {
89  
90              return StringPool.BLANK;
91          }
92  
93          String exportProgressId = ParamUtil.getString(
94              request, "exportProgressId");
95  
96          ProgressTracker progressTracker = new ProgressTracker(
97              request, exportProgressId);
98  
99          progressTracker.start();
100 
101         List<User> users = UserLocalServiceUtil.search(
102             themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
103             QueryUtil.ALL_POS, QueryUtil.ALL_POS, (OrderByComparator)null);
104 
105         int percentage = 10;
106         int total = users.size();
107 
108         progressTracker.updateProgress(percentage);
109 
110         StringBuilder sb = new StringBuilder(users.size() * 50);
111 
112         Iterator<User> itr = users.iterator();
113 
114         for (int i = 0; itr.hasNext(); i++) {
115             User user = itr.next();
116 
117             sb.append(user.getFullName());
118             sb.append(StringPool.COMMA);
119             sb.append(user.getEmailAddress());
120             sb.append(StringPool.NEW_LINE);
121 
122             percentage = Math.min(10 + (i * 90) / total, 99);
123 
124             progressTracker.updateProgress(percentage);
125         }
126 
127         progressTracker.finish();
128 
129         return sb.toString();
130     }
131 
132 }