View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.modules.actions.portlets;
17  
18  import java.util.List;
19  import org.apache.turbine.util.RunData;
20  import org.apache.velocity.context.Context;
21  
22  import org.apache.jetspeed.portal.Portlet;
23  import org.apache.jetspeed.util.PortletSessionState;
24  import org.apache.jetspeed.services.search.Search;
25  import org.apache.jetspeed.services.search.ParsedObject;
26  import org.apache.jetspeed.services.search.SearchResults;
27  
28  /***
29   * Search Actions built here
30   *
31   * @author <a href="mailto:taylor@apache.org">David Sean taylor</a>
32   * @version $Id: SearchAction.java,v 1.3 2004/02/23 02:56:58 jford Exp $
33   */
34  
35  public class SearchAction extends GenericMVCAction
36  {
37      public final static String SEARCH_STRING = "search";
38      public final static String SEARCH_RESULTS = "search_results";    
39      public final static String SEARCH_RESULTSIZE = "search_resultsize";    
40      
41      /***
42       * Subclasses must override this method to provide default behavior
43       * for the portlet action
44       */
45      public void buildNormalContext(Portlet portlet, 
46                                     Context context, 
47                                     RunData rundata)
48      throws Exception
49      {     
50          List results = (List)rundata.getRequest().getAttribute(SEARCH_RESULTS);           
51          if (null == results)
52          {
53              results = (List)PortletSessionState.getAttribute(rundata, SEARCH_RESULTS);
54              //System.out.println("session results = " + results); 
55              
56          }
57          else
58          {
59              //System.out.println("qp results = " + results);
60              PortletSessionState.setAttribute(rundata, SEARCH_RESULTS, results);
61          }
62          
63          if (null != results)
64          {
65              context.put(SEARCH_RESULTS, results);
66          }
67          
68          String searchString = (String)rundata.getParameters().getString(SEARCH_STRING);
69          if (searchString == null || searchString.trim().length() == 0)
70          {
71              searchString = (String)PortletSessionState.getAttribute(rundata, SEARCH_STRING);
72          }
73          else
74          {
75              PortletSessionState.setAttribute(rundata, SEARCH_STRING, searchString);
76          }
77          
78          context.put(SEARCH_STRING, searchString);
79          
80          if (results != null)
81          {
82              //System.out.println("size = " + results.size());
83              context.put(SEARCH_RESULTSIZE, new Integer(results.size()));
84          }
85      }
86  
87      public void doSearch(RunData rundata, Context context)
88      {
89          // get posted new target
90          String searchString = (String)rundata.getParameters().getString(SEARCH_STRING);
91          
92          if (searchString == null || searchString.trim().length() == 0)
93          {
94              return;
95          }
96          
97          //
98          // execute the query
99          //
100         ParsedObject result = null;
101         SearchResults results  = Search.search(searchString);
102         //System.out.println("Query hits = " + results.size());
103         rundata.getRequest().setAttribute(SEARCH_RESULTS, results.getResults());                
104     }
105 
106 }
107