From b4cb1af3222587a6c7ead2f473eb84e23c75c727 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 22 May 2013 18:56:37 +0200 Subject: [PATCH] SONAR-4301 Set maxResultsReached parameter from IssueDao --- .../core/issue/DefaultIssueQueryResult.java | 178 ++++++++++++++++++ .../org/sonar/core/issue/db/IssueDao.java | 44 ++--- .../org/sonar/core/issue/db/IssueMapper.java | 2 - .../org/sonar/core/issue/db/IssueMapper.xml | 5 - .../org/sonar/core/issue/db/IssueDaoTest.java | 50 ++--- .../server/issue/DefaultIssueFinder.java | 142 ++------------ .../server/issue/DefaultIssueFinderTest.java | 28 +-- .../server/issue/IssuesFinderSortTest.java | 41 ++-- 8 files changed, 256 insertions(+), 234 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueQueryResult.java diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueQueryResult.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueQueryResult.java new file mode 100644 index 00000000000..5b2b562f8fc --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssueQueryResult.java @@ -0,0 +1,178 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.core.issue; + +import com.google.common.collect.Maps; +import org.sonar.api.component.Component; +import org.sonar.api.issue.ActionPlan; +import org.sonar.api.issue.Issue; +import org.sonar.api.issue.IssueQueryResult; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rules.Rule; +import org.sonar.api.user.User; +import org.sonar.api.utils.Paging; + +import javax.annotation.CheckForNull; + +import java.util.Collection; +import java.util.List; +import java.util.Map; + +public class DefaultIssueQueryResult implements IssueQueryResult { + + private List issues; + private final Map rulesByKey = Maps.newHashMap(); + private final Map componentsByKey = Maps.newHashMap(); + private final Map projectsByKey = Maps.newHashMap(); + private final Map actionPlansByKey = Maps.newHashMap(); + private final Map usersByLogin = Maps.newHashMap(); + private boolean securityExclusions; + private boolean maxResultsReached; + private Paging paging; + + public DefaultIssueQueryResult setIssues(List issues){ + this.issues = issues; + return this; + } + + public DefaultIssueQueryResult addRules(Collection rules){ + for (Rule rule : rules) { + rulesByKey.put(rule.ruleKey(), rule); + } + return this; + } + + public DefaultIssueQueryResult addComponents(Collection components){ + for (Component component : components) { + componentsByKey.put(component.key(), component); + } + return this; + } + + public DefaultIssueQueryResult addProjects(Collection projects){ + for (Component project : projects) { + projectsByKey.put(project.key(), project); + } + return this; + } + + public DefaultIssueQueryResult addActionPlans(Collection actionPlans){ + for (ActionPlan actionPlan : actionPlans) { + actionPlansByKey.put(actionPlan.key(), actionPlan); + } + return this; + } + + public DefaultIssueQueryResult addUsers(Collection users){ + for (User user : users) { + usersByLogin.put(user.login(), user); + } + return this; + } + + public DefaultIssueQueryResult setSecurityExclusions(boolean securityExclusions){ + this.securityExclusions = securityExclusions; + return this; + } + + public DefaultIssueQueryResult setMaxResultsReached(boolean maxResultsReached){ + this.maxResultsReached = maxResultsReached; + return this; + } + + public DefaultIssueQueryResult setPaging(Paging paging){ + this.paging = paging; + return this; + } + + @Override + public List issues() { + return issues; + } + + @Override + public Rule rule(Issue issue) { + return rulesByKey.get(issue.ruleKey()); + } + + @Override + public Collection rules() { + return rulesByKey.values(); + } + + @Override + public Component component(Issue issue) { + return componentsByKey.get(issue.componentKey()); + } + + @Override + public Collection components() { + return componentsByKey.values(); + } + + @Override + public Component project(Issue issue) { + return projectsByKey.get(issue.projectKey()); + } + + @Override + public Collection projects() { + return projectsByKey.values(); + } + + @Override + public ActionPlan actionPlan(Issue issue) { + return actionPlansByKey.get(issue.actionPlanKey()); + } + + @Override + public Collection actionPlans() { + return actionPlansByKey.values(); + } + + @Override + public Collection users() { + return usersByLogin.values(); + } + + @Override + @CheckForNull + public User user(String login) { + return usersByLogin.get(login); + } + + @Override + public boolean securityExclusions() { + return securityExclusions; + } + + @Override + public boolean maxResultsReached() { + return maxResultsReached; + } + + @Override + public Paging paging() { + return paging; + } + + +} diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java index 89043f3791c..aeeb37bf502 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueDao.java @@ -28,6 +28,7 @@ import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import org.sonar.api.issue.IssueQuery; +import org.sonar.core.issue.DefaultIssueQueryResult; import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; @@ -83,28 +84,11 @@ public class IssueDao implements BatchComponent, ServerComponent { } @VisibleForTesting - List selectIssueAndComponentIds(IssueQuery query) { + List selectIssueAndProjectIds(final IssueQuery query, final DefaultIssueQueryResult defaultIssueQueryResult, + final Collection authorizedRootProjectIds) { SqlSession session = mybatis.openSession(); try { - return selectIssueAndComponentIds(query, session); - } finally { - MyBatis.closeQuietly(session); - } - } - - /** - * The returned IssueDto list contains only the issue id and the resource id - */ - public List selectIssueAndComponentIds(IssueQuery query, SqlSession session) { - IssueMapper mapper = session.getMapper(IssueMapper.class); - return mapper.selectIssueAndComponentIds(query); - } - - @VisibleForTesting - List selectIssueAndProjectIds(IssueQuery query, Collection authorizedRootProjectIds, Integer maxResults) { - SqlSession session = mybatis.openSession(); - try { - return selectIssueAndProjectIds(query, authorizedRootProjectIds, maxResults, session); + return selectIssueAndProjectIds(query, defaultIssueQueryResult, authorizedRootProjectIds, session); } finally { MyBatis.closeQuietly(session); } @@ -113,28 +97,26 @@ public class IssueDao implements BatchComponent, ServerComponent { /** * The returned IssueDto list contains only the issue id, the project id and the sort column */ - public List selectIssueAndProjectIds(final IssueQuery query, final Collection authorizedRootProjectIds, SqlSession session) { - return selectIssueAndProjectIds(query, authorizedRootProjectIds, query.maxResults(), session); - } - - private List selectIssueAndProjectIds(final IssueQuery query, final Collection authorizedRootProjectIds, final Integer maxResults, SqlSession session) { - final List issues = newArrayList(); + public List selectIssueAndProjectIds(final IssueQuery query, final DefaultIssueQueryResult defaultIssueQueryResult, + final Collection authorizedRootProjectIds, SqlSession sqlSession){ + final List authorizedIssues = newArrayList(); ResultHandler resultHandler = new ResultHandler(){ @Override public void handleResult(ResultContext context) { IssueDto issueDto = (IssueDto) context.getResultObject(); if (authorizedRootProjectIds.contains(issueDto.getProjectId())) { - issues.add(issueDto); + authorizedIssues.add(issueDto); } else { - // reject because user not authorized + defaultIssueQueryResult.setSecurityExclusions(true); } - if (issues.size() >= maxResults) { + if (authorizedIssues.size() >= query.maxResults()) { + defaultIssueQueryResult.setMaxResultsReached(true); context.stop(); } } }; - session.select("selectIssueAndProjectIds", query, resultHandler); - return issues; + sqlSession.select("selectIssueAndProjectIds", query, resultHandler); + return authorizedIssues; } @VisibleForTesting diff --git a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java index 478a66da894..a08f85530a9 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/db/IssueMapper.java @@ -27,8 +27,6 @@ public interface IssueMapper { IssueDto selectByKey(String key); - List selectIssueAndComponentIds(IssueQuery query); - List select(IssueQuery query); List selectNonClosedIssues(int rootComponentId); 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 19aea4a64af..dce3b71c648 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 @@ -146,11 +146,6 @@ - -