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.springframework.util.StringUtils;
024: import org.springframework.validation.BindException;
025: import org.springframework.web.servlet.ModelAndView;
026:
027: /**
028: * A general Spring's MVC Controller that perform the index operation of
029: * <code>CompassGps</code>.
030: * <p>
031: * Will perform the index operation if the
032: * {@link org.compass.spring.web.mvc.CompassIndexCommand} <code>doIndex</code>
033: * property is set to true.
034: * <p>
035: * The controller has two views to be set, the <code>indexView</code>, which
036: * is the view that holds the screen which the user will initiate the index
037: * operation, and the <code>indexResultsView</code>, which will show the
038: * results of the index operation.
039: * <p>
040: * The results of the index operation will be saved under the
041: * <code>indexResultsName</code>, which defaults to "indexResults".
042: *
043: * @author kimchy
044: */
045: public class CompassIndexController extends
046: AbstractCompassGpsCommandController {
047:
048: private String indexView;
049:
050: private String indexResultsView;
051:
052: private String indexResultsName = "indexResults";
053:
054: public CompassIndexController() {
055: setCommandClass(CompassIndexCommand.class);
056: }
057:
058: public void afterPropertiesSet() throws Exception {
059: super .afterPropertiesSet();
060: if (indexView == null) {
061: throw new IllegalArgumentException(
062: "Must set the indexView property");
063: }
064: if (indexResultsView == null) {
065: throw new IllegalArgumentException(
066: "Must set the indexResultsView property");
067: }
068: }
069:
070: protected ModelAndView handle(HttpServletRequest request,
071: HttpServletResponse response, Object command,
072: BindException errors) throws Exception {
073: CompassIndexCommand indexCommand = (CompassIndexCommand) command;
074:
075: if (!StringUtils.hasText(indexCommand.getDoIndex())
076: || !indexCommand.getDoIndex().equalsIgnoreCase("true")) {
077: return new ModelAndView(getIndexView(), getCommandName(),
078: indexCommand);
079: }
080:
081: long time = System.currentTimeMillis();
082:
083: getCompassGps().index();
084:
085: time = System.currentTimeMillis() - time;
086: CompassIndexResults indexResults = new CompassIndexResults(time);
087: HashMap data = new HashMap();
088: data.put(getCommandName(), indexCommand);
089: data.put(getIndexResultsName(), indexResults);
090: return new ModelAndView(getIndexResultsView(), data);
091: }
092:
093: /**
094: * Returns the view that holds the screen which the user will initiate the
095: * index operation.
096: */
097: public String getIndexView() {
098: return indexView;
099: }
100:
101: /**
102: * Sets the view that holds the screen which the user will initiate the
103: * index operation.
104: */
105: public void setIndexView(String indexView) {
106: this .indexView = indexView;
107: }
108:
109: /**
110: * Returns the name of the results that the {@link CompassIndexResults} will
111: * be saved under. Defaults to "indexResults".
112: */
113: public String getIndexResultsName() {
114: return indexResultsName;
115: }
116:
117: /**
118: * Sets the name of the results that the {@link CompassIndexResults} will be
119: * saved under. Defaults to "indexResults".
120: *
121: * @param indexResultsName
122: */
123: public void setIndexResultsName(String indexResultsName) {
124: this .indexResultsName = indexResultsName;
125: }
126:
127: /**
128: * Returns the view which will show the results of the index operation.
129: */
130: public String getIndexResultsView() {
131: return indexResultsView;
132: }
133:
134: /**
135: * Sets the view which will show the results of the index operation.
136: *
137: * @param indexResultsView
138: */
139: public void setIndexResultsView(String indexResultsView) {
140: this.indexResultsView = indexResultsView;
141: }
142: }
|