KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > search > SearchAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.webapp.action.core.search;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.util.PartialCollection;
20 import com.blandware.atleap.model.core.User;
21 import com.blandware.atleap.search.SearchManager;
22 import com.blandware.atleap.service.core.UserManager;
23 import com.blandware.atleap.webapp.action.core.grid.BaseGridAction;
24 import com.blandware.atleap.webapp.form.core.SearchForm;
25 import com.blandware.atleap.webapp.util.core.GlobalProperties;
26 import com.blandware.atleap.webapp.util.core.WebappConstants;
27 import com.blandware.atleap.webapp.util.core.WebappUtil;
28 import org.apache.lucene.queryParser.ParseException;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Locale JavaDoc;
40
41 /**
42  * <p>Returns page with search results (hits)
43  * </p>
44  * <p><a HREF="SearchAction.java.htm"><i>View Source</i></a></p>
45  * <p/>
46  *
47  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
48  * @version $Revision: 1.13 $ $Date: 2005/08/24 10:22:29 $
49  * @struts.action path="/core/search/search"
50  * validate="true"
51  * input="inputForward"
52  * name="searchForm"
53  * @struts.action-forward name="searchResults"
54  * path=".core.search.results"
55  * @struts.action-forward name="inputForward"
56  * path="/core/search/callSearch.do"
57  */

58 public final class SearchAction extends BaseGridAction {
59
60     /**
61      * @param mapping The ActionMapping used to select this instance
62      * @param form The optional ActionForm bean for this request (if any)
63      * @param request The HTTP request we are proceeding
64      * @param response The HTTP response we are creating
65      * @return an ActionForward instance describing where and how
66      * control should be forwarded, or null if response
67      * has already been completed
68      */

69     public ActionForward execute(ActionMapping mapping, ActionForm form,
70                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71
72         SearchForm searchForm = (SearchForm) form;
73
74         //set page number
75
int pageNumber = 1;
76         if ( request.getParameter("pageNumber") != null ) {
77             pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
78         } else {
79             //if pageNubmer is not specified it means that this the first query
80
request.getSession().removeAttribute(WebappConstants.SEARCH_HITS_COLLECTION_KEY);
81             request.getSession().removeAttribute(WebappConstants.SEARCH_QUERY_KEY);
82         }
83
84         //set language
85
boolean allLanguages = false;
86         String JavaDoc language = searchForm.getSearchLanguage();
87         if (language == null || language.trim().length() == 0) {
88             allLanguages = true;
89             if (request.getSession().getAttribute(Globals.LOCALE_KEY) != null) {
90                 language = ((Locale JavaDoc) request.getSession().getAttribute(Globals.LOCALE_KEY)).getLanguage();
91             }
92         }
93         if ( language == null ) {
94             language = Locale.getDefault().getLanguage();
95         }
96
97         //set time period in days
98
int days = 0;
99         if ( searchForm.getTimePeriod() != null ) {
100             days = Integer.parseInt(searchForm.getTimePeriod());
101         }
102
103         //set query criteria
104
List JavaDoc hits = null;
105         String JavaDoc criteria = searchForm.getQuery();
106         if ( criteria == null || criteria.trim().length() == 0 ) {
107             //get from session
108
hits = (List JavaDoc) request.getSession().getAttribute(WebappConstants.SEARCH_HITS_COLLECTION_KEY);
109             if ( hits != null ) {
110                 criteria = (String JavaDoc) request.getSession().getAttribute(WebappConstants.SEARCH_QUERY_KEY);
111             } else {
112                 criteria = "";
113             }
114         }
115
116         //get comma separated roles string of current user
117
String JavaDoc roles = "";
118         User user = (User) request.getSession().getAttribute(Constants.USER_KEY);
119         if ( user != null ) {
120             UserManager userManager = (UserManager) getBean(Constants.USER_MANAGER_BEAN);
121             user = userManager.retrieveUser(user.getName());
122             if ( user != null ) {
123                 roles = WebappUtil.rolesToString(user.getRoles());
124             }
125         }
126
127         int pageSize = GlobalProperties.getInstance(servlet.getServletContext()).getInteger(WebappConstants.GRID_PAGE_SIZE, 10).intValue();
128         int offset = (pageNumber - 1) * pageSize;
129
130         if ( log.isDebugEnabled() ) {
131             log.debug("Begin to search with following criteria: '" + criteria + "' and language: " + language);
132         }
133
134         SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
135         try {
136             hits = searchManager.search(hits, criteria, language, allLanguages, days, roles, offset, pageSize, request);
137             request.getSession().setAttribute(WebappConstants.SEARCH_HITS_COLLECTION_KEY, hits);
138             request.getSession().setAttribute(WebappConstants.SEARCH_QUERY_KEY, criteria);
139         } catch ( ParseException ex ) {
140             ActionMessages errors = new ActionMessages();
141             errors.add("searchQueryInvalid", new ActionMessage("core.search.errors.queryInvalid", ex.getMessage()));
142             saveToken(request);
143             return mapping.getInputForward();
144         }
145
146         int limit = (offset + pageSize) > hits.size() ? hits.size() : (offset + pageSize);
147         List JavaDoc pagedHits = hits.subList(offset, limit);
148         request.setAttribute(WebappConstants.HIT_COLLECTION_KEY, new PartialCollection(pagedHits, hits.size()));
149
150         return mapping.findForward("searchResults");
151     }
152
153
154 }
Popular Tags