Search in sources :

Example 1 with PageableImpl

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);
    }
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Workflow(io.gravitee.repository.management.model.Workflow) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) UnauthorizedAccessException(io.gravitee.rest.api.service.exceptions.UnauthorizedAccessException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with PageableImpl

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;
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) PageEntity(io.gravitee.rest.api.model.PageEntity) PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Upgrader(io.gravitee.rest.api.service.Upgrader) Ordered(org.springframework.core.Ordered) Logger(org.slf4j.Logger) Pageable(io.gravitee.rest.api.model.common.Pageable) Page(io.gravitee.common.data.domain.Page) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Component(org.springframework.stereotype.Component) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageService(io.gravitee.rest.api.service.PageService) PageRevisionService(io.gravitee.rest.api.service.PageRevisionService) Pageable(io.gravitee.rest.api.model.common.Pageable)

Example 3 with PageableImpl

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;
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) UserCriteria(io.gravitee.repository.management.api.search.UserCriteria) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity)

Example 4 with PageableImpl

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);
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Ticket(io.gravitee.repository.management.model.Ticket) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TicketQuery(io.gravitee.rest.api.model.api.TicketQuery) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Pageable(io.gravitee.repository.management.api.search.Pageable) Sortable(io.gravitee.repository.management.api.search.Sortable) TicketCriteria(io.gravitee.repository.management.api.search.TicketCriteria) Test(org.junit.Test)

Example 5 with PageableImpl

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());
}
Also used : PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Page(io.gravitee.common.data.domain.Page)

Aggregations

PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)10 SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)5 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)4 Test (org.junit.Test)4 TicketQuery (io.gravitee.rest.api.model.api.TicketQuery)3 Page (io.gravitee.common.data.domain.Page)2 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Pageable (io.gravitee.repository.management.api.search.Pageable)2 Sortable (io.gravitee.repository.management.api.search.Sortable)2 TicketCriteria (io.gravitee.repository.management.api.search.TicketCriteria)2 UserCriteria (io.gravitee.repository.management.api.search.UserCriteria)2 Api (io.gravitee.repository.management.model.Api)2 Ticket (io.gravitee.repository.management.model.Ticket)2 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)2 ApiCriteria (io.gravitee.repository.management.api.search.ApiCriteria)1 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)1 Workflow (io.gravitee.repository.management.model.Workflow)1 PageEntity (io.gravitee.rest.api.model.PageEntity)1 TicketEntity (io.gravitee.rest.api.model.TicketEntity)1 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)1