diff options
author | James Moger <james.moger@gitblit.com> | 2012-03-13 18:53:27 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2012-03-13 18:53:27 -0400 |
commit | 7d97c619b1c067f971cfe0fda2ca8a56eef1a316 (patch) | |
tree | 64c78e6e7d910bd51c05d55d5340da94d2bc5eb3 /src | |
parent | 98b4edf75ffe0fd57caece27701250e42c901ff3 (diff) | |
download | gitblit-7d97c619b1c067f971cfe0fda2ca8a56eef1a316.tar.gz gitblit-7d97c619b1c067f971cfe0fda2ca8a56eef1a316.zip |
Made LucenePage stateless
Diffstat (limited to 'src')
-rw-r--r-- | src/com/gitblit/wicket/pages/LucenePage.html | 2 | ||||
-rw-r--r-- | src/com/gitblit/wicket/pages/LucenePage.java | 134 |
2 files changed, 71 insertions, 65 deletions
diff --git a/src/com/gitblit/wicket/pages/LucenePage.html b/src/com/gitblit/wicket/pages/LucenePage.html index 6999d783..0abe3d15 100644 --- a/src/com/gitblit/wicket/pages/LucenePage.html +++ b/src/com/gitblit/wicket/pages/LucenePage.html @@ -17,7 +17,7 @@ <div class="span9">
<div>
<h3><wicket:message key="gb.query"></wicket:message></h3>
- <input class="span8" wicket:id="fragment" placeholder="enter search text"></input>
+ <input class="span8" wicket:id="query" placeholder="enter search text"></input>
<button class="btn btn-primary" type="submit" value="Search"><wicket:message key="gb.search"></wicket:message></button>
</div>
<div style="margin-top:10px;">
diff --git a/src/com/gitblit/wicket/pages/LucenePage.java b/src/com/gitblit/wicket/pages/LucenePage.java index f72aeda2..dc740125 100644 --- a/src/com/gitblit/wicket/pages/LucenePage.java +++ b/src/com/gitblit/wicket/pages/LucenePage.java @@ -33,6 +33,7 @@ import org.eclipse.jgit.lib.Repository; import com.gitblit.Constants.SearchType;
import com.gitblit.GitBlit;
import com.gitblit.models.SearchResult;
+import com.gitblit.utils.ArrayUtils;
import com.gitblit.utils.LuceneUtils;
import com.gitblit.utils.StringUtils;
import com.gitblit.wicket.WicketUtils;
@@ -53,18 +54,73 @@ public class LucenePage extends RootPage { private void setup(PageParameters params) {
setupPage("", "");
- String repository = null;
- String value = null;
- com.gitblit.Constants.SearchType searchType = null;
+ // default values
+ ArrayList<String> repositories = new ArrayList<String>();
+ String query = "";
if (params != null) {
- repository = WicketUtils.getRepositoryName(params);
- value = WicketUtils.getSearchString(params);
- String type = WicketUtils.getSearchType(params);
- searchType = com.gitblit.Constants.SearchType.forName(type);
+ String repository = WicketUtils.getRepositoryName(params);
+ if (!StringUtils.isEmpty(repository)) {
+ repositories.add(repository);
+ }
+
+ if (params.containsKey("repositories")) {
+ String value = params.getString("repositories", "");
+ List<String> list = StringUtils.getStringsFromValue(value);
+ repositories.addAll(list);
+ }
+
+ if (params.containsKey("query")) {
+ query = params.getString("query", "");
+ } else {
+ String value = WicketUtils.getSearchString(params);
+ String type = WicketUtils.getSearchType(params);
+ com.gitblit.Constants.SearchType searchType = com.gitblit.Constants.SearchType.forName(type);
+ if (!StringUtils.isEmpty(value)) {
+ if (searchType == SearchType.COMMIT) {
+ query = "type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"";
+ } else {
+ query = searchType.name().toLowerCase() + ":\"" + value + "\"";
+ }
+ }
+ }
}
+ // search form
+ final Model<String> queryModel = new Model<String>(query);
+ final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(repositories);
+ StatelessForm<Void> form = new StatelessForm<Void>("searchForm") {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void onSubmit() {
+ String q = queryModel.getObject();
+ if (StringUtils.isEmpty(q)) {
+ error("Query is empty!");
+ return;
+ }
+ if (repositoriesModel.getObject().size() == 0) {
+ error("Please select one or more repositories!");
+ return;
+ }
+ PageParameters params = new PageParameters();
+ params.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
+ params.put("query", queryModel.getObject());
+ setResponsePage(LucenePage.class, params);
+ }
+ };
+ ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", repositoriesModel, GitBlit.self().getRepositoryList());
+ selections.setMaxRows(10);
+ form.add(selections);
+ form.add(new TextField<String>("query", queryModel));
+ add(form);
+
+ // execute search
final List<SearchResult> results = new ArrayList<SearchResult>();
+ results.addAll(search(repositories, query));
+
+ // search results view
ListDataProvider<SearchResult> resultsDp = new ListDataProvider<SearchResult>(results);
final DataView<SearchResult> resultsView = new DataView<SearchResult>("searchResults", resultsDp) {
private static final long serialVersionUID = 1L;
@@ -99,69 +155,19 @@ public class LucenePage extends RootPage { item.add(new Label("author", sr.author));
item.add(WicketUtils.createTimestampLabel("date", sr.date, getTimeZone()));
}
- };
-
- // initial query
- final Model<String> fragment = new Model<String>();
- if (!StringUtils.isEmpty(value)) {
- if (searchType == SearchType.COMMIT) {
- fragment.setObject("type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"");
- } else {
- fragment.setObject(searchType.name().toLowerCase() + ":\"" + value + "\"");
- }
- }
-
- // selected repositories
- final Model<ArrayList<String>> repositories = new Model<ArrayList<String>>();
- if (!StringUtils.isEmpty(repository)) {
- ArrayList<String> list = new ArrayList<String>();
- list.add(repository);
- repositories.setObject(list);
- }
-
- // search form
- StatelessForm<Void> form = new StatelessForm<Void>("searchForm") {
-
- private static final long serialVersionUID = 1L;
-
- @Override
- public void onSubmit() {
- String f = fragment.getObject();
- if (StringUtils.isEmpty(f)) {
- error("Query is empty!");
- return;
- }
- if (repositories.getObject().size() == 0) {
- error("Please select one or more repositories!");
- return;
- }
- results.clear();
- results.addAll(search(repositories, fragment));
- resultsView.setVisible(true);
- }
};
- ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", repositories, GitBlit.self().getRepositoryList());
- selections.setMaxRows(10);
- form.add(selections);
- form.add(new TextField<String>("fragment", fragment));
- add(form);
- if (!StringUtils.isEmpty(repository) && !StringUtils.isEmpty(fragment.getObject())) {
- // search is defined by url parameters
- results.clear();
- results.addAll(search(repositories, fragment));
- add(resultsView.setVisible(true));
- } else {
- // no pre-defined search
- add(resultsView.setVisible(false));
- }
+ add(resultsView.setVisible(results.size() > 0));
}
- private List<SearchResult> search(Model<ArrayList<String>> repositories, Model<String> fragment) {
+ private List<SearchResult> search(List<String> repositories, String query) {
+ if (ArrayUtils.isEmpty(repositories) || StringUtils.isEmpty(query)) {
+ return new ArrayList<SearchResult>();
+ }
List<Repository> repos = new ArrayList<Repository>();
- for (String r : repositories.getObject()) {
+ for (String r : repositories) {
repos.add(GitBlit.self().getRepository(r));
}
- List<SearchResult> srs = LuceneUtils.search(fragment.getObject(), 100, repos.toArray(new Repository[repos.size()]));
+ List<SearchResult> srs = LuceneUtils.search(query, 100, repos.toArray(new Repository[repos.size()]));
for (Repository r : repos) {
r.close();
}
|