Exemplo n.º 1
0
  @CheckForNull
  private static RuleDebt toRuleDebt(RuleDto rule, DebtModel debtModel) {
    RuleDebt ruleDebt =
        new RuleDebt().setRuleKey(RuleKey.of(rule.getRepositoryKey(), rule.getRuleKey()));
    Integer effectiveSubCharacteristicId =
        rule.getSubCharacteristicId() != null
            ? rule.getSubCharacteristicId()
            : rule.getDefaultSubCharacteristicId();
    DebtCharacteristic subCharacteristic =
        (effectiveSubCharacteristicId != null
                && !RuleDto.DISABLED_CHARACTERISTIC_ID.equals(effectiveSubCharacteristicId))
            ? debtModel.characteristicById(effectiveSubCharacteristicId)
            : null;
    if (subCharacteristic != null) {
      ruleDebt.setSubCharacteristicKey(subCharacteristic.key());

      String overriddenFunction = rule.getRemediationFunction();
      String defaultFunction = rule.getDefaultRemediationFunction();
      if (overriddenFunction != null) {
        ruleDebt.setFunction(overriddenFunction);
        ruleDebt.setCoefficient(rule.getRemediationCoefficient());
        ruleDebt.setOffset(rule.getRemediationOffset());
        return ruleDebt;
      } else if (defaultFunction != null) {
        ruleDebt.setFunction(defaultFunction);
        ruleDebt.setCoefficient(rule.getDefaultRemediationCoefficient());
        ruleDebt.setOffset(rule.getDefaultRemediationOffset());
        return ruleDebt;
      }
    }
    return null;
  }
Exemplo n.º 2
0
  private void restoreRules(
      List<CharacteristicDto> allCharacteristicDtos,
      List<RuleDto> rules,
      List<RuleDebt> ruleDebts,
      ValidationMessages validationMessages,
      Date updateDate,
      DbSession session) {
    for (RuleDto rule : rules) {
      RuleDebt ruleDebt = ruleDebt(rule.getRepositoryKey(), rule.getRuleKey(), ruleDebts);
      String subCharacteristicKey = ruleDebt != null ? ruleDebt.subCharacteristicKey() : null;
      CharacteristicDto subCharacteristicDto =
          subCharacteristicKey != null
              ? characteristicByKey(ruleDebt.subCharacteristicKey(), allCharacteristicDtos, true)
              : null;
      ruleOperations.updateRule(
          rule,
          subCharacteristicDto,
          ruleDebt != null ? ruleDebt.function() : null,
          ruleDebt != null ? ruleDebt.coefficient() : null,
          ruleDebt != null ? ruleDebt.offset() : null,
          session);
      rule.setUpdatedAt(updateDate);
      ruleDebts.remove(ruleDebt);
    }

    for (RuleDebt ruleDebt : ruleDebts) {
      validationMessages.addWarningText(
          String.format("The rule '%s' does not exist.", ruleDebt.ruleKey()));
    }
  }
Exemplo n.º 3
0
  private void resetRules(
      List<RuleDto> ruleDtos,
      List<RulesDefinition.Rule> rules,
      List<CharacteristicDto> allCharacteristicDtos,
      Date updateDate,
      DbSession session) {
    for (RuleDto rule : ruleDtos) {
      // Restore default debt definitions

      RulesDefinition.Rule ruleDef;
      if (rule.getTemplateId() != null) {
        RuleDto templateRule = rule(rule.getTemplateId(), ruleDtos);
        ruleDef = ruleDef(templateRule.getRepositoryKey(), templateRule.getRuleKey(), rules);
      } else {
        ruleDef = ruleDef(rule.getRepositoryKey(), rule.getRuleKey(), rules);
      }

      if (ruleDef != null) {
        String subCharacteristicKey = ruleDef.debtSubCharacteristic();
        CharacteristicDto subCharacteristicDto =
            characteristicByKey(subCharacteristicKey, allCharacteristicDtos, false);
        DebtRemediationFunction remediationFunction = ruleDef.debtRemediationFunction();
        boolean hasDebtDefinition = subCharacteristicDto != null && remediationFunction != null;

        rule.setDefaultSubCharacteristicId(hasDebtDefinition ? subCharacteristicDto.getId() : null);
        rule.setDefaultRemediationFunction(
            hasDebtDefinition ? remediationFunction.type().name() : null);
        rule.setDefaultRemediationCoefficient(
            hasDebtDefinition ? remediationFunction.coefficient() : null);
        rule.setDefaultRemediationOffset(hasDebtDefinition ? remediationFunction.offset() : null);
      }

      // Reset overridden debt definitions
      rule.setSubCharacteristicId(null);
      rule.setRemediationFunction(null);
      rule.setRemediationCoefficient(null);
      rule.setRemediationOffset(null);
      rule.setUpdatedAt(updateDate);
      dbClient.ruleDao().update(session, rule);
    }
  }
Exemplo n.º 4
0
 @Override
 public boolean equals(Object obj) {
   if (!(obj instanceof RuleDto)) {
     return false;
   }
   if (this == obj) {
     return true;
   }
   RuleDto other = (RuleDto) obj;
   return new EqualsBuilder()
       .append(repositoryKey, other.getRepositoryKey())
       .append(ruleKey, other.getRuleKey())
       .isEquals();
 }
Exemplo n.º 5
0
  private RuleKey createCustomRule(NewRule newRule, DbSession dbSession) {
    RuleKey templateKey = newRule.templateKey();
    if (templateKey == null) {
      throw new IllegalArgumentException("Rule template key should not be null");
    }
    RuleDto templateRule = dbClient.ruleDao().getByKey(dbSession, templateKey);
    if (!templateRule.isTemplate()) {
      throw new IllegalArgumentException(
          "This rule is not a template rule: " + templateKey.toString());
    }
    validateCustomRule(newRule, dbSession, templateKey);

    RuleKey customRuleKey = RuleKey.of(templateRule.getRepositoryKey(), newRule.ruleKey());

    RuleDto existingRule = loadRule(customRuleKey, dbSession);
    if (existingRule != null) {
      updateExistingRule(existingRule, newRule, dbSession);
    } else {
      createCustomRule(customRuleKey, newRule, templateRule, dbSession);
    }

    dbSession.commit();
    return customRuleKey;
  }