001
014
015 package com.liferay.portlet.enterpriseadmin.action;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.servlet.SessionErrors;
020 import com.liferay.portal.kernel.util.ContentTypes;
021 import com.liferay.portal.kernel.util.OrderByComparator;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.kernel.util.ProgressTracker;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.model.User;
027 import com.liferay.portal.security.permission.ActionKeys;
028 import com.liferay.portal.security.permission.PermissionChecker;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portal.service.http.UserJSONSerializer;
031 import com.liferay.portal.service.permission.PortalPermissionUtil;
032 import com.liferay.portal.struts.ActionConstants;
033 import com.liferay.portal.struts.PortletAction;
034 import com.liferay.portal.theme.ThemeDisplay;
035 import com.liferay.portal.util.PortalUtil;
036 import com.liferay.portal.util.PortletKeys;
037 import com.liferay.portal.util.PropsValues;
038 import com.liferay.portal.util.WebKeys;
039 import com.liferay.portlet.ActionResponseImpl;
040 import com.liferay.portlet.enterpriseadmin.search.UserSearch;
041 import com.liferay.portlet.enterpriseadmin.search.UserSearchTerms;
042 import com.liferay.portlet.expando.model.ExpandoBridge;
043 import com.liferay.util.servlet.ServletResponseUtil;
044
045 import java.util.Iterator;
046 import java.util.LinkedHashMap;
047 import java.util.List;
048
049 import javax.portlet.ActionRequest;
050 import javax.portlet.ActionResponse;
051 import javax.portlet.PortletConfig;
052 import javax.portlet.PortletURL;
053
054 import javax.servlet.http.HttpServletRequest;
055 import javax.servlet.http.HttpServletResponse;
056
057 import org.apache.struts.action.ActionForm;
058 import org.apache.struts.action.ActionMapping;
059
060
064 public class ExportUsersAction extends PortletAction {
065
066 public void processAction(
067 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
068 ActionRequest actionRequest, ActionResponse actionResponse)
069 throws Exception {
070
071 try {
072 String csv = getUsersCSV(actionRequest, actionResponse);
073
074 String fileName = "users.csv";
075 byte[] bytes = csv.getBytes();
076
077 HttpServletRequest request = PortalUtil.getHttpServletRequest(
078 actionRequest);
079 HttpServletResponse response = PortalUtil.getHttpServletResponse(
080 actionResponse);
081
082 ServletResponseUtil.sendFile(
083 request, response, fileName, bytes, ContentTypes.TEXT_CSV_UTF8);
084
085 setForward(actionRequest, ActionConstants.COMMON_NULL);
086 }
087 catch (Exception e) {
088 SessionErrors.add(actionRequest, e.getClass().getName());
089
090 setForward(actionRequest, "portlet.enterprise_admin.error");
091 }
092 }
093
094 protected String getUserCSV(User user) {
095 StringBundler sb = new StringBundler(
096 PropsValues.USERS_EXPORT_CSV_FIELDS.length * 2);
097
098 JSONObject jsonObject = UserJSONSerializer.toJSONObject(user);
099
100 for (int i = 0; i < PropsValues.USERS_EXPORT_CSV_FIELDS.length; i++) {
101 String field = PropsValues.USERS_EXPORT_CSV_FIELDS[i];
102
103 if (field.equals("fullName")) {
104 sb.append(user.getFullName());
105 }
106 else if (field.startsWith("expando:")) {
107 String attributeName = field.substring(7);
108
109 ExpandoBridge expandoBridge = user.getExpandoBridge();
110
111 sb.append(expandoBridge.getAttribute(attributeName));
112 }
113 else {
114 sb.append(jsonObject.getString(field));
115 }
116
117 if ((i + 1) < PropsValues.USERS_EXPORT_CSV_FIELDS.length) {
118 sb.append(StringPool.COMMA);
119 }
120 }
121
122 sb.append(StringPool.NEW_LINE);
123
124 return sb.toString();
125 }
126
127 protected List<User> getUsers(
128 ActionRequest actionRequest, ActionResponse actionResponse,
129 ThemeDisplay themeDisplay)
130 throws Exception {
131
132 PortletURL portletURL =
133 ((ActionResponseImpl)actionResponse).createRenderURL(
134 PortletKeys.ENTERPRISE_ADMIN_USERS);
135
136 UserSearch userSearch = new UserSearch(actionRequest, portletURL);
137
138 UserSearchTerms searchTerms =
139 (UserSearchTerms)userSearch.getSearchTerms();
140
141 if (!searchTerms.isAdvancedSearch() && !searchTerms.hasActive()) {
142 searchTerms.setActive(Boolean.TRUE);
143 }
144
145 LinkedHashMap<String, Object> params =
146 new LinkedHashMap<String, Object>();
147
148 long organizationId = searchTerms.getOrganizationId();
149
150 if (organizationId > 0) {
151 params.put("usersOrgs", new Long(organizationId));
152 }
153
154 long roleId = searchTerms.getRoleId();
155
156 if (roleId > 0) {
157 params.put("usersRoles", new Long(roleId));
158 }
159
160 long userGroupId = searchTerms.getUserGroupId();
161
162 if (userGroupId > 0) {
163 params.put("usersUserGroups", new Long(userGroupId));
164 }
165
166 if (searchTerms.isAdvancedSearch()) {
167 return UserLocalServiceUtil.search(
168 themeDisplay.getCompanyId(), searchTerms.getFirstName(),
169 searchTerms.getMiddleName(), searchTerms.getLastName(),
170 searchTerms.getScreenName(), searchTerms.getEmailAddress(),
171 searchTerms.getActive(), params, searchTerms.isAndOperator(),
172 QueryUtil.ALL_POS, QueryUtil.ALL_POS, (OrderByComparator)null);
173 }
174 else {
175 return UserLocalServiceUtil.search(
176 themeDisplay.getCompanyId(), searchTerms.getKeywords(),
177 searchTerms.getActive(), params, QueryUtil.ALL_POS,
178 QueryUtil.ALL_POS, (OrderByComparator)null);
179 }
180 }
181
182 protected String getUsersCSV(
183 ActionRequest actionRequest, ActionResponse actionResponse)
184 throws Exception {
185
186 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
187 WebKeys.THEME_DISPLAY);
188
189 PermissionChecker permissionChecker =
190 themeDisplay.getPermissionChecker();
191
192 if (!PortalPermissionUtil.contains(
193 permissionChecker, ActionKeys.EXPORT_USER)) {
194
195 return StringPool.BLANK;
196 }
197
198 String exportProgressId = ParamUtil.getString(
199 actionRequest, "exportProgressId");
200
201 ProgressTracker progressTracker = new ProgressTracker(
202 actionRequest, exportProgressId);
203
204 progressTracker.start();
205
206 List<User> users = getUsers(
207 actionRequest, actionResponse, themeDisplay);
208
209 int percentage = 10;
210 int total = users.size();
211
212 progressTracker.updateProgress(percentage);
213
214 if (total == 0) {
215 return StringPool.BLANK;
216 }
217
218 StringBundler sb = new StringBundler(users.size() * 4);
219
220 Iterator<User> itr = users.iterator();
221
222 for (int i = 0; itr.hasNext(); i++) {
223 User user = itr.next();
224
225 sb.append(getUserCSV(user));
226
227 percentage = Math.min(10 + (i * 90) / total, 99);
228
229 progressTracker.updateProgress(percentage);
230 }
231
232 progressTracker.finish();
233
234 return sb.toString();
235 }
236
237 }