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.search;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.search.CmsSearchResult;
032import org.opencms.util.CmsUUID;
033import org.opencms.workplace.CmsDialog;
034import org.opencms.workplace.CmsWorkplace;
035import org.opencms.workplace.editors.CmsEditor;
036import org.opencms.workplace.explorer.CmsExplorer;
037import org.opencms.workplace.explorer.CmsResourceUtil;
038import org.opencms.workplace.list.A_CmsListExplorerDialog;
039import org.opencms.workplace.list.CmsListColumnAlignEnum;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListExplorerColumn;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListItemDetails;
044import org.opencms.workplace.list.CmsListMetadata;
045import org.opencms.workplace.list.CmsListOrderEnum;
046import org.opencms.workplace.list.I_CmsListFormatter;
047import org.opencms.workplace.list.I_CmsListResourceCollector;
048
049import java.io.IOException;
050import java.util.Collections;
051import java.util.HashMap;
052import java.util.Iterator;
053import java.util.Locale;
054import java.util.Map;
055
056import javax.servlet.ServletException;
057import javax.servlet.http.HttpServletRequest;
058import javax.servlet.http.HttpServletResponse;
059import javax.servlet.jsp.PageContext;
060
061/**
062 * Explorer list for the search results.<p>
063 *
064 * @since 6.0.0
065 */
066public class CmsSearchResultsList extends A_CmsListExplorerDialog {
067
068    /** List column id constant. */
069    public static final String LIST_COLUMN_SCORE = "cs";
070
071    /** list item detail id constant. */
072    public static final String LIST_DETAIL_EXCERPT = "de";
073
074    /** list id constant. */
075    public static final String LIST_ID = "lsr";
076
077    /** Path to the list buttons. */
078    public static final String PATH_BUTTONS = "tools/ex_search/buttons/";
079
080    /** The internal collector instance. */
081    private I_CmsListResourceCollector m_collector;
082
083    /** Search parameters. */
084    private CmsSearchWorkplaceBean m_searchParams;
085
086    /**
087     * Public constructor with JSP action element.<p>
088     *
089     * @param jsp an initialized JSP action element
090     */
091    public CmsSearchResultsList(CmsJspActionElement jsp) {
092
093        super(
094            jsp,
095            LIST_ID,
096            Messages.get().container(Messages.GUI_SEARCH_LIST_NAME_0),
097            A_CmsListExplorerDialog.LIST_COLUMN_NAME,
098            CmsListOrderEnum.ORDER_ASCENDING,
099            null);
100    }
101
102    /**
103     * Public constructor with JSP variables.<p>
104     *
105     * @param context the JSP page context
106     * @param req the JSP request
107     * @param res the JSP response
108     */
109    public CmsSearchResultsList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
110
111        this(new CmsJspActionElement(context, req, res));
112    }
113
114    /**
115     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
116     */
117    @Override
118    public void executeListMultiActions() {
119
120        throwListUnsupportedActionException();
121    }
122
123    /**
124     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
125     */
126    @Override
127    public void executeListSingleActions() throws IOException, ServletException {
128
129        if (getParamListAction().equals(LIST_ACTION_EDIT)) {
130            // forward to the editor
131            Map<String, String[]> params = new HashMap<String, String[]>();
132            params.put(CmsDialog.PARAM_ACTION, new String[] {CmsDialog.DIALOG_INITIAL});
133            params.put(CmsDialog.PARAM_CLOSELINK, new String[] {CmsWorkplace.VFS_PATH_VIEWS + "workplace.jsp"});
134            params.put(CmsEditor.PARAM_BACKLINK, new String[] {CmsWorkplace.VFS_PATH_VIEWS + "workplace.jsp"});
135            params.put(CmsDialog.PARAM_RESOURCE, new String[] {(String)getSelectedItem().get(LIST_COLUMN_NAME)});
136            getToolManager().jspForwardPage(this, "/system/workplace/explorer/search/edit.jsp", params);
137        } else {
138            throwListUnsupportedActionException();
139        }
140    }
141
142    /**
143     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#getCollector()
144     */
145    @Override
146    public I_CmsListResourceCollector getCollector() {
147
148        if (m_collector == null) {
149            m_collector = new CmsSearchResourcesCollector(
150                this,
151                getSearchParams().getQuery(),
152                getSearchParams().getSortOrder(),
153                getSearchParams().getFields(),
154                Collections.singletonList(getSearchParams().getSearchPath()),
155                getSearchParams().getMinDateCreated(),
156                getSearchParams().getMaxDateCreated(),
157                getSearchParams().getMinDateLastModified(),
158                getSearchParams().getMaxDateLastModified(),
159                getSearchParams().getIndexName());
160
161            // set the right resource util parameters
162            CmsResourceUtil resUtil = getResourceUtil();
163            resUtil.setAbbrevLength(50);
164            resUtil.setSiteMode(CmsResourceUtil.SITE_MODE_MATCHING);
165        }
166        return m_collector;
167    }
168
169    /**
170     * Generates the dialog starting html code.<p>
171     *
172     * @return html code
173     */
174    @Override
175    protected String defaultActionHtmlStart() {
176
177        StringBuffer result = new StringBuffer(2048);
178        result.append(htmlStart(null));
179        result.append(getList().listJs());
180        result.append(CmsListExplorerColumn.getExplorerStyleDef());
181        result.append("<script >\n");
182        result.append(new CmsExplorer(getJsp()).getInitializationHeader());
183        result.append("\ntop.updateWindowStore();\n");
184        result.append("top.displayHead(top.win.head, 0, 1);\n}\n");
185        result.append("</script>");
186        result.append(bodyStart("dialog", "onload='initialize();'"));
187        result.append(dialogStart());
188        result.append(dialogContentStart(getParamTitle()));
189        return result.toString();
190    }
191
192    /**
193     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
194     */
195    @Override
196    protected void fillDetails(String detailId) {
197
198        // excerpt detail is enabled
199        if (detailId.equals(LIST_DETAIL_EXCERPT)) {
200            CmsSearchResourcesCollector collector = (CmsSearchResourcesCollector)getCollector();
201            Iterator<CmsListItem> itResources = getList().getAllContent().iterator();
202            while (itResources.hasNext()) {
203                CmsListItem item = itResources.next();
204                // get excerpt for item
205                if (!item.getId().equals(CmsUUID.getNullUUID().toString())) {
206                    CmsSearchResult result = collector.getSearchResult(item.getId());
207                    if (result != null) {
208                        item.set(detailId, result.getExcerpt());
209                    }
210                }
211            }
212        }
213    }
214
215    /**
216     * @see org.opencms.workplace.CmsWorkplace#initMessages()
217     */
218    @Override
219    protected void initMessages() {
220
221        // add specific dialog resource bundle
222        addMessages(Messages.get().getBundleName());
223        // add default resource bundles
224        super.initMessages();
225    }
226
227    /**
228     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
229     */
230    @Override
231    protected void setColumns(CmsListMetadata metadata) {
232
233        super.setColumns(metadata);
234
235        // last position: score
236        CmsListColumnDefinition scoreCol = new CmsListExplorerColumn(LIST_COLUMN_SCORE);
237        scoreCol.setName(Messages.get().container(Messages.GUI_SEARCH_LIST_COLS_SCORE_0));
238        scoreCol.setHelpText(Messages.get().container(Messages.GUI_SEARCH_LIST_COLS_SCORE_HELP_0));
239        scoreCol.setAlign(CmsListColumnAlignEnum.ALIGN_RIGHT);
240        metadata.addColumn(scoreCol);
241
242        Iterator<CmsListColumnDefinition> it = metadata.getColumnDefinitions().iterator();
243        while (it.hasNext()) {
244            CmsListColumnDefinition column = it.next();
245            column.setSorteable(false);
246        }
247    }
248
249    /**
250     * @see org.opencms.workplace.list.A_CmsListExplorerDialog#setColumnVisibilities()
251     */
252    @Override
253    protected void setColumnVisibilities() {
254
255        super.setColumnVisibilities();
256        setColumnVisibility(LIST_COLUMN_EDIT.hashCode(), LIST_COLUMN_EDIT.hashCode());
257    }
258
259    /**
260     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
261     */
262    @Override
263    protected void setIndependentActions(CmsListMetadata metadata) {
264
265        super.setIndependentActions(metadata);
266        // add excerpt details
267        CmsListItemDetails excerptDetails = new CmsListItemDetails(LIST_DETAIL_EXCERPT);
268        excerptDetails.setAtColumn(LIST_COLUMN_NAME);
269        excerptDetails.setVisible(true);
270        excerptDetails.setShowActionName(Messages.get().container(Messages.GUI_SEARCH_DETAIL_SHOW_EXCERPT_NAME_0));
271        excerptDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_SEARCH_DETAIL_SHOW_EXCERPT_HELP_0));
272        excerptDetails.setHideActionName(Messages.get().container(Messages.GUI_SEARCH_DETAIL_HIDE_EXCERPT_NAME_0));
273        excerptDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_SEARCH_DETAIL_HIDE_EXCERPT_HELP_0));
274        excerptDetails.setName(Messages.get().container(Messages.GUI_SEARCH_DETAIL_EXCERPT_NAME_0));
275        excerptDetails.setFormatter(new I_CmsListFormatter() {
276
277            /**
278             * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
279             */
280            @Override
281            public String format(Object data, Locale locale) {
282
283                return (String)data;
284            }
285        });
286        metadata.addItemDetails(excerptDetails);
287    }
288
289    /**
290     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
291     */
292    @Override
293    protected void setMultiActions(CmsListMetadata metadata) {
294
295        // no LMAs
296    }
297
298    /**
299     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
300     */
301    @Override
302    protected void validateParamaters() throws Exception {
303
304        if (getSearchParams() == null) {
305            throw new Exception();
306        }
307    }
308
309    /**
310     * Returns the search parameter bean.<p>
311     *
312     * @return the search parameter bean
313     */
314    private CmsSearchWorkplaceBean getSearchParams() {
315
316        if ((m_searchParams == null) && (getSettings().getDialogObject() instanceof Map<?, ?>)) {
317            Map<?, ?> dialogObject = (Map<?, ?>)getSettings().getDialogObject();
318            if (dialogObject.get(CmsSearchDialog.class.getName()) instanceof CmsSearchWorkplaceBean) {
319                m_searchParams = (CmsSearchWorkplaceBean)dialogObject.get(CmsSearchDialog.class.getName());
320            }
321        }
322        return m_searchParams;
323    }
324}