KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > search > ManageSearch


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// ManageSearch
14
//
15
// 02.04.2002 NK added in jahia.
16
//
17

18 package org.jahia.admin.search;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29
30 import org.jahia.bin.JahiaAdministration;
31 import org.jahia.data.JahiaData;
32 import org.jahia.exceptions.JahiaException;
33 import org.jahia.params.ParamBean;
34 import org.jahia.registries.ServicesRegistry;
35 import org.jahia.resourcebundle.JahiaResourceBundle;
36 import org.jahia.services.search.JahiaSearchService;
37 import org.jahia.services.sites.JahiaSite;
38 import org.jahia.services.sites.JahiaSiteTools;
39 import org.jahia.services.sites.JahiaSitesService;
40 import org.jahia.services.usermanager.JahiaUser;
41
42
43 /**
44  * This class is used by the administration to manage the search engine.
45  *
46  * Copyright: Copyright (c) 2001
47  * Company: Jahia
48  *
49  * @author <Khue Nguyen> <a HREF="mailto:khue@jahia.org">khue@jahia.org</a>
50  * @version 1.0
51  */

52 public class ManageSearch {
53
54     private static final org.apache.log4j.Logger logger =
55             org.apache.log4j.Logger.getLogger (ManageSearch.class.getName ());
56
57     private static final String JavaDoc CLASS_NAME = ManageSearch.class.getName ();
58     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
59     private static final String JavaDoc HTTP_FILES = "/jsp/jahia/engines";
60     private static final String JavaDoc JSP = "/jsp/jahia/administration/search/main.jsp?includejsp=";
61
62     private static ServletContext JavaDoc context;
63
64     private ServicesRegistry sReg;
65     private JahiaSitesService siteServ;
66     private JahiaSearchService searchServ;
67
68     private JahiaSite site;
69     private JahiaUser user;
70     private Hashtable JavaDoc values;
71
72     private String JavaDoc operation = ""; // current operation
73

74
75     /**
76      * Default constructor.
77      *
78      * @param request Servlet request.
79      * @param response Servlet response.
80      * @param session Servlet session for the current user.
81      * @param context Servlet context.
82      */

83     public ManageSearch (HttpServletRequest JavaDoc request,
84                          HttpServletResponse JavaDoc response,
85                          HttpSession JavaDoc session,
86                          ServletContext JavaDoc context)
87             throws Throwable JavaDoc {
88         ManageSearch.context = context;
89         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
90         ParamBean jParams = null;
91         if (jData != null) {
92             jParams = jData.params ();
93         }
94
95         sReg = ServicesRegistry.getInstance ();
96
97         if (sReg == null) {
98             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.serviceRegistyNotAvailable.label",
99                     jParams, jParams.getLocale ());
100             displayError (request, response, dspMsg);
101             return;
102         }
103
104         siteServ = sReg.getJahiaSitesService ();
105         searchServ = sReg.getJahiaSearchService ();
106
107         if (siteServ == null || searchServ == null) {
108             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestServiceNotAvailable.label",
109                     jParams, jParams.getLocale ());
110             displayError (request, response, dspMsg);
111             return;
112         }
113
114         userRequestDispatcher (request, response, session);
115     }
116
117
118     /**
119      * This method is used like a dispatcher for user requests.
120      *
121      * @param request Servlet request.
122      * @param response Servlet response.
123      * @param session Servlet session for the current user.
124      */

125     private void userRequestDispatcher (HttpServletRequest JavaDoc request,
126                                         HttpServletResponse JavaDoc response,
127                                         HttpSession JavaDoc session)
128             throws IOException JavaDoc, ServletException JavaDoc {
129
130         // check if the user has really admin access to this site...
131
user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER);
132         site = (JahiaSite)session.getAttribute (ParamBean.SESSION_SITE);
133         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
134         ParamBean jParams = null;
135         if (jData != null) {
136             jParams = jData.params ();
137         }
138
139         if (site == null || user == null || sReg == null) {
140             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestInfoNotAvailable.label",
141                     jParams, jParams.getLocale ());
142             displayError (request, response, dspMsg);
143             return;
144         }
145
146         // restore data stored in session
147
values = (Hashtable JavaDoc)session.getAttribute ("org.jahia.adminsearch.values." + site.getSiteKey ());
148         if (values == null) {
149             Hashtable JavaDoc values = new Hashtable JavaDoc ();
150             session.setAttribute ("org.jahia.adminsearch.values." + site.getSiteKey (), values);
151             this.values = values;
152         }
153
154         try {
155
156             if (JahiaSiteTools.getAdminGroup (site).isMember (user)) {
157
158                 operation = request.getParameter ("sub");
159
160                 if (operation.equals ("display")) {
161                     displayMain (request, response, session);
162                 } else if (operation.equals ("chooseoperation")) {
163                     processChooseOperation (request, response, session);
164                 } else if (operation.equals ("doindex")) {
165                     processDoIndex (request, response, session);
166                 } else if (operation.equals ("abortIndexation")) {
167                     processAbortIndexation (request, response, session);
168                 }
169             } else {
170                 String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label",
171                         jParams, jParams.getLocale ());
172                 displayError (request, response, dspMsg);
173             }
174
175         } catch (Throwable JavaDoc t) {
176             t.printStackTrace ();
177             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.processingRequestError.label",
178                     jParams, jParams.getLocale ());
179             displayError (request, response, dspMsg);
180         }
181     }
182
183     /**
184      * Forward the servlet request and servlet response objects, using the request
185      * dispatcher (from the ServletContext). Note: please be careful, use only
186      * context relative path.
187      *
188      * @param request servlet request.
189      * @param response servlet response.
190      * @param target target, context-relative path.
191      * @exception IOException an I/O exception occured during the process.
192      * @exception ServletException a servlet exception occured during the process.
193      */

194     private void doRedirect (HttpServletRequest JavaDoc request,
195                              HttpServletResponse JavaDoc response,
196                              String JavaDoc target)
197             throws IOException JavaDoc, ServletException JavaDoc {
198         try {
199             request.setAttribute ("currentSite", site);
200             request.setAttribute ("user", user);
201
202             // check current operation
203
if (request.getAttribute ("operation") == null) {
204                 request.setAttribute ("operation", operation);
205             }
206
207             // check null warning msg
208
if (request.getAttribute ("warningMsg") == null) {
209                 request.setAttribute ("warningMsg", "");
210             }
211
212             // check null jsp bottom message, and fill in if necessary...
213
if (request.getAttribute ("msg") == null) {
214                 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
215                 ParamBean jParams = null;
216                 if (jData != null) {
217                     jParams = jData.params ();
218                 }
219                 request.setAttribute ("msg",
220                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
221                         jParams, jParams.getLocale()));
222
223             }
224
225             if (request.getAttribute ("focus") == null) {
226                 request.setAttribute ("focus", "-none-");
227             }
228
229             // check null configuration step title, and fill in if necessary...
230
if (request.getAttribute ("title") == null) {
231                 request.setAttribute ("title", "Manage Search Engine");
232             }
233
234             // set input values on request...
235
request.setAttribute ("values", values);
236
237             // add http files path in request...
238
request.setAttribute ("url", request.getContextPath () + HTTP_FILES);
239
240             // get browser entity...
241
String JavaDoc userAgent = request.getHeader ("user-agent");
242
243             Integer JavaDoc navigator = (userAgent.indexOf ("IE"))
244                     != -1 ? new Integer JavaDoc (0) : new Integer JavaDoc (1);
245             request.setAttribute ("navigator", navigator);
246
247             boolean isLynx = false;
248
249             if (userAgent.indexOf ("Lynx") != -1) {
250                 isLynx = true;
251             }
252             request.setAttribute ("isLynx", new Boolean JavaDoc (isLynx));
253
254             // set browser response content type
255
HttpSession JavaDoc session = request.getSession ();
256             JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
257             ParamBean jParams = null;
258             if (jData != null) {
259                 jParams = jData.params ();
260             }
261             Locale JavaDoc currentLocale = (Locale JavaDoc)session.getAttribute (ParamBean.SESSION_LOCALE);
262             if (currentLocale == null) {
263                 currentLocale = jParams.getLocale ();
264             }
265
266             String JavaDoc contentTypeStr = "text/html;charset=";
267             contentTypeStr = contentTypeStr + "ISO8859-1";
268             request.setAttribute ("content-type", contentTypeStr);
269
270             // redirect!
271
context.getRequestDispatcher (target).forward (request, response);
272
273         } catch (IOException JavaDoc ie) {
274             logger.error (ie.getMessage ());
275         } catch (ServletException JavaDoc se) {
276             logger.error (se.getMessage ());
277         }
278     }
279
280     /**
281      * Display the search engine main admin page.
282      *
283      * @param request Servlet request.
284      * @param response Servlet response.
285      * @param session Servlet session for the current user.
286      */

287     private void displayMain (HttpServletRequest JavaDoc request,
288                               HttpServletResponse JavaDoc response,
289                               HttpSession JavaDoc session)
290             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
291
292         // set the default action
293
request.setAttribute ("subAction", "chooseoperation");
294         // set the default step
295
request.setAttribute ("go", "change");
296
297         // default values
298
String JavaDoc operation = "doindex";
299
300         String JavaDoc val = null;
301         val = (String JavaDoc)values.get ("operation");
302         if (val != null) {
303             operation = val;
304         }
305
306         // check if the index exists yet for the current site.
307
String JavaDoc indexPath = searchServ.getSiteIndex (site.getID ());
308         boolean indexExists = (indexPath != null);
309
310         values.put ("operation", operation);
311
312         request.setAttribute ("operation", operation);
313         request.setAttribute ("indexExists", new Boolean JavaDoc (indexExists));
314         request.setAttribute ("nbDocsInMainQueue", new Integer JavaDoc(searchServ.getNbDocumentsInQueue()));
315         request.setAttribute ("nbFilesInMainQueue", new Integer JavaDoc(searchServ.getNbOfFilesInQueue()));
316         request.setAttribute ("siteIndexationStatus", searchServ.getSiteIndexationStatus(site.getID()));
317
318         doRedirect (request, response, JSP + "operations");
319     }
320
321     /**
322      * Process the form choose operations.
323      *
324      * @param request Servlet request.
325      * @param response Servlet response.
326      * @param session Servlet session for the current user.
327      */

328     private void processChooseOperation (HttpServletRequest JavaDoc request,
329                                          HttpServletResponse JavaDoc response,
330                                          HttpSession JavaDoc session)
331             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
332
333         // get submitted data
334
String JavaDoc operation = request.getParameter ("operation");
335         if (operation == null)
336             displayMain (request, response, session);
337
338         values.put ("operation", operation);
339
340         if (operation.equals ("doindex")) {
341             displayIndexForm (request, response, session);
342         } else if (operation.equals ("abortIndexation")) {
343             displayAbortIndexationForm (request, response, session);
344         } else {
345             displayMain (request, response, session);
346         }
347     }
348
349     /**
350      * Index the site.
351      *
352      * @param request Servlet request.
353      * @param response Servlet response.
354      * @param session Servlet session for the current user.
355      */

356     private void processDoIndex (HttpServletRequest JavaDoc request,
357                                  HttpServletResponse JavaDoc response,
358                                  HttpSession JavaDoc session)
359             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
360
361         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
362         ParamBean jParams = null;
363         if (jData != null) {
364             jParams = jData.params ();
365         }
366         if (!user.isAdminMember(site.getID()) && !user.isRoot ()) {
367             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label",
368                     jParams, jParams.getLocale ());
369             displayError (request, response, dspMsg);
370             return;
371         }
372
373         //JahiaData jData = (JahiaData)request.getAttribute("org.jahia.data.JahiaData");
374

375         // get submitted data
376
String JavaDoc go = request.getParameter ("go");
377         if (go == null)
378             displayMain (request, response, session);
379
380         if (go.equals ("ok")) {
381
382             // process the indexation of the full site.
383
boolean result = searchServ.indexSite (site.getID (), jData.params (), false, true);
384             //searchServ.optimizeIndex (site.getID ());
385

386             request.setAttribute ("result", new Boolean JavaDoc (result));
387
388             // set the default action
389
request.setAttribute ("subAction", "display");
390             // set the default step
391
request.setAttribute ("go", "change");
392
393             doRedirect (request, response, JSP + "indexresult");
394
395         } else {
396             displayMain (request, response, session);
397         }
398     }
399
400     /**
401      * Display the indexation form.
402      *
403      * @param request Servlet request.
404      * @param response Servlet response.
405      * @param session Servlet session for the current user.
406      */

407     private void displayIndexForm (HttpServletRequest JavaDoc request,
408                                    HttpServletResponse JavaDoc response,
409                                    HttpSession JavaDoc session)
410             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
411
412         // set the default action
413
request.setAttribute ("subAction", "doindex");
414         // set the default event
415
request.setAttribute ("go", "change");
416
417         doRedirect (request, response, JSP + "index");
418     }
419
420     /**
421      * Abort site's indexation.
422      *
423      * @param request Servlet request.
424      * @param response Servlet response.
425      * @param session Servlet session for the current user.
426      */

427     private void processAbortIndexation (HttpServletRequest JavaDoc request,
428                                  HttpServletResponse JavaDoc response,
429                                  HttpSession JavaDoc session)
430             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
431
432         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
433         ParamBean jParams = null;
434         if (jData != null) {
435             jParams = jData.params ();
436         }
437         if (!user.isAdminMember(site.getID()) && !user.isRoot ()) {
438             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label",
439                     jParams, jParams.getLocale ());
440             displayError (request, response, dspMsg);
441             return;
442         }
443
444         //JahiaData jData = (JahiaData)request.getAttribute("org.jahia.data.JahiaData");
445

446         // get submitted data
447
String JavaDoc go = request.getParameter ("go");
448         if (go == null)
449             displayMain (request, response, session);
450
451         if (go.equals ("ok")) {
452
453             // process the indexation of the full site.
454
boolean result = searchServ.abortSiteIndexation(site.getID ());
455             request.setAttribute ("result", new Boolean JavaDoc (result));
456             // set the default action
457
request.setAttribute ("subAction", "display");
458             // set the default step
459
request.setAttribute ("go", "change");
460
461             doRedirect (request, response, JSP + "abortindexationresult");
462
463         } else {
464             displayMain (request, response, session);
465         }
466     }
467
468     /**
469      * Display the abort indexation form.
470      *
471      * @param request Servlet request.
472      * @param response Servlet response.
473      * @param session Servlet session for the current user.
474      */

475     private void displayAbortIndexationForm (HttpServletRequest JavaDoc request,
476                                    HttpServletResponse JavaDoc response,
477                                    HttpSession JavaDoc session)
478             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
479
480         // set the default action
481
request.setAttribute ("subAction", "abortIndexation");
482         // set the default event
483
request.setAttribute ("go", "change");
484
485         doRedirect (request, response, JSP + "abortindexation");
486     }
487
488     /**
489      * Forward to error page.
490      *
491      * @param request Servlet request.
492      * @param response Servlet response.
493      * @param msg the warning msg.
494      */

495     private void displayError (HttpServletRequest JavaDoc request,
496                                HttpServletResponse JavaDoc response,
497                                String JavaDoc msg)
498             throws IOException JavaDoc, ServletException JavaDoc {
499         request.setAttribute ("warningMsg", msg);
500         doRedirect (request, response, JSP + "error");
501     }
502
503
504 }
Popular Tags