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

org.sonar.db.user.GroupMembershipDao 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.user;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.Dao;
import org.sonar.db.DatabaseUtils;
import org.sonar.db.DbSession;
import org.sonar.db.MyBatis;

public class GroupMembershipDao implements Dao {

  private final MyBatis mybatis;

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

  // TODO Remove this method and associated client code when the UI is migrated to Backbone
  public List selectGroups(GroupMembershipQuery query, Long userId, int offset, int limit) {
    SqlSession session = mybatis.openSession(false);
    try {
      return selectGroups(session, query, userId, offset, limit);
    } finally {
      MyBatis.closeQuietly(session);
    }
  }

  public List selectGroups(SqlSession session, GroupMembershipQuery query, Long userId, int offset, int limit) {
    Map params = ImmutableMap.of("query", query, "userId", userId);
    return mapper(session).selectGroups(params, new RowBounds(offset, limit));
  }

  public int countGroups(SqlSession session, GroupMembershipQuery query, Long userId) {
    Map params = ImmutableMap.of("query", query, "userId", userId);
    return mapper(session).countGroups(params);
  }

  public List selectMembers(SqlSession session, UserMembershipQuery query, int offset, int limit) {
    Map params = ImmutableMap.of("query", query, "groupId", query.groupId());
    return mapper(session).selectMembers(params, new RowBounds(offset, limit));
  }

  public int countMembers(SqlSession session, UserMembershipQuery query) {
    Map params = ImmutableMap.of("query", query, "groupId", query.groupId());
    return mapper(session).countMembers(params);
  }

  public Map countUsersByGroups(final DbSession session, Collection groupIds) {
    final Map result = Maps.newHashMap();
    DatabaseUtils.executeLargeInputs(groupIds, new Function, List>() {
      @Override
      public List apply(@Nonnull List input) {
        List userCounts = mapper(session).countUsersByGroup(input);
        for (GroupUserCount count : userCounts) {
          result.put(count.groupName(), count.userCount());
        }
        return userCounts;
      }
    });

    return result;
  }

  public Multimap selectGroupsByLogins(final DbSession session, Collection logins) {
    final Multimap result = ArrayListMultimap.create();
    DatabaseUtils.executeLargeInputs(logins, new Function, List>() {
      @Override
      public List apply(@Nonnull List input) {
        List groupMemberships = mapper(session).selectGroupsByLogins(input);
        for (LoginGroup membership : groupMemberships) {
          result.put(membership.login(), membership.groupName());
        }
        return groupMemberships;
      }
    });

    return result;
  }

  @VisibleForTesting
  List selectGroups(GroupMembershipQuery query, Long userId) {
    return selectGroups(query, userId, 0, Integer.MAX_VALUE);
  }

  private GroupMembershipMapper mapper(SqlSession session) {
    return session.getMapper(GroupMembershipMapper.class);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy