Search in sources :

Example 1 with ApiFieldExclusionFilter

use of io.gravitee.repository.management.api.search.ApiFieldExclusionFilter 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)

Aggregations

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 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 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 Pageable (io.gravitee.rest.api.model.common.Pageable)1 PageableImpl (io.gravitee.rest.api.model.common.PageableImpl)1 Metadata (io.gravitee.rest.api.model.pagedresult.Metadata)1