Example #1
0
  private void disableDeprecatedUserRules(DatabaseSession session) {
    List<Integer> deprecatedUserRuleIds = Lists.newLinkedList();
    deprecatedUserRuleIds.addAll(
        session
            .createQuery(
                "SELECT r.id FROM "
                    + Rule.class.getSimpleName()
                    + " r WHERE r.parent IS NOT NULL AND NOT EXISTS(FROM "
                    + Rule.class.getSimpleName()
                    + " p WHERE r.parent=p)")
            .getResultList());

    deprecatedUserRuleIds.addAll(
        session
            .createQuery(
                "SELECT r.id FROM "
                    + Rule.class.getSimpleName()
                    + " r WHERE r.parent IS NOT NULL AND EXISTS(FROM "
                    + Rule.class.getSimpleName()
                    + " p WHERE r.parent=p and p.enabled=false)")
            .getResultList());

    for (Integer deprecatedUserRuleId : deprecatedUserRuleIds) {
      Rule rule = session.getSingleResult(Rule.class, "id", deprecatedUserRuleId);
      rule.setEnabled(false);
      session.saveWithoutFlush(rule);
    }
  }
Example #2
0
  private void updateRule(Rule persistedRule, Rule rule, DatabaseSession session) {
    persistedRule.setName(rule.getName());
    persistedRule.setConfigKey(rule.getConfigKey());
    persistedRule.setDescription(rule.getDescription());
    persistedRule.setSeverity(rule.getSeverity());
    persistedRule.setEnabled(true);
    persistedRule.setCardinality(rule.getCardinality());

    // delete deprecated params
    deleteDeprecatedParameters(persistedRule, rule, session);

    // add new params and update existing params
    updateParameters(persistedRule, rule);

    session.saveWithoutFlush(persistedRule);
  }
Example #3
0
 public ValidationMessages restoreProfile(String xmlBackup) {
   ValidationMessages messages = ValidationMessages.create();
   RulesProfile profile = xmlProfileParser.parse(new StringReader(xmlBackup), messages);
   if (profile != null) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile existingProfile =
         session.getSingleResult(
             RulesProfile.class, "name", profile.getName(), "language", profile.getLanguage());
     if (existingProfile != null) {
       messages.addErrorText(
           "The profile " + profile + " already exists. Please delete it before restoring.");
     } else if (!messages.hasErrors()) {
       session.saveWithoutFlush(profile);
       session.commit();
     }
   }
   return messages;
 }
Example #4
0
 /** Important : the ruby controller has already create the profile */
 public ValidationMessages importProfile(
     String profileName, String language, String importerKey, String profileDefinition) {
   ValidationMessages messages = ValidationMessages.create();
   ProfileImporter importer = getProfileImporter(importerKey);
   RulesProfile profile = importer.importProfile(new StringReader(profileDefinition), messages);
   if (!messages.hasErrors()) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile persistedProfile =
         session.getSingleResult(RulesProfile.class, "name", profileName, "language", language);
     for (ActiveRule activeRule : profile.getActiveRules()) {
       ActiveRule persistedActiveRule =
           persistedProfile.activateRule(activeRule.getRule(), activeRule.getSeverity());
       for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
         persistedActiveRule.setParameter(activeRuleParam.getKey(), activeRuleParam.getValue());
       }
     }
     session.saveWithoutFlush(persistedProfile);
     session.commit();
   }
   return messages;
 }
Example #5
0
 private void saveNewRules(Collection<Rule> rules, DatabaseSession session) {
   for (Rule rule : rules) {
     rule.setEnabled(true);
     session.saveWithoutFlush(rule);
   }
 }