KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/searchindex/A_CmsEditSearchIndexDialog.java,v $
3  * Date : $Date: 2006/10/17 09:09:23 $
4  * Version: $Revision: 1.4 $
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.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsIllegalStateException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.search.CmsSearchIndex;
39 import org.opencms.search.CmsSearchIndexSource;
40 import org.opencms.search.CmsSearchManager;
41 import org.opencms.workplace.CmsWidgetDialog;
42 import org.opencms.workplace.CmsWorkplaceSettings;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.servlet.jsp.PageContext JavaDoc;
52
53 /**
54  * Abstract dialog class for all dialogs that work on a <code>CmsSearchIndex</code>.<p>
55  *
56  * The <code>{@link #PARAM_INDEXNAME}</code> ("searchindex") is supported
57  * by means of widget technology (setter / getter).<p>
58  *
59  * Also - for accessing search functionality a member <code>{@link #m_searchManager}</code>
60  * is accessible for implementations. <p>
61  *
62  * @author Achim Westermann
63  *
64  * @version $Revision: 1.4 $
65  *
66  * @since 6.0.0
67  */

68 public abstract class A_CmsEditSearchIndexDialog extends CmsWidgetDialog {
69
70     /** localized messages Keys prefix. */
71     public static final String JavaDoc KEY_PREFIX = "searchindex";
72
73     /** Defines which pages are valid for this dialog. */
74     public static final String JavaDoc[] PAGES = {"page1"};
75
76     /**
77      * The request parameter for the search index to work with when contacting
78      * this dialog from another. <p>
79      *
80      * It may be emtpy if we are on the new index dialog (/searchindex/new-index.jsp).<p>
81      *
82      **/

83     public static final String JavaDoc PARAM_INDEXNAME = "indexname";
84
85     /** The user object that is edited on this dialog. */
86     protected CmsSearchIndex m_index;
87
88     /** The search manager singleton for convenient access. **/
89     protected CmsSearchManager m_searchManager;
90
91     /** Stores the value of the request parameter for the search index Name. */
92     private String JavaDoc m_paramIndexName;
93
94     /**
95      * Public constructor with JSP action element.<p>
96      *
97      * @param jsp an initialized JSP action element
98      */

99     public A_CmsEditSearchIndexDialog(CmsJspActionElement jsp) {
100
101         super(jsp);
102     }
103
104     /**
105      * Public constructor with JSP variables.<p>
106      *
107      * @param context the JSP page context
108      * @param req the JSP request
109      * @param res the JSP response
110      */

111     public A_CmsEditSearchIndexDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
112
113         this(new CmsJspActionElement(context, req, res));
114     }
115
116     /**
117      * Writes the updated search configuration back to the XML
118      * configuration file and refreshes the complete list.<p>
119      */

120     protected static void writeConfiguration() {
121
122         // update the XML configuration
123
OpenCms.writeConfiguration(CmsSearchConfiguration.class);
124     }
125
126     /**
127      * Commits the edited search index to the search manager.<p>
128      */

129     public void actionCommit() {
130
131         List JavaDoc errors = new ArrayList JavaDoc();
132
133         try {
134
135             // if new create it first
136
if (!m_searchManager.getSearchIndexes().contains(m_index)) {
137                 // empty or null name and uniqueness check in add method
138
m_searchManager.addSearchIndex(m_index);
139             }
140             writeConfiguration();
141
142         } catch (Throwable JavaDoc t) {
143             errors.add(t);
144         }
145
146         // set the list of errors to display when saving failed
147
setCommitErrors(errors);
148     }
149
150     /**
151      * Returns the request parameter value for parameter paramSearchIndex. <p>
152      *
153      * @return the request parameter value for parameter paramSearchIndex
154      */

155     public String JavaDoc getParamIndexName() {
156
157         return m_paramIndexName;
158     }
159
160     /**
161      * Sets the value of the request parameter paramSearchIndex.
162      *
163      * @param paramSearchIndex the value of the request parameter paramSearchIndex to set
164      */

165     public void setParamIndexName(String JavaDoc paramSearchIndex) {
166
167         m_paramIndexName = paramSearchIndex;
168     }
169
170     /**
171      * Initializes the user object (a <code>{@link CmsSearchIndex}</code> instance.<p>
172      *
173      * Implementation always have to call <code>"super.defineWidgets()"</code> first as
174      * this action may only be done here (relies on filled request parameters, the next
175      * following operation <code>{@link CmsWidgetDialog#createDialogHtml()}</code> will
176      * rely on this. <p>
177      *
178      * @see org.opencms.workplace.CmsWidgetDialog#defineWidgets()
179      */

180     protected void defineWidgets() {
181
182         initUserObject();
183         setKeyPrefix(KEY_PREFIX);
184
185     }
186
187     /**
188      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
189      */

190     protected String JavaDoc[] getPageArray() {
191
192         return PAGES;
193     }
194
195     /**
196      * Returns the root path of this dialog (path relative to "/system/workplace/admin").<p>
197      *
198      * @return the root path of this dialog (path relative to "/system/workplace/admin")
199      */

200     protected String JavaDoc getToolPath() {
201
202         return "/searchindex";
203     }
204
205     /**
206      * @see org.opencms.workplace.CmsWorkplace#initMessages()
207      */

208     protected void initMessages() {
209
210         // add specific dialog resource bundle
211
addMessages(Messages.get().getBundleName());
212         // add default resource bundles
213
super.initMessages();
214     }
215
216     /**
217      * Initializes the user object to work with depending on the dialog state and request parameters.<p>
218      *
219      * Two initializations of the user object on first dialog call are possible:
220      * <ul>
221      * <li>edit an existing search index</li>
222      * <li>create a new search index with default initialization</li>
223      * </ul>
224      */

225     protected void initUserObject() {
226
227         try {
228             m_index = m_searchManager.getIndex(getParamIndexName());
229             if (m_index == null) {
230                 m_index = createDummySearchIndex();
231             }
232         } catch (Exception JavaDoc e) {
233             m_index = createDummySearchIndex();
234         }
235     }
236
237     /**
238      * Overridden to initialize the internal <code>CmsSearchManager</code> before initWorkplaceRequestValues ->
239      * defineWidgets -> will access it (NPE). <p>
240      *
241      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
242      */

243     protected void initWorkplaceMembers(CmsJspActionElement jsp) {
244
245         m_searchManager = OpenCms.getSearchManager();
246         super.initWorkplaceMembers(jsp);
247     }
248
249     /**
250      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
251      */

252     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
253
254         // initialize parameters and dialog actions in super implementation
255
super.initWorkplaceRequestValues(settings, request);
256
257         // save the current search index
258
Map JavaDoc dialogObject = (Map JavaDoc)getDialogObject();
259         if (dialogObject == null) {
260             dialogObject = new HashMap JavaDoc();
261             dialogObject.put(PARAM_INDEXNAME, m_index);
262             setDialogObject(dialogObject);
263         }
264
265     }
266
267     /**
268      * Checks if the new search index dialog has to be displayed.<p>
269      *
270      * @return <code>true</code> if the new search index dialog has to be displayed
271      */

272     protected boolean isNewSearchIndex() {
273
274         return DIALOG_INITIAL.equals(getParamAction());
275     }
276
277     /**
278      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
279      */

280     protected void validateParamaters() throws Exception JavaDoc {
281
282         if (!isNewSearchIndex()) {
283             // test the needed parameters: if initial we have "indexname", if from same widget we have name.0
284
if (getParamIndexName() == null && getJsp().getRequest().getParameter("name.0") == null) {
285                 throw new CmsIllegalStateException(Messages.get().container(
286                     Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
287                     PARAM_INDEXNAME));
288             }
289         }
290     }
291
292     private CmsSearchIndexSource createDummyIndexSource() {
293
294         CmsSearchIndexSource result = new CmsSearchIndexSource();
295         result.setName("default");
296         result.setIndexerClassName("org.opencms.search.CmsVfsIndexer");
297         result.addDocumentType("html");
298         result.addDocumentType("generic");
299         result.addDocumentType("pdf");
300         // add search index source to config:
301
m_searchManager.addSearchIndexSource(result);
302         return result;
303     }
304
305     /**
306      * Creates a "dummy" search index that is not linked to the search manager and has
307      * a <code>null</code> name property that will be used for being filled with
308      * the widget bean technology. <p>
309      *
310      * @return a "dummy" search index that is not linked to the search manager and has
311      * a <code>null</code> name property that will be used for being filled with
312      * the widget bean technology
313      */

314     private CmsSearchIndex createDummySearchIndex() {
315
316         CmsSearchIndex result = new CmsSearchIndex();
317         result.setLocale("en");
318         result.setProjectName("Online");
319         result.setRebuildMode("auto");
320
321         // find default source
322
Map JavaDoc sources = m_searchManager.getSearchIndexSources();
323         if (sources.isEmpty()) {
324             CmsSearchIndexSource source = createDummyIndexSource();
325             sources.put(source.getName(), source);
326         }
327         result.addSourceName((String JavaDoc)sources.keySet().iterator().next());
328
329         return result;
330
331     }
332 }
Popular Tags