Search in sources :

Example 1 with FilteredEntities

use of io.gravitee.rest.api.model.filtering.FilteredEntities in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceImpl method getRatedApis.

private FilteredEntities<ApiEntity> getRatedApis(Collection<ApiEntity> apis, boolean excluded) {
    // keep apis with ratings
    Map<ApiEntity, RatingSummaryEntity> ratings = new HashMap<>();
    // APIPortal: should create a specific service to retrieve all the information
    // in one call to the repository
    apis.forEach(api -> {
        RatingSummaryEntity apiRatingSummary = ratingService.findSummaryByApi(api.getId());
        if (apiRatingSummary != null && apiRatingSummary.getNumberOfRatings() > 0) {
            ratings.put(api, apiRatingSummary);
        }
    });
    if (excluded) {
        return new FilteredEntities<>(apis.stream().filter(api -> !ratings.containsKey(api)).collect(Collectors.toList()), null);
    } else {
        // sort apis by ratings, nb of ratings, and name
        return new FilteredEntities<>(ratings.entrySet().stream().sorted((e1, e2) -> {
            RatingSummaryEntity o1 = e1.getValue();
            RatingSummaryEntity o2 = e2.getValue();
            int averageRateComparaison = Double.compare(o2.getAverageRate(), o1.getAverageRate());
            if (averageRateComparaison != 0) {
                return averageRateComparaison;
            }
            int nbRatingsComparaison = Integer.compare(o2.getNumberOfRatings(), o1.getNumberOfRatings());
            if (nbRatingsComparaison != 0) {
                return nbRatingsComparaison;
            }
            return String.CASE_INSENSITIVE_ORDER.compare(e1.getKey().getName(), e2.getKey().getName());
        }).map(Map.Entry::getKey).collect(Collectors.toList()), null);
    }
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities)

Example 2 with FilteredEntities

use of io.gravitee.rest.api.model.filtering.FilteredEntities in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceImpl method getCurrentUserSubscribedApis.

private FilteredEntities<ApiEntity> getCurrentUserSubscribedApis(Collection<ApiEntity> apis, boolean excluded) {
    // get Current user applications
    List<String> currentUserApplicationsId = applicationService.findByUser(getAuthenticatedUser().getUsername()).stream().map(ApplicationListItem::getId).collect(Collectors.toList());
    // find all subscribed apis for these applications
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApplications(currentUserApplicationsId);
    List<String> subscribedApis = subscriptionService.search(subscriptionQuery).stream().map(SubscriptionEntity::getApi).distinct().collect(Collectors.toList());
    // filter apis list with subscribed apis list
    return new FilteredEntities<>(apis.stream().filter(api -> (!excluded && subscribedApis.contains(api.getId())) || (excluded && !subscribedApis.contains(api.getId()))).sorted((a1, a2) -> String.CASE_INSENSITIVE_ORDER.compare(a1.getName(), a2.getName())).collect(Collectors.toList()), null);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) TopApiService(io.gravitee.rest.api.service.TopApiService) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) RatingService(io.gravitee.rest.api.service.RatingService) Autowired(org.springframework.beans.factory.annotation.Autowired) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) Component(org.springframework.stereotype.Component) RatingSummaryEntity(io.gravitee.rest.api.model.RatingSummaryEntity) ApplicationService(io.gravitee.rest.api.service.ApplicationService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) FilteringService(io.gravitee.rest.api.service.filtering.FilteringService) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery)

Example 3 with FilteredEntities

use of io.gravitee.rest.api.model.filtering.FilteredEntities in project gravitee-management-rest-api by gravitee-io.

the class ApplicationsResourceTest method shouldGetApplicationsOrderByNbSubscriptionsDesc.

@Test
public void shouldGetApplicationsOrderByNbSubscriptionsDesc() {
    FilteredEntities<ApplicationListItem> mockFilteredApp = new FilteredEntities<ApplicationListItem>(Collections.emptyList(), null);
    doReturn(mockFilteredApp).when(filteringService).getEntitiesOrderByNumberOfSubscriptions(anyCollection(), isNull(), eq(false));
    final Response response = target().queryParam("order", "-nbSubscriptions").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
    Mockito.verify(filteringService).getEntitiesOrderByNumberOfSubscriptions(anyCollection(), isNull(), eq(false));
}
Also used : Response(javax.ws.rs.core.Response) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) Test(org.junit.Test)

Example 4 with FilteredEntities

use of io.gravitee.rest.api.model.filtering.FilteredEntities in project gravitee-management-rest-api by gravitee-io.

the class FilteringServiceImpl method getEntitiesOrderByNumberOfSubscriptions.

/**
 * Filters and sorts input entities by number of subscriptions.
 *
 * @param items    Can be {@link ApiEntity} or {@link ApplicationListItem}
 * @param excluded If set to true, only entities without subscriptions are returned. Else, only entities with subscriptions are returned.
 * @return a {@link FilteredEntities} object with the filtered and sorted list of items and a metadata map.
 */
@Override
public <T extends FilterableItem> FilteredEntities<T> getEntitiesOrderByNumberOfSubscriptions(Collection<T> items, Boolean excluded, boolean isAsc) {
    if (items == null || items.isEmpty()) {
        return new FilteredEntities<>(Collections.emptyList(), new HashMap<>());
    }
    Function<SubscriptionEntity, String> getItemFunction;
    if (items.toArray()[0] instanceof ApiEntity) {
        getItemFunction = (SubscriptionEntity sub) -> sub.getApi();
    } else if (items.toArray()[0] instanceof ApplicationListItem) {
        getItemFunction = (SubscriptionEntity sub) -> sub.getApplication();
    } else {
        throw new IllegalStateException("Only ApiEntity and ApplicationListItem are allowed");
    }
    // find all subscribed items
    SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
    subscriptionQuery.setApis(items.stream().map(FilterableItem::getId).collect(Collectors.toList()));
    subscriptionQuery.setStatuses(Arrays.asList(SubscriptionStatus.ACCEPTED, SubscriptionStatus.PAUSED));
    // group by items
    Map<String, Long> subscribedItemsWithCount = subscriptionService.search(subscriptionQuery).stream().collect(Collectors.groupingBy(getItemFunction, Collectors.counting()));
    // link an item with its nb of subscriptions
    Map<FilterableItem, Long> itemsWithCount = new HashMap<>();
    Map<String, Map<String, Object>> itemsMetadata = new HashMap<>();
    Map<String, Object> subscriptionsMetadata = new HashMap<>();
    itemsMetadata.put("subscriptions", subscriptionsMetadata);
    items.forEach(item -> {
        Long itemSubscriptionsCount = subscribedItemsWithCount.get(item.getId());
        if ((excluded == null) || (!excluded && itemSubscriptionsCount != null) || (excluded && itemSubscriptionsCount == null)) {
            // creation of a map which will be sorted to retrieve items in the right order
            itemsWithCount.put(item, itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
            // creation of a metadata map
            subscriptionsMetadata.put(item.getId(), itemSubscriptionsCount == null ? 0L : itemSubscriptionsCount);
        }
    });
    // order the list
    Comparator<Map.Entry<FilterableItem, Long>> comparingByValue = Map.Entry.comparingByValue();
    if (!isAsc) {
        comparingByValue = comparingByValue.reversed();
    }
    return new FilteredEntities(itemsWithCount.entrySet().stream().sorted(Map.Entry.<FilterableItem, Long>comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey(Comparator.comparing(FilterableItem::getName, String.CASE_INSENSITIVE_ORDER)))).map(Map.Entry::getKey).collect(Collectors.toList()), itemsMetadata);
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TopApiEntity(io.gravitee.rest.api.model.TopApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) FilteredEntities(io.gravitee.rest.api.model.filtering.FilteredEntities) FilterableItem(io.gravitee.rest.api.model.filtering.FilterableItem)

Aggregations

ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)4 FilteredEntities (io.gravitee.rest.api.model.filtering.FilteredEntities)4 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)3 TopApiEntity (io.gravitee.rest.api.model.TopApiEntity)3 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 FilterableItem (io.gravitee.rest.api.model.filtering.FilterableItem)3 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)3 RatingSummaryEntity (io.gravitee.rest.api.model.RatingSummaryEntity)2 SubscriptionStatus (io.gravitee.rest.api.model.SubscriptionStatus)2 ApplicationService (io.gravitee.rest.api.service.ApplicationService)2 RatingService (io.gravitee.rest.api.service.RatingService)2 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)2 TopApiService (io.gravitee.rest.api.service.TopApiService)2 FilteringService (io.gravitee.rest.api.service.filtering.FilteringService)2 AbstractService (io.gravitee.rest.api.service.impl.AbstractService)2 java.util (java.util)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2