Search in sources :

Example 1 with JdbcUser

use of io.gravitee.am.repository.jdbc.management.api.model.JdbcUser in project gravitee-access-management by gravitee-io.

the class JdbcUserRepository method search.

@Override
public Single<Page<User>> search(ReferenceType referenceType, String referenceId, FilterCriteria criteria, int page, int size) {
    LOGGER.debug("search({}, {}, {}, {}, {})", referenceType, referenceId, criteria, page, size);
    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append(" FROM users WHERE reference_id = :refId AND reference_type = :refType AND ");
    ScimUserSearch search = this.databaseDialectHelper.prepareScimSearchUserQuery(queryBuilder, criteria, page, size);
    // execute query
    org.springframework.r2dbc.core.DatabaseClient.GenericExecuteSpec executeSelect = template.getDatabaseClient().sql(search.getSelectQuery());
    executeSelect = executeSelect.bind("refType", referenceType.name()).bind("refId", referenceId);
    for (Map.Entry<String, Object> entry : search.getBinding().entrySet()) {
        executeSelect = executeSelect.bind(entry.getKey(), entry.getValue());
    }
    Flux<JdbcUser> userFlux = executeSelect.map(row -> rowMapper.read(JdbcUser.class, row)).all();
    // execute count to provide total in the Page
    org.springframework.r2dbc.core.DatabaseClient.GenericExecuteSpec executeCount = template.getDatabaseClient().sql(search.getCountQuery());
    executeCount = executeCount.bind("refType", referenceType.name()).bind("refId", referenceId);
    for (Map.Entry<String, Object> entry : search.getBinding().entrySet()) {
        executeCount = executeCount.bind(entry.getKey(), entry.getValue());
    }
    Mono<Long> userCount = executeCount.map(row -> row.get(0, Long.class)).first();
    return fluxToFlowable(userFlux).map(this::toEntity).flatMap(user -> completeUser(user).toFlowable()).toList().flatMap(list -> monoToSingle(userCount).map(total -> new Page<User>(list, page, total)));
}
Also used : DOMAIN(io.gravitee.am.model.ReferenceType.DOMAIN) ScimUserSearch(io.gravitee.am.repository.jdbc.common.dialect.ScimUserSearch) java.util(java.util) Address(io.gravitee.am.model.scim.Address) UserRepository(io.gravitee.am.repository.management.api.UserRepository) FilterCriteria(io.gravitee.am.repository.management.api.search.FilterCriteria) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) Query(org.springframework.data.relational.core.query.Query) LocalDateTime(java.time.LocalDateTime) JdbcUser(io.gravitee.am.repository.jdbc.management.api.model.JdbcUser) Autowired(org.springframework.beans.factory.annotation.Autowired) Criteria.where(org.springframework.data.relational.core.query.Criteria.where) Field(io.gravitee.am.common.analytics.Field) AnalyticsQuery(io.gravitee.am.model.analytics.AnalyticsQuery) AbstractRole(io.gravitee.am.repository.jdbc.management.api.model.JdbcUser.AbstractRole) Single(io.reactivex.Single) InitializingBean(org.springframework.beans.factory.InitializingBean) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) AbstractJdbcRepository(io.gravitee.am.repository.jdbc.management.AbstractJdbcRepository) io.gravitee.am.repository.jdbc.management.api.spring.user(io.gravitee.am.repository.jdbc.management.api.spring.user) Attribute(io.gravitee.am.model.scim.Attribute) Flowable(io.reactivex.Flowable) ReferenceType(io.gravitee.am.model.ReferenceType) Sort(org.springframework.data.domain.Sort) Stream.concat(java.util.stream.Stream.concat) User(io.gravitee.am.model.User) Repository(org.springframework.stereotype.Repository) StreamUtils(org.springframework.data.util.StreamUtils) Page(io.gravitee.am.model.common.Page) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Mono(reactor.core.publisher.Mono) RandomString(io.gravitee.am.common.utils.RandomString) Flux(reactor.core.publisher.Flux) ChronoUnit(java.time.temporal.ChronoUnit) Stream(java.util.stream.Stream) RxJava2Adapter(reactor.adapter.rxjava.RxJava2Adapter) UTC(java.time.ZoneOffset.UTC) ScimUserSearch(io.gravitee.am.repository.jdbc.common.dialect.ScimUserSearch) Page(io.gravitee.am.model.common.Page) RandomString(io.gravitee.am.common.utils.RandomString) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) JdbcUser(io.gravitee.am.repository.jdbc.management.api.model.JdbcUser)

Aggregations

Field (io.gravitee.am.common.analytics.Field)1 RandomString (io.gravitee.am.common.utils.RandomString)1 ReferenceType (io.gravitee.am.model.ReferenceType)1 DOMAIN (io.gravitee.am.model.ReferenceType.DOMAIN)1 User (io.gravitee.am.model.User)1 AnalyticsQuery (io.gravitee.am.model.analytics.AnalyticsQuery)1 Page (io.gravitee.am.model.common.Page)1 Address (io.gravitee.am.model.scim.Address)1 Attribute (io.gravitee.am.model.scim.Attribute)1 ScimUserSearch (io.gravitee.am.repository.jdbc.common.dialect.ScimUserSearch)1 AbstractJdbcRepository (io.gravitee.am.repository.jdbc.management.AbstractJdbcRepository)1 JdbcUser (io.gravitee.am.repository.jdbc.management.api.model.JdbcUser)1 AbstractRole (io.gravitee.am.repository.jdbc.management.api.model.JdbcUser.AbstractRole)1 io.gravitee.am.repository.jdbc.management.api.spring.user (io.gravitee.am.repository.jdbc.management.api.spring.user)1 UserRepository (io.gravitee.am.repository.management.api.UserRepository)1 FilterCriteria (io.gravitee.am.repository.management.api.search.FilterCriteria)1 Completable (io.reactivex.Completable)1 Flowable (io.reactivex.Flowable)1 Maybe (io.reactivex.Maybe)1 Single (io.reactivex.Single)1