gt:     DbSession session = mybatis.openSession(false);
context: /*
   * SonarQube, open source software quality management tool.
   * Copyright (C) 2008-2014 SonarSource
   * mailto:contact AT sonarsource DOT com
   *
   * SonarQube is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or (at your option) any later version.
   *
   * SonarQube is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   */
  
  package org.sonar.core.qualityprofile.db;
  
  import com.google.common.base.Preconditions;
  import com.google.common.collect.Lists;
  import org.sonar.api.ServerComponent;
  import org.sonar.api.utils.System2;
  import org.sonar.core.component.ComponentDto;
  import org.sonar.core.persistence.DaoComponent;
  import org.sonar.core.persistence.DbSession;
  import org.sonar.core.persistence.MyBatis;
  
  import javax.annotation.CheckForNull;
  
  import java.util.Date;
  import java.util.List;
  
  public class QualityProfileDao implements ServerComponent, DaoComponent {
  
    private final MyBatis mybatis;
    private final System2 system;
  
    public QualityProfileDao(MyBatis mybatis, System2 system) {
      this.mybatis = mybatis;
      this.system = system;
    }
  
    @CheckForNull
    public QualityProfileDto getByKey(DbSession session, String key) {
      return session.getMapper(QualityProfileMapper.class).selectByKey(key);
    }
  
    public QualityProfileDto getNonNullByKey(DbSession session, String key) {
      QualityProfileDto dto = getByKey(session, key);
      if (dto == null) {
        throw new IllegalArgumentException("Quality profile not found: " + key);
      }
      return dto;
    }
  
    public List<QualityProfileDto> findAll(DbSession session) {
      return session.getMapper(QualityProfileMapper.class).selectAll();
    }
  
    public void insert(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) {
      QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class);
      doInsert(mapper, profile);
      for (QualityProfileDto other : otherProfiles) {
        doInsert(mapper, other);
      }
    }
  
    private void doInsert(QualityProfileMapper mapper, QualityProfileDto profile) {
      Preconditions.checkArgument(profile.getId() == null, "Quality profile is already persisted (got id %d)", profile.getId());
      Date now = new Date(system.now());
      profile.setCreatedAt(now);
      profile.setUpdatedAt(now);
      mapper.insert(profile);
    }
  
    /**
     * @deprecated use {@link #insert(org.sonar.core.persistence.DbSession, QualityProfileDto, QualityProfileDto...)}
     */
    @Deprecated
    public void insert(QualityProfileDto dto) {
      DbSession session = mybatis.openSession(false);
      try {
        insert(session, dto);
        session.commit();
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    public void update(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) {
      QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class);
      doUpdate(mapper, profile);
      for (QualityProfileDto otherProfile : otherProfiles) {
        doUpdate(mapper, otherProfile);
      }
    }
  
    private void doUpdate(QualityProfileMapper mapper, QualityProfileDto profile) {
      Preconditions.checkArgument(profile.getId() != null, "Quality profile is not persisted");
      profile.setUpdatedAt(new Date(system.now()));
      mapper.update(profile);
    }
  
    /**
     * @deprecated use {@link #update(DbSession, QualityProfileDto, QualityProfileDto...)}
     */
    @Deprecated
    public void update(QualityProfileDto dto) {
      DbSession session = mybatis.openSession(false);
      try {
        update(session, dto);
        session.commit();
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    public void delete(DbSession session, QualityProfileDto profile, QualityProfileDto... otherProfiles) {
      QualityProfileMapper mapper = session.getMapper(QualityProfileMapper.class);
      doDelete(mapper, profile);
      for (QualityProfileDto otherProfile : otherProfiles) {
        doDelete(mapper, otherProfile);
      }
    }
  
    private void doDelete(QualityProfileMapper mapper, QualityProfileDto profile) {
      Preconditions.checkNotNull(profile.getId(), "Quality profile is not persisted");
      mapper.delete(profile.getId());
    }
  
    /**
     * @deprecated use {@link #delete(DbSession, QualityProfileDto, QualityProfileDto...)}
     */
    @Deprecated
    public void delete(int id, DbSession session) {
      session.getMapper(QualityProfileMapper.class).delete(id);
    }
  
    /**
     * @deprecated use {@link #delete(DbSession, QualityProfileDto, QualityProfileDto...)}
     */
    @Deprecated
    public void delete(int id) {
      DbSession session = mybatis.openSession(false);
      try {
        delete(id, session);
        session.commit();
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    /**
     * @deprecated Replaced by
     *    {@link #findAll(DbSession)}
     */
    @Deprecated
    public List<QualityProfileDto> findAll() {
      DbSession session = mybatis.openSession(false);
      try {
        return session.getMapper(QualityProfileMapper.class).selectAll();
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    @CheckForNull
    public QualityProfileDto getDefaultProfile(String language, DbSession session) {
      return session.getMapper(QualityProfileMapper.class).selectDefaultProfile(language, String.format("sonar.profile.%s", language));
    }
  
    @CheckForNull
    public QualityProfileDto getDefaultProfile(String language) {
      DbSession session = mybatis.openSession(false);
      try {
        return getDefaultProfile(language, session);
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    @CheckForNull
    public QualityProfileDto getByProjectAndLanguage(long projectId, String language, String key) {
      DbSession session = mybatis.openSession(false);
      try {
        return session.getMapper(QualityProfileMapper.class).selectByProjectIdAndLanguage(projectId, language, key);
      } finally {
        MyBatis.closeQuietly(session);
      }
    }
  
    @CheckForNull
    public QualityProfileDto getByProjectAndLanguage(String projectKey, String language, String propertyKeyPrefix, DbSession session) {
      return session.getMapper(QualityProfileMapper.class).selectByProjectAndLanguage(projectKey, language, propertyKeyPrefix);
    }
  
    public List<QualityProfileDto> findByLanguage(String language) {
  