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.main.CmsLog;
032import org.opencms.main.OpenCms;
033import org.opencms.module.CmsModuleManager;
034import org.opencms.report.A_CmsReportThread;
035import org.opencms.report.I_CmsReport;
036import org.opencms.util.CmsUUID;
037
038import java.util.Collections;
039import java.util.Iterator;
040import java.util.List;
041
042import org.apache.commons.logging.Log;
043
044/**
045 * Deletes a module.<p>
046 *
047 * @since 6.0.0
048 */
049public class CmsModuleDeleteThread extends A_CmsReportThread {
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsModuleDeleteThread.class);
053
054    /** A list of module name to delete. */
055    private List<String> m_moduleNames;
056
057    /** mode indicating if pre-replacement or final deletion. */
058    private boolean m_replaceMode;
059
060    /**
061     * Creates the module delete thread.<p>
062     *
063     * @param cms the current cms context
064     * @param moduleNames the name of the module
065     * @param replaceMode the replace mode
066     */
067    public CmsModuleDeleteThread(CmsObject cms, List<String> moduleNames, boolean replaceMode) {
068
069        super(cms, Messages.get().getBundle().key(Messages.GUI_DELETE_MODULE_THREAD_NAME_1, moduleNames));
070        m_moduleNames = moduleNames;
071        m_replaceMode = replaceMode;
072        initHtmlReport(cms.getRequestContext().getLocale());
073        if (LOG.isDebugEnabled()) {
074            LOG.debug(Messages.get().getBundle().key(Messages.LOG_DELETE_THREAD_CONSTRUCTED_0));
075        }
076    }
077
078    /**
079     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
080     */
081    @Override
082    public String getReportUpdate() {
083
084        return getReport().getReportUpdate();
085    }
086
087    /**
088     * @see java.lang.Runnable#run()
089     */
090    @Override
091    public void run() {
092
093        I_CmsReport report = getReport();
094        CmsUUID pauseId = OpenCms.getSearchManager().pauseOfflineIndexing();
095        try {
096            if (LOG.isDebugEnabled()) {
097                LOG.debug(Messages.get().getBundle().key(Messages.LOG_DELETE_THREAD_STARTED_0));
098            }
099            if (!m_replaceMode) {
100                OpenCms.getModuleManager().checkModuleSelectionList(m_moduleNames, null, true);
101            }
102            m_moduleNames = CmsModuleManager.topologicalSort(m_moduleNames, null);
103            Collections.reverse(m_moduleNames);
104
105            Iterator<String> j = m_moduleNames.iterator();
106            while (j.hasNext()) {
107                String moduleName = j.next();
108
109                moduleName = moduleName.replace('\\', '/');
110
111                // now delete the module
112                OpenCms.getModuleManager().deleteModule(getCms(), moduleName, m_replaceMode, report);
113            }
114
115            if (LOG.isDebugEnabled()) {
116                LOG.debug(Messages.get().getBundle().key(Messages.LOG_DELETE_THREAD_FINISHED_0));
117            }
118        } catch (Throwable e) {
119            report.println(e);
120            LOG.error(Messages.get().getBundle().key(Messages.LOG_MODULE_DELETE_FAILED_1, m_moduleNames), e);
121        } finally {
122            OpenCms.getSearchManager().resumeOfflineIndexing(pauseId);
123        }
124    }
125}