diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-17 16:32:23 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2015-04-20 11:44:43 +0200 |
commit | 5f49af5dab1f5e76bbe1281855cf52cf369b6c95 (patch) | |
tree | 1881411ff22cdb44572435009805fba7483950d6 /sonar-core | |
parent | 350db49873cf3c7ef2ff53ca8ed1e22651fea5cf (diff) | |
download | sonarqube-5f49af5dab1f5e76bbe1281855cf52cf369b6c95.tar.gz sonarqube-5f49af5dab1f5e76bbe1281855cf52cf369b6c95.zip |
SONAR-6298 Add project count in qprofile search WS response
Diffstat (limited to 'sonar-core')
6 files changed, 113 insertions, 27 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java index 81b7ddbf24b..19af5b22d78 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java @@ -22,6 +22,7 @@ package org.sonar.core.qualityprofile.db; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.sonar.api.ServerComponent; import org.sonar.api.utils.System2; import org.sonar.core.component.ComponentDto; @@ -34,6 +35,7 @@ import javax.annotation.Nullable; import java.util.Date; import java.util.List; +import java.util.Map; public class QualityProfileDao implements ServerComponent, DaoComponent { @@ -47,7 +49,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getByKey(DbSession session, String key) { - return session.getMapper(QualityProfileMapper.class).selectByKey(key); + return getMapper(session).selectByKey(key); } public QualityProfileDto getNonNullByKey(DbSession session, String key) { @@ -59,11 +61,11 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { } public List<QualityProfileDto> findAll(DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectAll(); + return getMapper(session).selectAll(); } public void insert(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) { - QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class); + QualityProfileMapper mapper = getMapper(session); doInsert(mapper, profile); for (QualityProfileDto other : otherProfiles) { doInsert(mapper, other); @@ -93,7 +95,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { } public void update(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) { - QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class); + QualityProfileMapper mapper = getMapper(session); doUpdate(mapper, profile); for (QualityProfileDto otherProfile : otherProfiles) { doUpdate(mapper, otherProfile); @@ -121,7 +123,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { } public void delete(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) { - QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class); + QualityProfileMapper mapper = getMapper(session); doDelete(mapper, profile); for (QualityProfileDto otherProfile : otherProfiles) { doDelete(mapper, otherProfile); @@ -138,7 +140,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { */ @Deprecated public void delete(int id, DbSession session) { - session.getMapper(QualityProfileMapper.class).delete(id); + getMapper(session).delete(id); } /** @@ -163,7 +165,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { public List<QualityProfileDto> findAll() { DbSession session = mybatis.openSession(false); try { - return session.getMapper(QualityProfileMapper.class).selectAll(); + return getMapper(session).selectAll(); } finally { MyBatis.closeQuietly(session); } @@ -171,7 +173,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getDefaultProfile(String language, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language); + return getMapper(session).selectDefaultProfile(language); } @CheckForNull @@ -188,7 +190,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { public QualityProfileDto getByProjectAndLanguage(long projectId, String language) { DbSession session = mybatis.openSession(false); try { - return session.getMapper(QualityProfileMapper.class).selectByProjectIdAndLanguage(projectId, language); + return getMapper(session).selectByProjectIdAndLanguage(projectId, language); } finally { MyBatis.closeQuietly(session); } @@ -196,13 +198,13 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getByProjectAndLanguage(String projectKey, String language, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectByProjectAndLanguage(projectKey, language); + return getMapper(session).selectByProjectAndLanguage(projectKey, language); } public List<QualityProfileDto> findByLanguage(String language) { DbSession session = mybatis.openSession(false); try { - return session.getMapper(QualityProfileMapper.class).selectByLanguage(language); + return getMapper(session).selectByLanguage(language); } finally { MyBatis.closeQuietly(session); } @@ -215,7 +217,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @Deprecated @CheckForNull public QualityProfileDto getById(int id, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectById(id); + return getMapper(session).selectById(id); } /** @@ -235,7 +237,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getParent(String childKey, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectParent(childKey); + return getMapper(session).selectParent(childKey); } @CheckForNull @@ -250,7 +252,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getParentById(int childId, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectParentById(childId); + return getMapper(session).selectParentById(childId); } @CheckForNull @@ -264,7 +266,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { } public List<QualityProfileDto> findChildren(DbSession session, String key) { - return session.getMapper(QualityProfileMapper.class).selectChildren(key); + return getMapper(session).selectChildren(key); } /** @@ -281,7 +283,7 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { @CheckForNull public QualityProfileDto getByNameAndLanguage(String name, String language, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectByNameAndLanguage(name, language); + return getMapper(session).selectByNameAndLanguage(name, language); } /** @@ -308,50 +310,68 @@ public class QualityProfileDao implements ServerComponent, DaoComponent { } public List<ComponentDto> selectProjects(String profileName, String language, DbSession session) { - return session.getMapper(QualityProfileMapper.class).selectProjects(profileName, language); + return getMapper(session).selectProjects(profileName, language); } public int countProjects(String profileName, String language) { DbSession session = mybatis.openSession(false); try { - return session.getMapper(QualityProfileMapper.class).countProjects(profileName, language); + return getMapper(session).countProjects(profileName, language); + } finally { + MyBatis.closeQuietly(session); + } + } + + public Map<String, Long> countProjectsByProfileKey() { + DbSession session = mybatis.openSession(false); + try { + Map<String, Long> countByKey = Maps.newHashMap(); + for (QualityProfileProjectCount count : getMapper(session).countProjectsByProfile()) { + countByKey.put(count.getProfileKey(), count.getProjectCount()); + } + return countByKey; } finally { MyBatis.closeQuietly(session); } } public void insertProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) { - session.getMapper(QualityProfileMapper.class).insertProjectProfileAssociation(projectUuid, profileKey); + getMapper(session).insertProjectProfileAssociation(projectUuid, profileKey); } public void deleteProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) { - session.getMapper(QualityProfileMapper.class).deleteProjectProfileAssociation(projectUuid, profileKey); + getMapper(session).deleteProjectProfileAssociation(projectUuid, profileKey); } public void updateProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) { - session.getMapper(QualityProfileMapper.class).updateProjectProfileAssociation(projectUuid, profileKey); + getMapper(session).updateProjectProfileAssociation(projectUuid, profileKey); } public void deleteAllProjectProfileAssociation(String profileKey, DbSession session) { - session.getMapper(QualityProfileMapper.class).deleteAllProjectProfileAssociation(profileKey); + getMapper(session).deleteAllProjectProfileAssociation(profileKey); } public List<ProjectQprofileAssociationDto> selectSelectedProjects(String profileKey, @Nullable String query, DbSession session) { String nameQuery = sqlQueryString(query); - return session.getMapper(QualityProfileMapper.class).selectSelectedProjects(profileKey, nameQuery); + return getMapper(session).selectSelectedProjects(profileKey, nameQuery); } public List<ProjectQprofileAssociationDto> selectDeselectedProjects(String profileKey, @Nullable String query, DbSession session) { String nameQuery = sqlQueryString(query); - return session.getMapper(QualityProfileMapper.class).selectDeselectedProjects(profileKey, nameQuery); + return getMapper(session).selectDeselectedProjects(profileKey, nameQuery); } public List<ProjectQprofileAssociationDto> selectProjectAssociations(String profileKey, @Nullable String query, DbSession session) { String nameQuery = sqlQueryString(query); - return session.getMapper(QualityProfileMapper.class).selectProjectAssociations(profileKey, nameQuery); + return getMapper(session).selectProjectAssociations(profileKey, nameQuery); } private String sqlQueryString(String query) { return query == null ? "%" : "%" + query.toUpperCase() + "%"; } + + private QualityProfileMapper getMapper(DbSession session) { + return session.getMapper(QualityProfileMapper.class); + } + } diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java index 940c9adce99..a54a4789283 100644 --- a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java @@ -67,6 +67,8 @@ public interface QualityProfileMapper { int countProjects(@Param("profileName") String profileName, @Param("language") String language); + List<QualityProfileProjectCount> countProjectsByProfile(); + QualityProfileDto selectByProjectIdAndLanguage(@Param("projectId") Long projectId, @Param("language") String language); QualityProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language); diff --git a/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileProjectCount.java b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileProjectCount.java new file mode 100644 index 00000000000..a18bd449c8f --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileProjectCount.java @@ -0,0 +1,43 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 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.qualityprofile.db; + + +public class QualityProfileProjectCount { + + private String profileKey; + private Long projectCount; + + public String getProfileKey() { + return profileKey; + } + + public void setProfileKey(String profileKey) { + this.profileKey = profileKey; + } + + public Long getProjectCount() { + return projectCount; + } + + public void setProjectCount(Long projectCount) { + this.projectCount = projectCount; + } +} diff --git a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml index 3221d9bbf3d..9356b4f26d1 100644 --- a/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml @@ -151,6 +151,15 @@ </where> </select> + <select id="countProjectsByProfile" resultType="org.sonar.core.qualityprofile.db.QualityProfileProjectCount"> + SELECT pp.profile_key as profileKey, count(projects.id) as projectCount + FROM projects projects + INNER JOIN project_qprofiles pp ON pp.project_uuid=projects.uuid + INNER JOIN rules_profiles prof ON pp.profile_key=prof.kee + WHERE projects.enabled=${_true} + GROUP BY profileKey + </select> + <select id="selectByProjectIdAndLanguage" parameterType="map" resultType="QualityProfile"> SELECT <include refid="profilesColumns"/> FROM rules_profiles p diff --git a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java index 7518834fdd7..1839da2b120 100644 --- a/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/qualityprofile/db/QualityProfileDaoTest.java @@ -20,6 +20,7 @@ package org.sonar.core.qualityprofile.db; +import org.assertj.core.data.MapEntry; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -226,6 +227,15 @@ public class QualityProfileDaoTest extends AbstractDaoTestCase { } @Test + public void count_projects_by_profile() { + setupData("projects"); + + assertThat(dao.countProjectsByProfileKey()).containsOnly( + MapEntry.entry("java_sonar_way", 2L), + MapEntry.entry("js_sonar_way", 2L)); + } + + @Test public void select_by_project_id_and_language() { setupData("projects"); diff --git a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml index 8e6fb4c83af..94df8ad98ee 100644 --- a/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml +++ b/sonar-core/src/test/resources/org/sonar/core/qualityprofile/db/QualityProfileDaoTest/projects.xml @@ -5,12 +5,14 @@ <rules_profiles id="2" name="Sonar Way" language="js" parent_kee="[null]" kee="js_sonar_way" is_default="[true]" rules_updated_at="[null]" created_at="[null]" updated_at="[null]"/> - <projects id="1" uuid="A" kee="org.codehaus.sonar:sonar" name="SonarQube"/> - <projects id="2" uuid="B" kee="org.codehaus.sonar-plugins.java:java" name="SonarQube Java"/> + <projects id="1" uuid="A" kee="org.codehaus.sonar:sonar" name="SonarQube" enabled="[true]"/> + <projects id="2" uuid="B" kee="org.codehaus.sonar-plugins.java:java" name="SonarQube Java" enabled="[true]"/> + <projects id="3" uuid="C" kee="disabled:project" name="Disabled Project" enabled="[false]"/> <project_qprofiles id="1" project_uuid="A" profile_key="java_sonar_way"/> <project_qprofiles id="2" project_uuid="B" profile_key="java_sonar_way"/> <project_qprofiles id="3" project_uuid="A" profile_key="js_sonar_way"/> <project_qprofiles id="4" project_uuid="B" profile_key="js_sonar_way"/> + <project_qprofiles id="5" project_uuid="C" profile_key="js_sonar_way"/> </dataset> |