001/*
002 * This program is part of the Alkacon OpenCms Software library.
003 *
004 * This license applies to all programs, pages, Java classes, parts and
005 * modules of the Alkacon OpenCms Software library published by
006 * Alkacon Software GmbH & Co. KG, unless otherwise noted.
007 *
008 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
009 *
010 * This program is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published by
012 * the Free Software Foundation; either version 2 of the License, or (at
013 * your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with this program; if not, write to the Free Software
022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
023 *
024 * For further information about Alkacon Software GmbH & Co. KG, please see the
025 * companys website: http://www.alkacon.com.
026 *
027 * For further information about OpenCms, please see the OpenCms project
028 * website: http://www.opencms.org.
029 *
030 * The names "Alkacon", "Alkacon Software GmbH & Co. KG" and "OpenCms" must not be used
031 * to endorse or promote products derived from this software without prior
032 * written permission. For written permission, please contact info@alkacon.com.
033 *
034 * Products derived from this software may not be called "Alkacon",
035 * "Alkacon Software GmbH & Co. KG" or "OpenCms", nor may "Alkacon", "Alkacon Software GmbH & Co. KG"
036 * or "OpenCms" appear in their name, without prior written permission of
037 * Alkacon Software GmbH & Co. KG.
038 *
039 * This program is also available under a commercial non-GPL license. For
040 * pricing and ordering information, please inquire at sales@alkacon.com.
041 */
042
043package org.opencms.ui.apps.searchindex;
044
045import org.opencms.file.CmsObject;
046import org.opencms.main.I_CmsEventListener;
047import org.opencms.main.OpenCms;
048import org.opencms.report.A_CmsReportThread;
049import org.opencms.report.I_CmsReport;
050import org.opencms.ui.apps.Messages;
051import org.opencms.util.CmsStringUtil;
052
053import java.util.HashMap;
054import java.util.List;
055import java.util.Map;
056
057/**
058 * Implements methods to utilize a report thread for <code>CmsIndexingReport</code>.<p>
059 *
060 * @since 6.0.0
061 */
062public class CmsIndexingReportThread extends A_CmsReportThread {
063
064    /** The last error occurred. */
065    private Throwable m_error;
066
067    /** A list of names of the indexes to refresh or null for all indexes. */
068    private List<String> m_indexNames;
069
070    /**
071     * Creates an indexing Thread for full update.<p>
072     *
073     * @param cms the current OpenCms context object
074     * @param indexNames a list of names of the indexes to refresh or null for all indexes
075     */
076    public CmsIndexingReportThread(CmsObject cms, List<String> indexNames) {
077
078        super(cms, Messages.get().getBundle().key(Messages.GUI_INDEXING_THREAD_NAME_0));
079        initHtmlReport(cms.getRequestContext().getLocale());
080
081        m_indexNames = indexNames;
082    }
083
084    /**
085     * Returns the last error.<p>
086     *
087     * @see org.opencms.report.A_CmsReportThread#getError()
088     */
089    @Override
090    public Throwable getError() {
091
092        return m_error;
093    }
094
095    /**
096     * Updates the report.<p>
097     *
098     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
099     */
100    @Override
101    public String getReportUpdate() {
102
103        return getReport().getReportUpdate();
104    }
105
106    /**
107     * Starts the indexing report thread.<p>
108     *
109     * @see java.lang.Runnable#run()
110     */
111    @Override
112    public void run() {
113
114        getReport().println(
115            Messages.get().container(Messages.RPT_REBUILD_SEARCH_INDEXES_BEGIN_0),
116            I_CmsReport.FORMAT_HEADLINE);
117
118        try {
119            Map<String, Object> params = new HashMap<String, Object>();
120            params.put(I_CmsEventListener.KEY_REPORT, getReport());
121            if (m_indexNames != null) {
122                params.put(I_CmsEventListener.KEY_INDEX_NAMES, CmsStringUtil.collectionAsString(m_indexNames, ","));
123            }
124            OpenCms.fireCmsEvent(I_CmsEventListener.EVENT_REBUILD_SEARCHINDEXES, params);
125            getReport().println(
126                Messages.get().container(Messages.RPT_REBUILD_SEARCH_INDEXES_END_0),
127                I_CmsReport.FORMAT_HEADLINE);
128        } catch (Throwable exc) {
129            getReport().println(
130                org.opencms.search.Messages.get().container(org.opencms.search.Messages.RPT_SEARCH_INDEXING_FAILED_0),
131                I_CmsReport.FORMAT_WARNING);
132            getReport().println(exc);
133            m_error = exc;
134        }
135    }
136}