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.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  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                  RoleConstants.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, (OrderByComparator)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 }