Search in sources :

Example 6 with Paging

use of org.sonar.api.utils.Paging in project sonarqube by SonarSource.

the class UsersAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION));
        Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
        checkProjectAdmin(userSession, org.getUuid(), projectId);
        PermissionQuery query = buildPermissionQuery(request, projectId);
        List<UserDto> users = findUsers(dbSession, org, query);
        int total = dbClient.userPermissionDao().countUsers(dbSession, org.getUuid(), query);
        List<UserPermissionDto> userPermissions = findUserPermissions(dbSession, org, users, projectId);
        Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(query.getPageSize()).andTotal(total);
        UsersWsResponse usersWsResponse = buildResponse(users, userPermissions, paging);
        writeProtobuf(usersWsResponse, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserDto(org.sonar.db.user.UserDto) Paging(org.sonar.api.utils.Paging) ProjectId(org.sonar.server.permission.ProjectId) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) PermissionQuery(org.sonar.db.permission.PermissionQuery) UsersWsResponse(org.sonarqube.ws.WsPermissions.UsersWsResponse)

Example 7 with Paging

use of org.sonar.api.utils.Paging in project sonarqube by SonarSource.

the class SearchAction method doHandle.

private SearchWsResponse doHandle(SearchWsRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto organization = support.getOrganization(dbSession, ofNullable(request.getOrganization()).orElseGet(defaultOrganizationProvider.get()::getKey));
        userSession.checkPermission(OrganizationPermission.ADMINISTER, organization);
        ComponentQuery query = buildQuery(request);
        Paging paging = buildPaging(dbSession, request, organization, query);
        List<ComponentDto> components = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), query, paging.offset(), paging.pageSize());
        return buildResponse(components, organization, paging);
    }
}
Also used : DbSession(org.sonar.db.DbSession) ComponentQuery(org.sonar.db.component.ComponentQuery) Paging(org.sonar.api.utils.Paging) ComponentDto(org.sonar.db.component.ComponentDto) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 8 with Paging

use of org.sonar.api.utils.Paging in project sonarqube by SonarSource.

the class ProjectsAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    String profileKey = request.mandatoryParam(PARAM_KEY);
    DbSession session = dbClient.openSession(false);
    try {
        checkProfileExists(profileKey, session);
        String selected = request.param(Param.SELECTED);
        String query = request.param(PARAM_QUERY);
        int pageSize = request.mandatoryParamAsInt(PARAM_PAGE_SIZE);
        int page = request.mandatoryParamAsInt(PARAM_PAGE);
        List<ProjectQprofileAssociationDto> projects = loadProjects(profileKey, session, selected, query);
        Collections.sort(projects, new Comparator<ProjectQprofileAssociationDto>() {

            @Override
            public int compare(ProjectQprofileAssociationDto o1, ProjectQprofileAssociationDto o2) {
                return new CompareToBuilder().append(o1.getProjectName(), o2.getProjectName()).append(o1.getProjectUuid(), o2.getProjectUuid()).toComparison();
            }
        });
        Collection<Long> projectIds = Collections2.transform(projects, new NonNullInputFunction<ProjectQprofileAssociationDto, Long>() {

            @Override
            protected Long doApply(ProjectQprofileAssociationDto input) {
                return input.getProjectId();
            }
        });
        Collection<Long> authorizedProjectIds = dbClient.authorizationDao().keepAuthorizedProjectIds(session, projectIds, userSession.getUserId(), UserRole.USER);
        Iterable<ProjectQprofileAssociationDto> authorizedProjects = Iterables.filter(projects, new Predicate<ProjectQprofileAssociationDto>() {

            @Override
            public boolean apply(ProjectQprofileAssociationDto input) {
                return authorizedProjectIds.contains(input.getProjectId());
            }
        });
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(authorizedProjectIds.size());
        List<ProjectQprofileAssociationDto> pagedAuthorizedProjects = Lists.newArrayList(authorizedProjects);
        if (pagedAuthorizedProjects.size() <= paging.offset()) {
            pagedAuthorizedProjects = Lists.newArrayList();
        } else if (pagedAuthorizedProjects.size() > paging.pageSize()) {
            int endIndex = Math.min(paging.offset() + pageSize, pagedAuthorizedProjects.size());
            pagedAuthorizedProjects = pagedAuthorizedProjects.subList(paging.offset(), endIndex);
        }
        writeProjects(response.newJsonWriter(), pagedAuthorizedProjects, paging);
    } finally {
        session.close();
    }
}
Also used : Paging(org.sonar.api.utils.Paging) ProjectQprofileAssociationDto(org.sonar.db.qualityprofile.ProjectQprofileAssociationDto) DbSession(org.sonar.db.DbSession) CompareToBuilder(org.apache.commons.lang.builder.CompareToBuilder)

Example 9 with Paging

use of org.sonar.api.utils.Paging in project sonarqube by SonarSource.

the class UsersAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    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);
    try (DbSession dbSession = dbClient.openSession(false)) {
        GroupId group = support.findGroup(dbSession, request);
        userSession.checkPermission(OrganizationPermission.ADMINISTER, group.getOrganizationUuid());
        UserMembershipQuery query = UserMembershipQuery.builder().groupId(group.getId()).memberSearch(queryString).membership(getMembership(selected)).pageIndex(page).pageSize(pageSize).build();
        int total = dbClient.groupMembershipDao().countMembers(dbSession, query);
        Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(total);
        List<UserMembershipDto> users = dbClient.groupMembershipDao().selectMembers(dbSession, query, paging.offset(), paging.pageSize());
        JsonWriter json = response.newJsonWriter().beginObject();
        writeMembers(json, users);
        writePaging(json, paging);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) Paging(org.sonar.api.utils.Paging) UserMembershipQuery(org.sonar.db.user.UserMembershipQuery) UserMembershipDto(org.sonar.db.user.UserMembershipDto) JsonWriter(org.sonar.api.utils.text.JsonWriter)

Example 10 with Paging

use of org.sonar.api.utils.Paging 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)14 DbSession (org.sonar.db.DbSession)12 OrganizationDto (org.sonar.db.organization.OrganizationDto)6 ComponentDto (org.sonar.db.component.ComponentDto)4 PermissionQuery (org.sonar.db.permission.PermissionQuery)4 UserDto (org.sonar.db.user.UserDto)3 JsonWriter (org.sonar.api.utils.text.JsonWriter)2 ComponentQuery (org.sonar.db.component.ComponentQuery)2 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)2 GroupDto (org.sonar.db.user.GroupDto)2 ProjectId (org.sonar.server.permission.ProjectId)2 WsPermissions (org.sonarqube.ws.WsPermissions)2 UsersWsResponse (org.sonarqube.ws.WsPermissions.UsersWsResponse)2 CompareToBuilder (org.apache.commons.lang.builder.CompareToBuilder)1 OrganizationQuery (org.sonar.db.organization.OrganizationQuery)1 GroupPermissionDto (org.sonar.db.permission.GroupPermissionDto)1 UserPermissionDto (org.sonar.db.permission.UserPermissionDto)1 PermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateGroupDto)1 PermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateUserDto)1 ProjectQgateAssociation (org.sonar.db.qualitygate.ProjectQgateAssociation)1