From a502d96a860456ec5e8c96761db70f7cabb74751 Mon Sep 17 00:00:00 2001
From: Paul Martin <paul@paulsputer.com>
Date: Sat, 30 Apr 2016 04:19:14 -0400
Subject: [PATCH] Merge pull request #1073 from gitblit/1062-DocEditorUpdates

---
 src/main/java/com/gitblit/wicket/pages/LuceneSearchPage.java |   92 +++++++++++++++++++++++++---------------------
 1 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/src/main/java/com/gitblit/wicket/pages/LuceneSearchPage.java b/src/main/java/com/gitblit/wicket/pages/LuceneSearchPage.java
index 79795ff..1d81061 100644
--- a/src/main/java/com/gitblit/wicket/pages/LuceneSearchPage.java
+++ b/src/main/java/com/gitblit/wicket/pages/LuceneSearchPage.java
@@ -17,11 +17,14 @@
 
 import java.text.MessageFormat;
 import java.util.ArrayList;
+import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.PageParameters;
 import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.CheckBox;
 import org.apache.wicket.markup.html.form.ListMultipleChoice;
 import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.panel.Fragment;
@@ -32,7 +35,6 @@
 import org.eclipse.jgit.lib.Constants;
 
 import com.gitblit.Constants.SearchType;
-import com.gitblit.GitBlit;
 import com.gitblit.Keys;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.SearchResult;
@@ -60,12 +62,23 @@
 
 	private void setup(PageParameters params) {
 		setupPage("", "");
-		
+
 		// default values
-		ArrayList<String> repositories = new ArrayList<String>();				
+		ArrayList<String> repositories = new ArrayList<String>();
 		String query = "";
+		boolean allRepos = false;
+
 		int page = 1;
-		int pageSize = GitBlit.getInteger(Keys.web.itemsPerPage, 50);
+		int pageSize = app().settings().getInteger(Keys.web.itemsPerPage, 50);
+
+		// display user-accessible selections
+		UserModel user = GitBlitWebSession.get().getUser();
+		List<String> availableRepositories = new ArrayList<String>();
+		for (RepositoryModel model : app().repositories().getRepositoryModels(user)) {
+			if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {
+				availableRepositories.add(model.name);
+			}
+		}
 
 		if (params != null) {
 			String repository = WicketUtils.getRepositoryName(params);
@@ -74,38 +87,35 @@
 			}
 
 			page = WicketUtils.getPage(params);
-			
+
 			if (params.containsKey("repositories")) {
 				String value = params.getString("repositories", "");
-				List<String> list = StringUtils.getStringsFromValue(value);			
+				List<String> list = StringUtils.getStringsFromValue(value);
 				repositories.addAll(list);
 			}
 
+			allRepos = params.getAsBoolean("allrepos", false);
+			if (allRepos) {
+				repositories.addAll(availableRepositories);
+			}
+
 			if (params.containsKey("query")) {
-				query = params.getString("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 + "\"";	
+						query = "type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"";
 					} else {
 						query = searchType.name().toLowerCase() + ":\"" + value + "\"";
 					}
 				}
 			}
 		}
-		
-		// display user-accessible selections
-		UserModel user = GitBlitWebSession.get().getUser();
-		List<String> availableRepositories = new ArrayList<String>();
-		for (RepositoryModel model : GitBlit.self().getRepositoryModels(user)) {
-			if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {
-				availableRepositories.add(model.name);
-			}
-		}
-		boolean luceneEnabled = GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true);
+
+		boolean luceneEnabled = app().settings().getBoolean(Keys.web.allowLuceneIndexing, true);
 		if (luceneEnabled) {
 			if (availableRepositories.size() == 0) {
 				info(getString("gb.noIndexedRepositoriesWarning"));
@@ -115,18 +125,20 @@
 		}
 
 		// enforce user-accessible repository selections
-		ArrayList<String> searchRepositories = new ArrayList<String>();
+		Set<String> uniqueRepositories = new LinkedHashSet<String>();
 		for (String selectedRepository : repositories) {
 			if (availableRepositories.contains(selectedRepository)) {
-				searchRepositories.add(selectedRepository);
+				uniqueRepositories.add(selectedRepository);
 			}
 		}
-		
+		ArrayList<String> searchRepositories = new ArrayList<String>(uniqueRepositories);
+
 		// search form
 		final Model<String> queryModel = new Model<String>(query);
 		final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(searchRepositories);
+		final Model<Boolean> allreposModel = new Model<Boolean>(allRepos);
 		SessionlessForm<Void> form = new SessionlessForm<Void>("searchForm", getClass()) {
-			
+
 			private static final long serialVersionUID = 1L;
 
 			@Override
@@ -135,32 +147,34 @@
 				if (StringUtils.isEmpty(q)) {
 					error(getString("gb.undefinedQueryWarning"));
 					return;
-				}				
-				if (repositoriesModel.getObject().size() == 0) {
+				}
+				if (repositoriesModel.getObject().size() == 0 && !allreposModel.getObject()) {
 					error(getString("gb.noSelectedRepositoriesWarning"));
 					return;
 				}
 				PageParameters params = new PageParameters();
 				params.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
 				params.put("query", queryModel.getObject());
+				params.put("allrepos", allreposModel.getObject());
 				LuceneSearchPage page = new LuceneSearchPage(params);
 				setResponsePage(page);
 			}
 		};
-				
-		ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", 
+
+		ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories",
 				repositoriesModel, availableRepositories, new StringChoiceRenderer());
 		selections.setMaxRows(8);
 		form.add(selections.setEnabled(luceneEnabled));
 		form.add(new TextField<String>("query", queryModel).setEnabled(luceneEnabled));
+		form.add(new CheckBox("allrepos", allreposModel));
 		add(form.setEnabled(luceneEnabled));
-				
+
 		// execute search
 		final List<SearchResult> results = new ArrayList<SearchResult>();
 		if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
-			results.addAll(GitBlit.self().search(query, page, pageSize, searchRepositories));
+			results.addAll(app().repositories().search(query, page, pageSize, searchRepositories));
 		}
-		
+
 		// results header
 		if (results.size() == 0) {
 			if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
@@ -176,11 +190,12 @@
 					results.get(0).hitId, results.get(results.size() - 1).hitId, results.get(0).totalHits)).
 					setRenderBodyOnly(true));
 		}
-		
+
 		// 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;
+			@Override
 			public void populateItem(final Item<SearchResult> item) {
 				final SearchResult sr = item.getModelObject();
 				switch(sr.type) {
@@ -198,11 +213,12 @@
 					ListDataProvider<String> tagsDp = new ListDataProvider<String>(tags);
 					final DataView<String> tagsView = new DataView<String>("tag", tagsDp) {
 						private static final long serialVersionUID = 1L;
+						@Override
 						public void populateItem(final Item<String> item) {
 							String tag = item.getModelObject();
 							Component c = new LinkPanel("tagLink", null, tag, TagPage.class,
 									WicketUtils.newObjectParameter(sr.repository, Constants.R_TAGS + tag));
-							WicketUtils.setCssClass(c, "tagRef");							
+							WicketUtils.setCssClass(c, "tagRef");
 							item.add(c);
 						}
 					};
@@ -215,14 +231,6 @@
 					WicketUtils.setHtmlTooltip(icon, "blob");
 					item.add(icon);
 					item.add(new LinkPanel("summary", null, sr.path, BlobPage.class, WicketUtils.newPathParameter(sr.repository, sr.branch, sr.path)));
-					item.add(new Label("tags").setVisible(false));
-					break;
-				}
-				case issue: {
-					Label icon = WicketUtils.newIcon("type", "icon-file");
-					WicketUtils.setHtmlTooltip(icon, "issue");
-					item.add(icon);
-					item.add(new Label("summary", "issue: " + sr.issueId));
 					item.add(new Label("tags").setVisible(false));
 					break;
 				}
@@ -239,18 +247,18 @@
 			}
 		};
 		add(resultsView.setVisible(results.size() > 0));
-		
+
 		PageParameters pagerParams = new PageParameters();
 		pagerParams.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
 		pagerParams.put("query", queryModel.getObject());
-		
+
 		boolean showPager = false;
 		int totalPages = 0;
 		if (results.size() > 0) {
 			totalPages = (results.get(0).totalHits / pageSize) + (results.get(0).totalHits % pageSize > 0 ? 1 : 0);
 			showPager = results.get(0).totalHits > pageSize;
 		}
-		
+
 		add(new PagerPanel("topPager", page, totalPages, LuceneSearchPage.class, pagerParams).setVisible(showPager));
 		add(new PagerPanel("bottomPager", page, totalPages, LuceneSearchPage.class, pagerParams).setVisible(showPager));
 	}

--
Gitblit v1.9.1