1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.RoleConstants;
31  import com.liferay.portal.model.User;
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  public class ExportUsersAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              String csv = getUsersCSV(request);
64  
65              String fileName = "users.csv";
66              byte[] bytes = csv.getBytes();
67  
68              ServletResponseUtil.sendFile(
69                  response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
70  
71              return null;
72          }
73          catch (Exception e) {
74              PortalUtil.sendError(e, request, response);
75  
76              return null;
77          }
78      }
79  
80      protected String getUsersCSV(HttpServletRequest request) throws Exception {
81          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
82              WebKeys.THEME_DISPLAY);
83  
84          if (!RoleLocalServiceUtil.hasUserRole(
85                  themeDisplay.getUserId(), themeDisplay.getCompanyId(),
86                  RoleConstants.ADMINISTRATOR, true)) {
87  
88              return StringPool.BLANK;
89          }
90  
91          String exportProgressId = ParamUtil.getString(
92              request, "exportProgressId");
93  
94          ProgressTracker progressTracker = new ProgressTracker(
95              request, exportProgressId);
96  
97          progressTracker.start();
98  
99          List<User> users = UserLocalServiceUtil.search(
100             themeDisplay.getCompanyId(), null, Boolean.TRUE, null,
101             QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
102 
103         int percentage = 10;
104         int total = users.size();
105 
106         progressTracker.updateProgress(percentage);
107 
108         StringBuilder sb = new StringBuilder(users.size() * 50);
109 
110         Iterator<User> itr = users.iterator();
111 
112         for (int i = 0; itr.hasNext(); i++) {
113             User user = itr.next();
114 
115             sb.append(user.getFullName());
116             sb.append(StringPool.COMMA);
117             sb.append(user.getEmailAddress());
118             sb.append(StringPool.NEW_LINE);
119 
120             percentage = Math.min(10 + (i * 90) / total, 99);
121 
122             progressTracker.updateProgress(percentage);
123         }
124 
125         progressTracker.finish();
126 
127         return sb.toString();
128     }
129 
130 }