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.tools.searchindex;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsIllegalArgumentException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.report.I_CmsReportThread;
035import org.opencms.ui.apps.searchindex.CmsIndexingReportThread;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.CmsWorkplaceSettings;
038import org.opencms.workplace.list.A_CmsListReport;
039import org.opencms.workplace.tools.CmsToolManager;
040
041import java.util.ArrayList;
042import java.util.HashMap;
043import java.util.List;
044import java.util.Map;
045import java.util.StringTokenizer;
046
047import javax.servlet.http.HttpServletRequest;
048import javax.servlet.http.HttpServletResponse;
049import javax.servlet.jsp.PageContext;
050
051import org.apache.commons.logging.Log;
052
053/**
054 * A report for displaying the rebuild process of the corresponding
055 * <code>{@link org.opencms.ui.apps.searchindex.CmsIndexingReportThread}</code>.<p>
056 *
057 * @since 6.0.0
058 */
059public class CmsRebuildReport extends A_CmsListReport {
060
061    /** Indexes parameter: Value is a list of comma separated search index name. */
062    public static final String PARAM_INDEXES = "indexes";
063
064    /** The request parameter value for search indexes: comma-separated names. **/
065    private String m_paramIndexes;
066
067    /** The log object for this class. */
068    private static final Log LOG = CmsLog.getLog(CmsRebuildReport.class);
069
070    /**
071     * Public constructor with JSP action element.<p>
072     *
073     * @param jsp an initialized JSP action element
074     */
075    public CmsRebuildReport(CmsJspActionElement jsp) {
076
077        super(jsp);
078
079    }
080
081    /**
082     * Public constructor with JSP variables.<p>
083     *
084     * @param context the JSP page context
085     * @param req the JSP request
086     * @param res the JSP response
087     */
088    public CmsRebuildReport(PageContext context, HttpServletRequest req, HttpServletResponse res) {
089
090        super(context, req, res);
091
092    }
093
094    /**
095     * Returns the comma-separated String of index names of the indexes that have to be rebuilt.<p>
096     *
097     * @return the comma-separated String of index names of the indexes that have to be rebuilt
098     */
099    public String getParamIndexes() {
100
101        return m_paramIndexes;
102    }
103
104    /**
105     * Returns the <b>unstarted</b> <code>Thread</code> that will do the work of rebuilding the indexes
106     * provided by the request parameter "indexes" value (comma-separated List).<p>
107     *
108     * @throws CmsRuntimeException if the request parameter "indexes" is missing.
109     *
110     * @return the <b>unstarted</b> <code>Thread</code> that will do the work of rebuilding the indexes
111     *         provided by the request parameter "indexes" value (comma-separated List)
112     *
113     * @see org.opencms.workplace.list.A_CmsListReport#initializeThread()
114     */
115    @Override
116    public I_CmsReportThread initializeThread() throws CmsRuntimeException {
117
118        if (getParamIndexes() == null) {
119            CmsIllegalArgumentException ex = new CmsIllegalArgumentException(
120                Messages.get().container(Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1, PARAM_INDEXES));
121            LOG.warn(ex);
122
123            try {
124                getToolManager().jspForwardTool(this, "/searchindex", null);
125            } catch (Exception e) {
126                LOG.error(e.getLocalizedMessage(), e);
127            }
128
129            throw ex;
130        }
131        List<String> indexes = extractIndexNames();
132        CmsIndexingReportThread thread = new CmsIndexingReportThread(getCms(), indexes);
133        return thread;
134    }
135
136    /**
137     * Sets the comma-separated String of index names of the indexes that that have to be rebuilt.<p>
138     *
139     * @param paramIndexes the comma-separated String of index names of the indexes that have to be rebuilt
140     */
141    public void setParamIndexes(String paramIndexes) {
142
143        m_paramIndexes = paramIndexes;
144    }
145
146    /**
147     *
148     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
149     */
150    @Override
151    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
152
153        super.initWorkplaceRequestValues(settings, request);
154        // closelink is a bit complicated: If a forward from a single searchindex overview page
155        // was made, go back to that searchindex-overview. If more indexes are in the given
156        // parameter "indexes" go back to the search management entry page...
157        List<String> indexes = extractIndexNames();
158        if (indexes.size() == 1) {
159            // back to index overview
160            Map<String, String[]> params = new HashMap<String, String[]>();
161            params.put(A_CmsEditSearchIndexDialog.PARAM_INDEXNAME, new String[] {indexes.get(0)});
162            setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), "/searchindex/singleindex", params));
163        } else {
164            // back to search entry page
165            setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), "/searchindex"));
166        }
167    }
168
169    /**
170     * Extracts all modules to delete form the module parameter.<p>
171     * @return list of module names
172     */
173    private List<String> extractIndexNames() {
174
175        List<String> modules = new ArrayList<String>();
176
177        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamIndexes())) {
178            StringTokenizer tok = new StringTokenizer(getParamIndexes(), ",");
179            while (tok.hasMoreTokens()) {
180                String module = tok.nextToken();
181                modules.add(module);
182            }
183        }
184        return modules;
185    }
186}