1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.kernel.dao.orm.QueryUtil;
23  import com.liferay.portal.kernel.util.ContentTypes;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.ProgressTracker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.RoleConstants;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.RoleLocalServiceUtil;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.util.servlet.ServletResponseUtil;
35  
36  import java.util.Iterator;
37  import java.util.List;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  import org.apache.struts.action.Action;
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="ExportUsersAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ExportUsersAction extends Action {
54  
55      public ActionForward execute(
56              ActionMapping mapping, ActionForm form, HttpServletRequest request,
57              HttpServletResponse response)
58          throws Exception {
59  
60          try {
61              String csv = getUsersCSV(request);
62  
63              String fileName = "users.csv";
64              byte[] bytes = csv.getBytes();
65  
66              ServletResponseUtil.sendFile(
67                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
68  
69              return null;
70          }
71          catch (Exception e) {
72              PortalUtil.sendError(e, request, response);
73  
74              return null;
75          }
76      }
77  
78      protected String getUsersCSV(HttpServletRequest request) throws Exception {
79          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
80              WebKeys.THEME_DISPLAY);
81  
82          if (!RoleLocalServiceUtil.hasUserRole(
83                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
84                  RoleConstants.ADMINISTRATOR, true)) {
85  
86              return StringPool.BLANK;
87          }
88  
89          String exportProgressId = ParamUtil.getString(
90              request, "exportProgressId");
91  
92          ProgressTracker progressTracker = new ProgressTracker(
93              request, exportProgressId);
94  
95          progressTracker.start();
96  
97          List<User> users = UserLocalServiceUtil.search(
98              themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
99              QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
100 
101         int percentage = 10;
102         int total = users.size();
103 
104         progressTracker.updateProgress(percentage);
105 
106         StringBuilder sb = new StringBuilder(users.size() * 50);
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 }