Пример #1
0
  private String backupFromLanguage(@Nullable String languageKey) {
    checkPermission();

    DbSession session = dbClient.openSession(false);
    try {
      DebtModel debtModel = new DebtModel();
      List<CharacteristicDto> characteristicDtos =
          dbClient.debtCharacteristicDao().selectEnabledCharacteristics(session);
      for (CharacteristicDto characteristicDto : characteristicDtos) {
        if (characteristicDto.getParentId() == null) {
          debtModel.addRootCharacteristic(toDebtCharacteristic(characteristicDto));
          for (CharacteristicDto sub :
              subCharacteristics(characteristicDto.getId(), characteristicDtos)) {
            debtModel.addSubCharacteristic(toDebtCharacteristic(sub), characteristicDto.getKey());
          }
        }
      }

      List<RuleDebt> rules = newArrayList();
      for (RuleDto rule : dbClient.ruleDao().selectEnabledAndNonManual(session)) {
        if (languageKey == null || languageKey.equals(rule.getLanguage())) {
          RuleDebt ruleDebt = toRuleDebt(rule, debtModel);
          if (ruleDebt != null) {
            rules.add(ruleDebt);
          }
        }
      }
      return debtModelXMLExporter.export(debtModel, rules);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }
Пример #2
0
  private RuleKey createCustomRule(
      RuleKey ruleKey, NewRule newRule, RuleDto templateRuleDto, DbSession dbSession) {
    RuleDto ruleDto =
        RuleDto.createFor(ruleKey)
            .setTemplateId(templateRuleDto.getId())
            .setConfigKey(templateRuleDto.getConfigKey())
            .setName(newRule.name())
            .setDescription(newRule.markdownDescription())
            .setDescriptionFormat(Format.MARKDOWN)
            .setSeverity(newRule.severity())
            .setStatus(newRule.status())
            .setLanguage(templateRuleDto.getLanguage())
            .setDefaultSubCharacteristicId(templateRuleDto.getDefaultSubCharacteristicId())
            .setDefaultRemediationFunction(templateRuleDto.getDefaultRemediationFunction())
            .setDefaultRemediationCoefficient(templateRuleDto.getDefaultRemediationCoefficient())
            .setDefaultRemediationOffset(templateRuleDto.getDefaultRemediationOffset())
            .setEffortToFixDescription(templateRuleDto.getEffortToFixDescription())
            .setTags(templateRuleDto.getTags())
            .setSystemTags(templateRuleDto.getSystemTags());
    dbClient.ruleDao().insert(dbSession, ruleDto);

    for (RuleParamDto templateRuleParamDto :
        dbClient.ruleDao().findRuleParamsByRuleKey(dbSession, templateRuleDto.getKey())) {
      String customRuleParamValue =
          Strings.emptyToNull(newRule.parameter(templateRuleParamDto.getName()));
      createCustomRuleParams(customRuleParamValue, ruleDto, templateRuleParamDto, dbSession);
    }
    return ruleKey;
  }
 void verifyForActivation() {
   if (RuleStatus.REMOVED == rule.getStatus()) {
     throw new BadRequestException("Rule was removed: " + rule.getKey());
   }
   if (rule.isTemplate()) {
     throw new BadRequestException(
         "Rule template can't be activated on a Quality profile: " + rule.getKey());
   }
   if (rule.getKey().isManual()) {
     throw new BadRequestException(
         "Manual rule can't be activated on a Quality profile: " + rule.getKey());
   }
   if (!profile.getLanguage().equals(rule.getLanguage())) {
     throw new BadRequestException(
         String.format(
             "Rule %s and profile %s have different languages", rule.getKey(), profile.getKey()));
   }
 }