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
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
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