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.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.search.CmsSearchDocumentType;
037import org.opencms.search.CmsSearchIndexSource;
038import org.opencms.search.CmsSearchManager;
039import org.opencms.workplace.list.CmsListColumnAlignEnum;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListDirectAction;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListItemDetails;
044import org.opencms.workplace.list.CmsListItemDetailsFormatter;
045import org.opencms.workplace.list.CmsListMetadata;
046import org.opencms.workplace.list.CmsListOrderEnum;
047
048import java.util.ArrayList;
049import java.util.Collections;
050import java.util.Iterator;
051import java.util.List;
052
053import javax.servlet.http.HttpServletRequest;
054import javax.servlet.http.HttpServletResponse;
055import javax.servlet.jsp.PageContext;
056
057import org.apache.commons.logging.Log;
058
059/**
060 * A list that displays the document types of a request parameter given
061 * <code>{@link org.opencms.search.CmsSearchIndexSource}</code> ("indexsource").
062 *
063 * This list is no stand-alone page but has to be embedded in another dialog
064 * (see <code> {@link org.opencms.workplace.tools.searchindex.A_CmsEmbeddedListDialog}</code>. <p>
065 *
066 * @since 6.0.0
067 */
068public class CmsDocumentTypeList extends A_CmsEmbeddedListDialog {
069
070    /** list action dummy id constant. */
071    public static final String LIST_ACTION_NONE = "an";
072
073    /** list column id constant. */
074    public static final String LIST_COLUMN_DOCCLASS = "cdc";
075
076    /** list column id constant. */
077    public static final String LIST_COLUMN_ICON = "ci";
078
079    /** list column id constant. */
080    public static final String LIST_COLUMN_NAME = "cn";
081
082    /** list item detail id constant. */
083    public static final String LIST_DETAIL_MIMETYPES = "dmt";
084
085    /** list item detail id constant. */
086    public static final String LIST_DETAIL_RESOURCETYPES = "drt";
087
088    /** list id constant. */
089    public static final String LIST_ID = "lssisdt";
090
091    /** The list icon for a folder resource. **/
092    protected static final String ICON_DOCTYPE = "tools/searchindex/icons/small/indexsource-doctype.png";
093
094    /** The log object for this class. */
095    private static final Log LOG = CmsLog.getLog(CmsDocumentTypeList.class);
096
097    /** Stores the value of the request parameter for the search index source name. */
098    private String m_paramIndexsource;
099
100    /**
101     * Public constructor.<p>
102     *
103     * @param jsp an initialized JSP action element
104     */
105    public CmsDocumentTypeList(CmsJspActionElement jsp) {
106
107        this(jsp, LIST_ID, Messages.get().container(Messages.GUI_LIST_DOCUMENTTYPES_NAME_0));
108    }
109
110    /**
111     * Public constructor.<p>
112     *
113     * @param jsp an initialized JSP action element
114     * @param listId the id of the list
115     * @param listName the list name
116     */
117    public CmsDocumentTypeList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
118
119        this(jsp, listId, listName, LIST_COLUMN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
120    }
121
122    /**
123     * Public constructor.<p>
124     *
125     * @param jsp an initialized JSP action element
126     * @param listId the id of the displayed list
127     * @param listName the name of the list
128     * @param sortedColId the a priory sorted column
129     * @param sortOrder the order of the sorted column
130     * @param searchableColId the column to search into
131     */
132    public CmsDocumentTypeList(
133        CmsJspActionElement jsp,
134        String listId,
135        CmsMessageContainer listName,
136        String sortedColId,
137        CmsListOrderEnum sortOrder,
138        String searchableColId) {
139
140        super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
141
142    }
143
144    /**
145     * Public constructor.<p>
146     *
147     * Public constructor with JSP variables.<p>
148     *
149     * @param context the JSP page context
150     * @param req the JSP request
151     * @param res the JSP response
152     */
153    public CmsDocumentTypeList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
154
155        this(new CmsJspActionElement(context, req, res));
156    }
157
158    /**
159     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
160     */
161    @Override
162    public void executeListMultiActions() {
163
164        // view only
165    }
166
167    /**
168     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
169     */
170    @Override
171    public void executeListSingleActions() {
172
173        // view only
174    }
175
176    /**
177     * Returns the request parameter "indexsource".<p>
178     *
179     * @return the request parameter "indexsource"
180     */
181    public String getParamIndexsource() {
182
183        return m_paramIndexsource;
184    }
185
186    /**
187     * Sets the request parameter "indexsource". <p>
188     *
189     * Method intended for workplace-properietary automatic filling of
190     * request parameter values to dialogs, not for manual invocation. <p>
191     *
192     * @param indexsource the request parameter "indexsource" to set
193     */
194    public void setParamIndexsource(String indexsource) {
195
196        m_paramIndexsource = indexsource;
197    }
198
199    /**
200     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
201     */
202    @Override
203    protected void fillDetails(String detailId) {
204
205        // get content
206        List<CmsListItem> items = getList().getAllContent();
207        Iterator<CmsListItem> itItems = items.iterator();
208        CmsListItem item;
209        if (detailId.equals(LIST_DETAIL_MIMETYPES)) {
210            while (itItems.hasNext()) {
211                item = itItems.next();
212                fillDetailMimetypes(item, detailId);
213
214            }
215        }
216        if (detailId.equals(LIST_DETAIL_RESOURCETYPES)) {
217            while (itItems.hasNext()) {
218                item = itItems.next();
219                fillDetailResourceTypes(item, detailId);
220
221            }
222
223        }
224
225    }
226
227    /**
228     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
229     */
230    @Override
231    protected List<CmsListItem> getListItems() {
232
233        List<CmsListItem> result = new ArrayList<CmsListItem>();
234        // get content
235        List<CmsSearchDocumentType> doctypes = documentTypes();
236        Iterator<CmsSearchDocumentType> itDoctypes = doctypes.iterator();
237        CmsSearchDocumentType doctype;
238        while (itDoctypes.hasNext()) {
239            doctype = itDoctypes.next();
240            CmsListItem item = getList().newItem(doctype.getName());
241            item.set(LIST_COLUMN_NAME, doctype.getName());
242            item.set(LIST_COLUMN_DOCCLASS, doctype.getClassName());
243            result.add(item);
244        }
245        return result;
246    }
247
248    /**
249     * @see org.opencms.workplace.CmsWorkplace#initMessages()
250     */
251    @Override
252    protected void initMessages() {
253
254        // add specific dialog resource bundle
255        addMessages(Messages.get().getBundleName());
256        // add default resource bundles
257        super.initMessages();
258    }
259
260    /**
261     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
262     */
263    @Override
264    protected void setColumns(CmsListMetadata metadata) {
265
266        // create dummy column for corporate design reasons
267        CmsListColumnDefinition dummyCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
268        dummyCol.setName(Messages.get().container(Messages.GUI_LIST_DOCUMENTTYPE_NAME_0));
269        dummyCol.setHelpText(Messages.get().container(Messages.GUI_LIST_DOCUMENTTYPE_NAME_HELP_0));
270        dummyCol.setWidth("20");
271        dummyCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
272        dummyCol.setSorteable(false);
273        // add dummy icon
274        CmsListDirectAction dummyAction = new CmsListDirectAction(LIST_ACTION_NONE);
275        dummyAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
276        dummyAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
277        dummyAction.setIconPath(CmsDocumentTypeList.ICON_DOCTYPE);
278        // disable!
279        dummyAction.setEnabled(false);
280        dummyCol.addDirectAction(dummyAction);
281        // add it to the list definition
282        metadata.addColumn(dummyCol);
283
284        // add column for name
285        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
286        nameCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
287        nameCol.setName(Messages.get().container(Messages.GUI_LIST_SEARCHINDEX_COL_NAME_0));
288        nameCol.setWidth("50%");
289        metadata.addColumn(nameCol);
290
291        // add column for document implementation class
292        CmsListColumnDefinition docclassCol = new CmsListColumnDefinition(LIST_COLUMN_DOCCLASS);
293        docclassCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
294        docclassCol.setName(Messages.get().container(Messages.GUI_LIST_DOCTYPE_COL_DOCCLASS_0));
295        docclassCol.setWidth("50%");
296        metadata.addColumn(docclassCol);
297
298    }
299
300    /**
301     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
302     */
303    @Override
304    protected void setIndependentActions(CmsListMetadata metadata) {
305
306        // add document types of index source detail help
307        CmsListItemDetails mimetypeDetails = new CmsListItemDetails(LIST_DETAIL_MIMETYPES);
308        mimetypeDetails.setAtColumn(LIST_COLUMN_NAME);
309        mimetypeDetails.setVisible(false);
310        mimetypeDetails.setShowActionName(Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_SHOW_0));
311        mimetypeDetails.setShowActionHelpText(
312            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_SHOW_HELP_0));
313        mimetypeDetails.setHideActionName(Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_HIDE_0));
314        mimetypeDetails.setHideActionHelpText(
315            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_HIDE_HELP_0));
316        mimetypeDetails.setName(Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_NAME_0));
317        mimetypeDetails.setFormatter(
318            new CmsListItemDetailsFormatter(
319                Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_MIMETYPES_NAME_0)));
320        metadata.addItemDetails(mimetypeDetails);
321
322        // add resources of index source detail help
323        CmsListItemDetails resourceDetails = new CmsListItemDetails(LIST_DETAIL_RESOURCETYPES);
324        resourceDetails.setAtColumn(LIST_COLUMN_NAME);
325        resourceDetails.setVisible(false);
326        resourceDetails.setShowActionName(
327            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_SHOW_0));
328        resourceDetails.setShowActionHelpText(
329            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_SHOW_HELP_0));
330        resourceDetails.setHideActionName(
331            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_HIDE_0));
332        resourceDetails.setHideActionHelpText(
333            Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_HIDE_HELP_0));
334        resourceDetails.setName(Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_NAME_0));
335        resourceDetails.setFormatter(
336            new CmsListItemDetailsFormatter(
337                Messages.get().container(Messages.GUI_LIST_DOCTYPE_DETAIL_RESOURCETYPES_NAME_0)));
338        metadata.addItemDetails(resourceDetails);
339
340    }
341
342    /**
343     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
344     */
345    @Override
346    protected void setMultiActions(CmsListMetadata metadata) {
347
348        // view only
349    }
350
351    /**
352     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
353     */
354    @Override
355    protected void validateParamaters() throws Exception {
356
357        // test the needed parameters
358        if (getParamIndexsource() == null) {
359            throw new CmsIllegalStateException(Messages.get().container(
360                Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
361                A_CmsEditIndexSourceDialog.PARAM_INDEXSOURCE));
362        }
363    }
364
365    /**
366     * Writes the updated search configuration back to the XML
367     * configuration file and refreshes the complete list.<p>
368     *
369     * @param refresh if true, the list items are refreshed
370     */
371    protected void writeConfiguration(boolean refresh) {
372
373        // update the XML configuration
374        OpenCms.writeConfiguration(CmsSearchConfiguration.class);
375        if (refresh) {
376            refreshList();
377        }
378    }
379
380    /**
381     * Returns the configured document types of the current indexsource.
382     *
383     * @return the configured document types of the current indexsource
384     */
385    private List<CmsSearchDocumentType> documentTypes() {
386
387        CmsSearchManager manager = OpenCms.getSearchManager();
388        CmsSearchIndexSource indexsource = manager.getIndexSource(getParamIndexsource());
389        List<CmsSearchDocumentType> result;
390        if (indexsource != null) {
391            List<String> doctypes = indexsource.getDocumentTypes();
392            // transform these mere names to real document types...
393            result = new ArrayList<CmsSearchDocumentType>(doctypes.size());
394            Iterator<String> it = doctypes.iterator();
395            String doctypename = "";
396            CmsSearchDocumentType doctype;
397            while (it.hasNext()) {
398                doctypename = it.next();
399                if (doctypename != null) {
400                    doctype = manager.getDocumentTypeConfig(doctypename);
401                    if (doctype != null) {
402                        result.add(doctype);
403                    }
404                }
405            }
406        } else {
407            result = Collections.emptyList();
408            if (LOG.isErrorEnabled()) {
409                LOG.error(
410                    Messages.get().getBundle().key(
411                        Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
412                        A_CmsEditIndexSourceDialog.PARAM_INDEXSOURCE));
413            }
414        }
415        return result;
416    }
417
418    /**
419     * Fills details about configured mime types of the document type into the given item. <p>
420     *
421     * @param item the list item to fill
422     * @param detailId the id for the detail to fill
423     *
424     */
425    private void fillDetailMimetypes(CmsListItem item, String detailId) {
426
427        CmsSearchManager searchManager = OpenCms.getSearchManager();
428        StringBuffer html = new StringBuffer();
429
430        String doctypeName = (String)item.get(LIST_COLUMN_NAME);
431        CmsSearchDocumentType docType = searchManager.getDocumentTypeConfig(doctypeName);
432
433        // output of mime types
434        Iterator<String> itMimetypes = docType.getMimeTypes().iterator();
435        html.append("<ul>\n");
436        while (itMimetypes.hasNext()) {
437            html.append("  <li>\n").append("  ").append(itMimetypes.next()).append("\n");
438            html.append("  </li>");
439        }
440
441        html.append("</ul>\n");
442        item.set(detailId, html.toString());
443    }
444
445    /**
446     * Fills details about resource types of the document type into the given item. <p>
447     *
448     * @param item the list item to fill
449     * @param detailId the id for the detail to fill
450     *
451     */
452    private void fillDetailResourceTypes(CmsListItem item, String detailId) {
453
454        CmsSearchManager searchManager = OpenCms.getSearchManager();
455        StringBuffer html = new StringBuffer();
456
457        String doctypeName = (String)item.get(LIST_COLUMN_NAME);
458        CmsSearchDocumentType docType = searchManager.getDocumentTypeConfig(doctypeName);
459
460        // output of resource types
461        Iterator<String> itResourcetypes = docType.getResourceTypes().iterator();
462        html.append("<ul>\n");
463        while (itResourcetypes.hasNext()) {
464            html.append("  <li>\n").append("  ").append(itResourcetypes.next()).append("\n");
465            html.append("  </li>");
466        }
467
468        html.append("</ul>\n");
469        item.set(detailId, html.toString());
470    }
471}