Search in sources :

Example 1 with GroupMembershipDto

use of org.sonar.db.user.GroupMembershipDto 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)

Example 2 with GroupMembershipDto

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

the class GroupsAction method writeGroups.

private static void writeGroups(JsonWriter json, List<GroupMembershipDto> groups) {
    json.name("groups").beginArray();
    for (GroupMembershipDto group : groups) {
        json.beginObject().prop(FIELD_ID, group.getId().toString()).prop(FIELD_NAME, group.getName()).prop(FIELD_DESCRIPTION, group.getDescription()).prop(FIELD_SELECTED, group.getUserId() != null).endObject();
    }
    json.endArray();
}
Also used : GroupMembershipDto(org.sonar.db.user.GroupMembershipDto)

Aggregations

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