KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > searchindex > CmsSearchIndexSourceList


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/searchindex/CmsSearchIndexSourceList.java,v $
3  * Date : $Date: 2006/03/27 14:52:21 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.searchindex;
33
34 import org.opencms.configuration.CmsSearchConfiguration;
35 import org.opencms.i18n.CmsMessageContainer;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.CmsIllegalStateException;
38 import org.opencms.main.OpenCms;
39 import org.opencms.search.CmsSearchDocumentType;
40 import org.opencms.search.CmsSearchIndex;
41 import org.opencms.search.CmsSearchIndexSource;
42 import org.opencms.search.CmsSearchManager;
43 import org.opencms.workplace.list.CmsListColumnAlignEnum;
44 import org.opencms.workplace.list.CmsListColumnDefinition;
45 import org.opencms.workplace.list.CmsListDefaultAction;
46 import org.opencms.workplace.list.CmsListDirectAction;
47 import org.opencms.workplace.list.CmsListItem;
48 import org.opencms.workplace.list.CmsListItemDetails;
49 import org.opencms.workplace.list.CmsListItemDetailsFormatter;
50 import org.opencms.workplace.list.CmsListMetadata;
51 import org.opencms.workplace.list.CmsListOrderEnum;
52 import org.opencms.workplace.tools.CmsToolDialog;
53
54 import java.io.IOException JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.HashMap JavaDoc;
57 import java.util.Iterator JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Map JavaDoc;
60
61 import javax.servlet.ServletException JavaDoc;
62 import javax.servlet.http.HttpServletRequest JavaDoc;
63 import javax.servlet.http.HttpServletResponse JavaDoc;
64 import javax.servlet.jsp.PageContext JavaDoc;
65
66 /**
67  * A list that displays information about the <code>{@link org.opencms.search.CmsSearchIndexSource}</code>
68  * that are members of the <code>{@link org.opencms.search.CmsSearchIndex}</code>
69  * in the current request scope (param "searchindex").<p>
70  *
71  * This list is no stand-alone page but has to be embedded in another dialog
72  * (see <code> {@link org.opencms.workplace.tools.searchindex.A_CmsEmbeddedListDialog}</code>. <p>
73  *
74  * @author Achim Westermann
75  *
76  * @version $Revision: 1.2 $
77  *
78  * @since 6.0.0
79  */

80 public class CmsSearchIndexSourceList extends A_CmsEmbeddedListDialog {
81
82     /** list column id constant. */
83     public static final String JavaDoc LIST_COLUMN_ICON = "ci";
84
85     /** list column id constant. */
86     public static final String JavaDoc LIST_COLUMN_INDEXER = "ca";
87
88     /** list column id constant. */
89     public static final String JavaDoc LIST_COLUMN_NAME = "cn";
90
91     /** list item detail id constant. */
92     public static final String JavaDoc LIST_DETAIL_DOCTYPES = "dd";
93
94     /** list item detail id constant. */
95     public static final String JavaDoc LIST_DETAIL_RESOURCES = "dr";
96
97     /** list id constant. */
98     public static final String JavaDoc LIST_ID = "lssis";
99
100     /** Stores the value of the request parameter for the search index Name. */
101     private String JavaDoc m_paramIndexName;
102
103     /**
104      * Public constructor.<p>
105      *
106      * @param jsp an initialized JSP action element
107      */

108     public CmsSearchIndexSourceList(CmsJspActionElement jsp) {
109
110         this(jsp, LIST_ID, Messages.get().container(Messages.GUI_LIST_INDEXSOURCES_NAME_0));
111     }
112
113     /**
114      * Public constructor.<p>
115      *
116      * @param jsp an initialized JSP action element
117      * @param listId the id of the list
118      * @param listName the list name
119      */

120     public CmsSearchIndexSourceList(CmsJspActionElement jsp, String JavaDoc listId, CmsMessageContainer listName) {
121
122         this(jsp, listId, listName, LIST_COLUMN_NAME, CmsListOrderEnum.ORDER_ASCENDING, null);
123     }
124
125     /**
126      * Public constructor.<p>
127      *
128      * @param jsp an initialized JSP action element
129      * @param listId the id of the displayed list
130      * @param listName the name of the list
131      * @param sortedColId the a priory sorted column
132      * @param sortOrder the order of the sorted column
133      * @param searchableColId the column to search into
134      */

135     public CmsSearchIndexSourceList(
136         CmsJspActionElement jsp,
137         String JavaDoc listId,
138         CmsMessageContainer listName,
139         String JavaDoc sortedColId,
140         CmsListOrderEnum sortOrder,
141         String JavaDoc searchableColId) {
142
143         super(jsp, listId, listName, sortedColId, sortOrder, searchableColId);
144
145     }
146
147     /**
148      * Public constructor.<p>
149      *
150      * Public constructor with JSP variables.<p>
151      *
152      * @param context the JSP page context
153      * @param req the JSP request
154      * @param res the JSP response
155      */

156     public CmsSearchIndexSourceList(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
157
158         this(new CmsJspActionElement(context, req, res));
159     }
160
161     /**
162      * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
163      */

164     public void executeListMultiActions() {
165
166         // view only
167
}
168
169     /**
170      * @throws ServletException if something goes wrong
171      *
172      * @throws IOException if something goes wrong
173      *
174      * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
175      */

176     public void executeListSingleActions() throws IOException JavaDoc, ServletException JavaDoc {
177
178         String JavaDoc indexSource = getSelectedItem().getId();
179         Map JavaDoc params = new HashMap JavaDoc();
180         String JavaDoc action = getParamListAction();
181         if (action.equals(CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE)
182             || action.equals(CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE2)) {
183             // forward to the index overview screen
184
params.put(A_CmsEditIndexSourceDialog.PARAM_INDEXSOURCE, indexSource);
185             params.put(PARAM_ACTION, DIALOG_INITIAL);
186             params.put(PARAM_STYLE, CmsToolDialog.STYLE_NEW);
187             getToolManager().jspForwardTool(this, "/searchindex/indexsources/indexsource", params);
188         }
189     }
190
191     /**
192      * Returns the request parameter mapped to member <code>m_searchindex</code>
193      * or null if no one was received. <p>
194      *
195      * @return the request parameter mapped to member <code>m_searchindex</code>
196      * or null if no one was received
197      */

198     public String JavaDoc getParamIndexName() {
199
200         return m_paramIndexName;
201     }
202
203     /**
204      * Maps the request parameter to member <code>m_searchindex</code>. <p>
205      *
206      * @param paramSearchIndex the request parameter <code>searchindex</code>
207      * that is filled using this method.
208      */

209     public void setParamIndexName(String JavaDoc paramSearchIndex) {
210
211         m_paramIndexName = paramSearchIndex;
212     }
213
214     /**
215      * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
216      */

217     protected void fillDetails(String JavaDoc detailId) {
218
219         // get content
220
List JavaDoc items = getList().getAllContent();
221         Iterator JavaDoc itItems = items.iterator();
222         CmsListItem item;
223         if (detailId.equals(LIST_DETAIL_DOCTYPES)) {
224             while (itItems.hasNext()) {
225                 item = (CmsListItem)itItems.next();
226                 fillDetailDocTypes(item, detailId);
227
228             }
229         }
230         if (detailId.equals(LIST_DETAIL_RESOURCES)) {
231             while (itItems.hasNext()) {
232                 item = (CmsListItem)itItems.next();
233                 fillDetailResources(item, detailId);
234
235             }
236         }
237
238     }
239
240     /**
241      * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
242      */

243     protected List JavaDoc getListItems() {
244
245         List JavaDoc result = new ArrayList JavaDoc();
246         // get content
247
List JavaDoc sources = searchIndexSources();
248         Iterator JavaDoc itSources = sources.iterator();
249         CmsSearchIndexSource source;
250         while (itSources.hasNext()) {
251             source = (CmsSearchIndexSource)itSources.next();
252             CmsListItem item = getList().newItem(source.getName());
253             // use "null" String to avoid exception if name is empty
254
item.set(LIST_COLUMN_NAME, String.valueOf(source.getName()));
255             item.set(LIST_COLUMN_INDEXER, source.getIndexer().getClass());
256             result.add(item);
257         }
258         return result;
259     }
260
261     /**
262      * @see org.opencms.workplace.CmsWorkplace#initMessages()
263      */

264     protected void initMessages() {
265
266         // add specific dialog resource bundle
267
addMessages(Messages.get().getBundleName());
268         // add default resource bundles
269
super.initMessages();
270     }
271
272     /**
273      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
274      */

275     protected void setColumns(CmsListMetadata metadata) {
276
277         // create column for edit
278
CmsListColumnDefinition editCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
279         editCol.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
280         editCol.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
281         editCol.setWidth("5");
282         editCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
283         editCol.setSorteable(false);
284         // add dummy icon
285
CmsListDirectAction editAction = new CmsListDirectAction(
286             CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE);
287         editAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_0));
288         editAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_ICON_NAME_HELP_0));
289         editAction.setIconPath(CmsSearchIndexList.LIST_ICON_INDEXSOURCE);
290         editCol.addDirectAction(editAction);
291         // add it to the list definition
292
metadata.addColumn(editCol);
293
294         // add column for name
295
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
296         nameCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
297         nameCol.setName(Messages.get().container(Messages.GUI_LIST_SEARCHINDEX_COL_NAME_0));
298         nameCol.setWidth("55%");
299         // add overview action
300
CmsListDefaultAction defEditAction = new CmsListDefaultAction(
301             CmsSearchIndexSourceControlList.LIST_ACTION_OVERVIEW_INDEXSOURCE2);
302         defEditAction.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_OVERVIEW_NAME_0));
303         defEditAction.setHelpText(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_OVERVIEW_NAME_HELP_0));
304         nameCol.addDefaultAction(defEditAction);
305         metadata.addColumn(nameCol);
306
307         // add column for analyzer
308
CmsListColumnDefinition analyzerCol = new CmsListColumnDefinition(LIST_COLUMN_INDEXER);
309         analyzerCol.setAlign(CmsListColumnAlignEnum.ALIGN_LEFT);
310         analyzerCol.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_COL_INDEXER_0));
311         analyzerCol.setWidth("45%");
312         metadata.addColumn(analyzerCol);
313
314     }
315
316     /**
317      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
318      */

319     protected void setIndependentActions(CmsListMetadata metadata) {
320
321         // add document types of index source detail help
322
CmsListItemDetails doctypeDetails = new CmsListItemDetails(LIST_DETAIL_DOCTYPES);
323         doctypeDetails.setAtColumn(LIST_COLUMN_NAME);
324         doctypeDetails.setVisible(false);
325         doctypeDetails.setShowActionName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_SHOW_0));
326         doctypeDetails.setShowActionHelpText(Messages.get().container(
327             Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_SHOW_HELP_0));
328         doctypeDetails.setHideActionName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_HIDE_0));
329         doctypeDetails.setHideActionHelpText(Messages.get().container(
330             Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_HIDE_HELP_0));
331         doctypeDetails.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_DOCTYPE_NAME_0));
332         doctypeDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
333             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(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_SHOW_0));
341         resourceDetails.setShowActionHelpText(Messages.get().container(
342             Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_SHOW_HELP_0));
343         resourceDetails.setHideActionName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_HIDE_0));
344         resourceDetails.setHideActionHelpText(Messages.get().container(
345             Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_HIDE_HELP_0));
346         resourceDetails.setName(Messages.get().container(Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_NAME_0));
347         resourceDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
348             Messages.GUI_LIST_INDEXSOURCE_DETAIL_RESOURCE_NAME_0)));
349         metadata.addItemDetails(resourceDetails);
350
351     }
352
353     /**
354      * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
355      */

356     protected void setMultiActions(CmsListMetadata metadata) {
357
358         // view only
359
}
360
361     /**
362      * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
363      */

364     protected void validateParamaters() throws Exception JavaDoc {
365
366         // test the needed parameters
367
if (getParamIndexName() == null) {
368             throw new CmsIllegalStateException(Messages.get().container(
369                 Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
370                 A_CmsEditSearchIndexDialog.PARAM_INDEXNAME));
371         }
372     }
373
374     /**
375      * Writes the updated search configuration back to the XML
376      * configuration file and refreshes the complete list.<p>
377      *
378      * @param refresh if true, the list items are refreshed
379      */

380     protected void writeConfiguration(boolean refresh) {
381
382         // update the XML configuration
383
OpenCms.writeConfiguration(CmsSearchConfiguration.class);
384         if (refresh) {
385             refreshList();
386         }
387     }
388
389     /**
390      * Fills details about document types of the index source into the given item. <p>
391      *
392      * @param item the list item to fill
393      * @param detailId the id for the detail to fill
394      *
395      */

396     private void fillDetailDocTypes(CmsListItem item, String JavaDoc detailId) {
397
398         CmsSearchManager searchManager = OpenCms.getSearchManager();
399         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
400
401         // search for the corresponding CmsSearchIndexSource:
402
String JavaDoc idxSourceName = (String JavaDoc)item.get(LIST_COLUMN_NAME);
403         CmsSearchIndexSource idxSource = searchManager.getIndexSource(idxSourceName);
404
405         // get the index sources doc types
406
List JavaDoc docTypes = idxSource.getDocumentTypes();
407         // output of found index sources
408
Iterator JavaDoc itDocTypes = docTypes.iterator();
409         CmsSearchDocumentType docType;
410         html.append("<ul>\n");
411         while (itDocTypes.hasNext()) {
412             // get the instance (instead of plain name) for more detail in future...
413
docType = searchManager.getDocumentTypeConfig(itDocTypes.next().toString());
414             // harden against unconfigured doctypes that are refferred to by indexsource nodes
415
if (docType != null) {
416
417                 html.append(" <li>\n").append(" ").append(docType.getName()).append("\n");
418                 html.append(" </li>");
419             }
420         }
421
422         html.append("</ul>\n");
423         item.set(detailId, html.toString());
424     }
425
426     /**
427      * Fills details about resource paths of the index source into the given item. <p>
428      *
429      * @param item the list item to fill
430      * @param detailId the id for the detail to fill
431      *
432      */

433     private void fillDetailResources(CmsListItem item, String JavaDoc detailId) {
434
435         CmsSearchManager searchManager = OpenCms.getSearchManager();
436         StringBuffer JavaDoc html = new StringBuffer JavaDoc();
437
438         // search for the corresponding CmsSearchIndexSource:
439
String JavaDoc idxSourceName = (String JavaDoc)item.get(LIST_COLUMN_NAME);
440         CmsSearchIndexSource idxSource = searchManager.getIndexSource(idxSourceName);
441
442         // get the index sources resource strings
443
List JavaDoc resources = idxSource.getResourcesNames();
444         // output of found index sources
445
Iterator JavaDoc itResources = resources.iterator();
446         html.append("<ul>\n");
447         while (itResources.hasNext()) {
448
449             html.append(" <li>\n").append(" ").append(itResources.next().toString()).append("\n");
450             html.append(" </li>");
451         }
452
453         html.append("</ul>\n");
454
455         item.set(detailId, html.toString());
456     }
457
458     /**
459      * Returns the available search indexes of this installation.
460      *
461      * @return the available search indexes of this installation
462      */

463     private List JavaDoc searchIndexSources() {
464
465         CmsSearchManager manager = OpenCms.getSearchManager();
466         CmsSearchIndex index = manager.getIndex(getParamIndexName());
467         List JavaDoc sources = index.getSources();
468         return sources;
469     }
470
471 }
472
Popular Tags