cmsuserdataexportdialog.java

来自「找了很久才找到到源代码」· Java 代码 · 共 449 行 · 第 1/2 页

JAVA
449
字号
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsUserDataExportDialog.java,v $
 * Date   : $Date: 2007-08-13 16:29:45 $
 * Version: $Revision: 1.3 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.workplace.tools.accounts;

import org.opencms.db.CmsUserExportSettings;
import org.opencms.file.CmsUser;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsRuntimeException;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsRole;
import org.opencms.util.CmsStringUtil;
import org.opencms.widgets.CmsGroupWidget;
import org.opencms.widgets.CmsSelectWidget;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWidgetDialogParameter;
import org.opencms.workplace.CmsWorkplaceSettings;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

/**
 * Dialog to export user data.<p>
 * 
 * @author Raphael Schnuck 
 * 
 * @version $Revision: 1.3 $ 
 * 
 * @since 6.7.1
 */
public class CmsUserDataExportDialog extends A_CmsUserDataImexportDialog {

    /** localized messages Keys prefix. */
    public static final String KEY_PREFIX = "userdata.export";

    /** Stores the value of the request parameter for the export file. */
    private String m_paramExportfile;

    /**
     * Public constructor with JSP action element.<p>
     * 
     * @param jsp an initialized JSP action element
     */
    public CmsUserDataExportDialog(CmsJspActionElement jsp) {

        super(jsp);
    }

    /**
     * Public constructor with JSP variables.<p>
     * 
     * @param context the JSP page context
     * @param req the JSP request
     * @param res the JSP response
     */
    public CmsUserDataExportDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        this(new CmsJspActionElement(context, req, res));
    }

    /**
     * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#actionCommit()
     */
    public void actionCommit() throws IOException, ServletException {

        List errors = new ArrayList();
        // key CmsUser uuid, value CmsUser object
        Map exportUsers = new HashMap();
        try {
            if ((getGroups() == null || getGroups().size() < 1) && (getRoles() == null || getRoles().size() < 1)) {
                exportUsers = getExportAllUsers(exportUsers);
            } else {
                exportUsers = getExportUsersFromGroups(exportUsers);
                exportUsers = getExportUsersFromRoles(exportUsers);
            }
        } catch (CmsException e) {
            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_GET_EXPORT_USERS_0), e);
        }

        BufferedWriter bufferedWriter;
        File downloadFile;
        try {
            downloadFile = File.createTempFile("export_users", ".csv");
            FileWriter fileWriter = new FileWriter(downloadFile);
            bufferedWriter = new BufferedWriter(fileWriter);
        } catch (IOException e) {
            throw e;
        }

        CmsUserExportSettings settings = OpenCms.getImportExportManager().getUserExportSettings();

        String separator = CmsStringUtil.substitute(settings.getSeparator(), "\\t", "\t");
        List values = settings.getColumns();

        String headline = "";
        headline += "name";
        Iterator itValues = values.iterator();
        while (itValues.hasNext()) {
            headline += separator;
            headline += (String)itValues.next();
        }
        headline += "\n";
        try {
            bufferedWriter.write(headline);
        } catch (IOException e) {
            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_WRITE_TO_EXPORT_FILE_0), e);
        }

        Object[] users = exportUsers.values().toArray();

        for (int i = 0; i < users.length; i++) {
            CmsUser exportUser = (CmsUser)users[i];
            if (exportUser.getOuFqn().equals(getParamOufqn())) {
                String output = "";
                output += exportUser.getSimpleName();
                itValues = values.iterator();
                while (itValues.hasNext()) {
                    output += separator;
                    String curValue = (String)itValues.next();
                    try {
                        Method method = CmsUser.class.getMethod("get"
                            + curValue.substring(0, 1).toUpperCase()
                            + curValue.substring(1), null);
                        String curOutput = (String)method.invoke(exportUser, null);
                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(curOutput) || curOutput.equals("null")) {
                            curOutput = (String)exportUser.getAdditionalInfo(curValue);
                        }

                        if (curValue.equals("password")) {
                            curOutput = OpenCms.getPasswordHandler().getDigestType() + "_" + curOutput;
                        }

                        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(curOutput) && !curOutput.equals("null")) {
                            output += curOutput;
                        }
                    } catch (NoSuchMethodException e) {
                        String curOutput = (String)exportUser.getAdditionalInfo(curValue);
                        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(curOutput)) {
                            output += curOutput;
                        }
                    } catch (IllegalAccessException e) {
                        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ILLEGAL_ACCESS_0), e);
                    } catch (InvocationTargetException e) {
                        throw new CmsRuntimeException(Messages.get().container(Messages.ERR_INVOCATION_TARGET_0), e);
                    }
                }
                output += "\n";
                try {
                    bufferedWriter.write(output);
                } catch (IOException e) {
                    throw new CmsRuntimeException(Messages.get().container(Messages.ERR_WRITE_TO_EXPORT_FILE_0), e);
                }
            }
        }

        try {
            bufferedWriter.close();
        } catch (IOException e) {
            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_WRITE_TO_EXPORT_FILE_0), e);
        }

        Map params = new HashMap();
        params.put("exportfile", downloadFile.getAbsolutePath().replace('\\', '/'));
        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, getParamOufqn());
        params.put(CmsDialog.PARAM_CLOSELINK, getParamCloseLink());
        getToolManager().jspForwardTool(this, getCurrentToolPath(), params);
        setCommitErrors(errors);
    }

    /**
     * Returns a map with the users to export added.<p>
     * 
     * @param exportUsers the map to add the users
     * @return a map with the users to export added
     * @throws CmsException if getting users failed
     */
    public Map getExportAllUsers(Map exportUsers) throws CmsException {

        List users = OpenCms.getOrgUnitManager().getUsers(getCms(), getParamOufqn(), false);
        if (users != null && users.size() > 0) {
            Iterator itUsers = users.iterator();
            while (itUsers.hasNext()) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?