From 5c089eaaa71104b875ea6f79424b57897666fdb0 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 13 Jun 2013 11:17:36 +0200 Subject: [PATCH] SONAR-4383 Replace ruby issue_filter Ruby by Java IssueQuery in issues search page --- .../org/sonar/core/issue/db/IssueMapper.xml | 14 +++--- .../issue/InternalRubyIssueService.java | 48 ++++++++++++++++--- .../server/issue/IssueFilterService.java | 29 +++++++---- .../server/issue/PublicRubyIssueService.java | 3 +- .../app/controllers/issues_controller.rb | 24 ++++++---- .../WEB-INF/app/helpers/issues_helper.rb | 10 ++-- .../WEB-INF/app/views/issues/_list.html.erb | 32 ++++++------- .../app/views/issues/_sidebar.html.erb | 20 ++++---- .../WEB-INF/app/views/issues/search.html.erb | 4 +- .../issue/InternalRubyIssueServiceTest.java | 3 +- .../server/issue/IssueFilterServiceTest.java | 30 ++++++++++++ 11 files changed, 150 insertions(+), 67 deletions(-) diff --git a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml index 61246d6c239..11d5244da84 100644 --- a/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/issue/db/IssueMapper.xml @@ -240,22 +240,22 @@ r.plugin_name=#{rule.repository} and r.plugin_rule_key=#{rule.rule}) - + and i.kee in #{key} - + and i.severity in #{severity} - + and i.status in #{status} - + and i.resolution in #{resolution} @@ -268,12 +268,12 @@ and i.resolution is null - + and i.reporter in #{reporter} - + and i.assignee in #{assignee} @@ -294,7 +294,7 @@ and i.action_plan_key is null - + and i.action_plan_key in #{action_plan} diff --git a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java index 09a52b681f0..e944d5a325b 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java @@ -24,10 +24,7 @@ import com.google.common.base.Objects; import com.google.common.base.Strings; import org.apache.commons.lang.StringUtils; import org.sonar.api.ServerComponent; -import org.sonar.api.issue.ActionPlan; -import org.sonar.api.issue.Issue; -import org.sonar.api.issue.IssueComment; -import org.sonar.api.issue.IssueQuery; +import org.sonar.api.issue.*; import org.sonar.api.issue.action.Action; import org.sonar.api.issue.internal.DefaultIssue; import org.sonar.api.rule.RuleKey; @@ -36,6 +33,7 @@ import org.sonar.api.utils.SonarException; import org.sonar.core.issue.ActionPlanStats; import org.sonar.core.issue.DefaultActionPlan; import org.sonar.core.issue.DefaultIssueBuilder; +import org.sonar.core.issue.DefaultIssueFilter; import org.sonar.core.issue.workflow.Transition; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceDto; @@ -62,11 +60,13 @@ public class InternalRubyIssueService implements ServerComponent { private final IssueStatsFinder issueStatsFinder; private final ResourceDao resourceDao; private final ActionService actionService; + private final IssueFilterService issueFilterService; public InternalRubyIssueService(IssueService issueService, IssueCommentService commentService, IssueChangelogService changelogService, ActionPlanService actionPlanService, - IssueStatsFinder issueStatsFinder, ResourceDao resourceDao, ActionService actionService) { + IssueStatsFinder issueStatsFinder, ResourceDao resourceDao, ActionService actionService, + IssueFilterService issueFilterService) { this.issueService = issueService; this.commentService = commentService; this.changelogService = changelogService; @@ -74,6 +74,7 @@ public class InternalRubyIssueService implements ServerComponent { this.issueStatsFinder = issueStatsFinder; this.resourceDao = resourceDao; this.actionService = actionService; + this.issueFilterService = issueFilterService; } public IssueStatsFinder.IssueStatsResult findIssueAssignees(Map params) { @@ -347,8 +348,6 @@ public class InternalRubyIssueService implements ServerComponent { } public Result executeAction(String issueKey, String actionKey) { - // TODO verify authorization - Result result = Result.of(); try { result.set(actionService.execute(issueKey, actionKey, UserSession.get())); @@ -370,4 +369,39 @@ public class InternalRubyIssueService implements ServerComponent { return PublicRubyIssueService.toQuery(props); } + /** + * Execute issue filter + */ + public IssueQueryResult execute(IssueQuery issueQuery) { + return issueFilterService.execute(issueQuery); + } + + /** + * Create issue filter + */ + public void createIssueFilter(Map params) { + Result result = Result.of(); + try { + // mandatory parameters + String name = params.get("name"); + String description = params.get("description"); + String data = params.get("data"); + + if (result.ok()) { + DefaultIssueFilter defaultIssueFilter = new DefaultIssueFilter() + .setName(name) + .setDescription(description) + .setData(data) + ; + defaultIssueFilter = issueFilterService.create(defaultIssueFilter, UserSession.get()); + result.set(defaultIssueFilter); + } + + } catch (Exception e) { + result.addError(e.getMessage()); + } + + } + + } \ No newline at end of file diff --git a/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java b/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java index 4dabe48119c..d57b32537eb 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/IssueFilterService.java @@ -33,6 +33,7 @@ import org.sonar.core.issue.db.IssueFilterDao; import org.sonar.core.issue.db.IssueFilterDto; import org.sonar.server.user.UserSession; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -49,7 +50,7 @@ public class IssueFilterService implements ServerComponent { this.issueFinder = issueFinder; } - public DefaultIssueFilter create(DefaultIssueFilter issueFilter, IssueQuery issueQuery, UserSession userSession) { + public DefaultIssueFilter create(DefaultIssueFilter issueFilter, UserSession userSession) { // TODO // checkAuthorization(userSession, project, UserRole.ADMIN); issueFilterDao.insert(IssueFilterDto.toIssueFilter(issueFilter)); @@ -90,7 +91,7 @@ public class IssueFilterService implements ServerComponent { @VisibleForTesting Map dataAsMap(String data) { - Map props = newHashMap(); + Map map = newHashMap(); Iterable keyValues = Splitter.on(DefaultIssueFilter.SEPARATOR).split(data); for (String keyValue : keyValues) { @@ -102,19 +103,19 @@ public class IssueFilterService implements ServerComponent { String value = keyValueSplit[1]; String[] listValues = StringUtils.split(value, DefaultIssueFilter.LIST_SEPARATOR); if (listValues.length > 1) { - props.put(key, newArrayList(listValues)); + map.put(key, newArrayList(listValues)); } else { - props.put(key, value); + map.put(key, value); } } - return props; + return map; } @VisibleForTesting - String mapAsdata(Map props) { + String mapAsdata(Map map) { StringBuilder stringBuilder = new StringBuilder(); - for (Map.Entry entries : props.entrySet()){ + for (Map.Entry entries : map.entrySet()){ String key = entries.getKey(); Object value = entries.getValue(); @@ -130,13 +131,21 @@ public class IssueFilterService implements ServerComponent { } else { stringBuilder.append(value); } - for (Object valueList : valuesList) { + for (Iterator valueListIter = valuesList.iterator(); valueListIter.hasNext();) { + Object valueList = valueListIter.next(); stringBuilder.append(valueList); - stringBuilder.append(DefaultIssueFilter.LIST_SEPARATOR); + if (valueListIter.hasNext()) { + stringBuilder.append(DefaultIssueFilter.LIST_SEPARATOR); + } } - stringBuilder.append(DefaultIssueFilter.SEPARATOR); } + + if (stringBuilder.length() > 0) { + // Delete useless last separator character + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + return stringBuilder.toString(); } diff --git a/sonar-server/src/main/java/org/sonar/server/issue/PublicRubyIssueService.java b/sonar-server/src/main/java/org/sonar/server/issue/PublicRubyIssueService.java index 62e6763bc14..4d4b88df9b0 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/PublicRubyIssueService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/PublicRubyIssueService.java @@ -21,6 +21,7 @@ package org.sonar.server.issue; import com.google.common.base.Function; import com.google.common.base.Splitter; +import com.google.common.base.Strings; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import org.sonar.api.issue.IssueFinder; @@ -92,7 +93,7 @@ public class PublicRubyIssueService implements RubyIssueService { .pageSize(RubyUtils.toInteger(props.get("pageSize"))) .pageIndex(RubyUtils.toInteger(props.get("pageIndex"))); String sort = (String) props.get("sort"); - if (sort != null) { + if (!Strings.isNullOrEmpty(sort)) { builder.sort(sort); builder.asc(RubyUtils.toBoolean(props.get("asc"))); } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb index fb037453079..da523e067fa 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/issues_controller.rb @@ -27,21 +27,29 @@ class IssuesController < ApplicationController end def search - @filter = IssueFilter.new - @filter.criteria=criteria_params - @filter.execute + init_results + + @criteria_params = params.merge({:controller => nil, :action => nil, :search => nil, :widget_id => nil, :edit => nil}) + @criteria_params['pageSize'] = 100 + @issue_query = Internal.issues.toQuery(@criteria_params) + @issues_result = Internal.issues.execute(@issue_query) + @paging = @issues_result.paging + @issues = @issues_result.issues end - private + def init_results + @issues_result = nil + @paging = nil + @issues = nil + #criteria['pageSize'] = 100 + self + end + def init @options_for_statuses = Internal.issues.listStatus().map {|s| [message('issue.status.' + s), s]} @options_for_resolutions = Internal.issues.listResolutions().map {|s| [message('issue.resolution.' + s), s]} end - def criteria_params - params.merge({:controller => nil, :action => nil, :search => nil, :widget_id => nil, :edit => nil}) - end - end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/issues_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/issues_helper.rb index 413e03c6766..5e70609414c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/issues_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/issues_helper.rb @@ -19,12 +19,12 @@ # module IssuesHelper - def column_html(filter, column_label, column_tooltip, sort) - filter_sort = filter.criteria[:sort] - filter_asc = filter.criteria[:asc] == 'true' ? true : false + def column_html(issue_query, issues_result, column_label, column_tooltip, sort) + filter_sort = issue_query.sort + filter_asc = issue_query.asc html = h(column_label) - unless filter.issues_result.maxResultsReached() - html = link_to_function(h(column_label), "refreshList('#{escape_javascript sort}',#{!filter_asc}, #{filter.criteria[:page]||1})", :title => h(column_tooltip)) + unless issues_result.maxResultsReached() + html = link_to_function(h(column_label), "refreshList('#{escape_javascript sort}',#{!filter_asc}, #{issue_query.pageIndex||1})", :title => h(column_tooltip)) if sort == filter_sort html << (filter_asc ? image_tag("asc12.png") : image_tag("desc12.png")) end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/issues/_list.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/issues/_list.html.erb index 0952add6b13..9b93373ad25 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/issues/_list.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/issues/_list.html.erb @@ -1,6 +1,6 @@ <% content_for :script do %>