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.report.A_CmsReportThread;
034
035import java.util.ArrayList;
036import java.util.List;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * Replaces a module.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsModuleReplaceThread extends A_CmsReportThread {
046
047    /** The log object for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsModuleReplaceThread.class);
049
050    /** The delete thread. */
051    private A_CmsReportThread m_deleteThread;
052
053    /** The import thread. */
054    private A_CmsReportThread m_importThread;
055
056    /** The module name. */
057    private String m_moduleName;
058
059    /** The replacement phase. */
060    private int m_phase;
061
062    /** The report content. */
063    private String m_reportContent;
064
065    /** The zip file name. */
066    private String m_zipName;
067
068    /**
069     * Creates the module replace thread.<p>
070     * @param cms the current cms context
071     * @param moduleName the name of the module
072     * @param zipName the name of the module ZIP file
073     */
074    public CmsModuleReplaceThread(CmsObject cms, String moduleName, String zipName) {
075
076        super(cms, Messages.get().getBundle().key(Messages.GUI_REPLACE_MODULE_THREAD_NAME_1, moduleName));
077        m_moduleName = moduleName;
078        m_zipName = zipName;
079
080        List<String> modules = new ArrayList<String>();
081        modules.add(m_moduleName);
082        m_deleteThread = new CmsModuleDeleteThread(getCms(), modules, true);
083        m_importThread = new CmsDatabaseImportThread(getCms(), m_zipName, true);
084        if (LOG.isDebugEnabled()) {
085            LOG.debug(Messages.get().getBundle().key(Messages.LOG_REPLACE_THREAD_CONSTRUCTED_0));
086        }
087        m_phase = 0;
088    }
089
090    /**
091     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
092     */
093    @Override
094    public String getReportUpdate() {
095
096        switch (m_phase) {
097            case 1:
098                return m_deleteThread.getReportUpdate();
099            case 2:
100                String content;
101                if (m_reportContent != null) {
102                    content = m_reportContent;
103                    m_reportContent = null;
104                } else {
105                    content = "";
106                }
107                return content + m_importThread.getReportUpdate();
108            default:
109                // noop
110        }
111        return "";
112    }
113
114    /**
115     * @see java.lang.Runnable#run()
116     */
117    @Override
118    public void run() {
119
120        if (LOG.isDebugEnabled()) {
121            LOG.debug(Messages.get().getBundle().key(Messages.LOG_REPLACE_THREAD_START_DELETE_0));
122        }
123
124        try {
125            OpenCms.getSearchManager().pauseOfflineIndexing();
126            // phase 1: delete the existing module
127            m_phase = 1;
128            m_deleteThread.start();
129            try {
130                m_deleteThread.join();
131            } catch (InterruptedException e) {
132                // should never happen
133                if (LOG.isErrorEnabled()) {
134                    LOG.error(e.getLocalizedMessage(), e);
135                }
136            }
137            // get remaining report contents
138            m_reportContent = m_deleteThread.getReportUpdate();
139            if (LOG.isDebugEnabled()) {
140                LOG.debug(Messages.get().getBundle().key(Messages.LOG_REPLACE_THREAD_START_IMPORT_0));
141            }
142            // phase 2: import the new module
143            m_phase = 2;
144            m_importThread.start();
145            try {
146                m_importThread.join();
147            } catch (InterruptedException e) {
148                // should never happen
149                if (LOG.isErrorEnabled()) {
150                    LOG.error(e.getLocalizedMessage(), e);
151                }
152            }
153            if (LOG.isDebugEnabled()) {
154                LOG.debug(Messages.get().getBundle().key(Messages.LOG_REPLACE_THREAD_FINISHED_0));
155            }
156        } finally {
157            OpenCms.getSearchManager().resumeOfflineIndexing();
158        }
159    }
160}