@Override public void backup(DbSession dbSession, QProfileDto profile, Writer writer) { List<OrgActiveRuleDto> activeRules = db.activeRuleDao().selectByProfile(dbSession, profile); activeRules.sort(BackupActiveRuleComparator.INSTANCE); writeXml(dbSession, writer, profile, activeRules.iterator()); }
private void completeWithActiveRules(DbSession dbSession, RuleActivationContext.Builder builder, Collection<RuleDefinitionDto> rules, Collection<String> ruleProfileUuids) { Collection<ActiveRuleDto> activeRules = db.activeRuleDao().selectByRulesAndRuleProfileUuids(dbSession, rules, ruleProfileUuids); builder.setActiveRules(activeRules); List<Integer> activeRuleIds = activeRules.stream().map(ActiveRuleDto::getId).collect(MoreCollectors.toArrayList(activeRules.size())); builder.setActiveRuleParams(db.activeRuleDao().selectParamsByActiveRuleIds(dbSession, activeRuleIds)); }
@Override public void accept(@Nonnull ActiveRuleParamDto activeRuleParamDto) { if (activeRuleParamDto.getKey().equals(key)) { dbClient.activeRuleDao().deleteParamById(dbSession, activeRuleParamDto.getId()); } } }
private Multimap<ActiveRuleDto, ActiveRuleParamDto> getActiveRuleParamsByActiveRule(DbSession dbSession, OrganizationDto organization, RuleDto customRule) { List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByRuleId(dbSession, organization, customRule.getId()); Map<Integer, OrgActiveRuleDto> activeRuleById = from(activeRuleDtos).uniqueIndex(ActiveRuleDto::getId); List<Integer> activeRuleIds = Lists.transform(activeRuleDtos, ActiveRuleDto::getId); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, activeRuleIds); return from(activeRuleParamDtos) .index(new ActiveRuleParamToActiveRule(activeRuleById)); }
@Override public void accept(@Nonnull ActiveRuleDto activeRuleDto) { Map<String, ActiveRuleParamDto> activeRuleParamByKey = from(activeRuleParams.get(activeRuleDto)) .uniqueIndex(ActiveRuleParamDto::getKey); ActiveRuleParamDto activeRuleParamDto = activeRuleParamByKey.get(ruleParamDto.getName()); if (activeRuleParamDto != null) { dbClient.activeRuleDao().updateParam(dbSession, activeRuleParamDto.setValue(ruleParamDto.getDefaultValue())); } else { dbClient.activeRuleDao().insertParam(dbSession, activeRuleDto, ActiveRuleParamDto.createFor(ruleParamDto).setValue(ruleParamDto.getDefaultValue())); } } }
private ActiveRuleDto createActiveRuleWithSeverity(RuleDefinitionDto rule, QProfileDto profile, String severity) { ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) .setSeverity(severity); dbClient.activeRuleDao().insert(session, activeRule); return activeRule; }
@Override public void indexOnStartup(Set<IndexType> uninitializedIndexTypes) { try (DbSession dbSession = dbClient.openSession(false)) { BulkIndexer bulkIndexer = createBulkIndexer(Size.LARGE, IndexingListener.FAIL_ON_ERROR); bulkIndexer.start(); dbClient.activeRuleDao().scrollAllForIndexing(dbSession, ar -> bulkIndexer.add(newIndexRequest(ar))); bulkIndexer.stop(); } }
private void assertThatRuleIsNotPresent(QProfileDto profile, RuleDefinitionDto rule) { Optional<OrgActiveRuleDto> activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile) .stream() .filter(ar -> ar.getRuleKey().equals(rule.getKey())) .findFirst(); assertThat(activeRule).isEmpty(); }
private void assertThatRuleIsNotPresent(QProfileDto profile, RuleDefinitionDto rule) { Optional<OrgActiveRuleDto> activeRule = db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile) .stream() .filter(ar -> ar.getRuleKey().equals(rule.getKey())) .findFirst(); assertThat(activeRule).isEmpty(); }
private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) { long now = new Date().getTime(); ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()) .setUpdatedAt(now) .setCreatedAt(now); dbClient.activeRuleDao().insert(dbSession, activeRule); return activeRule; }
private Statistics(DbSession dbSession, OrganizationDto organization, List<QProfileDto> profiles) { ActiveRuleDao dao = dbClient.activeRuleDao(); ActiveRuleCountQuery.Builder builder = ActiveRuleCountQuery.builder().setOrganization(organization); countRulesByProfileKey = dao.countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).build()); countOverridingRulesByProfileKey = dao.countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).setInheritance(OVERRIDES).build()); } }
private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) { ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()); dbClient.activeRuleDao().insert(dbSession, activeRule); return activeRule; } }
private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QProfileDto profile) { ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule) .setSeverity(rule.getSeverityString()); dbClient.activeRuleDao().insert(session, activeRule); return activeRule; }
public ActiveRuleDto activateRule(QProfileDto profile, RuleDefinitionDto rule, Consumer<ActiveRuleDto> consumer) { ActiveRuleDto activeRule = createFor(profile, rule) .setSeverity(Severity.ALL.get(nextInt(Severity.ALL.size()))) .setCreatedAt(nextLong()) .setUpdatedAt(nextLong()); consumer.accept(activeRule); dbClient.activeRuleDao().insert(dbSession, activeRule); dbSession.commit(); return activeRule; }
private static void assertThatRuleHasParams(DbTester db, ActiveRuleDto activeRule, Tuple... expectedParams) { assertThat(db.getDbClient().activeRuleDao().selectParamsByActiveRuleId(db.getSession(), activeRule.getId())) .extracting(ActiveRuleParamDto::getKey, ActiveRuleParamDto::getValue) .containsExactlyInAnyOrder(expectedParams); }
@Test public void commitAndIndex_deletes_the_documents_that_dont_exist_in_database() { ActiveRuleDto ar = db.qualityProfiles().activateRule(profile1, rule1); indexAll(); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(1); db.getDbClient().activeRuleDao().delete(db.getSession(), ar.getKey()); commitAndIndex(rule1, ar); assertThat(es.countDocuments(INDEX_TYPE_ACTIVE_RULE)).isEqualTo(0); assertThatEsQueueTableIsEmpty(); }
private ActiveRuleDto createActiveRuleWithParam(RuleDefinitionDto rule, QProfileDto profile, String value) { ActiveRuleDto activeRule = createActiveRule(rule, profile); RuleParamDto paramDto = dbClient.ruleDao().selectRuleParamsByRuleKey(session, rule.getKey()).get(0); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(paramDto).setValue(value); dbClient.activeRuleDao().insertParam(session, activeRule, activeRuleParam); return activeRule; }
@Test public void activate_one_rule() { OrganizationDto organization = db.organizations().insert(); QProfileDto qProfile = db.qualityProfiles().insert(organization); RuleDefinitionDto rule = db.rules().insert(r -> r.setLanguage(qProfile.getLanguage())); RuleActivation ruleActivation = RuleActivation.create(rule.getId(), Severity.CRITICAL, Collections.emptyMap()); qProfileRules.activateAndCommit(db.getSession(), qProfile, singleton(ruleActivation)); assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), qProfile)) .extracting(ActiveRuleDto::getRuleKey, ActiveRuleDto::getSeverityString) .containsExactlyInAnyOrder(tuple(rule.getKey(), Severity.CRITICAL)); }
private ActiveRuleDto activate(QProfileDto profile, RuleDefinitionDto rule, RuleParamDto param) { ActiveRuleDto activeRule = db.qualityProfiles().activateRule(profile, rule); ActiveRuleParamDto dto = ActiveRuleParamDto.createFor(param) .setValue("20") .setActiveRuleId(activeRule.getId()); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, dto); return activeRule; }
private void activateRuleInDb(RulesProfileDto profile, RuleDefinitionDto rule, RulePriority severity) { ActiveRuleDto dto = new ActiveRuleDto() .setProfileId(profile.getId()) .setSeverity(severity.name()) .setRuleId(rule.getId()) .setCreatedAt(PAST) .setUpdatedAt(PAST); db.getDbClient().activeRuleDao().insert(db.getSession(), dto); db.commit(); } }