use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.
the class JpaMapKeycloakTransaction method read.
@Override
@SuppressWarnings("unchecked")
public Stream<E> read(QueryParameters<M> queryParameters) {
JpaModelCriteriaBuilder mcb = queryParameters.getModelCriteriaBuilder().flashToModelCriteriaBuilder(createJpaModelCriteriaBuilder());
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<RE> query = cb.createQuery(entityType);
Root<RE> root = query.from(entityType);
query.select(selectCbConstruct(cb, root));
// ordering
if (!queryParameters.getOrderBy().isEmpty()) {
List<Order> orderByList = new LinkedList<>();
for (QueryParameters.OrderBy<M> order : queryParameters.getOrderBy()) {
switch(order.getOrder()) {
case ASCENDING:
orderByList.add(cb.asc(root.get(order.getModelField().getName())));
break;
case DESCENDING:
orderByList.add(cb.desc(root.get(order.getModelField().getName())));
break;
default:
throw new UnsupportedOperationException("Unknown ordering.");
}
}
query.orderBy(orderByList);
}
BiFunction<CriteriaBuilder, Root<RE>, Predicate> predicateFunc = mcb.getPredicateFunc();
if (predicateFunc != null)
query.where(predicateFunc.apply(cb, root));
return closing(paginateQuery(em.createQuery(query), queryParameters.getOffset(), queryParameters.getLimit()).getResultStream()).map(this::mapToEntityDelegate);
}
use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.
the class HotRodMapStorage method delete.
@Override
public long delete(QueryParameters<M> queryParameters) {
IckleQueryMapModelCriteriaBuilder<E, M> iqmcb = queryParameters.getModelCriteriaBuilder().flashToModelCriteriaBuilder(createCriteriaBuilder());
String queryString = "SELECT id " + iqmcb.getIckleQuery();
if (!queryParameters.getOrderBy().isEmpty()) {
queryString += " ORDER BY " + queryParameters.getOrderBy().stream().map(HotRodMapStorage::toOrderString).collect(Collectors.joining(", "));
}
LOG.tracef("Executing delete Ickle query: %s", queryString);
QueryFactory queryFactory = Search.getQueryFactory(remoteCache);
Query<Object[]> query = paginateQuery(queryFactory.create(queryString), queryParameters.getOffset(), queryParameters.getLimit());
query.setParameters(iqmcb.getParameters());
AtomicLong result = new AtomicLong();
CloseableIterator<Object[]> iterator = query.iterator();
StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false).peek(e -> result.incrementAndGet()).map(a -> a[0]).map(String.class::cast).forEach(this::delete);
iterator.close();
return result.get();
}
use of org.keycloak.models.map.storage.QueryParameters in project keycloak by keycloak.
the class ConcurrentHashMapStorage method delete.
@Override
public long delete(QueryParameters<M> queryParameters) {
DefaultModelCriteria<M> criteria = queryParameters.getModelCriteriaBuilder();
if (criteria == null) {
long res = store.size();
store.clear();
return res;
}
@SuppressWarnings("unchecked") MapModelCriteriaBuilder<K, V, M> mcb = criteria.flashToModelCriteriaBuilder(createCriteriaBuilder());
Predicate<? super K> keyFilter = mcb.getKeyFilter();
Predicate<? super V> entityFilter = mcb.getEntityFilter();
Stream<Entry<K, V>> storeStream = store.entrySet().stream();
final AtomicLong res = new AtomicLong(0);
if (!queryParameters.getOrderBy().isEmpty()) {
Comparator<V> comparator = MapFieldPredicates.getComparator(queryParameters.getOrderBy().stream());
storeStream = storeStream.sorted((entry1, entry2) -> comparator.compare(entry1.getValue(), entry2.getValue()));
}
paginatedStream(storeStream.filter(next -> keyFilter.test(next.getKey()) && entityFilter.test(next.getValue())), queryParameters.getOffset(), queryParameters.getLimit()).peek(item -> {
res.incrementAndGet();
}).map(Entry::getKey).forEach(store::remove);
return res.get();
}
Aggregations