From: James Moger Date: Tue, 13 Mar 2012 22:53:27 +0000 (-0400) Subject: Made LucenePage stateless X-Git-Tag: v0.9.0~66 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7d97c619b1c067f971cfe0fda2ca8a56eef1a316;p=gitblit.git Made LucenePage stateless --- 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 @@

- +
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 repositories = new ArrayList(); + 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 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 queryModel = new Model(query); + final Model> repositoriesModel = new Model>(repositories); + StatelessForm form = new StatelessForm("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 selections = new ListMultipleChoice("repositories", repositoriesModel, GitBlit.self().getRepositoryList()); + selections.setMaxRows(10); + form.add(selections); + form.add(new TextField("query", queryModel)); + add(form); + + // execute search final List results = new ArrayList(); + results.addAll(search(repositories, query)); + + // search results view ListDataProvider resultsDp = new ListDataProvider(results); final DataView resultsView = new DataView("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 fragment = new Model(); - 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> repositories = new Model>(); - if (!StringUtils.isEmpty(repository)) { - ArrayList list = new ArrayList(); - list.add(repository); - repositories.setObject(list); - } - - // search form - StatelessForm form = new StatelessForm("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 selections = new ListMultipleChoice("repositories", repositories, GitBlit.self().getRepositoryList()); - selections.setMaxRows(10); - form.add(selections); - form.add(new TextField("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 search(Model> repositories, Model fragment) { + private List search(List repositories, String query) { + if (ArrayUtils.isEmpty(repositories) || StringUtils.isEmpty(query)) { + return new ArrayList(); + } List repos = new ArrayList(); - for (String r : repositories.getObject()) { + for (String r : repositories) { repos.add(GitBlit.self().getRepository(r)); } - List srs = LuceneUtils.search(fragment.getObject(), 100, repos.toArray(new Repository[repos.size()])); + List srs = LuceneUtils.search(query, 100, repos.toArray(new Repository[repos.size()])); for (Repository r : repos) { r.close(); }