Search in sources :

Example 1 with Metadata

use of io.gravitee.rest.api.model.pagedresult.Metadata in project gravitee-management-rest-api by gravitee-io.

the class MembershipServiceImpl method findUserMembershipMetadata.

@Override
public Metadata findUserMembershipMetadata(List<UserMembership> memberships, MembershipReferenceType type) {
    if (memberships == null || memberships.isEmpty() || type == null || (!type.equals(MembershipReferenceType.API) && !type.equals(MembershipReferenceType.APPLICATION) && !type.equals(MembershipReferenceType.GROUP))) {
        return new Metadata();
    }
    try {
        Metadata metadata = new Metadata();
        if (type.equals(MembershipReferenceType.API)) {
            ApiCriteria.Builder criteria = new ApiCriteria.Builder();
            ApiFieldExclusionFilter filter = (new ApiFieldExclusionFilter.Builder()).excludeDefinition().excludePicture().build();
            criteria.ids(memberships.stream().map(UserMembership::getReference).toArray(String[]::new));
            apiRepository.search(criteria.build(), filter).forEach(api -> {
                metadata.put(api.getId(), "name", api.getName());
                metadata.put(api.getId(), "version", api.getVersion());
                metadata.put(api.getId(), "visibility", api.getVisibility());
            });
        } else if (type.equals(MembershipReferenceType.APPLICATION)) {
            applicationRepository.findByIds(memberships.stream().map(UserMembership::getReference).collect(Collectors.toList())).forEach(application -> {
                metadata.put(application.getId(), "name", application.getName());
            });
        }
        return metadata;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to get user membership metadata", ex);
        throw new TechnicalManagementException("An error occurs while trying to get user membership metadata", ex);
    }
}
Also used : io.gravitee.rest.api.service(io.gravitee.rest.api.service) java.util(java.util) Pageable(io.gravitee.rest.api.model.common.Pageable) MEMBERSHIP_CREATED(io.gravitee.repository.management.model.Membership.AuditEvent.MEMBERSHIP_CREATED) Page(io.gravitee.common.data.domain.Page) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) MembershipRepository(io.gravitee.repository.management.api.MembershipRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) ApplicationAlertEventType(io.gravitee.rest.api.model.alert.ApplicationAlertEventType) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) ApiCriteria(io.gravitee.repository.management.api.search.ApiCriteria) ApplicationAlertMembershipEvent(io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent) Collections.singleton(java.util.Collections.singleton) ApiRepository(io.gravitee.repository.management.api.ApiRepository) io.gravitee.rest.api.model(io.gravitee.rest.api.model) ApplicationRepository(io.gravitee.repository.management.api.ApplicationRepository) User(io.gravitee.rest.api.model.providers.User) Collectors.toSet(java.util.stream.Collectors.toSet) PageableImpl(io.gravitee.rest.api.model.common.PageableImpl) Collections.emptyMap(java.util.Collections.emptyMap) UuidString(io.gravitee.rest.api.service.common.UuidString) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) Audit(io.gravitee.repository.management.model.Audit) PRIMARY_OWNER(io.gravitee.rest.api.model.permissions.SystemRole.PRIMARY_OWNER) Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) Collectors(java.util.stream.Collectors) ApiFieldExclusionFilter(io.gravitee.repository.management.api.search.ApiFieldExclusionFilter) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) EventManager(io.gravitee.common.event.EventManager) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) MEMBERSHIP_DELETED(io.gravitee.repository.management.model.Membership.AuditEvent.MEMBERSHIP_DELETED) CacheBuilder(com.google.common.cache.CacheBuilder) Cache(com.google.common.cache.Cache) SystemRole(io.gravitee.rest.api.model.permissions.SystemRole) ApiCriteria(io.gravitee.repository.management.api.search.ApiCriteria) ApiFieldExclusionFilter(io.gravitee.repository.management.api.search.ApiFieldExclusionFilter) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) NotificationParamsBuilder(io.gravitee.rest.api.service.notification.NotificationParamsBuilder) EmailNotificationBuilder(io.gravitee.rest.api.service.builder.EmailNotificationBuilder) CacheBuilder(com.google.common.cache.CacheBuilder) Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) UuidString(io.gravitee.rest.api.service.common.UuidString)

Example 2 with Metadata

use of io.gravitee.rest.api.model.pagedresult.Metadata in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceImpl method getMetadata.

@Override
public Metadata getMetadata(List<SubscriptionEntity> subscriptions) {
    Metadata metadata = new Metadata();
    subscriptions.forEach(subscription -> {
        if (!metadata.containsKey(subscription.getApplication())) {
            ApplicationEntity applicationEntity = applicationService.findById(subscription.getApplication());
            metadata.put(subscription.getApplication(), "name", applicationEntity.getName());
        }
        if (!metadata.containsKey(subscription.getPlan())) {
            PlanEntity planEntity = planService.findById(subscription.getPlan());
            metadata.put(subscription.getPlan(), "name", planEntity.getName());
        }
        if (!metadata.containsKey(subscription.getApi())) {
            ApiEntity api = apiService.findById(subscription.getApi());
            metadata.put(subscription.getApi(), "name", api.getName());
        }
    });
    return metadata;
}
Also used : Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity)

Example 3 with Metadata

use of io.gravitee.rest.api.model.pagedresult.Metadata in project gravitee-management-rest-api by gravitee-io.

the class TaskServiceImpl method getMetadata.

@Override
public Metadata getMetadata(List<TaskEntity> tasks) {
    final Metadata metadata = new Metadata();
    tasks.forEach(task -> {
        final Object data = task.getData();
        if (data instanceof SubscriptionEntity) {
            final SubscriptionEntity subscription = (SubscriptionEntity) data;
            if (!metadata.containsKey(subscription.getApplication())) {
                ApplicationEntity applicationEntity = applicationService.findById(subscription.getApplication());
                metadata.put(subscription.getApplication(), "name", applicationEntity.getName());
            }
            if (!metadata.containsKey(subscription.getPlan())) {
                PlanEntity planEntity = planService.findById(subscription.getPlan());
                String apiId = planEntity.getApi();
                ApiEntity api = apiService.findById(apiId);
                metadata.put(subscription.getPlan(), "name", planEntity.getName());
                metadata.put(subscription.getPlan(), "api", apiId);
                metadata.put(apiId, "name", api.getName());
            }
        } else if (data instanceof Workflow) {
            final Workflow workflow = (Workflow) data;
            if (API.name().equals(workflow.getReferenceType()) && !metadata.containsKey(workflow.getReferenceId())) {
                ApiEntity api = apiService.findById(workflow.getReferenceId());
                metadata.put(api.getId(), "name", api.getName());
            }
        }
    });
    return metadata;
}
Also used : Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Workflow(io.gravitee.repository.management.model.Workflow)

Example 4 with Metadata

use of io.gravitee.rest.api.model.pagedresult.Metadata in project gravitee-management-rest-api by gravitee-io.

the class UserResource method getUserMemberships.

@GET
@Path("/memberships")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "List of memberships the user belongs to", notes = "User must have the ORGANIZATION_USERS[READ] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "List of user memberships"), @ApiResponse(code = 404, message = "User not found"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions(@Permission(value = RolePermission.ORGANIZATION_USERS, acls = RolePermissionAction.READ))
public UserMembershipList getUserMemberships(@QueryParam("type") String sType) {
    MembershipReferenceType type = null;
    if (sType != null) {
        type = MembershipReferenceType.valueOf(sType.toUpperCase());
    }
    List<UserMembership> userMemberships = membershipService.findUserMembership(type, userId);
    Metadata metadata = membershipService.findUserMembershipMetadata(userMemberships, type);
    UserMembershipList userMembershipList = new UserMembershipList();
    userMembershipList.setMemberships(userMemberships);
    userMembershipList.setMetadata(metadata.getMetadata());
    return userMembershipList;
}
Also used : Metadata(io.gravitee.rest.api.model.pagedresult.Metadata) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Aggregations

Metadata (io.gravitee.rest.api.model.pagedresult.Metadata)4 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)3 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Page (io.gravitee.common.data.domain.Page)1 EventManager (io.gravitee.common.event.EventManager)1 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)1 ApiRepository (io.gravitee.repository.management.api.ApiRepository)1 ApplicationRepository (io.gravitee.repository.management.api.ApplicationRepository)1 MembershipRepository (io.gravitee.repository.management.api.MembershipRepository)1 ApiCriteria (io.gravitee.repository.management.api.search.ApiCriteria)1 ApiFieldExclusionFilter (io.gravitee.repository.management.api.search.ApiFieldExclusionFilter)1 Audit (io.gravitee.repository.management.model.Audit)1 MEMBERSHIP_CREATED (io.gravitee.repository.management.model.Membership.AuditEvent.MEMBERSHIP_CREATED)1 MEMBERSHIP_DELETED (io.gravitee.repository.management.model.Membership.AuditEvent.MEMBERSHIP_DELETED)1 Workflow (io.gravitee.repository.management.model.Workflow)1 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)1 io.gravitee.rest.api.model (io.gravitee.rest.api.model)1 ApplicationAlertEventType (io.gravitee.rest.api.model.alert.ApplicationAlertEventType)1 ApplicationAlertMembershipEvent (io.gravitee.rest.api.model.alert.ApplicationAlertMembershipEvent)1