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);
}
}
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);
}
}
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();
}
}
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();
}
}
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();
}
}
Aggregations