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 GmbH & Co. KG, 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.workplace.tools.accounts;
029
030import org.opencms.i18n.CmsEncoder;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.util.CmsRequestUtil;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsXsltUtil;
037import org.opencms.widgets.CmsGroupWidget;
038import org.opencms.widgets.CmsHttpUploadWidget;
039import org.opencms.widgets.CmsInputWidget;
040import org.opencms.widgets.CmsSelectWidget;
041import org.opencms.workplace.CmsDialog;
042import org.opencms.workplace.CmsWidgetDialogParameter;
043import org.opencms.workplace.CmsWorkplaceSettings;
044
045import java.io.BufferedReader;
046import java.io.File;
047import java.io.FileOutputStream;
048import java.io.FileReader;
049import java.io.IOException;
050import java.util.ArrayList;
051import java.util.HashMap;
052import java.util.Iterator;
053import java.util.List;
054import java.util.Map;
055
056import javax.servlet.ServletException;
057import javax.servlet.http.HttpServletRequest;
058import javax.servlet.http.HttpServletResponse;
059import javax.servlet.jsp.PageContext;
060
061import org.apache.commons.fileupload.FileItem;
062
063/**
064 * Dialog to import user data.<p>
065 *
066 * @since 6.5.6
067 */
068public class CmsUserDataImportDialog extends A_CmsUserDataImexportDialog {
069
070    /** localized messages Keys prefix. */
071    public static final String KEY_PREFIX = "userdata.import";
072
073    /** The path to the file to import. */
074    private String m_importFile;
075
076    /** The password to use in the import. */
077    private String m_password;
078
079    /**
080     * Public constructor with JSP action element.<p>
081     *
082     * @param jsp an initialized JSP action element
083     */
084    public CmsUserDataImportDialog(CmsJspActionElement jsp) {
085
086        super(jsp);
087    }
088
089    /**
090     * Public constructor with JSP variables.<p>
091     *
092     * @param context the JSP page context
093     * @param req the JSP request
094     * @param res the JSP response
095     */
096    public CmsUserDataImportDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
097
098        this(new CmsJspActionElement(context, req, res));
099    }
100
101    /**
102     * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#actionCommit()
103     */
104    @Override
105    public void actionCommit() throws IOException, ServletException {
106
107        List errors = new ArrayList();
108
109        // get the file item from the multipart request
110        Iterator it = getMultiPartFileItems().iterator();
111        FileItem fi = null;
112        while (it.hasNext()) {
113            fi = (FileItem)it.next();
114            if (fi.getName() != null) {
115                // found the file object, leave iteration
116                break;
117            } else {
118                // this is no file object, check next item
119                continue;
120            }
121        }
122
123        if ((fi != null) && CmsStringUtil.isNotEmptyOrWhitespaceOnly(fi.getName())) {
124            byte[] content = fi.get();
125            File importFile = File.createTempFile("import_users", ".csv");
126            m_importFile = importFile.getAbsolutePath();
127
128            FileOutputStream fileOutput = new FileOutputStream(importFile);
129            fileOutput.write(content);
130            fileOutput.close();
131            fi.delete();
132
133            FileReader fileReader = new FileReader(importFile);
134            BufferedReader bufferedReader = new BufferedReader(fileReader);
135            String line = bufferedReader.readLine();
136
137            if (line != null) {
138                List colDefs = CmsStringUtil.splitAsList(line, CmsXsltUtil.getPreferredDelimiter(line));
139                if (!colDefs.contains("name")) {
140                    errors.add(
141                        new CmsRuntimeException(
142                            Messages.get().container(Messages.ERR_USERDATA_IMPORT_CSV_MISSING_NAME_0)));
143                }
144                if ((line.indexOf("password") == -1) && CmsStringUtil.isEmptyOrWhitespaceOnly(m_password)) {
145                    errors.add(
146                        new CmsRuntimeException(
147                            Messages.get().container(Messages.ERR_USERDATA_IMPORT_CSV_MISSING_PASSWORD_0)));
148                }
149            }
150            bufferedReader.close();
151        } else {
152            errors.add(
153                new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_USERDATA_IMPORT_NO_CONTENT_0)));
154        }
155
156        if (errors.isEmpty()) {
157            Map params = new HashMap();
158            params.put("groups", CmsStringUtil.collectionAsString(getGroups(), ","));
159            params.put("roles", CmsStringUtil.collectionAsString(getRoles(), ","));
160            params.put("importfile", m_importFile);
161            params.put("password", m_password);
162            params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, getParamOufqn());
163            // set action parameter to initial dialog call
164            params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
165
166            getToolManager().jspForwardTool(this, getCurrentToolPath() + "/list", params);
167        }
168        // set the list of errors to display when something goes wrong
169        setCommitErrors(errors);
170    }
171
172    /**
173     * @see org.opencms.workplace.CmsWidgetDialog#dialogButtonsCustom()
174     */
175    @Override
176    public String dialogButtonsCustom() {
177
178        StringBuffer result = new StringBuffer(256);
179        result.append(dialogButtonRow(HTML_START));
180        result.append("<input name=\"ok\" value=\"");
181        result.append(key(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_CONTINUE_0) + "\"");
182        result.append(" type=\"submit\"");
183        result.append(" class=\"dialogbutton\"");
184        result.append(">\n");
185        dialogButtonsHtml(result, BUTTON_CANCEL, "");
186        result.append(dialogButtonRow(HTML_END));
187        return result.toString();
188    }
189
190    /**
191     * Returns the path of the file to import.<p>
192     *
193     * @return the path of the file to import
194     */
195    public String getImportFile() {
196
197        return m_importFile;
198    }
199
200    /**
201     * Returns the password to set during import.<p>
202     *
203     * @return the password to set during import
204     */
205    public String getPassword() {
206
207        return m_password;
208    }
209
210    /**
211     * Sets the path of the import file.<p>
212     *
213     * @param importFile the import file path
214     */
215    public void setImportFile(String importFile) {
216
217        m_importFile = importFile;
218    }
219
220    /**
221     * Sets the password to use during import.<p>
222     *
223     * @param password the password to use during import
224     */
225    public void setPassword(String password) {
226
227        m_password = password;
228    }
229
230    /**
231     * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
232     *
233     * This overwrites the method from the super class to create a layout variation for the widgets.<p>
234     *
235     * @param dialog the dialog (page) to get the HTML for
236     * @return the dialog HTML for all defined widgets of the named dialog (page)
237     */
238    @Override
239    protected String createDialogHtml(String dialog) {
240
241        StringBuffer result = new StringBuffer(1024);
242
243        result.append(createWidgetTableStart());
244        // show error header once if there were validation errors
245        result.append(createWidgetErrorHeader());
246
247        if (dialog.equals(PAGES[0])) {
248            // create the widgets for the first dialog page
249            result.append(dialogBlockStart(key(Messages.GUI_USERDATA_IMPORT_LABEL_HINT_BLOCK_0)));
250            result.append(key(Messages.GUI_USERDATA_IMPORT_LABEL_HINT_TEXT_0));
251            result.append(dialogBlockEnd());
252            result.append(dialogBlockStart(key(Messages.GUI_USERDATA_IMPORT_LABEL_DATA_BLOCK_0)));
253            result.append(createWidgetTableStart());
254            result.append(createDialogRowsHtml(0, 3));
255            result.append(createWidgetTableEnd());
256            result.append(dialogBlockEnd());
257        }
258
259        result.append(createWidgetTableEnd());
260        return result.toString();
261    }
262
263    /**
264     * @see org.opencms.workplace.CmsWidgetDialog#defaultActionHtmlContent()
265     */
266    @Override
267    protected String defaultActionHtmlContent() {
268
269        StringBuffer result = new StringBuffer(2048);
270        result.append("<form name=\"EDITOR\" id=\"EDITOR\" method=\"post\" action=\"").append(getDialogRealUri());
271        result.append("\" class=\"nomargin\" onsubmit=\"return submitAction('").append(DIALOG_OK).append(
272            "', null, 'EDITOR');\" enctype=\"multipart/form-data\">\n");
273        result.append(dialogContentStart(getDialogTitle()));
274        result.append(buildDialogForm());
275        result.append(dialogContentEnd());
276        result.append(dialogButtonsCustom());
277        result.append(paramsAsHidden());
278        if (getParamFramename() == null) {
279            result.append("\n<input type=\"hidden\" name=\"").append(PARAM_FRAMENAME).append("\" value=\"\">\n");
280        }
281        result.append("</form>\n");
282        result.append(getWidgetHtmlEnd());
283        return result.toString();
284    }
285
286    /**
287     * @see org.opencms.workplace.tools.accounts.A_CmsUserDataImexportDialog#defineWidgets()
288     */
289    @Override
290    protected void defineWidgets() {
291
292        initImportObject();
293        setKeyPrefix(KEY_PREFIX);
294
295        addWidget(
296            new CmsWidgetDialogParameter(this, "groups", PAGES[0], new CmsGroupWidget(null, null, getParamOufqn())));
297        addWidget(new CmsWidgetDialogParameter(this, "roles", PAGES[0], new CmsSelectWidget(getSelectRoles())));
298        addWidget(new CmsWidgetDialogParameter(this, "password", PAGES[0], new CmsInputWidget()));
299        addWidget(new CmsWidgetDialogParameter(this, "importFile", PAGES[0], new CmsHttpUploadWidget()));
300    }
301
302    /**
303     * @see org.opencms.workplace.CmsWidgetDialog#fillWidgetValues(javax.servlet.http.HttpServletRequest)
304     */
305    @Override
306    protected void fillWidgetValues(HttpServletRequest request) {
307
308        Map parameters;
309        if (getMultiPartFileItems() != null) {
310            parameters = CmsRequestUtil.readParameterMapFromMultiPart(
311                getCms().getRequestContext().getEncoding(),
312                getMultiPartFileItems());
313        } else {
314            parameters = request.getParameterMap();
315        }
316        Map processedParameters = new HashMap();
317        Iterator p = parameters.entrySet().iterator();
318        // make sure all "hidden" widget parameters are decoded
319        while (p.hasNext()) {
320            Map.Entry entry = (Map.Entry)p.next();
321            String key = (String)entry.getKey();
322            String[] values = (String[])entry.getValue();
323            if (key.startsWith(HIDDEN_PARAM_PREFIX)) {
324                // this is an encoded hidden parameter
325                key = key.substring(HIDDEN_PARAM_PREFIX.length());
326                String[] newValues = new String[values.length];
327                for (int l = 0; l < values.length; l++) {
328                    newValues[l] = CmsEncoder.decode(values[l], getCms().getRequestContext().getEncoding());
329                }
330                values = newValues;
331            }
332            processedParameters.put(key, values);
333        }
334
335        // now process the parameters
336        m_widgetParamValues = new HashMap();
337        Iterator i = getWidgets().iterator();
338
339        while (i.hasNext()) {
340            // check for all widget base parameters
341            CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
342
343            List params = new ArrayList();
344            int maxOccurs = base.getMaxOccurs();
345
346            boolean onPage = false;
347            if (base.isCollectionBase()) {
348                // for a collection base, check if we are on the page where the collection base is shown
349                if (CmsStringUtil.isNotEmpty(getParamAction()) && !DIALOG_INITIAL.equals(getParamAction())) {
350                    // if no action set (usually for first display of dialog) make sure all values are shown
351                    // DIALOG_INITIAL is a special value for the first display and must be handled the same way
352                    String page = getParamPage();
353                    // keep in mind that since the paramPage will be set AFTER the widget values are filled,
354                    // so the first time this page is called from another page the following will result to "false",
355                    // but for every "submit" on the page this will be "true"
356                    onPage = CmsStringUtil.isEmpty(page)
357                        || CmsStringUtil.isEmpty(base.getDialogPage())
358                        || base.getDialogPage().equals(page);
359                }
360            }
361
362            for (int j = 0; j < maxOccurs; j++) {
363                // check for all possible values in the request parameters
364                String id = CmsWidgetDialogParameter.createId(base.getName(), j);
365
366                boolean required = (params.size() < base.getMinOccurs())
367                    || (processedParameters.get(id) != null)
368                    || (!onPage && base.hasValue(j));
369
370                if (required) {
371                    CmsWidgetDialogParameter param = new CmsWidgetDialogParameter(base, params.size(), j);
372                    param.setKeyPrefix(KEY_PREFIX);
373                    base.getWidget().setEditorValue(getCms(), processedParameters, this, param);
374                    params.add(param);
375                }
376            }
377            m_widgetParamValues.put(base.getName(), params);
378        }
379    }
380
381    /**
382     * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
383     */
384    protected void initImportObject() {
385
386        try {
387            if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
388                // create a new list
389                setGroups(new ArrayList());
390                setRoles(new ArrayList());
391            } else {
392                // this is not the initial call, get the message info object from session
393                setGroups((List)((Map)getDialogObject()).get("groups"));
394                setRoles((List)((Map)getDialogObject()).get("roles"));
395                m_importFile = (String)((Map)getDialogObject()).get("importfile");
396                m_password = (String)((Map)getDialogObject()).get("password");
397            }
398        } catch (Exception e) {
399            // create a new list
400            setGroups(new ArrayList());
401            setRoles(new ArrayList());
402        }
403    }
404
405    /**
406     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
407     */
408    @Override
409    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
410
411        // initialize parameters and dialog actions in super implementation
412        super.initWorkplaceRequestValues(settings, request);
413
414        HashMap objectsMap = new HashMap();
415        objectsMap.put("groups", getGroups());
416        objectsMap.put("roles", getRoles());
417        objectsMap.put("importfile", m_importFile);
418        objectsMap.put("password", m_password);
419
420        // save the current state of the message (may be changed because of the widget values)
421        setDialogObject(objectsMap);
422    }
423}