import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
+import org.sonar.core.util.stream.MoreCollectors;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
return mapper(dbSession).selectByNameAndLanguages(organization.getUuid(), name, languages);
}
- public Map<String, Long> countProjectsByProfileUuid(DbSession dbSession, OrganizationDto organization) {
- return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileUuid(organization.getUuid()));
+ public Map<String, Long> countProjectsByOrganizationAndProfiles(DbSession dbSession, OrganizationDto organization, List<QProfileDto> profiles) {
+ List<String> profileUuids = profiles.stream().map(QProfileDto::getKee).collect(MoreCollectors.toList());
+ return KeyLongValue.toMap(executeLargeInputs(profileUuids, partition -> mapper(dbSession).countProjectsByOrganizationAndProfiles(organization.getUuid(), partition)));
}
public void insertProjectProfileAssociation(DbSession dbSession, ComponentDto project, QProfileDto profile) {
// PROJECTS
- List<KeyLongValue> countProjectsByProfileUuid(@Param("organizationUuid") String organizationUuid);
+ List<KeyLongValue> countProjectsByOrganizationAndProfiles(@Param("organizationUuid") String organizationUuid, @Param("profileUuids") List<String> profiles);
@CheckForNull
QProfileDto selectAssociatedToProjectUuidAndLanguage(
ORDER BY rp.name
</select>
- <select id="countProjectsByProfileUuid" resultType="KeyLongValue" parameterType="map">
+ <select id="countProjectsByOrganizationAndProfiles" resultType="KeyLongValue" parameterType="map">
select pqp.profile_key as "key", count(pj.uuid) as "value"
from projects pj
inner join project_qprofiles pqp on pqp.project_uuid = pj.uuid
pj.enabled = ${_true}
and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
and oqp.organization_uuid = pj.organization_uuid
+ and <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")">
+ oqp.uuid = #{profileUuid, jdbcType=VARCHAR}
+ </foreach>
group by pqp.profile_key
</select>
ComponentDto projectInOtherOrg = db.components().insertPrivateProject(otherOrg);
db.qualityProfiles().associateWithProject(projectInOtherOrg, profileInOtherOrg);
- assertThat(underTest.countProjectsByProfileUuid(dbSession, organization)).containsOnly(
+ assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, organization, asList(profileWithoutProjects, profileWithProjects, profileInOtherOrg))).containsOnly(
MapEntry.entry(profileWithProjects.getKee(), 2L));
+ assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, otherOrg, singletonList(profileWithoutProjects))).isEmpty();
+ assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, organization, Collections.emptyList())).isEmpty();
}
@Test
dbClient.activeRuleDao().countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).build()))
.setActiveDeprecatedRuleCountByProfileKey(
dbClient.activeRuleDao().countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).setRuleStatus(DEPRECATED).build()))
- .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileUuid(dbSession, organization))
+ .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByOrganizationAndProfiles(dbSession, organization, profiles))
.setDefaultProfileKeys(defaultProfiles);
}
}
.containsExactlyInAnyOrder(tuple(true, 2L), tuple(false, 0L));
}
+ @Test
+ public void no_profile() {
+ SearchWsResponse result = call(ws.newRequest());
+
+ assertThat(result.getProfilesList()).isEmpty();
+ }
+
@Test
public void map_dates() {
long time = DateUtils.parseDateTime("2016-12-22T19:10:03+0100").getTime();