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.configuration.CmsSearchConfiguration;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsIllegalStateException;
034import org.opencms.main.OpenCms;
035import org.opencms.search.CmsSearchDocumentType;
036import org.opencms.search.CmsSearchIndexSource;
037import org.opencms.search.CmsSearchManager;
038import org.opencms.search.I_CmsSearchIndex;
039import org.opencms.workplace.list.CmsListColumnAlignEnum;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListDefaultAction;
042import org.opencms.workplace.list.CmsListDirectAction;
043import org.opencms.workplace.list.CmsListItem;
044import org.opencms.workplace.list.CmsListItemDetails;
045import org.opencms.workplace.list.CmsListItemDetailsFormatter;
046import org.opencms.workplace.list.CmsListMetadata;
047import org.opencms.workplace.list.CmsListOrderEnum;
048import org.opencms.workplace.tools.CmsToolDialog;
049
050import java.io.IOException;
051import java.util.ArrayList;
052import java.util.HashMap;
053import java.util.Iterator;
054import java.util.List;
055import java.util.Map;
056
057import javax.servlet.ServletException;
058import javax.servlet.http.HttpServletRequest;
059import javax.servlet.http.HttpServletResponse;
060import javax.servlet.jsp.PageContext;
061
062/**
063 * A list that displays information about the <code>{@link org.opencms.search.CmsSearchIndexSource}</code>
064 * that are members of the <code>{@link org.opencms.search.CmsSearchIndex}</code>
065 * in the current request scope (param "searchindex").<p>
066 *
067 * This list is no stand-alone page but has to be embedded in another dialog
068 * (see <code> {@link org.opencms.workplace.tools.searchindex.A_CmsEmbeddedListDialog}</code>. <p>
069 *
070 * @since 6.0.0
071 */
072public class CmsSearchIndexSourceList extends A_CmsEmbeddedListDialog {
073
074    /** list column id constant. */
075    public static final String LIST_COLUMN_ICON = "ci";
076
077    /** list column id constant. */
078    public static final String LIST_COLUMN_INDEXER = "ca";
079
080    /** list column id constant. */
081    public static final String LIST_COLUMN_NAME = "cn";
082
083    /** list item detail id constant. */
084    public static final String LIST_DETAIL_DOCTYPES = "dd";
085
086    /** list item detail id constant. */
087    public static final String LIST_DETAIL_RESOURCES = "dr";
088
089    /** list id constant. */
090    public static final String LIST_ID = "lssis";
091
092    /** Stores the value of the request parameter for the search index Name. */
093    private String m_paramIndexName;
094
095    /**
096     * Public constructor.<p>
097     *
098     * @param jsp an initialized JSP action element
099     */
100    public CmsSearchIndexSourceList(CmsJspActionElement jsp) {
101
102        this(jsp, LIST_ID, Messages.get().container(Messages.GUI_LIST_INDEXSOURCES_NAME_0));
103    }
104
105    /**
106     * Public constructor.<p>
107     *
108     * @param jsp an initialized JSP action element
109     * @param listId the id of the list
110     * @param listName the list name
111     */
112    public CmsSearchIndexSourceList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
113
114        this(jsp, listId, listName, LIST_COLUMN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
115    }
116
117    /**
118     * Public constructor.<p>
119     *
120     * @param jsp an initialized JSP action element
121     * @param listId the id of the displayed list
122     * @param listName the name of the list
123     * @param sortedColId the a priory sorted column
124     * @param sortOrder the order of the sorted column
125     * @param searchableColId the column to search into
126     */
127    public CmsSearchIndexSourceList(
128        CmsJspActionElement jsp,
129        String listId,
130        CmsMessageContainer listName,
131        String sortedColId,
132        CmsListOrderEnum sortOrder,
133        String searchableColId) {
134
135        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
136
137    }
138
139    /**
140     * Public constructor.<p>
141     *
142     * Public constructor with JSP variables.<p>
143     *
144     * @param context the JSP page context
145     * @param req the JSP request
146     * @param res the JSP response
147     */
148    public CmsSearchIndexSourceList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
149
150        this(new CmsJspActionElement(context, req, res));
151    }
152
153    /**
154     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
155     */
156    @Override
157    public void executeListMultiActions() {
158
159        // view only
160    }
161
162    /**
163     * @throws ServletException if something goes wrong
164     *
165     * @throws IOException if something goes wrong
166     *
167     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
168     */
169    @Override
170    public void executeListSingleActions() throws IOException, ServletException {
171
172        String indexSource = getSelectedItem().getId();
173        Map<String, String[]> params = new HashMap<String, String[]>();
174        String action = getParamListAction();
175        if (action.equals(CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE)
176            || action.equals(CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE2)) {
177            // forward to the index overview screen
178            params.put(A_CmsEditIndexSourceDialog.PARAM_INDEXSOURCE, new String[] {indexSource});
179            params.put(PARAM_ACTION, new String[] {DIALOG_INITIAL});
180            params.put(PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
181            getToolManager().jspForwardTool(this, "/searchindex/indexsources/indexsource", params);
182        }
183    }
184
185    /**
186     * Returns the request parameter mapped to member <code>m_searchindex</code>
187     * or null if no one was received. <p>
188     *
189     * @return the request parameter mapped to member <code>m_searchindex</code>
190     *          or null if no one was received
191     */
192    public String getParamIndexName() {
193
194        return m_paramIndexName;
195    }
196
197    /**
198     * Maps the request parameter to member <code>m_searchindex</code>. <p>
199     *
200     * @param paramSearchIndex the request parameter <code>searchindex</code>
201     *        that is filled using this method.
202     */
203    public void setParamIndexName(String paramSearchIndex) {
204
205        m_paramIndexName = paramSearchIndex;
206    }
207
208    /**
209     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
210     */
211    @Override
212    protected void fillDetails(String detailId) {
213
214        // get content
215        List<CmsListItem> items = getList().getAllContent();
216        Iterator<CmsListItem> itItems = items.iterator();
217        CmsListItem item;
218        if (detailId.equals(LIST_DETAIL_DOCTYPES)) {
219            while (itItems.hasNext()) {
220                item = itItems.next();
221                fillDetailDocTypes(item, detailId);
222
223            }
224        }
225        if (detailId.equals(LIST_DETAIL_RESOURCES)) {
226            while (itItems.hasNext()) {
227                item = itItems.next();
228                fillDetailResources(item, detailId);
229
230            }
231        }
232
233    }
234
235    /**
236     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
237     */
238    @Override
239    protected List<CmsListItem> getListItems() {
240
241        List<CmsListItem> result = new ArrayList<CmsListItem>();
242        // get content
243        List<CmsSearchIndexSource> sources = searchIndexSources();
244        Iterator<CmsSearchIndexSource> itSources = sources.iterator();
245        CmsSearchIndexSource source;
246        while (itSources.hasNext()) {
247            source = itSources.next();
248            CmsListItem item = getList().newItem(source.getName());
249            // use "null" String to avoid exception if name is empty
250            item.set(LIST_COLUMN_NAME, String.valueOf(source.getName()));
251            item.set(LIST_COLUMN_INDEXER, source.getIndexer().getClass());
252            result.add(item);
253        }
254        return result;
255    }
256
257    /**
258     * @see org.opencms.workplace.CmsWorkplace#initMessages()
259     */
260    @Override
261    protected void initMessages() {
262
263        // add specific dialog resource bundle
264        addMessages(Messages.get().getBundleName());
265        // add default resource bundles
266        super.initMessages();
267    }
268
269    /**
270     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
271     */
272    @Override
273    protected void setColumns(CmsListMetadata metadata) {
274
275        // create column for edit
276        CmsListColumnDefinition editCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
277        editCol.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
278        editCol.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
279        editCol.setWidth("5");
280        editCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
281        editCol.setSorteable(false);
282        // add dummy icon
283        CmsListDirectAction editAction = new CmsListDirectAction(
284            CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE);
285        editAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
286        editAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
287        editAction.setIconPath(CmsSearchIndexList.LIST_ICON_INDEXSOURCE);
288        editCol.addDirectAction(editAction);
289        // add it to the list definition
290        metadata.addColumn(editCol);
291
292        // add column for name
293        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
294        nameCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
295        nameCol.setName(Messages.get().container(Messages.GUI_LIST_SEARCHINDEX_COL_NAME_0));
296        nameCol.setWidth("55%");
297        // add overview action
298        CmsListDefaultAction defEditAction = new CmsListDefaultAction(
299            CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE2);
300        defEditAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_OVERVIEW_NAME_0));
301        defEditAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_OVERVIEW_NAME_HELP_0));
302        nameCol.addDefaultAction(defEditAction);
303        metadata.addColumn(nameCol);
304
305        // add column for analyzer
306        CmsListColumnDefinition analyzerCol = new CmsListColumnDefinition(LIST_COLUMN_INDEXER);
307        analyzerCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
308        analyzerCol.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_INDEXER_0));
309        analyzerCol.setWidth("45%");
310        metadata.addColumn(analyzerCol);
311
312    }
313
314    /**
315     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
316     */
317    @Override
318    protected void setIndependentActions(CmsListMetadata metadata) {
319
320        // add document types of index source detail help
321        CmsListItemDetails doctypeDetails = new CmsListItemDetails(LIST_DETAIL_DOCTYPES);
322        doctypeDetails.setAtColumn(LIST_COLUMN_NAME);
323        doctypeDetails.setVisible(false);
324        doctypeDetails.setShowActionName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_SHOW_0));
325        doctypeDetails.setShowActionHelpText(
326            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_SHOW_HELP_0));
327        doctypeDetails.setHideActionName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_HIDE_0));
328        doctypeDetails.setHideActionHelpText(
329            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_HIDE_HELP_0));
330        doctypeDetails.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_NAME_0));
331        doctypeDetails.setFormatter(
332            new CmsListItemDetailsFormatter(
333                Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_NAME_0)));
334        metadata.addItemDetails(doctypeDetails);
335
336        // add resources of index source detail help
337        CmsListItemDetails resourceDetails = new CmsListItemDetails(LIST_DETAIL_RESOURCES);
338        resourceDetails.setAtColumn(LIST_COLUMN_NAME);
339        resourceDetails.setVisible(false);
340        resourceDetails.setShowActionName(
341            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_SHOW_0));
342        resourceDetails.setShowActionHelpText(
343            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_SHOW_HELP_0));
344        resourceDetails.setHideActionName(
345            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_HIDE_0));
346        resourceDetails.setHideActionHelpText(
347            Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_HIDE_HELP_0));
348        resourceDetails.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_NAME_0));
349        resourceDetails.setFormatter(
350            new CmsListItemDetailsFormatter(
351                Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_NAME_0)));
352        metadata.addItemDetails(resourceDetails);
353
354    }
355
356    /**
357     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
358     */
359    @Override
360    protected void setMultiActions(CmsListMetadata metadata) {
361
362        // view only
363    }
364
365    /**
366     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
367     */
368    @Override
369    protected void validateParamaters() throws Exception {
370
371        // test the needed parameters
372        if (getParamIndexName() == null) {
373            throw new CmsIllegalStateException(
374                Messages.get().container(
375                    Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
376                    A_CmsEditSearchIndexDialog.PARAM_INDEXNAME));
377        }
378    }
379
380    /**
381     * Writes the updated search configuration back to the XML
382     * configuration file and refreshes the complete list.<p>
383     *
384     * @param refresh if true, the list items are refreshed
385     */
386    protected void writeConfiguration(boolean refresh) {
387
388        // update the XML configuration
389        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
390        if (refresh) {
391            refreshList();
392        }
393    }
394
395    /**
396     * Fills details about document types of the index source into the given item. <p>
397     *
398     * @param item the list item to fill
399     * @param detailId the id for the detail to fill
400     *
401     */
402    private void fillDetailDocTypes(CmsListItem item, String detailId) {
403
404        CmsSearchManager searchManager = OpenCms.getSearchManager();
405        StringBuffer html = new StringBuffer();
406
407        // search for the corresponding CmsSearchIndexSource:
408        String idxSourceName = (String)item.get(LIST_COLUMN_NAME);
409        CmsSearchIndexSource idxSource = searchManager.getIndexSource(idxSourceName);
410
411        // get the index sources doc types
412        List<String> docTypes = idxSource.getDocumentTypes();
413        // output of found index sources
414        Iterator<String> itDocTypes = docTypes.iterator();
415        CmsSearchDocumentType docType;
416        html.append("<ul>\n");
417        while (itDocTypes.hasNext()) {
418            // get the instance (instead of plain name) for more detail in future...
419            docType = searchManager.getDocumentTypeConfig(itDocTypes.next());
420            // harden against unconfigured doctypes that are refferred to by indexsource nodes
421            if (docType != null) {
422
423                html.append("  <li>\n").append("  ").append(docType.getName()).append("\n");
424                html.append("  </li>");
425            }
426        }
427
428        html.append("</ul>\n");
429        item.set(detailId, html.toString());
430    }
431
432    /**
433     * Fills details about resource paths of the index source into the given item. <p>
434     *
435     * @param item the list item to fill
436     * @param detailId the id for the detail to fill
437     *
438     */
439    private void fillDetailResources(CmsListItem item, String detailId) {
440
441        CmsSearchManager searchManager = OpenCms.getSearchManager();
442        StringBuffer html = new StringBuffer();
443
444        // search for the corresponding CmsSearchIndexSource:
445        String idxSourceName = (String)item.get(LIST_COLUMN_NAME);
446        CmsSearchIndexSource idxSource = searchManager.getIndexSource(idxSourceName);
447
448        // get the index sources resource strings
449        List<String> resources = idxSource.getResourcesNames();
450        // output of found index sources
451        Iterator<String> itResources = resources.iterator();
452        html.append("<ul>\n");
453        while (itResources.hasNext()) {
454
455            html.append("  <li>\n").append("  ").append(itResources.next()).append("\n");
456            html.append("  </li>");
457        }
458
459        html.append("</ul>\n");
460
461        item.set(detailId, html.toString());
462    }
463
464    /**
465     * Returns the available search indexes of this installation.
466     *
467     * @return the available search indexes of this installation
468     */
469    private List<CmsSearchIndexSource> searchIndexSources() {
470
471        CmsSearchManager manager = OpenCms.getSearchManager();
472        I_CmsSearchIndex index = manager.getIndex(getParamIndexName());
473        List<CmsSearchIndexSource> sources = index.getSources();
474        return sources;
475    }
476
477}