001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.user;
029
030import org.opencms.db.CmsUserExportSettings;
031import org.opencms.file.CmsUser;
032import org.opencms.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.security.I_CmsPrincipal;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.util.CmsStringUtil;
038import org.opencms.util.CmsUUID;
039
040import java.io.ByteArrayInputStream;
041import java.io.InputStream;
042import java.lang.reflect.InvocationTargetException;
043import java.lang.reflect.Method;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.Iterator;
047import java.util.List;
048import java.util.Map;
049
050import com.vaadin.server.FileDownloader;
051import com.vaadin.server.Resource;
052import com.vaadin.server.StreamResource;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Window;
055
056/**
057 * Class to export user.<p>
058 */
059public abstract class A_CmsImportExportUserDialog extends CmsBasicDialog {
060
061    /**vaadin serial id. */
062    private static final long serialVersionUID = -3990661225158677324L;
063
064    /**Non technical fields to be exported in reduced export. */
065    private static final List<String> NON_TECHNICAL_VALUES = Arrays.asList(
066        "description",
067        "lastname",
068        "firstname",
069        "email",
070        "address",
071        "zipcode",
072        "city",
073        "country");
074
075    /**File downloader. */
076    private FileDownloader m_fileDownloader;
077
078    /**Ou name to export from or import to. */
079    protected String m_ou;
080
081    /**Window. */
082    protected Window m_window;
083
084    /**
085     * Init method.<p>
086     *
087     * @param ou OU
088     * @param window window
089     */
090    protected void init(String ou, Window window) {
091
092        try {
093            displayResourceInfoDirectly(
094                Collections.singletonList(
095                    CmsAccountsApp.getOUInfo(
096                        OpenCms.getOrgUnitManager().readOrganizationalUnit(A_CmsUI.getCmsObject(), ou))));
097        } catch (CmsException e) {
098            //
099        }
100
101        m_ou = ou;
102        m_window = window;
103        getDownloadButton().setEnabled(true);
104        initDownloadButton();
105
106        getCloseButton().addClickListener(event -> window.close());
107    }
108
109    /**
110     * Initializes the download button.<p>
111     */
112    protected void initDownloadButton() {
113
114        if (m_fileDownloader != null) {
115            m_fileDownloader.remove();
116        }
117        m_fileDownloader = new FileDownloader(getDownloadResource());
118        m_fileDownloader.extend(getDownloadButton());
119    }
120
121    /**
122     * Checks if the user can be exported.<p>
123     *
124     * @param exportUser the suer to check
125     *
126     * @return <code>true</code> if the user can be exported
127     */
128    protected boolean isExportable(CmsUser exportUser) {
129
130        return exportUser.getFlags() < I_CmsPrincipal.FLAG_CORE_LIMIT;
131    }
132
133    /**
134     * Gets the close button from layout.
135     * @return Button
136     *  */
137    abstract Button getCloseButton();
138
139    /**
140     * Gets the download button from layout.
141     * @return Button
142     *  */
143    abstract Button getDownloadButton();
144
145    /**
146     * Save export file.<p>
147     * @return InputStream
148     */
149    ByteArrayInputStream getExportStream() {
150
151        Map<CmsUUID, CmsUser> exportUsers = getUserToExport();
152
153        StringBuffer buffer = new StringBuffer();
154        CmsUserExportSettings settings = OpenCms.getImportExportManager().getUserExportSettings();
155
156        String separator = CmsStringUtil.substitute(settings.getSeparator(), "\\t", "\t");
157        List<String> values = settings.getColumns();
158
159        buffer.append("name");
160        Iterator<String> itValues = values.iterator();
161        while (itValues.hasNext()) {
162            String colName = itValues.next();
163            if (isColumnExportable(colName)) {
164                buffer.append(separator);
165                buffer.append(colName);
166            }
167        }
168        buffer.append("\n");
169
170        Object[] users = exportUsers.values().toArray();
171
172        for (int i = 0; i < users.length; i++) {
173            CmsUser exportUser = (CmsUser)users[i];
174            if (!isExportable(exportUser)) {
175                continue;
176            }
177            buffer.append(exportUser.getSimpleName());
178            itValues = values.iterator();
179            while (itValues.hasNext()) {
180                buffer.append(separator);
181                String curValue = itValues.next();
182                if (isColumnExportable(curValue)) {
183                    try {
184                        Method method = CmsUser.class.getMethod(
185                            "get" + curValue.substring(0, 1).toUpperCase() + curValue.substring(1));
186                        String curOutput = (String)method.invoke(exportUser);
187                        if (CmsStringUtil.isEmptyOrWhitespaceOnly(curOutput) || curOutput.equals("null")) {
188                            curOutput = (String)exportUser.getAdditionalInfo(curValue);
189                        }
190
191                        if (curValue.equals("password")) {
192                            curOutput = OpenCms.getPasswordHandler().getDigestType() + "_" + curOutput;
193                        }
194
195                        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(curOutput) && !curOutput.equals("null")) {
196                            buffer.append(curOutput);
197                        }
198                    } catch (NoSuchMethodException e) {
199                        Object obj = exportUser.getAdditionalInfo(curValue);
200                        if (obj != null) {
201                            String curOutput = String.valueOf(obj);
202                            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(curOutput)) {
203                                buffer.append(curOutput);
204                            }
205                        }
206                    } catch (IllegalAccessException e) {
207                        //
208                    } catch (InvocationTargetException e) {
209                        //
210                    }
211                }
212            }
213            buffer.append("\n");
214
215        }
216        return new ByteArrayInputStream(buffer.toString().getBytes());
217    }
218
219    /**
220     * Gets the user to be exported.<p>
221     *
222     * @return Map of user
223     */
224    abstract Map<CmsUUID, CmsUser> getUserToExport();
225
226    /**
227     * Export including technical fields.<p>
228     *
229     * @return boolean
230     */
231    abstract boolean isExportWithTechnicalFields();
232
233    /**
234     * Get download resource for export.<p>
235     *
236     * @return Resource
237     */
238    private Resource getDownloadResource() {
239
240        return new StreamResource(new StreamResource.StreamSource() {
241
242            private static final long serialVersionUID = -8868657402793427460L;
243
244            public InputStream getStream() {
245
246                return getExportStream();
247            }
248        }, "User_Export.csv");
249    }
250
251    /**
252     * Checks if given column is to be exported.<p>
253     *
254     * @param colName to be checked
255     * @return boolean
256     */
257    private boolean isColumnExportable(String colName) {
258
259        if (isExportWithTechnicalFields()) {
260            return true;
261        }
262        return NON_TECHNICAL_VALUES.contains(colName.toLowerCase());
263    }
264}