1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.ParamUtil;
20  import com.liferay.portal.kernel.util.ProgressTracker;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.model.RoleConstants;
24  import com.liferay.portal.model.User;
25  import com.liferay.portal.service.RoleLocalServiceUtil;
26  import com.liferay.portal.service.UserLocalServiceUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.util.servlet.ServletResponseUtil;
31  
32  import java.util.Iterator;
33  import java.util.List;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  
38  import org.apache.struts.action.Action;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="ExportUsersAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class ExportUsersAction extends Action {
49  
50      public ActionForward execute(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          try {
56              String csv = getUsersCSV(request);
57  
58              String fileName = "users.csv";
59              byte[] bytes = csv.getBytes();
60  
61              ServletResponseUtil.sendFile(
62                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
63  
64              return null;
65          }
66          catch (Exception e) {
67              PortalUtil.sendError(e, request, response);
68  
69              return null;
70          }
71      }
72  
73      protected String getUsersCSV(HttpServletRequest request) throws Exception {
74          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
75              WebKeys.THEME_DISPLAY);
76  
77          if (!RoleLocalServiceUtil.hasUserRole(
78                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
79                  RoleConstants.ADMINISTRATOR, true)) {
80  
81              return StringPool.BLANK;
82          }
83  
84          String exportProgressId = ParamUtil.getString(
85              request, "exportProgressId");
86  
87          ProgressTracker progressTracker = new ProgressTracker(
88              request, exportProgressId);
89  
90          progressTracker.start();
91  
92          List<User> users = UserLocalServiceUtil.search(
93              themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
94              QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
95  
96          int percentage = 10;
97          int total = users.size();
98  
99          progressTracker.updateProgress(percentage);
100 
101         if (total == 0) {
102             return StringPool.BLANK;
103         }
104 
105         StringBundler sb = new StringBundler(users.size() * 4);
106 
107         Iterator<User> itr = users.iterator();
108 
109         for (int i = 0; itr.hasNext(); i++) {
110             User user = itr.next();
111 
112             sb.append(user.getFullName());
113             sb.append(StringPool.COMMA);
114             sb.append(user.getEmailAddress());
115             sb.append(StringPool.NEW_LINE);
116 
117             percentage = Math.min(10 + (i * 90) / total, 99);
118 
119             progressTracker.updateProgress(percentage);
120         }
121 
122         progressTracker.finish();
123 
124         return sb.toString();
125     }
126 
127 }