use of org.sonar.db.qualityprofile.ProjectQprofileAssociationDto 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.db.qualityprofile.ProjectQprofileAssociationDto in project sonarqube by SonarSource.
the class ProjectsAction method writeProjects.
private void writeProjects(JsonWriter json, List<ProjectQprofileAssociationDto> projects, Paging paging) {
json.beginObject();
json.name("results").beginArray();
for (ProjectQprofileAssociationDto project : projects) {
json.beginObject().prop("uuid", project.getProjectUuid()).prop("id", project.getProjectUuid()).prop("key", project.getProjectKey()).prop("name", project.getProjectName()).prop("selected", project.isAssociated()).endObject();
}
json.endArray();
json.prop("more", paging.hasNextPage());
json.endObject().close();
}
use of org.sonar.db.qualityprofile.ProjectQprofileAssociationDto in project sonarqube by SonarSource.
the class ProjectsAction method loadProjects.
private List<ProjectQprofileAssociationDto> loadProjects(String profileKey, DbSession session, String selected, String query) {
List<ProjectQprofileAssociationDto> projects = Lists.newArrayList();
SelectionMode selectionMode = SelectionMode.fromParam(selected);
if (SelectionMode.SELECTED == selectionMode) {
projects.addAll(dbClient.qualityProfileDao().selectSelectedProjects(profileKey, query, session));
} else if (SelectionMode.DESELECTED == selectionMode) {
projects.addAll(dbClient.qualityProfileDao().selectDeselectedProjects(profileKey, query, session));
} else {
projects.addAll(dbClient.qualityProfileDao().selectProjectAssociations(profileKey, query, session));
}
return projects;
}
Aggregations