use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.
the class TaskServiceImpl method findAll.
@Override
public List<TaskEntity> findAll(String userId) {
if (userId == null) {
throw new UnauthorizedAccessException();
}
try {
// because Tasks only consists on subscriptions, we can optimize the search by only look for apis where
// the user has a SUBSCRIPTION_UPDATE permission
// search for PENDING subscriptions
Set<String> apiIds = getApisForAPermission(userId, SUBSCRIPTION.getName());
final List<TaskEntity> tasks;
if (apiIds.isEmpty()) {
tasks = new ArrayList<>();
} else {
SubscriptionQuery query = new SubscriptionQuery();
query.setStatuses(singleton(PENDING));
query.setApis(apiIds);
tasks = subscriptionService.search(query).stream().map(this::convert).collect(toList());
}
// search for PENDING user registration
final Page<UserEntity> pendingUsers = userService.search(new UserCriteria.Builder().statuses(UserStatus.PENDING).build(), new PageableImpl(1, NUMBER_OF_PENDING_USERS_TO_SEARCH));
if (pendingUsers.getContent() != null && !pendingUsers.getContent().isEmpty()) {
tasks.addAll(pendingUsers.getContent().stream().map(this::convert).collect(toList()));
}
// search for IN_REVIEW apis
apiIds = getApisForAPermission(userId, REVIEWS.getName());
if (!apiIds.isEmpty()) {
apiIds.forEach(apiId -> {
final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
if (workflows != null && !workflows.isEmpty()) {
final Workflow currentWorkflow = workflows.get(0);
if (WorkflowState.IN_REVIEW.name().equals(currentWorkflow.getState())) {
tasks.add(convert(currentWorkflow));
}
}
});
}
// search for REQUEST_FOR_CHANGES apis
apiIds = getApisForAPermission(userId, DEFINITION.getName());
if (!apiIds.isEmpty()) {
apiIds.forEach(apiId -> {
final List<Workflow> workflows = workflowService.findByReferenceAndType(API, apiId, WorkflowType.REVIEW);
if (workflows != null && !workflows.isEmpty()) {
final Workflow currentWorkflow = workflows.get(0);
if (WorkflowState.REQUEST_FOR_CHANGES.name().equals(currentWorkflow.getState())) {
tasks.add(convert(currentWorkflow));
}
}
});
}
// search for TO_BE_VALIDATED promotions
tasks.addAll(promotionTasksService.getPromotionTasks(GraviteeContext.getCurrentOrganization()));
return tasks;
} catch (TechnicalException e) {
LOGGER.error("Error retrieving user tasks {}", e.getMessage());
throw new TechnicalManagementException("Error retreiving user tasks", e);
}
}
use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.
the class DefaultPageRevisionUpgrader method upgrade.
@Override
public boolean upgrade() {
if (hasNoRevisions()) {
logger.info("No page revisions found. Create a default revision based on pages.");
final int pageSize = 100;
int pageNumber = 0;
Page<PageEntity> pagesSubSet = null;
do {
Pageable pageable = new PageableImpl(pageNumber, pageSize);
pagesSubSet = pageService.findAll(pageable);
if (!pagesSubSet.getContent().isEmpty()) {
pagesSubSet.getContent().stream().filter(entity -> pageService.shouldHaveRevision(entity.getType())).forEach(entity -> pageRevisionService.create(convert(entity)));
++pageNumber;
}
} while (!pagesSubSet.getContent().isEmpty());
}
return true;
}
use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.
the class DefaultUserStatusUpgrader method upgrade.
@Override
public boolean upgrade() {
// Initialize default user status
UpdateUserEntity updateUserEntity = new UpdateUserEntity();
updateUserEntity.setStatus(UserStatus.ACTIVE.name());
userService.search(new UserCriteria.Builder().noStatus().build(), new PageableImpl(1, Integer.MAX_VALUE)).getContent().forEach(userEntity -> {
if (userEntity.getStatus() == null) {
userService.update(userEntity.getId(), updateUserEntity);
}
});
return true;
}
use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.
the class TicketServiceTest method shouldSearchForTicketsFromUser.
@Test
public void shouldSearchForTicketsFromUser() throws TechnicalException {
Ticket ticket = new Ticket();
ticket.setId("generatedId");
ticket.setApi(API_ID);
ticket.setApplication(APPLICATION_ID);
ticket.setSubject(EMAIL_SUBJECT);
ticket.setContent(EMAIL_CONTENT);
ticket.setCreatedAt(new Date());
ticket.setFromUser(USERNAME);
ApiEntity apiEntity = new ApiEntity();
ApplicationEntity appEntity = new ApplicationEntity();
List<Ticket> ticketList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Ticket t = new Ticket(ticket);
t.setId("ticket" + i);
ticketList.add(t);
}
when(ticketRepository.search(any(TicketCriteria.class), any(Sortable.class), any(Pageable.class))).thenReturn(new Page<>(ticketList, 0, 20, 20));
when(apiService.findById(API_ID)).thenReturn(apiEntity);
when(applicationService.findById(APPLICATION_ID)).thenReturn(appEntity);
TicketQuery query = new TicketQuery();
query.setFromUser("fromUser");
Page<TicketEntity> searchResult = ticketService.search(query, new SortableImpl("subject", true), new PageableImpl(1, Integer.MAX_VALUE));
assertEquals(searchResult.getContent().size(), 20);
assertEquals(searchResult.getPageNumber(), 1);
assertEquals(searchResult.getTotalElements(), 20);
}
use of io.gravitee.rest.api.model.common.PageableImpl in project gravitee-management-rest-api by gravitee-io.
the class MembershipServiceImpl method paginate.
private Page<MemberEntity> paginate(Collection<MemberEntity> members, Pageable pageable) {
// Pagination requires sorting members to be able to navigate through pages.
Comparator<MemberEntity> comparator = Comparator.comparing(memberEntity -> memberEntity.getDisplayName().toLowerCase(Locale.ROOT));
if (pageable == null) {
pageable = new PageableImpl(1, Integer.MAX_VALUE);
}
int totalCount = members.size();
int startIndex = (pageable.getPageNumber() - 1) * pageable.getPageSize();
if (pageable.getPageNumber() < 1 || (totalCount > 0 && startIndex >= totalCount)) {
throw new PaginationInvalidException();
}
List<MemberEntity> subsetApis = members.stream().sorted(comparator).skip(startIndex).limit(pageable.getPageSize()).collect(toList());
return new Page<>(subsetApis, pageable.getPageNumber(), pageable.getPageSize(), members.size());
}
Aggregations