Search in sources :

Example 1 with PageableBuilder

use of io.gravitee.repository.management.api.search.builder.PageableBuilder in project gravitee-management-rest-api by gravitee-io.

the class AuditServiceImpl method search.

@Override
public MetadataPage<AuditEntity> search(AuditQuery query) {
    Audit.AuditReferenceType referenceType = query.isManagementLogsOnly() ? Audit.AuditReferenceType.PORTAL : (query.getApiIds() != null && !query.getApiIds().isEmpty()) ? Audit.AuditReferenceType.API : (query.getApplicationIds() != null && !query.getApplicationIds().isEmpty()) ? Audit.AuditReferenceType.APPLICATION : null;
    Builder criteria = new Builder().from(query.getFrom()).to(query.getTo());
    if (referenceType != null) {
        List<String> referenceIds;
        switch(referenceType) {
            case API:
                referenceIds = query.getApiIds();
                break;
            case APPLICATION:
                referenceIds = query.getApplicationIds();
                break;
            default:
                referenceIds = Collections.singletonList("DEFAULT");
        }
        criteria.references(referenceType, referenceIds);
    }
    if (query.getEvents() != null && !query.getEvents().isEmpty()) {
        criteria.events(query.getEvents());
    }
    Page<Audit> auditPage = auditRepository.search(criteria.build(), new PageableBuilder().pageNumber(query.getPage() - 1).pageSize(query.getSize()).build());
    List<AuditEntity> content = auditPage.getContent().stream().map(this::convert).collect(Collectors.toList());
    return new MetadataPage<>(content, query.getPage(), query.getSize(), auditPage.getTotalElements(), getMetadata(content));
}
Also used : AuditEntity(io.gravitee.management.model.audit.AuditEntity) MetadataPage(io.gravitee.common.data.domain.MetadataPage) Builder(io.gravitee.repository.management.api.search.AuditCriteria.Builder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder)

Example 2 with PageableBuilder

use of io.gravitee.repository.management.api.search.builder.PageableBuilder in project gravitee-gateway by gravitee-io.

the class SyncManager method getLastEvent.

private Event getLastEvent(final String api, long nextLastRefreshAt) {
    final EventCriteria.Builder eventCriteriaBuilder;
    if (lastRefreshAt == -1) {
        eventCriteriaBuilder = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api);
    } else {
        eventCriteriaBuilder = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).from(lastRefreshAt - TIMEFRAME_BEFORE_DELAY).to(nextLastRefreshAt + TIMEFRAME_AFTER_DELAY);
    }
    List<Event> events = eventRepository.search(eventCriteriaBuilder.types(EventType.PUBLISH_API, EventType.UNPUBLISH_API, EventType.START_API, EventType.STOP_API).build(), new PageableBuilder().pageNumber(0).pageSize(1).build()).getContent();
    return (!events.isEmpty()) ? events.get(0) : null;
}
Also used : PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) Event(io.gravitee.repository.management.model.Event) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder)

Example 3 with PageableBuilder

use of io.gravitee.repository.management.api.search.builder.PageableBuilder in project gravitee-management-rest-api by gravitee-io.

the class SyncManager method getLastEvent.

private Event getLastEvent(String api) {
    EventCriteria eventCriteria;
    if (lastRefreshAt == -1) {
        eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).build();
    } else {
        eventCriteria = new EventCriteria.Builder().property(Event.EventProperties.API_ID.getValue(), api).from(lastRefreshAt).to(System.currentTimeMillis()).build();
    }
    List<Event> events = eventRepository.search(eventCriteria, new PageableBuilder().pageNumber(0).pageSize(1).build()).getContent();
    return (!events.isEmpty()) ? events.get(0) : null;
}
Also used : Event(io.gravitee.repository.management.model.Event) EventCriteria(io.gravitee.repository.management.api.search.EventCriteria) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder)

Example 4 with PageableBuilder

use of io.gravitee.repository.management.api.search.builder.PageableBuilder in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceTest method shouldFindByApi.

@Test
public void shouldFindByApi() throws TechnicalException {
    final Pageable pageable = mock(Pageable.class);
    when(pageable.pageNumber()).thenReturn(1);
    when(pageable.pageSize()).thenReturn(1);
    final Page<Rating> pageRating = mock(Page.class);
    when(pageRating.getPageNumber()).thenReturn(1);
    when(pageRating.getPageElements()).thenReturn(10L);
    when(pageRating.getTotalElements()).thenReturn(100L);
    when(pageRating.getContent()).thenReturn(singletonList(rating));
    when(ratingRepository.findByApiPageable(eq(API_ID), eq(new PageableBuilder().pageNumber(0).pageSize(1).build()))).thenReturn(pageRating);
    final Page<RatingEntity> pageRatingEntity = ratingService.findByApi(API_ID, pageable);
    assertEquals(1, pageRatingEntity.getPageNumber());
    assertEquals(10, pageRatingEntity.getPageElements());
    assertEquals(100, pageRatingEntity.getTotalElements());
    final RatingEntity ratingEntity = pageRatingEntity.getContent().get(0);
    assertEquals(USER, ratingEntity.getUser());
    assertEquals(API_ID, ratingEntity.getApi());
    assertEquals(TITLE, ratingEntity.getTitle());
    assertEquals(COMMENT, ratingEntity.getComment());
    assertEquals(RATE, ratingEntity.getRate(), 0);
    assertEquals(ratingEntity.getCreatedAt(), ratingEntity.getUpdatedAt());
}
Also used : Pageable(io.gravitee.repository.management.api.search.Pageable) Rating(io.gravitee.repository.management.model.Rating) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) Test(org.junit.Test)

Example 5 with PageableBuilder

use of io.gravitee.repository.management.api.search.builder.PageableBuilder in project gravitee-management-rest-api by gravitee-io.

the class RatingServiceImpl method findByApi.

@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
    if (!enabled) {
        throw new ApiRatingUnavailableException();
    }
    try {
        final Page<Rating> pageRating = ratingRepository.findByApiPageable(api, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
        final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
        return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
        throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) Page(io.gravitee.common.data.domain.Page) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)5 EventCriteria (io.gravitee.repository.management.api.search.EventCriteria)2 Event (io.gravitee.repository.management.model.Event)2 Rating (io.gravitee.repository.management.model.Rating)2 MetadataPage (io.gravitee.common.data.domain.MetadataPage)1 Page (io.gravitee.common.data.domain.Page)1 AuditEntity (io.gravitee.management.model.audit.AuditEntity)1 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 Builder (io.gravitee.repository.management.api.search.AuditCriteria.Builder)1 Pageable (io.gravitee.repository.management.api.search.Pageable)1 Test (org.junit.Test)1