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