Search in sources :

Example 1 with GroupMembershipQuery

use of org.sonar.db.user.GroupMembershipQuery in project sonarqube by SonarSource.

the class GroupsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn().checkIsSystemAdministrator();
    String login = request.mandatoryParam(PARAM_LOGIN);
    int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
    int page = request.mandatoryParamAsInt(Param.PAGE);
    String queryString = request.param(Param.TEXT_QUERY);
    String selected = request.mandatoryParam(Param.SELECTED);
    GroupMembershipQuery query = GroupMembershipQuery.builder().login(login).groupSearch(queryString).membership(getMembership(selected)).pageIndex(page).pageSize(pageSize).build();
    try (DbSession dbSession = dbClient.openSession(false)) {
        UserDto user = dbClient.userDao().selectByLogin(dbSession, login);
        if (user == null) {
            throw new NotFoundException(String.format("User with login '%s' has not been found", login));
        }
        int total = dbClient.groupMembershipDao().countGroups(dbSession, query, user.getId());
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
        List<GroupMembershipDto> groups = dbClient.groupMembershipDao().selectGroups(dbSession, query, user.getId(), paging.offset(), pageSize);
        JsonWriter json = response.newJsonWriter().beginObject();
        writeGroups(json, groups);
        writePaging(json, paging);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) GroupMembershipQuery(org.sonar.db.user.GroupMembershipQuery) GroupMembershipDto(org.sonar.db.user.GroupMembershipDto) UserDto(org.sonar.db.user.UserDto) Paging(org.sonar.api.utils.Paging) NotFoundException(org.sonar.server.exceptions.NotFoundException) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Aggregations

Paging (org.sonar.api.utils.Paging)1 JsonWriter (org.sonar.api.utils.text.JsonWriter)1 DbSession (org.sonar.db.DbSession)1 GroupMembershipDto (org.sonar.db.user.GroupMembershipDto)1 GroupMembershipQuery (org.sonar.db.user.GroupMembershipQuery)1 UserDto (org.sonar.db.user.UserDto)1 NotFoundException (org.sonar.server.exceptions.NotFoundException)1