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.dbmanager;
029
030import org.opencms.file.CmsResource;
031import org.opencms.importexport.CmsVfsImportExportHandler;
032import org.opencms.main.CmsLog;
033import org.opencms.report.A_CmsReportThread;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.Messages;
036import org.opencms.ui.components.CmsBasicDialog;
037import org.opencms.ui.report.CmsReportWidget;
038import org.opencms.util.CmsUUID;
039
040import java.io.FileInputStream;
041import java.io.InputStream;
042
043import org.apache.commons.logging.Log;
044
045import com.vaadin.server.FileDownloader;
046import com.vaadin.server.Resource;
047import com.vaadin.server.StreamResource;
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Window;
051
052/**
053 * The export thread report.<p>
054 */
055public class CmsExportThreadDialog extends CmsBasicDialog {
056
057    /** The serial version id. */
058    private static final long serialVersionUID = 660736768361578208L;
059
060    /** Log instance for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsExportThreadDialog.class);
062
063    /** The import / export handler. */
064    private CmsVfsImportExportHandler m_exportHandler;
065
066    /**
067     * Public constructor.<p>
068     *
069     * @param thread Thread to be run
070     * @param window holding this dialog
071     */
072    public CmsExportThreadDialog(
073        CmsVfsImportExportHandler exportHandler,
074        A_CmsReportThread thread,
075        final Window window) {
076
077        m_exportHandler = exportHandler;
078        window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_EXPORT_THREAD_CAPTION_0));
079        window.setHeight("600px");
080        window.center();
081        setSizeFull();
082        CmsReportWidget reportWidget = new CmsReportWidget(thread);
083        //        reportWidget.addStyleName(" o-shell-terminal");
084        reportWidget.setHeight("100%");
085        String id = "label" + new CmsUUID().getStringValue();
086        reportWidget.setId(id);
087        reportWidget.setWidth("100%");
088        reportWidget.addStyleName("o-sroll-x");
089        setContent(reportWidget);
090        Button button = createButtonClose();
091        button.addClickListener(new Button.ClickListener() {
092
093            private static final long serialVersionUID = -5567381118325538754L;
094
095            public void buttonClick(ClickEvent event) {
096
097                window.close();
098            }
099        });
100        if (!exportHandler.getExportParams().isExportAsFiles()) {
101            Button download = new Button(CmsVaadinUtils.getMessageText(org.opencms.ui.Messages.GUI_BUTTON_DOWNLOAD_0));
102            FileDownloader downloadContext = new FileDownloader(getDownloadResource());
103
104            download.setEnabled(false);
105            downloadContext.extend(download);
106            addButton(download, true);
107            reportWidget.addReportFinishedHandler(() -> {
108                int errors = thread.getErrors().size();
109                download.setEnabled(errors == 0);
110            });
111        }
112        addButton(button, true);
113    }
114
115    /**
116     * Gets the download resource.
117     *
118     * @return the download resource
119     */
120    private Resource getDownloadResource() {
121
122        return new StreamResource(() -> {
123            try {
124                String path = m_exportHandler.getExportParams().getPath();
125                InputStream stream = new FileInputStream(path);
126                return stream;
127            } catch (Exception e) {
128                LOG.error(e.getLocalizedMessage(), e);
129                return null;
130            }
131        }, CmsResource.getName(m_exportHandler.getExportParams().getPath()));
132
133    }
134}