All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.db.debt.CharacteristicDao Maven / Gradle / Ivy

There is a newer version: 6.3.1
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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.db.debt;

import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.List;
import javax.annotation.CheckForNull;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;

import static com.google.common.collect.Lists.newArrayList;

public class CharacteristicDao implements Dao {

  private final MyBatis mybatis;

  public CharacteristicDao(MyBatis mybatis) {
    this.mybatis = mybatis;
  }

  /**
   * @return enabled root characteristics and characteristics
   */
  public List selectEnabledCharacteristics() {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectEnabledCharacteristics(session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public List selectEnabledCharacteristics(SqlSession session) {
    return session.getMapper(CharacteristicMapper.class).selectEnabledCharacteristics();
  }

  /**
   * @return all characteristics
   */
  public List selectCharacteristics() {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectCharacteristics(session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public List selectCharacteristics(SqlSession session) {
    return session.getMapper(CharacteristicMapper.class).selectCharacteristics();
  }

  /**
   * @return only enabled root characteristics, order by order
   */
  public List selectEnabledRootCharacteristics() {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectEnabledRootCharacteristics(session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  /**
   * @return only enabled root characteristics, order by order
   */
  public List selectEnabledRootCharacteristics(SqlSession session) {
    return session.getMapper(CharacteristicMapper.class).selectEnabledRootCharacteristics();
  }

  public List selectCharacteristicsByParentId(int parentId) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectCharacteristicsByParentId(parentId, session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public List selectCharacteristicsByParentId(int parentId, SqlSession session) {
    return session.getMapper(CharacteristicMapper.class).selectCharacteristicsByParentId(parentId);
  }

  public List selectCharacteristicsByIds(Collection ids) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectCharacteristicsByIds(ids, session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public List selectCharacteristicsByIds(Collection ids, SqlSession session) {
    List dtos = newArrayList();
    List> partitionList = Lists.partition(newArrayList(ids), 1000);
    for (List partition : partitionList) {
      dtos.addAll(session.getMapper(CharacteristicMapper.class).selectCharacteristicsByIds(partition));
    }
    return dtos;
  }

  @CheckForNull
  public CharacteristicDto selectByKey(String key) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectByKey(key, session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  @CheckForNull
  public CharacteristicDto selectByKey(String key, SqlSession session) {
    return session.getMapper(CharacteristicMapper.class).selectByKey(key);
  }

  @CheckForNull
  public CharacteristicDto selectById(int id) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectById(session, id);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  @CheckForNull
  public CharacteristicDto selectById(SqlSession session, int id) {
    return session.getMapper(CharacteristicMapper.class).selectById(id);
  }

  @CheckForNull
  public CharacteristicDto selectByName(String name) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectByName(session, name);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  @CheckForNull
  public CharacteristicDto selectByName(SqlSession session, String name) {
    return session.getMapper(CharacteristicMapper.class).selectByName(name);
  }

  public int selectMaxCharacteristicOrder() {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectMaxCharacteristicOrder(session);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public int selectMaxCharacteristicOrder(SqlSession session) {
    Integer result = session.getMapper(CharacteristicMapper.class).selectMaxCharacteristicOrder();
    return result != null ? result : 0;
  }

  public void insert(SqlSession session, CharacteristicDto dto) {
    session.getMapper(CharacteristicMapper.class).insert(dto);
  }

  public void insert(CharacteristicDto dto) {
    SqlSession session = mybatis.openSession(false);
    try {
      insert(session, dto);
      session.commit();
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public void insert(DbSession session, Collection items) {
    for (CharacteristicDto item : items) {
      insert(session, item);
    }
  }

  public void insert(DbSession session, CharacteristicDto item, CharacteristicDto... others) {
    insert(session, Lists.asList(item, others));
  }

  public void update(CharacteristicDto dto, SqlSession session) {
    session.getMapper(CharacteristicMapper.class).update(dto);
  }

  public void update(CharacteristicDto dto) {
    SqlSession session = mybatis.openSession(false);
    try {
      update(dto, session);
      session.commit();
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy