BulkChangeResult bulkActivate(
      RuleQuery ruleQuery, QualityProfileKey profileKey, @Nullable String severity) {
    BulkChangeResult result = new BulkChangeResult();
    RuleIndex ruleIndex = index.get(RuleIndex.class);
    DbSession dbSession = db.openSession(false);
    try {
      Result<Rule> ruleSearchResult =
          ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
      Iterator<Rule> rules = ruleSearchResult.scroll();
      while (rules.hasNext()) {
        Rule rule = rules.next();
        try {
          ActiveRuleKey key = ActiveRuleKey.of(profileKey, rule.key());
          RuleActivation activation = new RuleActivation(key);
          activation.setSeverity(severity);
          List<ActiveRuleChange> changes = activate(dbSession, activation);
          result.addChanges(changes);
          result.incrementSucceeded();

        } catch (BadRequestException e) {
          // other exceptions stop the bulk activation
          result.incrementFailed();
          // TODO result.addMessage
        }
      }
      dbSession.commit();
    } finally {
      dbSession.close();
    }
    return result;
  }
  @Override
  public void handle(Request request, Response response) throws Exception {
    DbSession session = dbClient.openSession(false);
    try {
      String profileKey =
          QProfileIdentificationParamUtils.getProfileKeyFromParameters(
              request, profileFactory, session);
      if (dbClient.qualityProfileDao().getByKey(session, profileKey) == null) {
        throw new NotFoundException(
            String.format("Could not find a profile with key '%s'", profileKey));
      }

      QProfileActivityQuery query = new QProfileActivityQuery().setQprofileKey(profileKey);
      Date since = request.paramAsDateTime(PARAM_SINCE);
      if (since != null) {
        query.setSince(since);
      }
      Date to = request.paramAsDateTime(PARAM_TO);
      if (to != null) {
        query.setTo(to);
      }
      SearchOptions options = new SearchOptions();

      int page = request.mandatoryParamAsInt(Param.PAGE);
      options.setPage(page, request.mandatoryParamAsInt(Param.PAGE_SIZE));

      Result<QProfileActivity> result = searchActivities(query, options);
      writeResponse(
          response.newJsonWriter(),
          result,
          Paging.create(options.getLimit(), page, (int) result.getTotal()));
    } finally {
      session.close();
    }
  }
 public boolean rename(String key, String newName) {
   Verifications.check(StringUtils.isNotBlank(newName), "Name must be set");
   Verifications.check(
       newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
   DbSession dbSession = db.openSession(false);
   try {
     QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key);
     if (!StringUtils.equals(newName, profile.getName())) {
       if (db.qualityProfileDao().getByNameAndLanguage(newName, profile.getLanguage(), dbSession)
           != null) {
         throw new BadRequestException("Quality profile already exists: " + newName);
       }
       String previousName = profile.getName();
       profile.setName(newName);
       db.qualityProfileDao().update(dbSession, profile);
       db.propertiesDao()
           .updateProperties(
               PROFILE_PROPERTY_PREFIX + profile.getLanguage(), previousName, newName, dbSession);
       dbSession.commit();
       return true;
     }
     return false;
   } finally {
     dbSession.close();
   }
 }
 QualityProfileDto getByNameAndLanguage(String name, String language) {
   DbSession dbSession = db.openSession(false);
   try {
     return getByNameAndLanguage(dbSession, name, language);
   } finally {
     dbSession.close();
   }
 }
 QualityProfileDto getByProjectAndLanguage(String projectKey, String language) {
   DbSession dbSession = db.openSession(false);
   try {
     return getByProjectAndLanguage(dbSession, projectKey, language);
   } finally {
     dbSession.close();
   }
 }
 void setDefault(String profileKey) {
   DbSession dbSession = db.openSession(false);
   try {
     setDefault(dbSession, profileKey);
   } finally {
     dbSession.close();
   }
 }
 void delete(String key) {
   DbSession session = db.openSession(false);
   try {
     delete(session, key, false);
     session.commit();
   } finally {
     session.close();
   }
 }
 @CheckForNull
 QualityProfileDto getDefault(String language) {
   DbSession dbSession = db.openSession(false);
   try {
     return getDefault(dbSession, language);
   } finally {
     dbSession.close();
   }
 }
 /**
  * Activate a rule on a Quality profile. Update configuration (severity/parameters) if the rule is
  * already activated.
  *
  * @throws org.sonar.server.exceptions.BadRequestException if the profile, the rule or a rule
  *     parameter does not exist
  */
 public List<ActiveRuleChange> activate(RuleActivation activation) {
   DbSession dbSession = db.openSession(false);
   try {
     return activate(dbSession, activation);
   } finally {
     dbSession.commit();
     dbSession.close();
   }
 }
 /**
  * Deactivate a rule on a Quality profile. Does nothing if the rule is not activated, but fails
  * (fast) if the rule or the profile does not exist.
  */
 List<ActiveRuleChange> deactivate(ActiveRuleKey key) {
   DbSession dbSession = db.openSession(false);
   try {
     List<ActiveRuleChange> changes = deactivate(dbSession, key);
     dbSession.commit();
     return changes;
   } finally {
     dbSession.close();
   }
 }
  void setParent(QualityProfileKey key, @Nullable QualityProfileKey parentKey) {
    DbSession dbSession = db.openSession(false);
    try {
      setParent(dbSession, key, parentKey);
      dbSession.commit();

    } finally {
      dbSession.close();
    }
  }
  public RuleKey create(NewRule newRule) {
    DbSession dbSession = dbClient.openSession(false);
    try {

      if (newRule.isCustom()) {
        return createCustomRule(newRule, dbSession);
      }

      if (newRule.isManual()) {
        return createManualRule(newRule, dbSession);
      }

      throw new IllegalStateException("Only custom rule and manual rule can be created");
    } finally {
      dbSession.close();
    }
  }
 BulkChangeResult bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) {
   DbSession dbSession = db.openSession(false);
   try {
     RuleIndex ruleIndex = index.get(RuleIndex.class);
     BulkChangeResult result = new BulkChangeResult();
     Result<Rule> ruleSearchResult =
         ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
     Iterator<Rule> rules = ruleSearchResult.scroll();
     while (rules.hasNext()) {
       Rule rule = rules.next();
       ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key());
       List<ActiveRuleChange> changes = deactivate(dbSession, key);
       result.addChanges(changes);
       result.incrementSucceeded();
     }
     dbSession.commit();
     return result;
   } finally {
     dbSession.close();
   }
 }
  private Result<QProfileActivity> searchActivities(
      QProfileActivityQuery query, SearchOptions options) {
    DbSession session = dbClient.openSession(false);
    try {
      SearchResponse response = activityIndex.doSearch(query, options);
      Result<QProfileActivity> result = new Result<>(response);
      for (SearchHit hit : response.getHits().getHits()) {
        QProfileActivity profileActivity = new QProfileActivity(hit.getSource());
        RuleDto ruleDto = dbClient.ruleDao().getNullableByKey(session, profileActivity.ruleKey());
        profileActivity.ruleName(ruleDto != null ? ruleDto.getName() : null);

        String login = profileActivity.getLogin();
        if (login != null) {
          UserDto user = dbClient.userDao().selectActiveUserByLogin(session, login);
          profileActivity.authorName(user != null ? user.getName() : null);
        }
        result.getHits().add(profileActivity);
      }
      return result;
    } finally {
      session.close();
    }
  }
 @After
 public void tearDown() throws Exception {
   session.close();
 }
 @After
 public void after() throws Exception {
   dbSession.close();
 }
 @After
 public void after() {
   dbSession.close();
 }