Search in sources :

Example 1 with ApiMapper

use of io.gravitee.rest.api.portal.rest.mapper.ApiMapper in project gravitee-management-rest-api by gravitee-io.

the class ApplicationSubscribersResource method getSubscriberApisByApplicationId.

@GET
@Produces({ MediaType.APPLICATION_JSON })
public Response getSubscriberApisByApplicationId(@BeanParam PaginationParam paginationParam, @PathParam("applicationId") String applicationId, @QueryParam("statuses") List<SubscriptionStatus> statuses) {
    String currentUser = getAuthenticatedUserOrNull();
    Collection<ApplicationListItem> userApplications = applicationService.findByUser(currentUser);
    Optional<ApplicationListItem> optionalApplication = userApplications.stream().filter(a -> a.getId().equals(applicationId)).findFirst();
    if (optionalApplication.isPresent()) {
        SubscriptionQuery subscriptionQuery = new SubscriptionQuery();
        subscriptionQuery.setApplication(applicationId);
        subscriptionQuery.setStatuses(statuses);
        ApplicationListItem application = optionalApplication.get();
        if (!application.getPrimaryOwner().getId().equals(currentUser)) {
            Set<ApiEntity> userApis = this.apiService.findPublishedByUser(currentUser);
            if (userApis == null || userApis.isEmpty()) {
                return createListResponse(Collections.emptyList(), paginationParam);
            }
            subscriptionQuery.setApis(userApis.stream().map(ApiEntity::getId).collect(Collectors.toList()));
        }
        Map<String, Long> nbHitsByApp = getNbHitsByApplication(applicationId);
        Collection<SubscriptionEntity> subscriptions = subscriptionService.search(subscriptionQuery);
        List<Api> subscribersApis = subscriptions.stream().map(SubscriptionEntity::getApi).distinct().map(api -> apiService.findById(api)).map(apiMapper::convert).sorted((o1, o2) -> compareApp(nbHitsByApp, o1, o2)).collect(Collectors.toList());
        return createListResponse(subscribersApis, paginationParam);
    }
    throw new ApplicationNotFoundException(applicationId);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) java.util(java.util) ApiMapper(io.gravitee.rest.api.portal.rest.mapper.ApiMapper) Api(io.gravitee.rest.api.portal.rest.model.Api) SubscriptionStatus(io.gravitee.rest.api.model.SubscriptionStatus) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) GroupByQuery(io.gravitee.rest.api.model.analytics.query.GroupByQuery) Inject(javax.inject.Inject) SubscriptionService(io.gravitee.rest.api.service.SubscriptionService) MediaType(io.gravitee.common.http.MediaType) ChronoUnit(java.time.temporal.ChronoUnit) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) PaginationParam(io.gravitee.rest.api.portal.rest.resource.param.PaginationParam) ApplicationService(io.gravitee.rest.api.service.ApplicationService) TopHitsAnalytics(io.gravitee.rest.api.model.analytics.TopHitsAnalytics) AnalyticsService(io.gravitee.rest.api.service.AnalyticsService) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) SubscriptionEntity(io.gravitee.rest.api.model.SubscriptionEntity) SubscriptionQuery(io.gravitee.rest.api.model.subscription.SubscriptionQuery) ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationNotFoundException(io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException) Api(io.gravitee.rest.api.portal.rest.model.Api)

Aggregations

MediaType (io.gravitee.common.http.MediaType)1 SubscriptionEntity (io.gravitee.rest.api.model.SubscriptionEntity)1 SubscriptionStatus (io.gravitee.rest.api.model.SubscriptionStatus)1 TopHitsAnalytics (io.gravitee.rest.api.model.analytics.TopHitsAnalytics)1 GroupByQuery (io.gravitee.rest.api.model.analytics.query.GroupByQuery)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 ApplicationListItem (io.gravitee.rest.api.model.application.ApplicationListItem)1 SubscriptionQuery (io.gravitee.rest.api.model.subscription.SubscriptionQuery)1 ApiMapper (io.gravitee.rest.api.portal.rest.mapper.ApiMapper)1 Api (io.gravitee.rest.api.portal.rest.model.Api)1 PaginationParam (io.gravitee.rest.api.portal.rest.resource.param.PaginationParam)1 AnalyticsService (io.gravitee.rest.api.service.AnalyticsService)1 ApplicationService (io.gravitee.rest.api.service.ApplicationService)1 SubscriptionService (io.gravitee.rest.api.service.SubscriptionService)1 ApplicationNotFoundException (io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException)1 Instant (java.time.Instant)1 ChronoUnit (java.time.temporal.ChronoUnit)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1