public static Map<String, ActiveRuleParamDto> groupByKey(Collection<ActiveRuleParamDto> params) { Map<String, ActiveRuleParamDto> result = new HashMap<>(); for (ActiveRuleParamDto param : params) { result.put(param.getKey(), param); } return result; } }
private static Map<String, String> paramDtoToMap(List<ActiveRuleParamDto> params) { Map<String, String> map = new HashMap<>(); for (ActiveRuleParamDto dto : params) { map.put(dto.getKey(), dto.getValue()); } return map; }
@Override public void accept(@Nonnull ActiveRuleParamDto activeRuleParamDto) { if (activeRuleParamDto.getKey().equals(key)) { dbClient.activeRuleDao().deleteParamById(dbSession, activeRuleParamDto.getId()); } } }
public void deleteParamsByRuleParamOfAllOrganizations(DbSession dbSession, RuleParamDto param) { List<ActiveRuleDto> activeRules = selectByRuleIdOfAllOrganizations(dbSession, param.getRuleId()); for (ActiveRuleDto activeRule : activeRules) { for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleId(dbSession, activeRule.getId())) { if (activeParam.getKey().equals(param.getName())) { deleteParam(dbSession, activeParam); } } } }
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId)); ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId); for (ActiveRuleDto activeRule : activeRuleDtos) { // TODO all rules should be loaded by using one query with all active rule keys as parameter Rule rule = ruleFinder.findByKey(activeRule.getRuleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId()); for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); } } return target; }
@Test public void insertParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(reloaded).hasSize(1); assertThat(reloaded.get(0)) .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleId().equals(activeRule.getId())) .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) .matches(p -> p.getValue().equals("foo")); }
private ActiveRuleChange insertActiveRule(DbSession dbSession, RulesProfileDto rulesProfileDto, BuiltInQProfile.ActiveRule activeRule, long now) { RuleKey ruleKey = activeRule.getRuleKey(); RuleDefinitionDto ruleDefinitionDto = ruleRepository.getDefinition(ruleKey) .orElseThrow(() -> new IllegalStateException("RuleDefinition not found for key " + ruleKey)); ActiveRuleDto dto = new ActiveRuleDto(); dto.setProfileId(rulesProfileDto.getId()); dto.setRuleId(ruleDefinitionDto.getId()); dto.setKey(ActiveRuleKey.of(rulesProfileDto, ruleDefinitionDto.getKey())); dto.setSeverity(firstNonNull(activeRule.getBuiltIn().overriddenSeverity(), ruleDefinitionDto.getSeverityString())); dto.setUpdatedAt(now); dto.setCreatedAt(now); dbClient.activeRuleDao().insert(dbSession, dto); List<ActiveRuleParamDto> paramDtos = insertActiveRuleParams(dbSession, activeRule, dto); ActiveRuleChange change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, dto, ruleDefinitionDto); change.setSeverity(dto.getSeverityString()); paramDtos.forEach(paramDto -> change.setParameter(paramDto.getKey(), paramDto.getValue())); return change; }
@Test public void updateParam() { ActiveRuleDto activeRule = createFor(profile1, rule1).setSeverity(Severity.CRITICAL); underTest.insert(dbSession, activeRule); ActiveRuleParamDto activeRuleParam = ActiveRuleParamDto.createFor(rule1Param1).setValue("foo"); underTest.insertParam(dbSession, activeRule, activeRuleParam); underTest.updateParam(dbSession, activeRuleParam.setValue("bar")); List<ActiveRuleParamDto> reloaded = underTest.selectParamsByActiveRuleId(dbSession, activeRule.getId()); assertThat(reloaded).hasSize(1); assertThat(reloaded.get(0)) .matches(p -> Objects.equals(p.getId(), activeRuleParam.getId())) .matches(p -> p.getKey().equals(activeRuleParam.getKey())) .matches(p -> p.getActiveRuleId().equals(activeRule.getId())) .matches(p -> p.getRulesParameterId().equals(rule1Param1.getId())) .matches(p -> p.getValue().equals("bar")); }
private static Rules.Active buildActiveRuleResponse(OrgActiveRuleDto activeRule, List<ActiveRuleParamDto> parameters) { Rules.Active.Builder builder = Rules.Active.newBuilder(); builder.setQProfile(activeRule.getProfileUuid()); String inheritance = activeRule.getInheritance(); builder.setInherit(inheritance != null ? inheritance : ActiveRuleInheritance.NONE.name()); builder.setSeverity(activeRule.getSeverityString()); builder.setCreatedAt(DateUtils.formatDateTime(activeRule.getCreatedAt())); builder.setUpdatedAt(DateUtils.formatDateTime(activeRule.getUpdatedAt())); Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder(); for (ActiveRuleParamDto parameter : parameters) { builder.addParams(paramBuilder.clear() .setKey(parameter.getKey()) .setValue(nullToEmpty(parameter.getValue()))); } return builder.build(); }
private void writeXml(DbSession dbSession, Writer writer, QProfileDto profile, Iterator<OrgActiveRuleDto> activeRules) { XmlWriter xml = XmlWriter.of(writer).declaration(); xml.begin(ATTRIBUTE_PROFILE); xml.prop(ATTRIBUTE_NAME, profile.getName()); xml.prop(ATTRIBUTE_LANGUAGE, profile.getLanguage()); xml.begin(ATTRIBUTE_RULES); while (activeRules.hasNext()) { ActiveRuleDto activeRule = activeRules.next(); xml.begin(ATTRIBUTE_RULE); xml.prop(ATTRIBUTE_REPOSITORY_KEY, activeRule.getRuleKey().repository()); xml.prop(ATTRIBUTE_KEY, activeRule.getRuleKey().rule()); xml.prop(ATTRIBUTE_PRIORITY, activeRule.getSeverityString()); xml.begin(ATTRIBUTE_PARAMETERS); for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId())) { xml .begin(ATTRIBUTE_PARAMETER) .prop(ATTRIBUTE_PARAMETER_KEY, param.getKey()) .prop(ATTRIBUTE_PARAMETER_VALUE, param.getValue()) .end(); } xml.end(ATTRIBUTE_PARAMETERS); xml.end(ATTRIBUTE_RULE); } xml.end(ATTRIBUTE_RULES).end(ATTRIBUTE_PROFILE).close(); }
assertThat(activeList.getParamsList()).extracting("key", "value").containsExactlyInAnyOrder( tuple(ruleParam.getName(), ruleParam.getDefaultValue()), tuple(activeRuleParam.getKey(), ""));
change.setSeverity(activeRule.get().getSeverityString()); for (ActiveRuleParamDto activeParam : activeRule.getParams()) { change.setParameter(activeParam.getKey(), activeParam.getValue());
public static Map<String, ActiveRuleParamDto> groupByKey(Collection<ActiveRuleParamDto> params) { Map<String, ActiveRuleParamDto> result = new HashMap<>(); for (ActiveRuleParamDto param : params) { result.put(param.getKey(), param); } return result; } }
private static Map<String, String> paramDtoToMap(List<ActiveRuleParamDto> params) { Map<String, String> map = new HashMap<>(); for (ActiveRuleParamDto dto : params) { map.put(dto.getKey(), dto.getValue()); } return map; }
@Override public void accept(@Nonnull ActiveRuleParamDto activeRuleParamDto) { if (activeRuleParamDto.getKey().equals(key)) { dbClient.activeRuleDao().deleteParamById(dbSession, activeRuleParamDto.getId()); } } }
public void deleteParamsByRuleParam(DbSession dbSession, int ruleId, String paramKey) { List<ActiveRuleDto> activeRules = selectByRuleId(dbSession, ruleId); for (ActiveRuleDto activeRule : activeRules) { for (ActiveRuleParamDto activeParam : selectParamsByActiveRuleId(dbSession, activeRule.getId())) { if (activeParam.getKey().equals(paramKey)) { deleteParam(dbSession, activeRule, activeParam); } } } }
private RulesProfile wrap(DbSession dbSession, QProfileDto profile) { RulesProfile target = new RulesProfile(profile.getName(), profile.getLanguage()); List<OrgActiveRuleDto> activeRuleDtos = dbClient.activeRuleDao().selectByProfile(dbSession, profile); List<ActiveRuleParamDto> activeRuleParamDtos = dbClient.activeRuleDao().selectParamsByActiveRuleIds(dbSession, Lists.transform(activeRuleDtos, ActiveRuleDto::getId)); ListMultimap<Integer, ActiveRuleParamDto> activeRuleParamsByActiveRuleId = FluentIterable.from(activeRuleParamDtos).index(ActiveRuleParamDto::getActiveRuleId); for (ActiveRuleDto activeRule : activeRuleDtos) { // TODO all rules should be loaded by using one query with all active rule keys as parameter Rule rule = ruleFinder.findByKey(activeRule.getRuleKey()); org.sonar.api.rules.ActiveRule wrappedActiveRule = target.activateRule(rule, RulePriority.valueOf(activeRule.getSeverityString())); List<ActiveRuleParamDto> paramDtos = activeRuleParamsByActiveRuleId.get(activeRule.getId()); for (ActiveRuleParamDto activeRuleParamDto : paramDtos) { wrappedActiveRule.setParameter(activeRuleParamDto.getKey(), activeRuleParamDto.getValue()); } } return target; }
private ActiveRuleChange insertActiveRule(DbSession dbSession, RulesProfileDto rulesProfileDto, BuiltInQProfile.ActiveRule activeRule, long now) { RuleKey ruleKey = activeRule.getRuleKey(); RuleDefinitionDto ruleDefinitionDto = ruleRepository.getDefinition(ruleKey) .orElseThrow(() -> new IllegalStateException("RuleDefinition not found for key " + ruleKey)); ActiveRuleDto dto = new ActiveRuleDto(); dto.setProfileId(rulesProfileDto.getId()); dto.setRuleId(ruleDefinitionDto.getId()); dto.setKey(ActiveRuleKey.of(rulesProfileDto, ruleDefinitionDto.getKey())); dto.setSeverity(firstNonNull(activeRule.getBuiltIn().overriddenSeverity(), ruleDefinitionDto.getSeverityString())); dto.setUpdatedAt(now); dto.setCreatedAt(now); dbClient.activeRuleDao().insert(dbSession, dto); List<ActiveRuleParamDto> paramDtos = insertActiveRuleParams(dbSession, activeRule, dto); ActiveRuleChange change = new ActiveRuleChange(ActiveRuleChange.Type.ACTIVATED, dto, ruleDefinitionDto); change.setSeverity(dto.getSeverityString()); paramDtos.forEach(paramDto -> change.setParameter(paramDto.getKey(), paramDto.getValue())); return change; }
private static Rules.Active buildActiveRuleResponse(OrgActiveRuleDto activeRule, List<ActiveRuleParamDto> parameters) { Rules.Active.Builder builder = Rules.Active.newBuilder(); builder.setQProfile(activeRule.getProfileUuid()); String inheritance = activeRule.getInheritance(); builder.setInherit(inheritance != null ? inheritance : ActiveRuleInheritance.NONE.name()); builder.setSeverity(activeRule.getSeverityString()); builder.setCreatedAt(DateUtils.formatDateTime(activeRule.getCreatedAt())); Rules.Active.Param.Builder paramBuilder = Rules.Active.Param.newBuilder(); for (ActiveRuleParamDto parameter : parameters) { builder.addParams(paramBuilder.clear() .setKey(parameter.getKey()) .setValue(nullToEmpty(parameter.getValue()))); } return builder.build(); }
private void writeXml(DbSession dbSession, Writer writer, QProfileDto profile, Iterator<OrgActiveRuleDto> activeRules) { XmlWriter xml = XmlWriter.of(writer).declaration(); xml.begin(ATTRIBUTE_PROFILE); xml.prop(ATTRIBUTE_NAME, profile.getName()); xml.prop(ATTRIBUTE_LANGUAGE, profile.getLanguage()); xml.begin(ATTRIBUTE_RULES); while (activeRules.hasNext()) { ActiveRuleDto activeRule = activeRules.next(); xml.begin(ATTRIBUTE_RULE); xml.prop(ATTRIBUTE_REPOSITORY_KEY, activeRule.getRuleKey().repository()); xml.prop(ATTRIBUTE_KEY, activeRule.getRuleKey().rule()); xml.prop(ATTRIBUTE_PRIORITY, activeRule.getSeverityString()); xml.begin(ATTRIBUTE_PARAMETERS); for (ActiveRuleParamDto param : db.activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId())) { xml .begin(ATTRIBUTE_PARAMETER) .prop(ATTRIBUTE_PARAMETER_KEY, param.getKey()) .prop(ATTRIBUTE_PARAMETER_VALUE, param.getValue()) .end(); } xml.end(ATTRIBUTE_PARAMETERS); xml.end(ATTRIBUTE_RULE); } xml.end(ATTRIBUTE_RULES).end(ATTRIBUTE_PROFILE).close(); }