Пример #1
0
 public ValidationMessages changeParentProfile(
     Integer profileId, String parentName, String userName) {
   ValidationMessages messages = ValidationMessages.create();
   RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId);
   if (profile != null && !profile.getProvided()) {
     RulesProfile oldParent = getParentProfile(profile);
     RulesProfile newParent = getProfile(profile.getLanguage(), parentName);
     if (isCycle(profile, newParent)) {
       messages.addWarningText("Please do not select a child profile as parent.");
       return messages;
     }
     // Deactivate all inherited rules
     if (oldParent != null) {
       for (ActiveRule activeRule : oldParent.getActiveRules()) {
         deactivate(profile, activeRule.getRule(), userName);
       }
     }
     // Activate all inherited rules
     if (newParent != null) {
       for (ActiveRule activeRule : newParent.getActiveRules()) {
         activateOrChange(profile, activeRule, userName);
       }
     }
     profile.setParentName(newParent == null ? null : newParent.getName());
     getSession().saveWithoutFlush(profile);
     getSession().commit();
   }
   return messages;
 }
Пример #2
0
 /** Rule was deactivated in parent profile. */
 public void deactivated(int parentProfileId, int deactivatedRuleId, String userName) {
   ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, deactivatedRuleId);
   RulesProfile profile = getSession().getEntity(RulesProfile.class, parentProfileId);
   ruleDisabled(profile, parentActiveRule, userName);
   for (RulesProfile child : getChildren(parentProfileId)) {
     deactivate(child, parentActiveRule.getRule(), userName);
   }
   getSession().commit();
 }
Пример #3
0
 /** Rule was activated/changed in parent profile. */
 private void activatedOrChanged(int parentProfileId, int activeRuleId, String userName) {
   ActiveRule parentActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId);
   if (parentActiveRule.isInherited()) {
     parentActiveRule.setInheritance(ActiveRule.OVERRIDES);
     getSession().saveWithoutFlush(parentActiveRule);
   }
   for (RulesProfile child : getChildren(parentProfileId)) {
     activateOrChange(child, parentActiveRule, userName);
   }
   getSession().commit();
 }
Пример #4
0
  private void activateOrChange(
      RulesProfile profile, ActiveRule parentActiveRule, String userName) {
    ActiveRule oldActiveRule = profile.getActiveRule(parentActiveRule.getRule());
    if (oldActiveRule != null) {
      if (oldActiveRule.isInherited()) {
        removeActiveRule(profile, oldActiveRule);
      } else {
        oldActiveRule.setInheritance(ActiveRule.OVERRIDES);
        getSession().saveWithoutFlush(oldActiveRule);
        return; // no need to change in children
      }
    }
    ActiveRule newActiveRule = (ActiveRule) parentActiveRule.clone();
    newActiveRule.setRulesProfile(profile);
    newActiveRule.setInheritance(ActiveRule.INHERITED);
    profile.addActiveRule(newActiveRule);
    getSession().saveWithoutFlush(newActiveRule);

    if (oldActiveRule != null) {
      ruleChanged(profile, oldActiveRule, newActiveRule, userName);
    } else {
      ruleEnabled(profile, newActiveRule, userName);
    }

    for (RulesProfile child : getChildren(profile)) {
      activateOrChange(child, newActiveRule, userName);
    }
  }
Пример #5
0
  /** Rule severity was changed */
  public void ruleSeverityChanged(
      int profileId,
      int activeRuleId,
      RulePriority oldSeverity,
      RulePriority newSeverity,
      String userName) {
    ActiveRule activeRule = getSession().getEntity(ActiveRule.class, activeRuleId);
    RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId);

    ruleSeverityChanged(profile, activeRule.getRule(), oldSeverity, newSeverity, userName);

    // Notify child profiles
    activatedOrChanged(profileId, activeRuleId, userName);
  }
Пример #6
0
 /** Deal with creation of ActiveRuleChange item when a rule is disabled on a profile */
 private void ruleDisabled(RulesProfile profile, ActiveRule disabledRule, String userName) {
   incrementProfileVersionIfNeeded(profile);
   ActiveRuleChange rc = new ActiveRuleChange(userName, profile, disabledRule.getRule());
   rc.setEnabled(false);
   rc.setOldSeverity(disabledRule.getSeverity());
   if (disabledRule.getRule().getParams() != null) {
     for (RuleParam p : disabledRule.getRule().getParams()) {
       String oldParam = disabledRule.getParameter(p.getKey());
       if (oldParam != null) {
         rc.setParameterChange(p.getKey(), oldParam, null);
       }
     }
   }
   getSession().saveWithoutFlush(rc);
 }
Пример #7
0
 /** Deal with creation of ActiveRuleChange item when a rule is enabled on a profile */
 private void ruleEnabled(RulesProfile profile, ActiveRule newActiveRule, String userName) {
   incrementProfileVersionIfNeeded(profile);
   ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule());
   rc.setEnabled(true);
   rc.setNewSeverity(newActiveRule.getSeverity());
   if (newActiveRule.getRule().getParams() != null) {
     for (RuleParam p : newActiveRule.getRule().getParams()) {
       String newParam = newActiveRule.getParameter(p.getKey());
       if (newParam != null) {
         rc.setParameterChange(p.getKey(), null, newParam);
       }
     }
   }
   getSession().saveWithoutFlush(rc);
 }
Пример #8
0
  /** Rule param was changed */
  public void ruleParamChanged(
      int profileId,
      int activeRuleId,
      String paramKey,
      String oldValue,
      String newValue,
      String userName) {
    ActiveRule activeRule = getSession().getEntity(ActiveRule.class, activeRuleId);
    RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId);

    ruleParamChanged(profile, activeRule.getRule(), paramKey, oldValue, newValue, userName);

    // Notify child profiles
    activatedOrChanged(profileId, activeRuleId, userName);
  }
Пример #9
0
  private void deactivate(RulesProfile profile, Rule rule, String userName) {
    ActiveRule activeRule = profile.getActiveRule(rule);
    if (activeRule != null) {
      if (activeRule.isInherited()) {
        ruleDisabled(profile, activeRule, userName);
        removeActiveRule(profile, activeRule);
      } else {
        activeRule.setInheritance(null);
        getSession().saveWithoutFlush(activeRule);
        return; // no need to change in children
      }

      for (RulesProfile child : getChildren(profile)) {
        deactivate(child, rule, userName);
      }
    }
  }
Пример #10
0
  private List<ProfileDefinition> loadFromDeprecatedRepository(RulesRepository repository) {
    List<ProfileDefinition> result = new ArrayList<ProfileDefinition>();

    for (int index = 0; index < repository.getProvidedProfiles().size(); index++) {
      RulesProfile deprecated = (RulesProfile) repository.getProvidedProfiles().get(index);
      DefaultProfileDefinition providedProfile =
          DefaultProfileDefinition.create(deprecated.getName(), repository.getLanguage().getKey());
      for (ActiveRule deprecatedActiveRule : deprecated.getActiveRules(true)) {
        String repositoryKey = deprecatedActiveRule.getRepositoryKey();
        if (StringUtils.isBlank(repositoryKey)) {
          repositoryKey = getPluginKey(repository);
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, deprecatedActiveRule.getRuleKey());
        if (rule != null) {
          ActiveRule activeRule =
              providedProfile.activateRule(rule, deprecatedActiveRule.getSeverity());
          for (ActiveRuleParam arp : deprecatedActiveRule.getActiveRuleParams()) {
            activeRule.setParameter(arp.getKey(), arp.getValue());
          }
        }
      }
      result.add(providedProfile);
    }
    return result;
  }
Пример #11
0
 private ProfileDefinition loadFromDeprecatedCheckProfile(CheckProfile cp) {
   DefaultProfileDefinition definition =
       DefaultProfileDefinition.create(cp.getName(), cp.getLanguage());
   for (Check check : cp.getChecks()) {
     RulePriority priority = null;
     if (check.getPriority() != null) {
       priority = RulePriority.fromCheckPriority(check.getPriority());
     }
     Rule rule = ruleFinder.findByKey(check.getRepositoryKey(), check.getTemplateKey());
     if (rule != null) {
       ActiveRule activeRule = definition.activateRule(rule, priority);
       for (Map.Entry<String, String> entry : check.getProperties().entrySet()) {
         activeRule.setParameter(entry.getKey(), entry.getValue());
       }
     }
   }
   return definition;
 }
Пример #12
0
 private void importActiveRules(RulesDao rulesDao, RulesProfile profile) {
   for (Iterator<ActiveRule> iar = profile.getActiveRules(true).iterator(); iar.hasNext(); ) {
     ActiveRule activeRule = iar.next();
     Rule unMarshalledRule = activeRule.getRule();
     Rule matchingRuleInDb =
         rulesDao.getRuleByKey(unMarshalledRule.getRepositoryKey(), unMarshalledRule.getKey());
     if (matchingRuleInDb == null) {
       LoggerFactory.getLogger(getClass())
           .error(
               "Unable to find active rule "
                   + unMarshalledRule.getRepositoryKey()
                   + ":"
                   + unMarshalledRule.getKey());
       iar.remove();
       continue;
     }
     activeRule.setRule(matchingRuleInDb);
     activeRule.setRulesProfile(profile);
     activeRule.getActiveRuleParams();
     for (Iterator<ActiveRuleParam> irp = activeRule.getActiveRuleParams().iterator();
         irp.hasNext(); ) {
       ActiveRuleParam activeRuleParam = irp.next();
       RuleParam unMarshalledRP = activeRuleParam.getRuleParam();
       RuleParam matchingRPInDb = rulesDao.getRuleParam(matchingRuleInDb, unMarshalledRP.getKey());
       if (matchingRPInDb == null) {
         LoggerFactory.getLogger(getClass())
             .error("Unable to find active rule parameter " + unMarshalledRP.getKey());
         irp.remove();
         continue;
       }
       activeRuleParam.setActiveRule(activeRule);
       activeRuleParam.setRuleParam(matchingRPInDb);
     }
   }
 }
  private void verifyOneActiveRule(
      String profileKey,
      String expectedSeverity,
      @Nullable String expectedInheritance,
      Map<String, String> expectedParams) {

    List<ActiveRule> activeRules = Lists.newArrayList(index.findByProfile(profileKey));
    assertThat(activeRules).hasSize(1);
    ActiveRule activeRule = activeRules.get(0);
    assertThat(activeRule.severity()).isEqualTo(expectedSeverity);
    assertThat(activeRule.inheritance())
        .isEqualTo(
            expectedInheritance == null
                ? ActiveRule.Inheritance.NONE
                : ActiveRule.Inheritance.valueOf(expectedInheritance));

    // verify parameters
    assertThat(activeRule.params()).hasSize(expectedParams.size());
    for (Map.Entry<String, String> entry : expectedParams.entrySet()) {
      String value = activeRule.params().get(entry.getKey());
      assertThat(value).isEqualTo(entry.getValue());
    }
  }
Пример #14
0
  /**
   * Deal with creation of ActiveRuleChange item when a rule is changed (severity and/or param(s))
   * on a profile
   */
  private void ruleChanged(
      RulesProfile profile, ActiveRule oldActiveRule, ActiveRule newActiveRule, String userName) {
    incrementProfileVersionIfNeeded(profile);
    ActiveRuleChange rc = new ActiveRuleChange(userName, profile, newActiveRule.getRule());

    if (oldActiveRule.getSeverity() != newActiveRule.getSeverity()) {
      rc.setOldSeverity(oldActiveRule.getSeverity());
      rc.setNewSeverity(newActiveRule.getSeverity());
    }
    if (oldActiveRule.getRule().getParams() != null) {
      for (RuleParam p : oldActiveRule.getRule().getParams()) {
        String oldParam = oldActiveRule.getParameter(p.getKey());
        String newParam = newActiveRule.getParameter(p.getKey());
        if (!StringUtils.equals(oldParam, newParam)) {
          rc.setParameterChange(p.getKey(), oldParam, newParam);
        }
      }
    }

    getSession().saveWithoutFlush(rc);
  }
Пример #15
0
  public void revert(int profileId, int activeRuleId, String userName) {
    RulesProfile profile = getSession().getEntity(RulesProfile.class, profileId);
    ActiveRule oldActiveRule = getSession().getEntity(ActiveRule.class, activeRuleId);
    if (oldActiveRule != null && oldActiveRule.doesOverride()) {
      ActiveRule parentActiveRule =
          getParentProfile(profile).getActiveRule(oldActiveRule.getRule());
      removeActiveRule(profile, oldActiveRule);
      ActiveRule newActiveRule = (ActiveRule) parentActiveRule.clone();
      newActiveRule.setRulesProfile(profile);
      newActiveRule.setInheritance(ActiveRule.INHERITED);
      profile.addActiveRule(newActiveRule);
      getSession().saveWithoutFlush(newActiveRule);

      // Compute change
      ruleChanged(profile, oldActiveRule, newActiveRule, userName);

      for (RulesProfile child : getChildren(profile)) {
        activateOrChange(child, newActiveRule, userName);
      }

      getSession().commit();
    }
  }