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.threads;
029
030import org.opencms.file.CmsObject;
031import org.opencms.importexport.CmsImportParameters;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.report.A_CmsReportThread;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Imports an OpenCms export file into the VFS.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsDatabaseImportThread extends A_CmsReportThread {
044
045    /** The log object for this class. */
046    private static final Log LOG = CmsLog.getLog(CmsDatabaseImportThread.class);
047
048    /** The import file name. */
049    private String m_importFile;
050
051    /** The keep permissions flag. */
052    private boolean m_keepPermissions;
053
054    /**
055     * Imports an OpenCms export file into the VFS.<p>
056     *
057     * @param cms the current OpenCms context object
058     * @param importFile the file to import
059     * @param keepPermissions if set, the permissions set on existing resources will not be modified
060     */
061    public CmsDatabaseImportThread(CmsObject cms, String importFile, boolean keepPermissions) {
062
063        super(cms, Messages.get().getBundle().key(Messages.GUI_DB_IMPORT_THREAD_NAME_1, importFile));
064        m_importFile = importFile;
065        m_keepPermissions = keepPermissions;
066        initHtmlReport(cms.getRequestContext().getLocale());
067    }
068
069    /**
070     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
071     */
072    @Override
073    public String getReportUpdate() {
074
075        return getReport().getReportUpdate();
076    }
077
078    /**
079     * @see java.lang.Runnable#run()
080     */
081    @Override
082    public void run() {
083
084        CmsImportParameters parameters = new CmsImportParameters(m_importFile, "/", m_keepPermissions);
085        boolean indexingAlreadyPaused = OpenCms.getSearchManager().isOfflineIndexingPaused();
086        try {
087            if (!indexingAlreadyPaused) {
088                OpenCms.getSearchManager().pauseOfflineIndexing();
089            }
090            OpenCms.getImportExportManager().importData(getCms(), getReport(), parameters);
091        } catch (Throwable e) {
092            getReport().println(e);
093            if (LOG.isErrorEnabled()) {
094                LOG.error(Messages.get().getBundle().key(Messages.ERR_DB_IMPORT_0), e);
095            }
096        } finally {
097            if (!indexingAlreadyPaused) {
098                OpenCms.getSearchManager().resumeOfflineIndexing();
099            }
100        }
101    }
102}