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.sourcesearch;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.report.I_CmsReportThread;
032import org.opencms.workplace.CmsDialog;
033import org.opencms.workplace.list.A_CmsListExplorerDialog;
034import org.opencms.workplace.list.A_CmsListReport;
035import org.opencms.workplace.tools.CmsToolDialog;
036
037import java.util.Collection;
038import java.util.HashMap;
039import java.util.Map;
040
041import javax.servlet.http.HttpServletRequest;
042import javax.servlet.http.HttpServletResponse;
043import javax.servlet.http.HttpSession;
044import javax.servlet.jsp.JspException;
045import javax.servlet.jsp.PageContext;
046
047/**
048 * Provides a report for searching in contents.
049 * <p>
050 *
051 * @since 7.5.3
052 */
053public class CmsSourceSearchReport extends A_CmsListReport {
054
055    /**
056     * Public constructor with JSP action element.
057     * <p>
058     *
059     * @param jsp an initialized JSP action element
060     */
061    public CmsSourceSearchReport(CmsJspActionElement jsp) {
062
063        super(jsp);
064    }
065
066    /**
067     * Public constructor with JSP variables.
068     * <p>
069     *
070     * @param context the JSP page context.
071     *
072     * @param req the JSP request.
073     *
074     * @param res the JSP response.
075     */
076    public CmsSourceSearchReport(PageContext context, HttpServletRequest req, HttpServletResponse res) {
077
078        this(new CmsJspActionElement(context, req, res));
079    }
080
081    /**
082     * Used to close the current JSP dialog.<p>
083     *
084     * This method tries to include the URI stored in the workplace settings.
085     * This URI is determined by the frame name, which has to be set
086     * in the frame name parameter.<p>
087     *
088     * @throws JspException if including an element fails
089     */
090    @SuppressWarnings("rawtypes")
091    @Override
092    public void actionCloseDialog() throws JspException {
093
094        // create a map with empty "resource" parameter to avoid changing the folder when returning to explorer file list
095        Map<String, String[]> params = new HashMap<String, String[]>();
096        params.put(PARAM_RESOURCE, new String[] {""});
097        // set action parameter to initial dialog call
098        params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
099        params.put(A_CmsListExplorerDialog.PARAM_SHOW_EXPLORER, new String[] {Boolean.TRUE.toString()});
100        params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
101
102        // close link parameter present
103        try {
104            HttpSession session = getJsp().getJspContext().getSession();
105            Collection resultList = (Collection)session.getAttribute(
106                CmsSearchReplaceSettings.ATTRIBUTE_NAME_SOURCESEARCH_RESULT_LIST);
107            if ((resultList != null) && !resultList.isEmpty()) {
108                getToolManager().jspForwardTool(this, "/searchindex/sourcesearch/fileslist", params);
109            } else {
110                getToolManager().jspForwardPage(this, "index.jsp", params);
111            }
112        } catch (Exception e) {
113            // forward failed
114            throw new JspException(e.getMessage(), e);
115        }
116    }
117
118    /**
119     *
120     * @see org.opencms.workplace.list.A_CmsListReport#initializeThread()
121     */
122    @SuppressWarnings("rawtypes")
123    @Override
124    public I_CmsReportThread initializeThread() {
125
126        CmsSearchReplaceSettings settings = (CmsSearchReplaceSettings)((Map)getSettings().getDialogObject()).get(
127            CmsSourceSearchDialog.class.getName());
128        if (settings == null) {
129            settings = (CmsSearchReplaceSettings)((Map)getSettings().getDialogObject()).get(
130                CmsSourceSearchDialog.class.getName());
131        }
132
133        // clear the matched file list in the session
134        HttpSession session = getJsp().getJspContext().getSession();
135        session.removeAttribute(CmsSearchReplaceSettings.ATTRIBUTE_NAME_SOURCESEARCH_RESULT_LIST);
136
137        // no redirect to the matched file list is necessary here, because the
138        // org.opencms.workplace.list.A_CmsListReport.actionCloseDialog() method is overwritten here in this class
139
140        // start the thread
141        I_CmsReportThread searchThread = new CmsSearchReplaceThread(session, getCms(), settings);
142
143        return searchThread;
144    }
145}