001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.spring.web.mvc;
018:
019: import java.util.HashMap;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022:
023: import org.compass.core.support.search.CompassSearchCommand;
024: import org.compass.core.support.search.CompassSearchHelper;
025: import org.compass.core.support.search.CompassSearchResults;
026: import org.springframework.util.StringUtils;
027: import org.springframework.validation.BindException;
028: import org.springframework.web.servlet.ModelAndView;
029:
030: /**
031: * <p> A general Spring's MVC Controller that perform the search operation of <code>Compass</code>.
032: *
033: * <p>Will perform the search operation on the <code>Compass</code> instance using the query supplied
034: * by the {@link CompassSearchCommand}.
035: *
036: * <p>Pagination can be enabled by setting the <code>pageSize</code> property on the controller,
037: * as well as providing the <code>page</code> number property on the {@link CompassSearchCommand}.
038: *
039: * <p>The controller has two views to be set, the <code>searchView</code>, which is the view that
040: * holds the screen which the user will initiate the search operation, and the
041: * <code>searchResultsView</code>, which will show the results of the search operation (they can be
042: * the same page).
043: *
044: * <p>The results of the search operation will be saved under the <code>searchResultsName<code>,
045: * which defaults to "searchResults".
046: *
047: * <p>In order to extend and execute additional operations during the search process
048: * {@link org.compass.core.support.search.CompassSearchHelper} should be extended and
049: * set using {@link #setSearchHelper(org.compass.core.support.search.CompassSearchHelper)}.
050: *
051: * @author kimchy
052: */
053: public class CompassSearchController extends
054: AbstractCompassCommandController {
055:
056: private String searchView;
057:
058: private String searchResultsView;
059:
060: private String searchResultsName = "searchResults";
061:
062: private Integer pageSize;
063:
064: private CompassSearchHelper searchHelper;
065:
066: public CompassSearchController() {
067: setCommandClass(CompassSearchCommand.class);
068: }
069:
070: public void afterPropertiesSet() throws Exception {
071: super .afterPropertiesSet();
072: if (searchView == null) {
073: throw new IllegalArgumentException(
074: "Must set the searchView property");
075: }
076: if (searchResultsView == null) {
077: throw new IllegalArgumentException(
078: "Must set the serachResultsView property");
079: }
080: if (searchHelper == null) {
081: searchHelper = new CompassSearchHelper(getCompass(),
082: getPageSize());
083: }
084: }
085:
086: protected ModelAndView handle(HttpServletRequest request,
087: HttpServletResponse response, Object command,
088: BindException errors) throws Exception {
089: final CompassSearchCommand searchCommand = (CompassSearchCommand) command;
090: if (!StringUtils.hasText(searchCommand.getQuery())) {
091: return new ModelAndView(getSearchView(), getCommandName(),
092: searchCommand);
093: }
094: CompassSearchResults searchResults = searchHelper
095: .search(searchCommand);
096: HashMap data = new HashMap();
097: data.put(getCommandName(), searchCommand);
098: data.put(getSearchResultsName(), searchResults);
099: return new ModelAndView(getSearchResultsView(), data);
100: }
101:
102: /**
103: * Returns the view that holds the screen which the user will initiate the
104: * search operation.
105: */
106: public String getSearchView() {
107: return searchView;
108: }
109:
110: /**
111: * Sets the view that holds the screen which the user will initiate the
112: * search operation.
113: */
114: public void setSearchView(String searchView) {
115: this .searchView = searchView;
116: }
117:
118: /**
119: * Returns the name of the results that the {@link org.compass.core.support.search.CompassSearchResults}
120: * will be saved under. Defaults to "searchResults".
121: */
122: public String getSearchResultsName() {
123: return searchResultsName;
124: }
125:
126: /**
127: * Sets the name of the results that the {@link org.compass.core.support.search.CompassSearchResults} will
128: * be saved under. Defaults to "searchResults".
129: */
130: public void setSearchResultsName(String searchResultsName) {
131: this .searchResultsName = searchResultsName;
132: }
133:
134: /**
135: * Returns the view which will show the results of the search operation.
136: */
137: public String getSearchResultsView() {
138: return searchResultsView;
139: }
140:
141: /**
142: * Sets the view which will show the results of the search operation.
143: */
144: public void setSearchResultsView(String resultsView) {
145: this .searchResultsView = resultsView;
146: }
147:
148: /**
149: * Sets the page size for the pagination of the results. If not set, not
150: * pagination will be used.
151: */
152: public Integer getPageSize() {
153: return pageSize;
154: }
155:
156: /**
157: * Returns the page size for the pagination of the results. If not set, not
158: * pagination will be used.
159: *
160: * @param pageSize The page size when using paginated results
161: */
162: public void setPageSize(Integer pageSize) {
163: this .pageSize = pageSize;
164: }
165:
166: /**
167: * <p>The search helper is used to execute teh actual search. By default (if not set)
168: * the search controller will create a new search helper. If provided, the search
169: * controller will use it to perform the search.
170: *
171: * <p>Mainly used to extend the search helper and execute additional operation within
172: * specific calbacks the search helper exposes.
173: *
174: * @param searchHelper A specific search helper to use
175: */
176: public void setSearchHelper(CompassSearchHelper searchHelper) {
177: this.searchHelper = searchHelper;
178: }
179: }
|