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