public boolean isDefault(DbSession dbSession, String organizationUuid, String qProfileUuid) { return selectExistingQProfileUuids(dbSession, organizationUuid, singletonList(qProfileUuid)).contains(qProfileUuid); }
public void setDefault(DbSession dbSession, QProfileDto profile) { dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(profile)); } }
public void deleteByQProfileUuids(DbSession dbSession, Collection<String> qProfileUuids) { DefaultQProfileMapper mapper = mapper(dbSession); DatabaseUtils.executeLargeUpdates(qProfileUuids, mapper::deleteByQProfileUuids); }
Set<String> uuids = dbClient.defaultQProfileDao().selectExistingQProfileUuids(dbSession, qp.getOrganizationUuid(), Collections.singleton(qp.getKee())); dbClient.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids); dbClient.defaultQProfileDao().insertOrUpdate(dbSession, new DefaultQProfileDto() .setQProfileUuid(qualityProfile.getKee()) .setLanguage(qp.getLanguage())
private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtIn, RulesProfileDto rulesProfileDto) { List<String> orgUuids = dbClient.organizationDao().selectAllUuids(dbSession); Set<String> orgUuidsWithoutDefault = dbClient.defaultQProfileDao().selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, builtIn.getLanguage()); List<DefaultQProfileDto> defaults = new ArrayList<>(); orgUuids.forEach(orgUuid -> { OrgQProfileDto dto = new OrgQProfileDto() .setOrganizationUuid(orgUuid) .setRulesProfileUuid(rulesProfileDto.getKee()) .setUuid(uuidFactory.create()); if (builtIn.isDefault() && orgUuidsWithoutDefault.contains(orgUuid)) { // rows of table default_qprofiles must be inserted after // in order to benefit from batch SQL inserts defaults.add(new DefaultQProfileDto() .setQProfileUuid(dto.getUuid()) .setOrganizationUuid(orgUuid) .setLanguage(builtIn.getLanguage())); } dbClient.qualityProfileDao().insert(batchDbSession, dto); }); defaults.forEach(defaultQProfileDto -> dbClient.defaultQProfileDao().insertOrUpdate(dbSession, defaultQProfileDto)); }
@Test public void deleteByQProfileUuids_deletes_rows_related_to_specified_profile() { OrganizationDto org1 = dbTester.organizations().insert(); OrganizationDto org2 = dbTester.organizations().insert(); underTest.insertOrUpdate(dbSession, new DefaultQProfileDto().setOrganizationUuid(org1.getUuid()).setLanguage("java").setQProfileUuid("u1")); underTest.insertOrUpdate(dbSession, new DefaultQProfileDto().setOrganizationUuid(org1.getUuid()).setLanguage("js").setQProfileUuid("u2")); underTest.insertOrUpdate(dbSession, new DefaultQProfileDto().setOrganizationUuid(org2.getUuid()).setLanguage("java").setQProfileUuid("u3")); underTest.insertOrUpdate(dbSession, new DefaultQProfileDto().setOrganizationUuid(org2.getUuid()).setLanguage("js").setQProfileUuid("u4")); underTest.deleteByQProfileUuids(dbSession, asList("u1", "u3")); dbSession.commit(); assertThat(countRows()).isEqualTo(2); assertThat(selectUuidOfDefaultProfile(org1, "java")).isEmpty(); assertThat(selectUuidOfDefaultProfile(org1, "js")).hasValue("u2"); assertThat(selectUuidOfDefaultProfile(org2, "java")).isEmpty(); assertThat(selectUuidOfDefaultProfile(org2, "js")).hasValue("u4"); }
@Test public void isDefault_returns_true_if_profile_is_marked_as_default() { OrganizationDto org = dbTester.organizations().insert(); QProfileDto profile1 = dbTester.qualityProfiles().insert(org); QProfileDto profile2 = dbTester.qualityProfiles().insert(org); dbTester.qualityProfiles().setAsDefault(profile1); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile1.getKee())).isTrue(); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile2.getKee())).isFalse(); assertThat(underTest.isDefault(dbSession, org.getUuid(), "does_not_exist")).isFalse(); }
db.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids); db.qualityProfileDao().deleteOrgQProfilesByUuids(dbSession, uuids);
@Test public void selectUuidsOfOrganizationsWithoutDefaultProfile() { OrganizationDto org1 = dbTester.organizations().insert(); OrganizationDto org2 = dbTester.organizations().insert(); QProfileDto profileInOrg1 = dbTester.qualityProfiles().insert(org1, p -> p.setLanguage("java")); QProfileDto profileInOrg2 = dbTester.qualityProfiles().insert(org2, p -> p.setLanguage("java")); dbTester.qualityProfiles().setAsDefault(profileInOrg1); assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "java")) .containsExactly(org2.getUuid()); assertThat(underTest.selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, "js")) .containsExactlyInAnyOrder(org1.getUuid(), org2.getUuid()); }
Set<String> uuids = dbClient.defaultQProfileDao().selectExistingQProfileUuids(dbSession, qp.getOrganizationUuid(), Collections.singleton(qp.getKee())); dbClient.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids); dbClient.defaultQProfileDao().insertOrUpdate(dbSession, new DefaultQProfileDto() .setQProfileUuid(qProfileDto.getKee()) .setLanguage(qp.getLanguage())
private void associateToOrganizations(DbSession dbSession, DbSession batchDbSession, BuiltInQProfile builtIn, RulesProfileDto rulesProfileDto) { List<String> orgUuids = dbClient.organizationDao().selectAllUuids(dbSession); Set<String> orgUuidsWithoutDefault = dbClient.defaultQProfileDao().selectUuidsOfOrganizationsWithoutDefaultProfile(dbSession, builtIn.getLanguage()); List<DefaultQProfileDto> defaults = new ArrayList<>(); orgUuids.forEach(orgUuid -> { OrgQProfileDto dto = new OrgQProfileDto() .setOrganizationUuid(orgUuid) .setRulesProfileUuid(rulesProfileDto.getKee()) .setUuid(uuidFactory.create()); if (builtIn.isDefault() && orgUuidsWithoutDefault.contains(orgUuid)) { // rows of table default_qprofiles must be inserted after // in order to benefit from batch SQL inserts defaults.add(new DefaultQProfileDto() .setQProfileUuid(dto.getUuid()) .setOrganizationUuid(orgUuid) .setLanguage(builtIn.getLanguage())); } dbClient.qualityProfileDao().insert(batchDbSession, dto); }); defaults.forEach(defaultQProfileDto -> dbClient.defaultQProfileDao().insertOrUpdate(dbSession, defaultQProfileDto)); }
private void assertThatIsDefault(OrganizationDto org, QProfileDto profile) { assertThat(selectUuidOfDefaultProfile(org, profile.getLanguage())).hasValue(profile.getKee()); assertThat(underTest.isDefault(dbSession, org.getUuid(), profile.getKee())).isTrue(); }
db.defaultQProfileDao().deleteByQProfileUuids(dbSession, uuids); db.qualityProfileDao().deleteOrgQProfilesByUuids(dbSession, uuids);
public QualityProfileDbTester setAsDefault(QProfileDto profile, QProfileDto... others) { dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(profile)); for (QProfileDto other : others) { dbClient.defaultQProfileDao().insertOrUpdate(dbSession, DefaultQProfileDto.from(other)); } dbSession.commit(); return this; }
private void ensureNoneIsMarkedAsDefault(DbSession dbSession, QProfileDto profile, Collection<QProfileDto> descendants) { Set<String> allUuids = new HashSet<>(); allUuids.add(profile.getKee()); descendants.forEach(p -> allUuids.add(p.getKee())); Set<String> uuidsOfDefaultProfiles = dbClient.defaultQProfileDao().selectExistingQProfileUuids(dbSession, profile.getOrganizationUuid(), allUuids); checkArgument(!uuidsOfDefaultProfiles.contains(profile.getKee()), "Profile '%s' cannot be deleted because it is marked as default", profile.getName()); descendants.stream() .filter(p -> uuidsOfDefaultProfiles.contains(p.getKee())) .findFirst() .ifPresent(p -> { throw new IllegalArgumentException(String.format("Profile '%s' cannot be deleted because its descendant named '%s' is marked as default", profile.getName(), p.getName())); }); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); String newName = request.mandatoryParam(PARAM_TO_NAME); String profileKey = request.mandatoryParam(PARAM_FROM_KEY); try (DbSession dbSession = dbClient.openSession(false)) { QProfileDto sourceProfile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey)); userSession.checkPermission(ADMINISTER_QUALITY_PROFILES, sourceProfile.getOrganizationUuid()); QProfileDto copiedProfile = profileCopier.copyToName(dbSession, sourceProfile, newName); boolean isDefault = dbClient.defaultQProfileDao().isDefault(dbSession, copiedProfile.getOrganizationUuid(), copiedProfile.getKee()); CopyWsResponse wsResponse = buildResponse(copiedProfile, isDefault); writeProtobuf(wsResponse, request, response); } }
public Set<String> selectUuidsOfOrganizationsWithoutDefaultProfile(DbSession dbSession, String language) { return mapper(dbSession).selectUuidsOfOrganizationsWithoutDefaultProfile(language); }
private void insertQualityProfiles(DbSession dbSession, DbSession batchDbSession, OrganizationDto organization) { Map<QProfileName, BuiltInQProfile> builtInsPerName = builtInQProfileRepository.get().stream() .collect(uniqueIndex(BuiltInQProfile::getQProfileName)); List<DefaultQProfileDto> defaults = new ArrayList<>(); dbClient.qualityProfileDao().selectBuiltInRuleProfiles(dbSession).forEach(rulesProfile -> { OrgQProfileDto dto = new OrgQProfileDto() .setOrganizationUuid(organization.getUuid()) .setRulesProfileUuid(rulesProfile.getKee()) .setUuid(uuidFactory.create()); QProfileName name = new QProfileName(rulesProfile.getLanguage(), rulesProfile.getName()); BuiltInQProfile builtIn = builtInsPerName.get(name); if (builtIn == null || builtIn.isDefault()) { // If builtIn == null, the plugin has been removed // rows of table default_qprofiles must be inserted after org_qprofiles // in order to benefit from batch SQL inserts defaults.add(new DefaultQProfileDto() .setQProfileUuid(dto.getUuid()) .setOrganizationUuid(organization.getUuid()) .setLanguage(rulesProfile.getLanguage())); } dbClient.qualityProfileDao().insert(batchDbSession, dto); }); defaults.forEach(defaultQProfileDto -> dbClient.defaultQProfileDao().insertOrUpdate(dbSession, defaultQProfileDto)); }
@Test public void selectExistingQProfileUuids_filters_defaults() { OrganizationDto org = dbTester.organizations().insert(); QProfileDto profile1 = dbTester.qualityProfiles().insert(org); QProfileDto profile2 = dbTester.qualityProfiles().insert(org); dbTester.qualityProfiles().setAsDefault(profile1); List<String> profileUuids = asList(profile1.getKee(), profile2.getKee(), "other"); assertThat(underTest.selectExistingQProfileUuids(dbSession, org.getUuid(), profileUuids)) .containsExactly(profile1.getKee()); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { QProfileDto profile = qProfileWsSupport.getProfile(dbSession, QProfileReference.fromKey(request.mandatoryParam(PARAM_KEY))); OrganizationDto organization = qProfileWsSupport.getOrganization(dbSession, profile); boolean isDefault = dbClient.defaultQProfileDao().isDefault(dbSession, profile.getOrganizationUuid(), profile.getKee()); ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization); long activeRuleCount = countActiveRulesByQuery(dbSession, profile, builder); long deprecatedActiveRuleCount = countActiveRulesByQuery(dbSession, profile, builder.setRuleStatus(DEPRECATED)); long projectCount = countProjectsByOrganizationAndProfiles(dbSession, organization, profile); CompareToSonarWay compareToSonarWay = getSonarWay(request, dbSession, organization, profile); writeProtobuf(buildResponse(profile, isDefault, getLanguage(profile), activeRuleCount, deprecatedActiveRuleCount, projectCount, compareToSonarWay), request, response); } }