]> source.dussan.org Git - gitblit.git/commitdiff
Made LucenePage stateless
authorJames Moger <james.moger@gitblit.com>
Tue, 13 Mar 2012 22:53:27 +0000 (18:53 -0400)
committerJames Moger <james.moger@gitblit.com>
Tue, 13 Mar 2012 22:53:27 +0000 (18:53 -0400)
src/com/gitblit/wicket/pages/LucenePage.html
src/com/gitblit/wicket/pages/LucenePage.java

index 6999d783b82322ef1e5177f87fa020396f83c659..0abe3d15bf0e998914e6bc6e295d1045c346fcfb 100644 (file)
@@ -17,7 +17,7 @@
                        <div class="span9">\r
                                <div>\r
                                        <h3><wicket:message key="gb.query"></wicket:message></h3>\r
-                                       <input class="span8" wicket:id="fragment" placeholder="enter search text"></input>\r
+                                       <input class="span8" wicket:id="query" placeholder="enter search text"></input>\r
                                        <button class="btn btn-primary" type="submit" value="Search"><wicket:message key="gb.search"></wicket:message></button>\r
                                </div>\r
                                <div style="margin-top:10px;">\r
index f72aeda27652b3ffc16355845acc7711706f5de9..dc740125e025c4b88ebffb80d32da3b565ee42f8 100644 (file)
@@ -33,6 +33,7 @@ import org.eclipse.jgit.lib.Repository;
 import com.gitblit.Constants.SearchType;\r
 import com.gitblit.GitBlit;\r
 import com.gitblit.models.SearchResult;\r
+import com.gitblit.utils.ArrayUtils;\r
 import com.gitblit.utils.LuceneUtils;\r
 import com.gitblit.utils.StringUtils;\r
 import com.gitblit.wicket.WicketUtils;\r
@@ -53,18 +54,73 @@ public class LucenePage extends RootPage {
        private void setup(PageParameters params) {\r
                setupPage("", "");\r
                \r
-               String repository = null;               \r
-               String value = null;\r
-               com.gitblit.Constants.SearchType searchType = null;\r
+               // default values\r
+               ArrayList<String> repositories = new ArrayList<String>();                               \r
+               String query = "";\r
 \r
                if (params != null) {\r
-                       repository = WicketUtils.getRepositoryName(params);\r
-                       value = WicketUtils.getSearchString(params);\r
-                       String type = WicketUtils.getSearchType(params);\r
-                       searchType = com.gitblit.Constants.SearchType.forName(type);\r
+                       String repository = WicketUtils.getRepositoryName(params);\r
+                       if (!StringUtils.isEmpty(repository)) {\r
+                               repositories.add(repository);\r
+                       }\r
+                       \r
+                       if (params.containsKey("repositories")) {\r
+                               String value = params.getString("repositories", "");\r
+                               List<String> list = StringUtils.getStringsFromValue(value);                     \r
+                               repositories.addAll(list);\r
+                       }\r
+\r
+                       if (params.containsKey("query")) {\r
+                               query = params.getString("query", "");  \r
+                       } else {\r
+                               String value = WicketUtils.getSearchString(params);\r
+                               String type = WicketUtils.getSearchType(params);\r
+                               com.gitblit.Constants.SearchType searchType = com.gitblit.Constants.SearchType.forName(type);\r
+                               if (!StringUtils.isEmpty(value)) {\r
+                                       if (searchType == SearchType.COMMIT) {\r
+                                               query = "type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"";   \r
+                                       } else {\r
+                                               query = searchType.name().toLowerCase() + ":\"" + value + "\"";\r
+                                       }\r
+                               }\r
+                       }\r
                }\r
                \r
+               // search form\r
+               final Model<String> queryModel = new Model<String>(query);\r
+               final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(repositories);\r
+               StatelessForm<Void> form = new StatelessForm<Void>("searchForm") {\r
+                       \r
+                       private static final long serialVersionUID = 1L;\r
+\r
+                       @Override\r
+                       public void onSubmit() {\r
+                               String q = queryModel.getObject();\r
+                               if (StringUtils.isEmpty(q)) {\r
+                                       error("Query is empty!");\r
+                                       return;\r
+                               }                               \r
+                               if (repositoriesModel.getObject().size() == 0) {\r
+                                       error("Please select one or more repositories!");\r
+                                       return;\r
+                               }\r
+                               PageParameters params = new PageParameters();\r
+                               params.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));\r
+                               params.put("query", queryModel.getObject());\r
+                               setResponsePage(LucenePage.class, params);\r
+                       }\r
+               };\r
+               ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", repositoriesModel, GitBlit.self().getRepositoryList());\r
+               selections.setMaxRows(10);\r
+               form.add(selections);\r
+               form.add(new TextField<String>("query", queryModel));\r
+               add(form);\r
+                               \r
+               // execute search\r
                final List<SearchResult> results = new ArrayList<SearchResult>();\r
+               results.addAll(search(repositories, query));\r
+               \r
+               // search results view\r
                ListDataProvider<SearchResult> resultsDp = new ListDataProvider<SearchResult>(results);\r
                final DataView<SearchResult> resultsView = new DataView<SearchResult>("searchResults", resultsDp) {\r
                        private static final long serialVersionUID = 1L;\r
@@ -99,69 +155,19 @@ public class LucenePage extends RootPage {
                                item.add(new Label("author", sr.author));\r
                                item.add(WicketUtils.createTimestampLabel("date", sr.date, getTimeZone()));\r
                        }\r
-               };              \r
-               \r
-               // initial query\r
-               final Model<String> fragment = new Model<String>();\r
-               if (!StringUtils.isEmpty(value)) {\r
-                       if (searchType == SearchType.COMMIT) {\r
-                               fragment.setObject("type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"");       \r
-                       } else {\r
-                               fragment.setObject(searchType.name().toLowerCase() + ":\"" + value + "\"");\r
-                       }\r
-               }\r
-               \r
-               // selected repositories\r
-               final Model<ArrayList<String>> repositories = new Model<ArrayList<String>>();\r
-               if (!StringUtils.isEmpty(repository)) {                 \r
-                       ArrayList<String> list = new ArrayList<String>();\r
-                       list.add(repository);\r
-                       repositories.setObject(list);\r
-               }\r
-               \r
-               // search form\r
-               StatelessForm<Void> form = new StatelessForm<Void>("searchForm") {\r
-                       \r
-                       private static final long serialVersionUID = 1L;\r
-\r
-                       @Override\r
-                       public void onSubmit() {\r
-                               String f = fragment.getObject();\r
-                               if (StringUtils.isEmpty(f)) {\r
-                                       error("Query is empty!");\r
-                                       return;\r
-                               }                               \r
-                               if (repositories.getObject().size() == 0) {\r
-                                       error("Please select one or more repositories!");\r
-                                       return;\r
-                               }\r
-                               results.clear();\r
-                               results.addAll(search(repositories, fragment));\r
-                               resultsView.setVisible(true);\r
-                       }\r
                };\r
-               ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", repositories, GitBlit.self().getRepositoryList());\r
-               selections.setMaxRows(10);\r
-               form.add(selections);\r
-               form.add(new TextField<String>("fragment", fragment));\r
-               add(form);\r
-               if (!StringUtils.isEmpty(repository) && !StringUtils.isEmpty(fragment.getObject())) {\r
-                       // search is defined by url parameters\r
-                       results.clear();\r
-                       results.addAll(search(repositories, fragment));\r
-                       add(resultsView.setVisible(true));\r
-               } else {\r
-                       // no pre-defined search\r
-                       add(resultsView.setVisible(false));\r
-               }\r
+               add(resultsView.setVisible(results.size() > 0));\r
        }\r
        \r
-       private List<SearchResult> search(Model<ArrayList<String>> repositories, Model<String> fragment) {\r
+       private List<SearchResult> search(List<String> repositories, String query) {\r
+               if (ArrayUtils.isEmpty(repositories) || StringUtils.isEmpty(query)) {\r
+                       return new ArrayList<SearchResult>();\r
+               }\r
                List<Repository> repos = new ArrayList<Repository>();\r
-               for (String r : repositories.getObject()) {\r
+               for (String r : repositories) {\r
                        repos.add(GitBlit.self().getRepository(r));\r
                }\r
-               List<SearchResult> srs = LuceneUtils.search(fragment.getObject(), 100, repos.toArray(new Repository[repos.size()]));\r
+               List<SearchResult> srs = LuceneUtils.search(query, 100, repos.toArray(new Repository[repos.size()]));\r
                for (Repository r : repos) {\r
                        r.close();\r
                }\r