use of javax.persistence.TypedQuery in project keycloak by keycloak.
the class JPAPermissionTicketStore method count.
@Override
public long count(Map<PermissionTicket.FilterOption, String> attributes, String resourceServerId) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> querybuilder = builder.createQuery(Long.class);
Root<PermissionTicketEntity> root = querybuilder.from(PermissionTicketEntity.class);
querybuilder.select(root.get("id"));
List<Predicate> predicates = getPredicates(builder, root, resourceServerId, attributes);
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("id")));
TypedQuery query = entityManager.createQuery(querybuilder);
return closing(query.getResultStream()).count();
}
use of javax.persistence.TypedQuery in project keycloak by keycloak.
the class JPAResourceStore method findByResourceServer.
@Override
public List<Resource> findByResourceServer(Map<Resource.FilterOption, String[]> attributes, String resourceServerId, int firstResult, int maxResult) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<ResourceEntity> querybuilder = builder.createQuery(ResourceEntity.class);
Root<ResourceEntity> root = querybuilder.from(ResourceEntity.class);
querybuilder.select(root.get("id"));
List<Predicate> predicates = new ArrayList();
if (resourceServerId != null) {
predicates.add(builder.equal(root.get("resourceServer"), resourceServerId));
}
attributes.forEach((filterOption, value) -> {
switch(filterOption) {
case ID:
case OWNER:
predicates.add(root.get(filterOption.getName()).in(value));
break;
case SCOPE_ID:
predicates.add(root.join("scopes").get("id").in(value));
break;
case OWNER_MANAGED_ACCESS:
predicates.add(builder.equal(root.get(filterOption.getName()), Boolean.valueOf(value[0])));
break;
case URI:
predicates.add(builder.lower(root.join("uris")).in(value[0].toLowerCase()));
break;
case URI_NOT_NULL:
// predicates.add(builder.isNotEmpty(root.get("uris"))); looks like there is a bug in hibernate and this line doesn't work: https://hibernate.atlassian.net/browse/HHH-6686
// Workaround
Expression<Integer> urisSize = builder.size(root.get("uris"));
predicates.add(builder.notEqual(urisSize, 0));
break;
case NAME:
case TYPE:
predicates.add(builder.like(builder.lower(root.get(filterOption.getName())), "%" + value[0].toLowerCase() + "%"));
break;
case EXACT_NAME:
predicates.add(builder.equal(builder.lower(root.get(filterOption.getName())), value[0].toLowerCase()));
break;
default:
throw new IllegalArgumentException("Unsupported filter [" + filterOption + "]");
}
});
querybuilder.where(predicates.toArray(new Predicate[predicates.size()])).orderBy(builder.asc(root.get("name")));
TypedQuery query = entityManager.createQuery(querybuilder);
List<String> result = paginateQuery(query, firstResult, maxResult).getResultList();
List<Resource> list = new LinkedList<>();
ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();
for (String id : result) {
Resource resource = resourceStore.findById(id, resourceServerId);
if (resource != null) {
list.add(resource);
}
}
return list;
}
use of javax.persistence.TypedQuery in project keycloak by keycloak.
the class JPAResourceStore method findByType.
@Override
public void findByType(String type, String owner, String resourceServerId, Consumer<Resource> consumer) {
TypedQuery<ResourceEntity> query;
if (owner != null) {
query = entityManager.createNamedQuery("findResourceIdByType", ResourceEntity.class);
} else {
query = entityManager.createNamedQuery("findResourceIdByTypeNoOwner", ResourceEntity.class);
}
query.setFlushMode(FlushModeType.COMMIT);
query.setParameter("type", type);
if (owner != null) {
query.setParameter("ownerId", owner);
}
query.setParameter("serverId", resourceServerId);
StoreFactory storeFactory = provider.getStoreFactory();
query.getResultList().stream().map(entity -> new ResourceAdapter(entity, entityManager, storeFactory)).forEach(consumer);
}
use of javax.persistence.TypedQuery in project UVMS-ActivityModule-APP by UnionVMS.
the class FishingTripIdentifierDao method getPreviousTrips.
public List<FishingTripIdentifierEntity> getPreviousTrips(String vesselId, String vesselSchemeId, String tripId, Integer limit) {
TypedQuery query = getEntityManager().createNamedQuery(FishingTripIdentifierEntity.FIND_PREVIOUS_TRIP, FishingTripIdentifierEntity.class);
query.setParameter(VESSEL_ID, vesselId);
query.setParameter(VESSEL_SCHEME_ID, vesselSchemeId);
query.setParameter(TRIP_ID, tripId);
query.setMaxResults(limit);
return query.getResultList();
}
use of javax.persistence.TypedQuery in project UVMS-ActivityModule-APP by UnionVMS.
the class FishingTripIdentifierDao method getCurrentTrip.
public FishingTripIdentifierEntity getCurrentTrip(String vesselId, String vesselSchemeId) {
TypedQuery query = getEntityManager().createNamedQuery(FishingTripIdentifierEntity.FIND_CURRENT_TRIP, FishingTripIdentifierEntity.class);
query.setParameter(VESSEL_ID, vesselId);
query.setParameter(VESSEL_SCHEME_ID, vesselSchemeId);
query.setMaxResults(1);
List<FishingTripIdentifierEntity> fishingTripIdentifies = query.getResultList();
if (CollectionUtils.isNotEmpty(fishingTripIdentifies)) {
return fishingTripIdentifies.get(0);
} else {
return null;
}
}
Aggregations