From bc57cd86db78fccf6b981375425d74bab540ad7b Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 29 Dec 2011 19:35:40 -0500 Subject: [PATCH] Integrated selectable days back filtering in filters menu --- .../gitblit/wicket/GitBlitWebApp.properties | 3 +- src/com/gitblit/wicket/PageRegistration.java | 30 ++++ .../gitblit/wicket/pages/ActivityPage.java | 38 ++++- .../wicket/pages/RepositoriesPage.java | 34 ++++- src/com/gitblit/wicket/pages/RootPage.java | 141 ++++++++++++------ .../gitblit/wicket/panels/DropDownMenu.java | 3 +- 6 files changed, 190 insertions(+), 59 deletions(-) diff --git a/src/com/gitblit/wicket/GitBlitWebApp.properties b/src/com/gitblit/wicket/GitBlitWebApp.properties index a2c81312..2abc5479 100644 --- a/src/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/com/gitblit/wicket/GitBlitWebApp.properties @@ -207,4 +207,5 @@ gb.accessPermissionsDescription = restrict access by users and teams gb.accessPermissionsForUserDescription = set team memberships or grant access to specific restricted repositories gb.accessPermissionsForTeamDescription = set team members and grant access to specific restricted repositories gb.federationRepositoryDescription = share this repository with other Gitblit servers -gb.hookScriptsDescription = run Groovy scripts on pushes to this Gitblit server \ No newline at end of file +gb.hookScriptsDescription = run Groovy scripts on pushes to this Gitblit server +gb.reset = reset \ No newline at end of file diff --git a/src/com/gitblit/wicket/PageRegistration.java b/src/com/gitblit/wicket/PageRegistration.java index f7ddcc5b..6c13b82e 100644 --- a/src/com/gitblit/wicket/PageRegistration.java +++ b/src/com/gitblit/wicket/PageRegistration.java @@ -76,6 +76,7 @@ public class PageRegistration implements Serializable { private static final long serialVersionUID = 1L; + final PageParameters params; final String displayText; final String parameter; final String value; @@ -87,6 +88,7 @@ public class PageRegistration implements Serializable { displayText = null; parameter = null; value = null; + params = null; } /** @@ -97,9 +99,33 @@ public class PageRegistration implements Serializable { * @param value */ public DropDownMenuItem(String displayText, String parameter, String value) { + this(displayText, parameter, value, null); + } + + /** + * Standard Menu Item constructor that preserves aggregate parameters. + * + * @param displayText + * @param parameter + * @param value + */ + public DropDownMenuItem(String displayText, String parameter, String value, + PageParameters params) { this.displayText = displayText; this.parameter = parameter; this.value = value; + if (params == null) { + this.params = new PageParameters(); + } else { + this.params = new PageParameters(params); + } + if (!StringUtils.isEmpty(parameter)) { + if (StringUtils.isEmpty(value)) { + this.params.remove(parameter); + } else { + this.params.put(parameter, value); + } + } } public String formatParameter() { @@ -109,6 +135,10 @@ public class PageRegistration implements Serializable { return parameter + "=" + value; } + public PageParameters getPageParameters() { + return params; + } + public boolean isDivider() { return displayText == null && value == null && parameter == null; } diff --git a/src/com/gitblit/wicket/pages/ActivityPage.java b/src/com/gitblit/wicket/pages/ActivityPage.java index ebdfee37..9b258a72 100644 --- a/src/com/gitblit/wicket/pages/ActivityPage.java +++ b/src/com/gitblit/wicket/pages/ActivityPage.java @@ -30,11 +30,13 @@ import org.apache.wicket.behavior.HeaderContributor; import org.apache.wicket.markup.html.basic.Label; import com.gitblit.GitBlit; +import com.gitblit.Keys; import com.gitblit.models.Activity; import com.gitblit.models.Metric; import com.gitblit.models.RepositoryModel; import com.gitblit.utils.ActivityUtils; import com.gitblit.wicket.PageRegistration; +import com.gitblit.wicket.PageRegistration.DropDownMenuItem; import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.charting.GoogleChart; @@ -94,12 +96,37 @@ public class ActivityPage extends RootPage { add(new ActivityPanel("activityPanel", recentActivity)); } } - + + @Override + protected boolean reusePageParameters() { + return true; + } + @Override protected void addDropDownMenus(List pages) { - DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters", ActivityPage.class); - menu.menuItems.addAll(getFilterMenuItems()); - pages.add(menu); + DropDownMenuRegistration filters = new DropDownMenuRegistration("gb.filters", + ActivityPage.class); + + PageParameters currentParameters = getPageParameters(); + int daysBack = GitBlit.getInteger(Keys.web.activityDuration, 14); + if (currentParameters.containsKey("db")) { + daysBack = currentParameters.getInt("db"); + } + if (daysBack < 1) { + daysBack = 14; + } + + // preserve time filter options on repository choices + filters.menuItems.addAll(getRepositoryFilterItems(new PageParameters("db=" + daysBack))); + + // preserve repository filter options on time choices + filters.menuItems.addAll(getTimeFilterItems(currentParameters)); + + if (filters.menuItems.size() > 0) { + // Reset Filter + filters.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null)); + } + pages.add(filters); } /** @@ -178,7 +205,7 @@ public class ActivityPage extends RootPage { return charts; } - + @Override protected void onBeforeRender() { if (GitBlit.isDebugMode()) { @@ -187,6 +214,7 @@ public class ActivityPage extends RootPage { } super.onBeforeRender(); } + @Override protected void onAfterRender() { if (GitBlit.isDebugMode()) { diff --git a/src/com/gitblit/wicket/pages/RepositoriesPage.java b/src/com/gitblit/wicket/pages/RepositoriesPage.java index 4c50b44c..21efddac 100644 --- a/src/com/gitblit/wicket/pages/RepositoriesPage.java +++ b/src/com/gitblit/wicket/pages/RepositoriesPage.java @@ -35,6 +35,7 @@ import com.gitblit.utils.MarkdownUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.PageRegistration; +import com.gitblit.wicket.PageRegistration.DropDownMenuItem; import com.gitblit.wicket.PageRegistration.DropDownMenuRegistration; import com.gitblit.wicket.WicketUtils; import com.gitblit.wicket.panels.RepositoriesPanel; @@ -51,6 +52,11 @@ public class RepositoriesPage extends RootPage { setup(params); } + @Override + protected boolean reusePageParameters() { + return true; + } + private void setup(PageParameters params) { setupPage("", ""); // check to see if we should display a login message @@ -85,8 +91,29 @@ public class RepositoriesPage extends RootPage { @Override protected void addDropDownMenus(List pages) { - DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters", RepositoriesPage.class); - menu.menuItems.addAll(getFilterMenuItems()); + int daysBack = 0; + PageParameters currentParameters = getPageParameters(); + if (currentParameters != null && currentParameters.containsKey("db")) { + daysBack = currentParameters.getInt("db"); + } + PageParameters params = null; + if (daysBack > 0) { + params = new PageParameters("db=" + daysBack); + } + + DropDownMenuRegistration menu = new DropDownMenuRegistration("gb.filters", + RepositoriesPage.class); + // preserve time filter option on repository choices + menu.menuItems.addAll(getRepositoryFilterItems(params)); + + // preserve repository filter option on time choices + menu.menuItems.addAll(getTimeFilterItems(currentParameters)); + + if (menu.menuItems.size() > 0) { + // Reset Filter + menu.menuItems.add(new DropDownMenuItem(getString("gb.reset"), null, null)); + } + pages.add(menu); } @@ -129,7 +156,7 @@ public class RepositoriesPage extends RootPage { } return message; } - + @Override protected void onBeforeRender() { if (GitBlit.isDebugMode()) { @@ -138,6 +165,7 @@ public class RepositoriesPage extends RootPage { } super.onBeforeRender(); } + @Override protected void onAfterRender() { if (GitBlit.isDebugMode()) { diff --git a/src/com/gitblit/wicket/pages/RootPage.java b/src/com/gitblit/wicket/pages/RootPage.java index f8fd8a8c..686fc725 100644 --- a/src/com/gitblit/wicket/pages/RootPage.java +++ b/src/com/gitblit/wicket/pages/RootPage.java @@ -17,8 +17,12 @@ package com.gitblit.wicket.pages; import java.text.MessageFormat; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; +import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -94,7 +98,8 @@ public abstract class RootPage extends BasePage { // navigation links List pages = new ArrayList(); - pages.add(new PageRegistration("gb.repositories", RepositoriesPage.class, getRootPageParameters())); + pages.add(new PageRegistration("gb.repositories", RepositoriesPage.class, + getRootPageParameters())); pages.add(new PageRegistration("gb.activity", ActivityPage.class, getRootPageParameters())); if (showAdmin) { pages.add(new PageRegistration("gb.users", UsersPage.class)); @@ -163,14 +168,21 @@ public abstract class RootPage extends BasePage { super.setupPage(repositoryName, pageName); } - + private PageParameters getRootPageParameters() { - PageParameters params = getPageParameters(); - if (params != null) { - // remove named repository parameter - params.remove("r"); + if (reusePageParameters()) { + PageParameters params = getPageParameters(); + if (params != null) { + // remove named repository parameter + params.remove("r"); + } + return params; } - return params; + return null; + } + + protected boolean reusePageParameters() { + return false; } private void loginUser(UserModel user) { @@ -195,7 +207,7 @@ public abstract class RootPage extends BasePage { } - protected List getFilterMenuItems() { + protected List getRepositoryFilterItems(PageParameters params) { final UserModel user = GitBlitWebSession.get().getUser(); Set filters = new LinkedHashSet(); List repositories = GitBlit.self().getRepositoryModels(user); @@ -217,7 +229,7 @@ public abstract class RootPage extends BasePage { Collections.sort(sets); for (String set : sets) { filters.add(new DropDownMenuItem(MessageFormat.format("{0} ({1})", set, - setMap.get(set).get()), "set", set)); + setMap.get(set).get()), "set", set, params)); } // divider filters.add(new DropDownMenuItem()); @@ -229,7 +241,7 @@ public abstract class RootPage extends BasePage { Collections.sort(teams); for (TeamModel team : teams) { filters.add(new DropDownMenuItem(MessageFormat.format("{0} ({1})", team.name, - team.repositories.size()), "team", team.name)); + team.repositories.size()), "team", team.name, params)); } // divider filters.add(new DropDownMenuItem()); @@ -243,22 +255,33 @@ public abstract class RootPage extends BasePage { for (String expression : expressions) { if (!StringUtils.isEmpty(expression)) { addedExpression = true; - filters.add(new DropDownMenuItem(null, "x", expression)); + filters.add(new DropDownMenuItem(null, "x", expression, params)); } } // if we added any custom expressions, add a divider if (addedExpression) { filters.add(new DropDownMenuItem()); } - } + } + return new ArrayList(filters); + } - if (filters.size() > 0) { - // add All Repositories - filters.add(new DropDownMenuItem(MessageFormat.format("{0} ({1})", - getString("gb.tokenJurDescription"), repositories.size()), null, null)); + protected List getTimeFilterItems(PageParameters params) { + // days back choices - additive parameters + int daysBack = GitBlit.getInteger(Keys.web.activityDuration, 14); + if (daysBack < 1) { + daysBack = 14; } - - return new ArrayList(filters); + List items = new ArrayList(); + Set choicesSet = new HashSet(Arrays.asList(daysBack, 14, 28, 60, 90, 180)); + List choices = new ArrayList(choicesSet); + Collections.sort(choices); + for (Integer db : choices) { + String txt = "last " + db + (db.intValue() > 1 ? " days" : "day"); + items.add(new DropDownMenuItem(txt, "db", db.toString(), params)); + } + items.add(new DropDownMenuItem()); + return items; } protected List getRepositories(PageParameters params) { @@ -267,74 +290,96 @@ public abstract class RootPage extends BasePage { return GitBlit.self().getRepositoryModels(user); } + boolean hasParameter = false; String repositoryName = WicketUtils.getRepositoryName(params); String set = WicketUtils.getSet(params); String regex = WicketUtils.getRegEx(params); String team = WicketUtils.getTeam(params); + int daysBack = params.getInt("db", 0); - List models = null; + List availableModels = GitBlit.self().getRepositoryModels(user); + Set models = new HashSet(); if (!StringUtils.isEmpty(repositoryName)) { // try named repository - models = new ArrayList(); - RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName); - if (user.canAccessRepository(model)) { - models.add(model); + hasParameter = true; + for (RepositoryModel model : availableModels) { + if (model.name.equalsIgnoreCase(repositoryName)) { + models.add(model); + break; + } } } - // get all user accessible repositories - if (models == null) { - models = GitBlit.self().getRepositoryModels(user); - } - if (!StringUtils.isEmpty(regex)) { // filter the repositories by the regex - List accessible = GitBlit.self().getRepositoryModels(user); - List matchingModels = new ArrayList(); + hasParameter = true; Pattern pattern = Pattern.compile(regex); - for (RepositoryModel aModel : accessible) { - if (pattern.matcher(aModel.name).find()) { - matchingModels.add(aModel); + for (RepositoryModel model : availableModels) { + if (pattern.matcher(model.name).find()) { + models.add(model); } } - models = matchingModels; - } else if (!StringUtils.isEmpty(set)) { + } + + if (!StringUtils.isEmpty(set)) { // filter the repositories by the specified sets + hasParameter = true; List sets = StringUtils.getStringsFromValue(set, ","); - List matchingModels = new ArrayList(); - for (RepositoryModel model : models) { + for (RepositoryModel model : availableModels) { for (String curr : sets) { if (model.federationSets.contains(curr)) { - matchingModels.add(model); + models.add(model); } } } - models = matchingModels; - } else if (!StringUtils.isEmpty(team)) { + } + + if (!StringUtils.isEmpty(team)) { // filter the repositories by the specified teams + hasParameter = true; List teams = StringUtils.getStringsFromValue(team, ","); // need TeamModels first List teamModels = new ArrayList(); for (String name : teams) { - TeamModel model = GitBlit.self().getTeamModel(name); - if (model != null) { - teamModels.add(model); + TeamModel teamModel = GitBlit.self().getTeamModel(name); + if (teamModel != null) { + teamModels.add(teamModel); } } // brute-force our way through finding the matching models - List matchingModels = new ArrayList(); - for (RepositoryModel repositoryModel : models) { + for (RepositoryModel repositoryModel : availableModels) { for (TeamModel teamModel : teamModels) { if (teamModel.hasRepository(repositoryModel.name)) { - matchingModels.add(repositoryModel); + models.add(repositoryModel); } } } - models = matchingModels; } - return models; + + if (!hasParameter) { + models.addAll(availableModels); + } + + // time-filter the list + if (daysBack > 0) { + Calendar cal = Calendar.getInstance(); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.add(Calendar.DATE, -1 * daysBack); + Date threshold = cal.getTime(); + Set timeFiltered = new HashSet(); + for (RepositoryModel model : models) { + if (model.lastChange.after(threshold)) { + timeFiltered.add(model); + } + } + models = timeFiltered; + } + return new ArrayList(models); } } diff --git a/src/com/gitblit/wicket/panels/DropDownMenu.java b/src/com/gitblit/wicket/panels/DropDownMenu.java index 8ad7d4ce..ef6a2fe4 100644 --- a/src/com/gitblit/wicket/panels/DropDownMenu.java +++ b/src/com/gitblit/wicket/panels/DropDownMenu.java @@ -15,7 +15,6 @@ */ package com.gitblit.wicket.panels; -import org.apache.wicket.PageParameters; import org.apache.wicket.markup.html.basic.Label; import org.apache.wicket.markup.html.panel.Panel; import org.apache.wicket.markup.repeater.Item; @@ -46,7 +45,7 @@ public class DropDownMenu extends Panel { WicketUtils.setCssClass(item, "divider"); } else { item.add(new LinkPanel("menuItem", null, entry.toString(), menu.pageClass, - new PageParameters(entry.formatParameter())).setRenderBodyOnly(true)); + entry.getPageParameters()).setRenderBodyOnly(true)); } } }; -- 2.39.5