]> source.dussan.org Git - gitblit.git/commitdiff
Integrated selectable days back filtering in filters menu
authorJames Moger <james.moger@gitblit.com>
Fri, 30 Dec 2011 00:35:40 +0000 (19:35 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 30 Dec 2011 00:35:40 +0000 (19:35 -0500)
src/com/gitblit/wicket/GitBlitWebApp.properties
src/com/gitblit/wicket/PageRegistration.java
src/com/gitblit/wicket/pages/ActivityPage.java
src/com/gitblit/wicket/pages/RepositoriesPage.java
src/com/gitblit/wicket/pages/RootPage.java
src/com/gitblit/wicket/panels/DropDownMenu.java

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