001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsUserDataExportDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:26 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.accounts;
033:
034: import org.opencms.db.CmsUserExportSettings;
035: import org.opencms.file.CmsUser;
036: import org.opencms.jsp.CmsJspActionElement;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsRuntimeException;
039: import org.opencms.main.OpenCms;
040: import org.opencms.security.CmsRole;
041: import org.opencms.util.CmsStringUtil;
042: import org.opencms.widgets.CmsGroupWidget;
043: import org.opencms.widgets.CmsSelectWidget;
044: import org.opencms.workplace.CmsDialog;
045: import org.opencms.workplace.CmsWidgetDialogParameter;
046: import org.opencms.workplace.CmsWorkplaceSettings;
047:
048: import java.io.BufferedWriter;
049: import java.io.File;
050: import java.io.FileWriter;
051: import java.io.IOException;
052: import java.lang.reflect.InvocationTargetException;
053: import java.lang.reflect.Method;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import java.util.Iterator;
057: import java.util.List;
058: import java.util.Map;
059:
060: import javax.servlet.ServletException;
061: import javax.servlet.http.HttpServletRequest;
062: import javax.servlet.http.HttpServletResponse;
063: import javax.servlet.jsp.PageContext;
064:
065: /**
066: * Dialog to export user data.<p>
067: *
068: * @author Raphael Schnuck
069: *
070: * @version $Revision: 1.5 $
071: *
072: * @since 6.7.1
073: */
074: public class CmsUserDataExportDialog extends
075: A_CmsUserDataImexportDialog {
076:
077: /** localized messages Keys prefix. */
078: public static final String KEY_PREFIX = "userdata.export";
079:
080: /** Stores the value of the request parameter for the export file. */
081: private String m_paramExportfile;
082:
083: /**
084: * Public constructor with JSP action element.<p>
085: *
086: * @param jsp an initialized JSP action element
087: */
088: public CmsUserDataExportDialog(CmsJspActionElement jsp) {
089:
090: super (jsp);
091: }
092:
093: /**
094: * Public constructor with JSP variables.<p>
095: *
096: * @param context the JSP page context
097: * @param req the JSP request
098: * @param res the JSP response
099: */
100: public CmsUserDataExportDialog(PageContext context,
101: HttpServletRequest req, HttpServletResponse res) {
102:
103: this (new CmsJspActionElement(context, req, res));
104: }
105:
106: /**
107: * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#actionCommit()
108: */
109: public void actionCommit() throws IOException, ServletException {
110:
111: List errors = new ArrayList();
112: // key CmsUser uuid, value CmsUser object
113: Map exportUsers = new HashMap();
114: try {
115: if (((getGroups() == null) || (getGroups().size() < 1))
116: && ((getRoles() == null) || (getRoles().size() < 1))) {
117: exportUsers = getExportAllUsers(exportUsers);
118: } else {
119: exportUsers = getExportUsersFromGroups(exportUsers);
120: exportUsers = getExportUsersFromRoles(exportUsers);
121: }
122: } catch (CmsException e) {
123: throw new CmsRuntimeException(Messages.get().container(
124: Messages.ERR_GET_EXPORT_USERS_0), e);
125: }
126:
127: BufferedWriter bufferedWriter;
128: File downloadFile;
129: try {
130: downloadFile = File.createTempFile("export_users", ".csv");
131: FileWriter fileWriter = new FileWriter(downloadFile);
132: bufferedWriter = new BufferedWriter(fileWriter);
133: } catch (IOException e) {
134: throw e;
135: }
136:
137: CmsUserExportSettings settings = OpenCms
138: .getImportExportManager().getUserExportSettings();
139:
140: String separator = CmsStringUtil.substitute(settings
141: .getSeparator(), "\\t", "\t");
142: List values = settings.getColumns();
143:
144: String headline = "";
145: headline += "name";
146: Iterator itValues = values.iterator();
147: while (itValues.hasNext()) {
148: headline += separator;
149: headline += (String) itValues.next();
150: }
151: headline += "\n";
152: try {
153: bufferedWriter.write(headline);
154: } catch (IOException e) {
155: throw new CmsRuntimeException(Messages.get().container(
156: Messages.ERR_WRITE_TO_EXPORT_FILE_0), e);
157: }
158:
159: Object[] users = exportUsers.values().toArray();
160:
161: for (int i = 0; i < users.length; i++) {
162: CmsUser exportUser = (CmsUser) users[i];
163: if (exportUser.getOuFqn().equals(getParamOufqn())) {
164: String output = "";
165: output += exportUser.getSimpleName();
166: itValues = values.iterator();
167: while (itValues.hasNext()) {
168: output += separator;
169: String curValue = (String) itValues.next();
170: try {
171: Method method = CmsUser.class.getMethod("get"
172: + curValue.substring(0, 1)
173: .toUpperCase()
174: + curValue.substring(1), null);
175: String curOutput = (String) method.invoke(
176: exportUser, null);
177: if (CmsStringUtil
178: .isEmptyOrWhitespaceOnly(curOutput)
179: || curOutput.equals("null")) {
180: curOutput = (String) exportUser
181: .getAdditionalInfo(curValue);
182: }
183:
184: if (curValue.equals("password")) {
185: curOutput = OpenCms.getPasswordHandler()
186: .getDigestType()
187: + "_" + curOutput;
188: }
189:
190: if (!CmsStringUtil
191: .isEmptyOrWhitespaceOnly(curOutput)
192: && !curOutput.equals("null")) {
193: output += curOutput;
194: }
195: } catch (NoSuchMethodException e) {
196: String curOutput = (String) exportUser
197: .getAdditionalInfo(curValue);
198: if (CmsStringUtil
199: .isNotEmptyOrWhitespaceOnly(curOutput)) {
200: output += curOutput;
201: }
202: } catch (IllegalAccessException e) {
203: throw new CmsRuntimeException(Messages.get()
204: .container(
205: Messages.ERR_ILLEGAL_ACCESS_0),
206: e);
207: } catch (InvocationTargetException e) {
208: throw new CmsRuntimeException(
209: Messages
210: .get()
211: .container(
212: Messages.ERR_INVOCATION_TARGET_0),
213: e);
214: }
215: }
216: output += "\n";
217: try {
218: bufferedWriter.write(output);
219: } catch (IOException e) {
220: throw new CmsRuntimeException(
221: Messages
222: .get()
223: .container(
224: Messages.ERR_WRITE_TO_EXPORT_FILE_0),
225: e);
226: }
227: }
228: }
229:
230: try {
231: bufferedWriter.close();
232: } catch (IOException e) {
233: throw new CmsRuntimeException(Messages.get().container(
234: Messages.ERR_WRITE_TO_EXPORT_FILE_0), e);
235: }
236:
237: Map params = new HashMap();
238: params.put("exportfile", downloadFile.getAbsolutePath()
239: .replace('\\', '/'));
240: params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, getParamOufqn());
241: params.put(CmsDialog.PARAM_CLOSELINK, getParamCloseLink());
242: getToolManager().jspForwardTool(this , getCurrentToolPath(),
243: params);
244: setCommitErrors(errors);
245: }
246:
247: /**
248: * Returns a map with the users to export added.<p>
249: *
250: * @param exportUsers the map to add the users
251: * @return a map with the users to export added
252: * @throws CmsException if getting users failed
253: */
254: public Map getExportAllUsers(Map exportUsers) throws CmsException {
255:
256: List users = OpenCms.getOrgUnitManager().getUsers(getCms(),
257: getParamOufqn(), false);
258: if ((users != null) && (users.size() > 0)) {
259: Iterator itUsers = users.iterator();
260: while (itUsers.hasNext()) {
261: CmsUser user = (CmsUser) itUsers.next();
262: if (!exportUsers.containsKey(user.getId())) {
263: exportUsers.put(user.getId(), user);
264: }
265: }
266: }
267: return exportUsers;
268: }
269:
270: /**
271: * Returns a map with the users to export added.<p>
272: *
273: * @param exportUsers the map to add the users
274: * @return a map with the users to export added
275: * @throws CmsException if getting groups or users of group failed
276: */
277: public Map getExportUsersFromGroups(Map exportUsers)
278: throws CmsException {
279:
280: List groups = getGroups();
281: if ((groups != null) && (groups.size() > 0)) {
282: Iterator itGroups = groups.iterator();
283: while (itGroups.hasNext()) {
284: List groupUsers = getCms().getUsersOfGroup(
285: (String) itGroups.next());
286: Iterator itGroupUsers = groupUsers.iterator();
287: while (itGroupUsers.hasNext()) {
288: CmsUser groupUser = (CmsUser) itGroupUsers.next();
289: if (!exportUsers.containsKey(groupUser.getId())) {
290: exportUsers.put(groupUser.getId(), groupUser);
291: }
292: }
293: }
294: }
295: return exportUsers;
296: }
297:
298: /**
299: * Returns a map with the users to export added.<p>
300: *
301: * @param exportUsers the map to add the users
302: * @return a map with the users to export added
303: * @throws CmsException if getting roles or users of role failed
304: */
305: public Map getExportUsersFromRoles(Map exportUsers)
306: throws CmsException {
307:
308: List roles = getRoles();
309: if ((roles != null) && (roles.size() > 0)) {
310: Iterator itRoles = roles.iterator();
311: while (itRoles.hasNext()) {
312: List roleUsers = OpenCms
313: .getRoleManager()
314: .getUsersOfRole(
315: getCms(),
316: CmsRole
317: .valueOfGroupName((String) itRoles
318: .next()), true, false);
319: Iterator itRoleUsers = roleUsers.iterator();
320: while (itRoleUsers.hasNext()) {
321: CmsUser roleUser = (CmsUser) itRoleUsers.next();
322: // contains
323: if (exportUsers.get(roleUser.getId()) == null) {
324: exportUsers.put(roleUser.getId(), roleUser);
325: }
326: }
327: }
328: }
329: return exportUsers;
330: }
331:
332: /**
333: * Returns the JavaScript code to execute during the load of the dialog.<p>
334: *
335: * @return the JavaScript code to execute
336: */
337: public String getOnloadJavaScript() {
338:
339: StringBuffer result = new StringBuffer(256);
340: if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_paramExportfile)) {
341: result.append("javascript:window.open(\"");
342: result.append(getJsp().link(getDownloadPath()));
343: result.append("?servletUrl=");
344: result
345: .append(getJsp()
346: .link(
347: "/system/workplace/admin/workplace/logfileview/downloadTrigger.jsp"));
348: result.append("&filePath=").append(getParamExportfile());
349: result
350: .append("\", \"download\", \"width=300,height=130,left=100,top=100,menubar=no,status=no,toolbar=no\");");
351: }
352: return result.toString();
353: }
354:
355: /**
356: * Returns the export file.<p>
357: *
358: * @return the export file
359: */
360: public String getParamExportfile() {
361:
362: return m_paramExportfile;
363: }
364:
365: /**
366: * @see org.opencms.workplace.tools.CmsToolDialog#pageBody(int, java.lang.String, java.lang.String)
367: */
368: public String pageBody(int segment, String className,
369: String parameters) {
370:
371: if (parameters != null) {
372: if (parameters.lastIndexOf("onload") != -1) {
373: // get substring until onload='
374: String subParameters = parameters.substring(0,
375: parameters.lastIndexOf("onload") + 8);
376: subParameters += getOnloadJavaScript();
377: // get the rest of the old parameter string
378: subParameters += parameters.substring(parameters
379: .lastIndexOf("onload") + 8);
380: parameters = subParameters;
381: }
382: }
383: return super .pageBody(segment, className, parameters);
384: }
385:
386: /**
387: * Sets the export file.<p>
388: *
389: * @param paramExportfile the export file to set
390: */
391: public void setParamExportfile(String paramExportfile) {
392:
393: m_paramExportfile = paramExportfile;
394: }
395:
396: /**
397: * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
398: *
399: * This overwrites the method from the super class to create a layout variation for the widgets.<p>
400: *
401: * @param dialog the dialog (page) to get the HTML for
402: * @return the dialog HTML for all defined widgets of the named dialog (page)
403: */
404: protected String createDialogHtml(String dialog) {
405:
406: StringBuffer result = new StringBuffer(1024);
407:
408: result.append(createWidgetTableStart());
409: // show error header once if there were validation errors
410: result.append(createWidgetErrorHeader());
411:
412: if (dialog.equals(PAGES[0])) {
413: // create the widgets for the first dialog page
414: result
415: .append(dialogBlockStart(key(Messages.GUI_USERDATA_EXPORT_LABEL_HINT_BLOCK_0)));
416: result
417: .append(key(Messages.GUI_USERDATA_EXPORT_LABEL_HINT_TEXT_0));
418: result.append(dialogBlockEnd());
419: result
420: .append(dialogBlockStart(key(Messages.GUI_USERDATA_EXPORT_LABEL_GROUPS_BLOCK_0)));
421: result.append(createWidgetTableStart());
422: result.append(createDialogRowsHtml(0, 0));
423: result.append(createWidgetTableEnd());
424: result.append(dialogBlockEnd());
425: result
426: .append(dialogBlockStart(key(Messages.GUI_USERDATA_EXPORT_LABEL_ROLES_BLOCK_0)));
427: result.append(createWidgetTableStart());
428: result.append(createDialogRowsHtml(1, 1));
429: result.append(createWidgetTableEnd());
430: result.append(dialogBlockEnd());
431: }
432:
433: result.append(createWidgetTableEnd());
434: return result.toString();
435: }
436:
437: /**
438: * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#defineWidgets()
439: */
440: protected void defineWidgets() {
441:
442: initExportObject();
443: setKeyPrefix(KEY_PREFIX);
444:
445: addWidget(new CmsWidgetDialogParameter(this , "groups",
446: PAGES[0], new CmsGroupWidget(null, null,
447: getParamOufqn())));
448: addWidget(new CmsWidgetDialogParameter(this , "roles", PAGES[0],
449: new CmsSelectWidget(getSelectRoles())));
450:
451: }
452:
453: /**
454: * Returns the path to the download jsp.<p>
455: *
456: * @return the path to the download jsp
457: */
458: protected String getDownloadPath() {
459:
460: return "/system/workplace/admin/accounts/imexport_user_data/dodownload.jsp";
461: }
462:
463: /**
464: * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#getPageArray()
465: */
466: protected String[] getPageArray() {
467:
468: return PAGES;
469: }
470:
471: /**
472: * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
473: */
474: protected void initExportObject() {
475:
476: try {
477: if (CmsStringUtil.isEmpty(getParamAction())
478: || CmsDialog.DIALOG_INITIAL
479: .equals(getParamAction())) {
480: // create a new list
481: setGroups(new ArrayList());
482: setRoles(new ArrayList());
483: } else {
484: // this is not the initial call, get the message info object from session
485: setGroups((List) ((Map) getDialogObject())
486: .get("groups"));
487: setRoles((List) ((Map) getDialogObject()).get("roles"));
488: }
489: } catch (Exception e) {
490: // create a new list
491: setGroups(new ArrayList());
492: setRoles(new ArrayList());
493: }
494: }
495:
496: /**
497: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
498: */
499: protected void initWorkplaceRequestValues(
500: CmsWorkplaceSettings settings, HttpServletRequest request) {
501:
502: // initialize parameters and dialog actions in super implementation
503: super .initWorkplaceRequestValues(settings, request);
504:
505: HashMap objectsMap = new HashMap();
506: objectsMap.put("groups", getGroups());
507: objectsMap.put("roles", getRoles());
508:
509: // save the current state of the message (may be changed because of the widget values)
510: setDialogObject(objectsMap);
511: }
512: }
|