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.sitemanager;
029
030import org.opencms.file.CmsObject;
031import org.opencms.importexport.CmsExportParameters;
032import org.opencms.importexport.CmsVfsImportExportHandler;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.module.CmsModule.ExportMode;
037import org.opencms.report.A_CmsReportThread;
038import org.opencms.site.CmsSite;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.ui.apps.Messages;
042import org.opencms.ui.apps.dbmanager.CmsExportThreadDialog;
043import org.opencms.ui.components.CmsBasicDialog;
044import org.opencms.ui.components.CmsResourceInfo;
045import org.opencms.util.CmsFileUtil;
046import org.opencms.workplace.threads.CmsExportThread;
047
048import java.io.File;
049import java.text.SimpleDateFormat;
050import java.util.Collections;
051import java.util.Date;
052
053import org.apache.commons.logging.Log;
054
055import com.vaadin.ui.Button;
056import com.vaadin.ui.Label;
057import com.vaadin.ui.Window;
058
059/**
060 * Dialog used to export the contents of a site.
061 */
062public class CmsExportSiteForm extends CmsBasicDialog {
063
064    /** The log for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsExportSiteForm.class);
066
067    /** The Cancel button. */
068    protected Button m_cancel;
069
070    /** The CMS context. */
071    protected CmsObject m_cms;
072
073    /** The site manager instance. */
074    protected CmsSiteManager m_manager;
075
076    /** The OK button. */
077    protected Button m_ok;
078
079    /** The project label. */
080    protected Label m_projectLabel;
081
082    /** The site root of the site to export. */
083    protected String m_siteRoot;
084
085    /**
086     * Creates a new instance.
087     *
088     * @param cms the CMS context
089     * @param manager the site manager app instance
090     * @param siteRoot the site root of the site to export
091     */
092    public CmsExportSiteForm(CmsObject cms, CmsSiteManager manager, String siteRoot) {
093
094        m_manager = manager;
095        m_siteRoot = siteRoot;
096        m_cms = cms;
097        String projectName = cms.getRequestContext().getCurrentProject().getName();
098
099        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
100        m_projectLabel.setValue(CmsVaadinUtils.getMessageText(Messages.GUI_SITE_EXPORT_PROJECT_LABEL_1, projectName));
101        m_cancel.addClickListener(event -> CmsVaadinUtils.closeWindow(CmsExportSiteForm.this));
102        CmsSite site = OpenCms.getSiteManager().getSiteForSiteRoot(siteRoot);
103        if (site != null) {
104            CmsResourceInfo resourceInfo = new CmsResourceInfo(
105                site.getTitle(),
106                site.getSiteRoot(),
107                m_manager.getFavIcon(site.getSiteRoot()));
108            resourceInfo.addStyleName("o-res-site-info");
109            displayResourceInfoDirectly(Collections.singletonList(resourceInfo));
110        }
111        m_ok.addClickListener(event -> startThread());
112    }
113
114    /**
115     * Starts the export thread and displays it's report.<p>
116     */
117    protected void startThread() {
118
119        CmsObject cms = null;
120        try {
121            cms = OpenCms.initCmsObject(m_cms);
122        } catch (CmsException e1) {
123            LOG.error(e1.getLocalizedMessage(), e1);
124        }
125        cms.getRequestContext().setSiteRoot(m_siteRoot);
126        CmsExportParameters m_exportParams = new CmsExportParameters();
127
128        m_exportParams.setExportAccountData(false);
129        m_exportParams.setExportAsFiles(false);
130        m_exportParams.setExportProjectData(false);
131        m_exportParams.setExportResourceData(true);
132        m_exportParams.setInProject(false);
133        m_exportParams.setIncludeSystemFolder(false);
134        m_exportParams.setIncludeUnchangedResources(true);
135        m_exportParams.setSkipParentFolders(false);
136        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
137        String dateStr = fmt.format(new Date());
138        String filename = dateStr
139            + "_"
140            + CmsFileUtil.removeTrailingSeparator(CmsFileUtil.removeLeadingSeparator(m_siteRoot)).replace("/", "_")
141            + ".zip";
142        String exportFilePath = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
143            OpenCms.getSystemInfo().getPackagesRfsPath() + File.separator + filename);
144        m_exportParams.setPath(exportFilePath);
145        m_exportParams.setRecursive(true);
146        m_exportParams.setResources(Collections.singletonList("/"));
147        m_exportParams.setExportMode(ExportMode.DEFAULT);
148        m_exportParams.setContentAge(0);
149
150        CmsVfsImportExportHandler handler = new CmsVfsImportExportHandler();
151        handler.setExportParams(m_exportParams);
152        A_CmsReportThread exportThread = new CmsExportThread(cms, handler, false);
153
154        Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
155        window.setContent(new CmsExportThreadDialog(handler, exportThread, window));
156        A_CmsUI.get().addWindow(window);
157        CmsVaadinUtils.closeWindow(CmsExportSiteForm.this);
158        exportThread.start();
159    }
160
161}