KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/searchindex/A_CmsEditIndexSourceDialog.java,v $
3  * Date : $Date: 2006/03/28 09:06:37 $
4  * Version: $Revision: 1.3 $
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.CmsSearchIndexSource;
39 import org.opencms.search.CmsSearchManager;
40 import org.opencms.workplace.CmsWidgetDialog;
41 import org.opencms.workplace.CmsWorkplaceSettings;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import javax.servlet.http.HttpServletRequest JavaDoc;
49 import javax.servlet.http.HttpServletResponse JavaDoc;
50 import javax.servlet.jsp.PageContext JavaDoc;
51
52 /**
53  * Abstract dialog class for editing or creating a <code>{@link org.opencms.search.CmsSearchIndexSource}</code>.<p>
54  *
55  * The <code>{@link #PARAM_INDEXSOURCE}</code> ("indexsource") is supported
56  * by means of widget technology (setter / getter).<p>
57  *
58  * Also - for accessing search functionality a member <code>{@link #m_searchManager}</code>
59  * is accessible for implementations. <p>
60  *
61  * If the property "admintoolhandler-args" contains the sequence "path:/searchindex/new-indexsource",
62  * a new indexsource will be created at first visit of this page
63  * (<code>{@link #initUserObject()}</code>). Else this dialog will be for editing an existing
64  * indexsource and the request parameter "indexsource" is mandatory. <p>
65  *
66  * @author Achim Westermann
67  *
68  * @version $Revision: 1.3 $
69  *
70  * @since 6.0.0
71  */

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

87     public static final String JavaDoc PARAM_INDEXSOURCE = "indexsource";
88
89     /** The user object that is edited on this dialog. */
90     protected CmsSearchIndexSource m_indexsource;
91
92     /** The search manager singleton for convenient access. **/
93     protected CmsSearchManager m_searchManager;
94
95     /** Stores the value of the request parameter for the search index Name. */
96     private String JavaDoc m_paramIndexSource;
97
98     /**
99      * Public constructor with JSP action element.<p>
100      *
101      * @param jsp an initialized JSP action element
102      */

103     public A_CmsEditIndexSourceDialog(CmsJspActionElement jsp) {
104
105         super(jsp);
106     }
107
108     /**
109      * Public constructor with JSP variables.<p>
110      *
111      * @param context the JSP page context
112      * @param req the JSP request
113      * @param res the JSP response
114      */

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

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

133     public void actionCommit() {
134
135         List JavaDoc errors = new ArrayList JavaDoc();
136
137         try {
138
139             // if new create it first
140
if (!m_searchManager.getSearchIndexSources().keySet().contains(m_indexsource.getName())) {
141                 m_searchManager.addSearchIndexSource(m_indexsource);
142             }
143             writeConfiguration();
144
145         } catch (Throwable JavaDoc t) {
146             errors.add(t);
147         }
148
149         // set the list of errors to display when saving failed
150
setCommitErrors(errors);
151     }
152
153     /**
154      * Returns the request parameter value for parameter indexsource. <p>
155      *
156      * @return the request parameter value for parameter indexsource
157      */

158     public String JavaDoc getParamIndexsource() {
159
160         return m_paramIndexSource;
161     }
162
163     /**
164      * Sets the request parameter value for parameter indexsource. <p>
165      *
166      * @param indexsource the request parameter value for parameter indexsource
167      */

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

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

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

203     protected String JavaDoc getToolPath() {
204
205         return "/searchindex/indexsources/indexsource";
206     }
207
208     /**
209      * @see org.opencms.workplace.CmsWorkplace#initMessages()
210      */

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

228     protected void initUserObject() {
229
230         if (m_indexsource == null) {
231             try {
232                 m_indexsource = m_searchManager.getIndexSource(getParamIndexsource());
233                 if (m_indexsource == null) {
234                     m_indexsource = new CmsSearchIndexSource();
235                 }
236             } catch (Exception JavaDoc e) {
237                 m_indexsource = new CmsSearchIndexSource();
238             }
239         }
240     }
241
242     /**
243      * Overridden to initialize the internal <code>CmsSearchManager</code> before initWorkplaceRequestValues ->
244      * defineWidgets -> will access it (NPE). <p>
245      *
246      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
247      */

248     protected void initWorkplaceMembers(CmsJspActionElement jsp) {
249
250         m_searchManager = OpenCms.getSearchManager();
251         super.initWorkplaceMembers(jsp);
252     }
253
254     /**
255      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
256      */

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

277     protected boolean isNewIndexSource() {
278
279         return DIALOG_INITIAL.equals(getParamAction());
280     }
281
282     /**
283      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
284      */

285     protected void validateParamaters() throws Exception JavaDoc {
286
287         if (!isNewIndexSource()) {
288             // test the needed parameters
289
if (getParamIndexsource() == null && getJsp().getRequest().getParameter("name.0") == null) {
290                 throw new CmsIllegalStateException(Messages.get().container(
291                     Messages.ERR_SEARCHINDEX_EDIT_MISSING_PARAM_1,
292                     PARAM_INDEXSOURCE));
293             }
294         }
295     }
296 }
Popular Tags