Updates Colaborators fetch algorithm
This commit is contained in:
parent
2775841d84
commit
ea66da7cff
|
@ -11,4 +11,5 @@ public interface UserInfoDao extends DatabaseAccessLayer<UserInfo, UUID> {
|
||||||
|
|
||||||
QueryableList<UserInfo> getWithCriteria(UserInfoCriteria criteria);
|
QueryableList<UserInfo> getWithCriteria(UserInfoCriteria criteria);
|
||||||
|
|
||||||
|
QueryableList<UserInfo> getAuthenticated(QueryableList<UserInfo> users, UUID principalId);
|
||||||
}
|
}
|
|
@ -5,61 +5,75 @@ import eu.eudat.data.dao.criteria.UserInfoCriteria;
|
||||||
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
|
import eu.eudat.data.dao.databaselayer.service.DatabaseService;
|
||||||
import eu.eudat.data.entities.UserInfo;
|
import eu.eudat.data.entities.UserInfo;
|
||||||
import eu.eudat.queryable.QueryableList;
|
import eu.eudat.queryable.QueryableList;
|
||||||
|
import eu.eudat.queryable.types.FieldSelectionType;
|
||||||
|
import eu.eudat.queryable.types.SelectionField;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@Component("userInfoDao")
|
@Component("userInfoDao")
|
||||||
public class UserInfoDaoImpl extends DatabaseAccess<UserInfo> implements UserInfoDao {
|
public class UserInfoDaoImpl extends DatabaseAccess<UserInfo> implements UserInfoDao {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UserInfoDaoImpl(DatabaseService<UserInfo> databaseService) {
|
public UserInfoDaoImpl(DatabaseService<UserInfo> databaseService) {
|
||||||
super(databaseService);
|
super(databaseService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryableList<UserInfo> getWithCriteria(UserInfoCriteria criteria) {
|
public QueryableList<UserInfo> getWithCriteria(UserInfoCriteria criteria) {
|
||||||
QueryableList<UserInfo> users = this.getDatabaseService().getQueryable(UserInfo.class);
|
QueryableList<UserInfo> users = this.getDatabaseService().getQueryable(UserInfo.class);
|
||||||
if (criteria.getAppRoles() != null && !criteria.getAppRoles().isEmpty())
|
if (criteria.getAppRoles() != null && !criteria.getAppRoles().isEmpty())
|
||||||
users.where((builder, root) -> root.join("userRoles").get("role").in(criteria.getAppRoles()));
|
users.where((builder, root) -> root.join("userRoles").get("role").in(criteria.getAppRoles()));
|
||||||
if (criteria.getLike() != null)
|
if (criteria.getLike() != null)
|
||||||
users.where((builder, root) -> builder.or(builder.like(builder.upper(root.get("name")), "%" + criteria.getLike().toUpperCase() + "%"), builder.like(root.get("email"), "%" + criteria.getLike() + "%")));
|
users.where((builder, root) -> builder.or(builder.like(builder.upper(root.get("name")), "%" + criteria.getLike().toUpperCase() + "%"), builder.like(root.get("email"), "%" + criteria.getLike() + "%")));
|
||||||
if (criteria.getEmail() != null)
|
if (criteria.getEmail() != null)
|
||||||
users.where((builder, root) -> builder.equal(root.get("email"), criteria.getEmail()));
|
users.where((builder, root) -> builder.equal(root.get("email"), criteria.getEmail()));
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserInfo createOrUpdate(UserInfo item) {
|
public QueryableList<UserInfo> getAuthenticated(QueryableList<UserInfo> users, UUID principalId) {
|
||||||
return this.getDatabaseService().createOrUpdate(item, UserInfo.class);
|
users.initSubQuery(UUID.class).where((builder, root) ->
|
||||||
}
|
builder.and(builder.equal(root.join("dmps").get("id"),
|
||||||
|
users.subQuery((builder1, root1) -> builder1.equal(root1.join("dmps").get("id"), principalId),
|
||||||
|
Arrays.asList(new SelectionField(FieldSelectionType.COMPOSITE_FIELD, "dmps:id")))),
|
||||||
|
builder.notEqual(root.get("id"), principalId)));
|
||||||
|
|
||||||
@Override
|
return users;
|
||||||
public UserInfo find(UUID id) {
|
}
|
||||||
return this.getDatabaseService().getQueryable(UserInfo.class).where((builder, root) -> builder.equal(root.get("id"), id)).getSingle();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void delete(UserInfo item) {
|
public UserInfo createOrUpdate(UserInfo item) {
|
||||||
this.getDatabaseService().delete(item);
|
return this.getDatabaseService().createOrUpdate(item, UserInfo.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryableList<UserInfo> asQueryable() {
|
public UserInfo find(UUID id) {
|
||||||
return this.getDatabaseService().getQueryable(UserInfo.class);
|
return this.getDatabaseService().getQueryable(UserInfo.class).where((builder, root) -> builder.equal(root.get("id"), id)).getSingle();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Override
|
||||||
@Override
|
public void delete(UserInfo item) {
|
||||||
public CompletableFuture<UserInfo> createOrUpdateAsync(UserInfo item) {
|
this.getDatabaseService().delete(item);
|
||||||
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserInfo find(UUID id, String hint) {
|
public QueryableList<UserInfo> asQueryable() {
|
||||||
throw new UnsupportedOperationException();
|
return this.getDatabaseService().getQueryable(UserInfo.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Async
|
||||||
|
@Override
|
||||||
|
public CompletableFuture<UserInfo> createOrUpdateAsync(UserInfo item) {
|
||||||
|
return CompletableFuture.supplyAsync(() -> this.createOrUpdate(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserInfo find(UUID id, String hint) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@ import java.util.*;
|
||||||
@NamedEntityGraphs({
|
@NamedEntityGraphs({
|
||||||
@NamedEntityGraph(
|
@NamedEntityGraph(
|
||||||
name = "userInfo",
|
name = "userInfo",
|
||||||
attributeNodes = {@NamedAttributeNode("userRoles"), @NamedAttributeNode("credentials")})
|
attributeNodes = {@NamedAttributeNode("userRoles"), @NamedAttributeNode("credentials"),@NamedAttributeNode("dmps")}),
|
||||||
})
|
})
|
||||||
public class UserInfo implements DataEntity<UserInfo, UUID> {
|
public class UserInfo implements DataEntity<UserInfo, UUID> {
|
||||||
|
|
||||||
|
|
|
@ -18,405 +18,415 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class QueryableHibernateList<T extends DataEntity> implements QueryableList<T> {
|
public class QueryableHibernateList<T extends DataEntity> implements QueryableList<T> {
|
||||||
|
|
||||||
private EntityManager manager;
|
private EntityManager manager;
|
||||||
private CriteriaQuery query;
|
private CriteriaQuery query;
|
||||||
private Class<T> tClass;
|
private Class<T> tClass;
|
||||||
private Root<T> root;
|
private Root<T> root;
|
||||||
private Root<T> nestedQueryRoot;
|
private Root<T> nestedQueryRoot;
|
||||||
private Subquery subquery;
|
private Subquery subquery;
|
||||||
private List<SinglePredicate<T>> singlePredicates = new LinkedList<>();
|
private List<SinglePredicate<T>> singlePredicates = new LinkedList<>();
|
||||||
private List<NestedQuerySinglePredicate<T>> nestedPredicates = new LinkedList<>();
|
private List<NestedQuerySinglePredicate<T>> nestedPredicates = new LinkedList<>();
|
||||||
private boolean distinct = false;
|
private boolean distinct = false;
|
||||||
private List<OrderByPredicate<T>> orderings = new LinkedList<>();
|
private List<OrderByPredicate<T>> orderings = new LinkedList<>();
|
||||||
private List<String> fields = new LinkedList<>();
|
private List<String> fields = new LinkedList<>();
|
||||||
private Integer length;
|
private Integer length;
|
||||||
private Integer offset;
|
private Integer offset;
|
||||||
private Set<String> hints;
|
private Set<String> hints;
|
||||||
private String hint;
|
private String hint;
|
||||||
|
|
||||||
public QueryableHibernateList(EntityManager manager, Class<T> tClass) {
|
public QueryableHibernateList(EntityManager manager, Class<T> tClass) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
this.tClass = tClass;
|
this.tClass = tClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableHibernateList<T> setManager(EntityManager manager) {
|
public QueryableHibernateList<T> setManager(EntityManager manager) {
|
||||||
this.manager = manager;
|
this.manager = manager;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<T> withHint(String hint) {
|
public QueryableList<T> withHint(String hint) {
|
||||||
this.hint = hint;
|
this.hint = hint;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryableList<T> withFields(List<String> fields) {
|
public QueryableList<T> withFields(List<String> fields) {
|
||||||
this.fields = fields;
|
this.fields = fields;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private QueryableList<T> selectFields() {
|
private QueryableList<T> selectFields() {
|
||||||
List<Selection> rootFields = fields.stream().map(field -> this.convertFieldToPath(field)).collect(Collectors.toList());
|
List<Selection> rootFields = fields.stream().map(field -> this.convertFieldToPath(field)).collect(Collectors.toList());
|
||||||
this.query.select(this.manager.getCriteriaBuilder().tuple(rootFields.toArray(new Selection[rootFields.size()])));
|
this.query.select(this.manager.getCriteriaBuilder().tuple(rootFields.toArray(new Selection[rootFields.size()])));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Path convertFieldToPath(String field) {
|
private Path convertFieldToPath(String field) {
|
||||||
if (!field.contains(".")) {
|
if (!field.contains(".")) {
|
||||||
Path path = this.root.get(field);
|
Path path = this.root.get(field);
|
||||||
path.alias(field);
|
path.alias(field);
|
||||||
return path;
|
return path;
|
||||||
} else {
|
} else {
|
||||||
String[] fields = field.split("\\.");
|
String[] fields = field.split("\\.");
|
||||||
Path path = this.root.get(fields[0]);
|
Path path = this.root.get(fields[0]);
|
||||||
Join join = null;
|
Join join = null;
|
||||||
path.alias(fields[0]);
|
path.alias(fields[0]);
|
||||||
for (int i = 1; i < fields.length; i++) {
|
for (int i = 1; i < fields.length; i++) {
|
||||||
join = join != null ? join.join(fields[i - 1], JoinType.LEFT) : this.root.join(fields[i - 1], JoinType.LEFT) ;
|
join = join != null ? join.join(fields[i - 1], JoinType.LEFT) : this.root.join(fields[i - 1], JoinType.LEFT);
|
||||||
path = join.get(fields[i]);
|
path = join.get(fields[i]);
|
||||||
path.alias(String.join(".", Arrays.asList(fields).subList(0, i + 1)));
|
path.alias(String.join(".", Arrays.asList(fields).subList(0, i + 1)));
|
||||||
}
|
}
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableHibernateList<T> setEntity(Class<T> type) {
|
public QueryableHibernateList<T> setEntity(Class<T> type) {
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initiateQueryableList(Class<T> type) {
|
public void initiateQueryableList(Class<T> type) {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
this.query = builder.createQuery(type);
|
this.query = builder.createQuery(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryableList<T> skip(Integer offset) {
|
public QueryableList<T> skip(Integer offset) {
|
||||||
this.offset = offset;
|
this.offset = offset;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public QueryableList<T> take(Integer length) {
|
public QueryableList<T> take(Integer length) {
|
||||||
this.length = length;
|
this.length = length;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<T> where(SinglePredicate<T> predicate) {
|
public QueryableList<T> where(SinglePredicate<T> predicate) {
|
||||||
this.singlePredicates.add(predicate);
|
this.singlePredicates.add(predicate);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<T> where(NestedQuerySinglePredicate<T> predicate) {
|
public QueryableList<T> where(NestedQuerySinglePredicate<T> predicate) {
|
||||||
this.nestedPredicates.add(predicate);
|
this.nestedPredicates.add(predicate);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> List<R> select(SelectPredicate<T, R> predicate) {
|
public <R> List<R> select(SelectPredicate<T, R> predicate) {
|
||||||
return this.toList().stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList());
|
return this.toList().stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> CompletableFuture<List<R>> selectAsync(SelectPredicate<T, R> predicate) {
|
public <R> CompletableFuture<List<R>> selectAsync(SelectPredicate<T, R> predicate) {
|
||||||
return this.toListAsync().thenApplyAsync(items -> items.stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList()));
|
return this.toListAsync().thenApplyAsync(items -> items.stream().map(item -> predicate.applySelection(item)).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<T> distinct() {
|
public QueryableList<T> distinct() {
|
||||||
this.distinct = true;
|
this.distinct = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryableList<T> orderBy(OrderByPredicate<T> predicate) {
|
public QueryableList<T> orderBy(OrderByPredicate<T> predicate) {
|
||||||
this.orderings.add(predicate);
|
this.orderings.add(predicate);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long count() {
|
public Long count() {
|
||||||
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
||||||
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
|
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
|
||||||
this.root = criteriaQuery.from(tClass);
|
this.root = criteriaQuery.from(tClass);
|
||||||
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
||||||
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
||||||
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
//if (distinct) criteriaQuery.distinct(true);
|
//if (distinct) criteriaQuery.distinct(true);
|
||||||
return this.manager.createQuery(criteriaQuery).getSingleResult();
|
return this.manager.createQuery(criteriaQuery).getSingleResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public CompletableFuture<Long> countAsync() {
|
public CompletableFuture<Long> countAsync() {
|
||||||
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
||||||
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
|
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
|
||||||
this.root = criteriaQuery.from(tClass);
|
this.root = criteriaQuery.from(tClass);
|
||||||
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
||||||
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
||||||
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
//if (distinct) criteriaQuery.distinct(true);
|
//if (distinct) criteriaQuery.distinct(true);
|
||||||
return CompletableFuture.supplyAsync(() -> this.manager.createQuery(criteriaQuery).getSingleResult());
|
return CompletableFuture.supplyAsync(() -> this.manager.createQuery(criteriaQuery).getSingleResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Predicate[] generateWherePredicates(List<SinglePredicate<T>> singlePredicates, Root<T> root, List<NestedQuerySinglePredicate<T>> nestedPredicates, Root<T> nestedQueryRoot) {
|
private Predicate[] generateWherePredicates(List<SinglePredicate<T>> singlePredicates, Root<T> root, List<NestedQuerySinglePredicate<T>> nestedPredicates, Root<T> nestedQueryRoot) {
|
||||||
List<Predicate> predicates = new LinkedList<>();
|
List<Predicate> predicates = new LinkedList<>();
|
||||||
predicates.addAll(Arrays.asList(this.generateSingleWherePredicates(singlePredicates, root)));
|
predicates.addAll(Arrays.asList(this.generateSingleWherePredicates(singlePredicates, root)));
|
||||||
predicates.addAll(Arrays.asList(this.generateNestedWherePredicates(nestedPredicates, root, nestedQueryRoot)));
|
predicates.addAll(Arrays.asList(this.generateNestedWherePredicates(nestedPredicates, root, nestedQueryRoot)));
|
||||||
return predicates.toArray(new Predicate[predicates.size()]);
|
return predicates.toArray(new Predicate[predicates.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate[] generateSingleWherePredicates(List<SinglePredicate<T>> singlePredicates, Root<T> root) {
|
private Predicate[] generateSingleWherePredicates(List<SinglePredicate<T>> singlePredicates, Root<T> root) {
|
||||||
List<Predicate> predicates = new LinkedList<>();
|
List<Predicate> predicates = new LinkedList<>();
|
||||||
for (SinglePredicate<T> singlePredicate : singlePredicates) {
|
for (SinglePredicate<T> singlePredicate : singlePredicates) {
|
||||||
predicates.add(singlePredicate.applyPredicate(this.manager.getCriteriaBuilder(), root));
|
predicates.add(singlePredicate.applyPredicate(this.manager.getCriteriaBuilder(), root));
|
||||||
}
|
}
|
||||||
return predicates.toArray(new Predicate[predicates.size()]);
|
return predicates.toArray(new Predicate[predicates.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Predicate[] generateNestedWherePredicates(List<NestedQuerySinglePredicate<T>> nestedPredicates, Root<T> root, Root<T> nestedQueryRoot) {
|
private Predicate[] generateNestedWherePredicates(List<NestedQuerySinglePredicate<T>> nestedPredicates, Root<T> root, Root<T> nestedQueryRoot) {
|
||||||
List<Predicate> predicates = new LinkedList<>();
|
List<Predicate> predicates = new LinkedList<>();
|
||||||
for (NestedQuerySinglePredicate<T> singlePredicate : nestedPredicates) {
|
for (NestedQuerySinglePredicate<T> singlePredicate : nestedPredicates) {
|
||||||
predicates.add(singlePredicate.applyPredicate(this.manager.getCriteriaBuilder(), root, nestedQueryRoot));
|
predicates.add(singlePredicate.applyPredicate(this.manager.getCriteriaBuilder(), root, nestedQueryRoot));
|
||||||
}
|
}
|
||||||
return predicates.toArray(new Predicate[predicates.size()]);
|
return predicates.toArray(new Predicate[predicates.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Order[] generateOrderPredicates(List<OrderByPredicate<T>> orderByPredicates, Root<T> root) {
|
private Order[] generateOrderPredicates(List<OrderByPredicate<T>> orderByPredicates, Root<T> root) {
|
||||||
List<Order> predicates = new LinkedList<>();
|
List<Order> predicates = new LinkedList<>();
|
||||||
for (OrderByPredicate<T> orderPredicate : orderByPredicates) {
|
for (OrderByPredicate<T> orderPredicate : orderByPredicates) {
|
||||||
predicates.add(orderPredicate.applyPredicate(this.manager.getCriteriaBuilder(), root));
|
predicates.add(orderPredicate.applyPredicate(this.manager.getCriteriaBuilder(), root));
|
||||||
}
|
}
|
||||||
return predicates.toArray(new Order[predicates.size()]);
|
return predicates.toArray(new Order[predicates.size()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<T> toList() {
|
public List<T> toList() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
if (distinct) this.query.distinct(true);
|
if (distinct) this.query.distinct(true);
|
||||||
if (!this.fields.isEmpty()) return this.toListWithFields();
|
if (!this.fields.isEmpty()) return this.toListWithFields();
|
||||||
else return this.toListWithOutFields();
|
else return this.toListWithOutFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<T> toListWithFields() {
|
private List<T> toListWithFields() {
|
||||||
List<Tuple> results = this.manager.createQuery(query).getResultList();
|
List<Tuple> results = this.manager.createQuery(query).getResultList();
|
||||||
Map<Object, List<Tuple>> groupedResults = results.stream()
|
Map<Object, List<Tuple>> groupedResults = results.stream()
|
||||||
.collect(Collectors.groupingBy(x -> x.get("id")));
|
.collect(Collectors.groupingBy(x -> x.get("id")));
|
||||||
return results.stream().map(x -> {
|
return results.stream().map(x -> {
|
||||||
try {
|
try {
|
||||||
return (T) this.tClass.newInstance().buildFromTuple(groupedResults.get(x.get("id")), "");
|
return (T) this.tClass.newInstance().buildFromTuple(groupedResults.get(x.get("id")), "");
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException | IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<T> toListWithOutFields() {
|
private List<T> toListWithOutFields() {
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.offset != null) typedQuery.setFirstResult(this.offset);
|
if (this.offset != null) typedQuery.setFirstResult(this.offset);
|
||||||
if (this.length != null) typedQuery.setMaxResults(this.length);
|
if (this.length != null) typedQuery.setMaxResults(this.length);
|
||||||
if (this.hint != null) {
|
if (this.hint != null) {
|
||||||
List ids = typedQuery.getResultList().stream().map(item -> item.getKeys()).collect(Collectors.toList());
|
List ids = typedQuery.getResultList().stream().map(item -> item.getKeys()).collect(Collectors.toList());
|
||||||
if (ids != null && !ids.isEmpty()) typedQuery = queryWithHint(ids);
|
if (ids != null && !ids.isEmpty()) typedQuery = queryWithHint(ids);
|
||||||
}
|
}
|
||||||
return typedQuery.getResultList();
|
return typedQuery.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public CompletableFuture<List<T>> toListAsync() {
|
public CompletableFuture<List<T>> toListAsync() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
if (!this.orderings.isEmpty()) this.query.orderBy(this.generateOrderPredicates(this.orderings, this.root));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
if (distinct) this.query.distinct(true);
|
if (distinct) this.query.distinct(true);
|
||||||
if (!this.fields.isEmpty()) return this.toListAsyncWithFields();
|
if (!this.fields.isEmpty()) return this.toListAsyncWithFields();
|
||||||
else return this.toListAsyncWithOutFields();
|
else return this.toListAsyncWithOutFields();
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<List<T>> toListAsyncWithFields() {
|
private CompletableFuture<List<T>> toListAsyncWithFields() {
|
||||||
List<Tuple> results = this.manager.createQuery(query).getResultList();
|
List<Tuple> results = this.manager.createQuery(query).getResultList();
|
||||||
Map<Object, List<Tuple>> groupedResults = results.stream()
|
Map<Object, List<Tuple>> groupedResults = results.stream()
|
||||||
.collect(Collectors.groupingBy(x -> x.get("id")));
|
.collect(Collectors.groupingBy(x -> x.get("id")));
|
||||||
return CompletableFuture.supplyAsync(() -> results.stream().map(x -> {
|
return CompletableFuture.supplyAsync(() -> results.stream().map(x -> {
|
||||||
try {
|
try {
|
||||||
return (T) this.tClass.newInstance().buildFromTuple(groupedResults.get(x.get("id")), "");
|
return (T) this.tClass.newInstance().buildFromTuple(groupedResults.get(x.get("id")), "");
|
||||||
} catch (InstantiationException | IllegalAccessException e) {
|
} catch (InstantiationException | IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}).collect(Collectors.toList()));
|
}).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private CompletableFuture<List<T>> toListAsyncWithOutFields() {
|
private CompletableFuture<List<T>> toListAsyncWithOutFields() {
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.offset != null) typedQuery.setFirstResult(this.offset);
|
if (this.offset != null) typedQuery.setFirstResult(this.offset);
|
||||||
if (this.length != null) typedQuery.setMaxResults(this.length);
|
if (this.length != null) typedQuery.setMaxResults(this.length);
|
||||||
return CompletableFuture.supplyAsync(() -> {
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
if (this.hint != null) {
|
if (this.hint != null) {
|
||||||
List ids = typedQuery.getResultList().stream().map(item -> item.getKeys()).collect(Collectors.toList());
|
List ids = typedQuery.getResultList().stream().map(item -> item.getKeys()).collect(Collectors.toList());
|
||||||
if (ids != null && !ids.isEmpty()) return queryWithHint(ids).getResultList();
|
if (ids != null && !ids.isEmpty()) return queryWithHint(ids).getResultList();
|
||||||
}
|
}
|
||||||
return typedQuery.getResultList();
|
return typedQuery.getResultList();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getSingle() {
|
public T getSingle() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.hint != null)
|
if (this.hint != null)
|
||||||
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
||||||
return typedQuery.getSingleResult();
|
return typedQuery.getSingleResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public CompletableFuture<T> getSingleAsync() {
|
public CompletableFuture<T> getSingleAsync() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.hint != null)
|
if (this.hint != null)
|
||||||
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
||||||
return CompletableFuture.supplyAsync(() -> typedQuery.getSingleResult());
|
return CompletableFuture.supplyAsync(() -> typedQuery.getSingleResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
public T getSingleOrDefault() {
|
public T getSingleOrDefault() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.hint != null)
|
if (this.hint != null)
|
||||||
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
||||||
List<T> results = typedQuery.getResultList();
|
List<T> results = typedQuery.getResultList();
|
||||||
if (results.size() == 0) return null;
|
if (results.size() == 0) return null;
|
||||||
if (results.size() == 1) return results.get(0);
|
if (results.size() == 1) return results.get(0);
|
||||||
else throw new NotSingleResultException("Query returned more than one items");
|
else throw new NotSingleResultException("Query returned more than one items");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
public CompletableFuture<T> getSingleOrDefaultAsync() {
|
public CompletableFuture<T> getSingleOrDefaultAsync() {
|
||||||
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder builder = this.manager.getCriteriaBuilder();
|
||||||
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
if (!this.fields.isEmpty()) this.query = builder.createTupleQuery();
|
||||||
else this.query = builder.createQuery(this.tClass);
|
else this.query = builder.createQuery(this.tClass);
|
||||||
this.root = this.query.from(this.tClass);
|
this.root = this.query.from(this.tClass);
|
||||||
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
this.query.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
if (!this.fields.isEmpty()) this.selectFields();
|
if (!this.fields.isEmpty()) this.selectFields();
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
TypedQuery<T> typedQuery = this.manager.createQuery(this.query);
|
||||||
if (this.hint != null)
|
if (this.hint != null)
|
||||||
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
||||||
return CompletableFuture.supplyAsync(() -> typedQuery.getResultList()).thenApply(list -> {
|
return CompletableFuture.supplyAsync(() -> typedQuery.getResultList()).thenApply(list -> {
|
||||||
if (list.size() == 0) return null;
|
if (list.size() == 0) return null;
|
||||||
if (list.size() == 1) return list.get(0);
|
if (list.size() == 1) return list.get(0);
|
||||||
else throw new NotSingleResultException("Query returned more than one items");
|
else throw new NotSingleResultException("Query returned more than one items");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypedQuery<T> queryWithHint(List ids) {
|
private TypedQuery<T> queryWithHint(List ids) {
|
||||||
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
CriteriaBuilder criteriaBuilder = this.manager.getCriteriaBuilder();
|
||||||
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(tClass);
|
CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(tClass);
|
||||||
Root<T> criteriaRoot = criteriaQuery.from(this.tClass);
|
Root<T> criteriaRoot = criteriaQuery.from(this.tClass);
|
||||||
criteriaQuery.where(criteriaRoot.get("id").in(ids));
|
criteriaQuery.where(criteriaRoot.get("id").in(ids));
|
||||||
if (!this.orderings.isEmpty())
|
if (!this.orderings.isEmpty())
|
||||||
criteriaQuery.orderBy(this.generateOrderPredicates(this.orderings, criteriaRoot));
|
criteriaQuery.orderBy(this.generateOrderPredicates(this.orderings, criteriaRoot));
|
||||||
|
|
||||||
TypedQuery<T> typedQuery = this.manager.createQuery(criteriaQuery);
|
TypedQuery<T> typedQuery = this.manager.createQuery(criteriaQuery);
|
||||||
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
typedQuery.setHint("javax.persistence.fetchgraph", this.manager.getEntityGraph(this.hint));
|
||||||
return typedQuery;
|
return typedQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subquery<T> subQuery(SinglePredicate<T> predicate, List<SelectionField> fields) {
|
public Subquery<T> subQuery(SinglePredicate<T> predicate, List<SelectionField> fields) {
|
||||||
Subquery<T> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(this.tClass);
|
Subquery<T> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(this.tClass);
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
||||||
subquery.select(this.nestedQueryRoot.get(fields.get(0).getField()));
|
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
||||||
return subquery;
|
subquery.select(this.nestedQueryRoot.get(fields.get(0).getField()));
|
||||||
}
|
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
||||||
|
subquery.select(this.nestedQueryRoot.join(fields.get(0).getField().split(":")[0]).get(fields.get(0).getField().split(":")[1]));
|
||||||
|
}
|
||||||
|
return subquery;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subquery<T> subQuery(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields) {
|
public Subquery<T> subQuery(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields) {
|
||||||
Subquery<T> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(this.tClass);
|
Subquery<T> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(this.tClass);
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
||||||
subquery.select(this.nestedQueryRoot.get(fields.get(0).getField()));
|
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
||||||
return subquery;
|
subquery.select(this.nestedQueryRoot.get(fields.get(0).getField()));
|
||||||
}
|
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
||||||
|
subquery.select(this.nestedQueryRoot.get(fields.get(0).getField().split(":")[0]).get(fields.get(0).getField().split(":")[1]));
|
||||||
|
}
|
||||||
|
return subquery;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subquery<Long> subQueryCount(SinglePredicate<T> predicate, List<SelectionField> fields) {
|
public Subquery<Long> subQueryCount(SinglePredicate<T> predicate, List<SelectionField> fields) {
|
||||||
Subquery<Long> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(Long.class);
|
Subquery<Long> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(Long.class);
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().count(this.nestedQueryRoot.get(fields.get(0).getField())));
|
subquery.select(this.manager.getCriteriaBuilder().count(this.nestedQueryRoot.get(fields.get(0).getField())));
|
||||||
return subquery;
|
return subquery;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subquery<Long> subQueryCount(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields) {
|
public Subquery<Long> subQueryCount(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields) {
|
||||||
Subquery<Long> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(Long.class);
|
Subquery<Long> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(Long.class);
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().count(this.nestedQueryRoot.get(fields.get(0).getField())));
|
subquery.select(this.manager.getCriteriaBuilder().count(this.nestedQueryRoot.get(fields.get(0).getField())));
|
||||||
return subquery;
|
return subquery;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <U extends Comparable> Subquery<U> subQueryMax(SinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass) {
|
public <U extends
|
||||||
Subquery<U> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
Comparable> Subquery<U> subQueryMax(SinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass) {
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
Subquery<U> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.nestedQueryRoot));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.<U>get(fields.get(0).getField())));
|
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
||||||
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.<U>get(fields.get(0).getField())));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.get(fields.get(0).getField().split(":")[0]).<U>get(fields.get(0).getField().split(":")[1])));
|
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
||||||
}
|
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.get(fields.get(0).getField().split(":")[0]).<U>get(fields.get(0).getField().split(":")[1])));
|
||||||
return subquery;
|
}
|
||||||
}
|
return subquery;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <U extends Comparable> Subquery<U> subQueryMax(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass) {
|
public <U extends
|
||||||
//Subquery<U> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
Comparable> Subquery<U> subQueryMax(NestedQuerySinglePredicate<T> predicate, List<SelectionField> fields, Class<U> uClass) {
|
||||||
//this.nestedQueryRoot = subquery.from(this.tClass);
|
//Subquery<U> subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
||||||
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
//this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
subquery.where(predicate.applyPredicate(this.manager.getCriteriaBuilder(), this.root, this.nestedQueryRoot));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.<U>get(fields.get(0).getField())));
|
if (fields.get(0).getType() == FieldSelectionType.FIELD)
|
||||||
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.<U>get(fields.get(0).getField())));
|
||||||
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.get(fields.get(0).getField().split(":")[0]).<U>get(fields.get(0).getField().split(":")[1])));
|
else if (fields.get(0).getType() == FieldSelectionType.COMPOSITE_FIELD) {
|
||||||
}
|
subquery.select(this.manager.getCriteriaBuilder().greatest(this.nestedQueryRoot.get(fields.get(0).getField().split(":")[0]).<U>get(fields.get(0).getField().split(":")[1])));
|
||||||
return subquery;
|
}
|
||||||
}
|
return subquery;
|
||||||
|
}
|
||||||
|
|
||||||
public <U extends Comparable> QueryableList<T> initSubQuery(Class<U> uClass) {
|
public <U extends Comparable> QueryableList<T> initSubQuery(Class<U> uClass) {
|
||||||
this.subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
this.subquery = this.manager.getCriteriaBuilder().createQuery().subquery(uClass);
|
||||||
this.nestedQueryRoot = subquery.from(this.tClass);
|
this.nestedQueryRoot = subquery.from(this.tClass);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <V> void update(EntitySelectPredicate<T> selectPredicate, V value) {
|
public <V> void update(EntitySelectPredicate<T> selectPredicate, V value) {
|
||||||
CriteriaBuilder builder = this.manager
|
CriteriaBuilder builder = this.manager
|
||||||
.getCriteriaBuilder();
|
.getCriteriaBuilder();
|
||||||
CriteriaUpdate<T> update = builder
|
CriteriaUpdate<T> update = builder
|
||||||
.createCriteriaUpdate(tClass);
|
.createCriteriaUpdate(tClass);
|
||||||
this.root = update.from(tClass);
|
this.root = update.from(tClass);
|
||||||
update.set(selectPredicate.applyPredicate(root), value)
|
update.set(selectPredicate.applyPredicate(root), value)
|
||||||
.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
this.manager
|
this.manager
|
||||||
.createQuery(update)
|
.createQuery(update)
|
||||||
.executeUpdate();
|
.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ public class Users extends BaseController {
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = {"/getCollaboratorsPaged"}, consumes = "application/json", produces = "application/json")
|
@RequestMapping(method = RequestMethod.POST, value = {"/getCollaboratorsPaged"}, consumes = "application/json", produces = "application/json")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<ResponseItem<DataTableData<UserListingModel>>> getCollaboratorsPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, @ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
|
ResponseEntity<ResponseItem<DataTableData<UserListingModel>>> getCollaboratorsPaged(@Valid @RequestBody UserInfoTableRequestItem userInfoTableRequestItem, Principal principal) throws Exception {
|
||||||
DataTableData<UserListingModel> dataTable = userManager.getCollaboratorsPaged(userInfoTableRequestItem, principal);
|
DataTableData<UserListingModel> dataTable = userManager.getCollaboratorsPaged(userInfoTableRequestItem, principal);
|
||||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<UserListingModel>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DataTableData<UserListingModel>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package eu.eudat.logic.managers;
|
package eu.eudat.logic.managers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import eu.eudat.data.dao.criteria.DataManagementPlanCriteria;
|
|
||||||
import eu.eudat.data.dao.entities.DMPDao;
|
|
||||||
import eu.eudat.data.dao.entities.UserInfoDao;
|
import eu.eudat.data.dao.entities.UserInfoDao;
|
||||||
import eu.eudat.data.entities.DMP;
|
import eu.eudat.data.entities.DMP;
|
||||||
import eu.eudat.data.entities.UserDMP;
|
import eu.eudat.data.entities.UserInfo;
|
||||||
import eu.eudat.data.entities.UserRole;
|
import eu.eudat.data.entities.UserRole;
|
||||||
import eu.eudat.data.query.items.table.userinfo.UserInfoTableRequestItem;
|
import eu.eudat.data.query.items.table.userinfo.UserInfoTableRequestItem;
|
||||||
import eu.eudat.exceptions.security.UnauthorisedException;
|
import eu.eudat.exceptions.security.UnauthorisedException;
|
||||||
|
@ -19,7 +17,6 @@ import eu.eudat.models.data.dmp.DataManagementPlan;
|
||||||
import eu.eudat.models.data.helpers.common.DataTableData;
|
import eu.eudat.models.data.helpers.common.DataTableData;
|
||||||
import eu.eudat.models.data.login.Credentials;
|
import eu.eudat.models.data.login.Credentials;
|
||||||
import eu.eudat.models.data.security.Principal;
|
import eu.eudat.models.data.security.Principal;
|
||||||
import eu.eudat.models.data.userinfo.UserInfo;
|
|
||||||
import eu.eudat.models.data.userinfo.UserListingModel;
|
import eu.eudat.models.data.userinfo.UserListingModel;
|
||||||
import eu.eudat.models.data.userinfo.UserProfile;
|
import eu.eudat.models.data.userinfo.UserProfile;
|
||||||
import eu.eudat.queryable.QueryableList;
|
import eu.eudat.queryable.QueryableList;
|
||||||
|
@ -30,114 +27,87 @@ import org.w3c.dom.Document;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UserManager {
|
public class UserManager {
|
||||||
|
|
||||||
private ApiContext apiContext;
|
private ApiContext apiContext;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public UserManager(ApiContext apiContext) {
|
public UserManager(ApiContext apiContext) {
|
||||||
this.apiContext = apiContext;
|
this.apiContext = apiContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public eu.eudat.models.data.user.composite.DatasetProfile generateDatasetProfileModel(eu.eudat.data.entities.DatasetProfile profile) {
|
public eu.eudat.models.data.user.composite.DatasetProfile generateDatasetProfileModel(eu.eudat.data.entities.DatasetProfile profile) {
|
||||||
Document viewStyleDoc = XmlBuilder.fromXml(profile.getDefinition());
|
Document viewStyleDoc = XmlBuilder.fromXml(profile.getDefinition());
|
||||||
Element root = (Element) viewStyleDoc.getDocumentElement();
|
Element root = (Element) viewStyleDoc.getDocumentElement();
|
||||||
eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel viewstyle = new eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel().fromXml(root);
|
eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel viewstyle = new eu.eudat.models.data.entities.xmlmodels.datasetprofiledefinition.ViewStyleModel().fromXml(root);
|
||||||
|
|
||||||
eu.eudat.models.data.user.composite.DatasetProfile datasetprofile = new eu.eudat.models.data.user.composite.DatasetProfile();
|
eu.eudat.models.data.user.composite.DatasetProfile datasetprofile = new eu.eudat.models.data.user.composite.DatasetProfile();
|
||||||
datasetprofile.buildProfile(viewstyle);
|
datasetprofile.buildProfile(viewstyle);
|
||||||
|
|
||||||
return datasetprofile;
|
return datasetprofile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataTableData<UserListingModel> getPaged(UserInfoTableRequestItem userInfoTableRequestItem) throws Exception {
|
public DataTableData<UserListingModel> getPaged(UserInfoTableRequestItem userInfoTableRequestItem) throws Exception {
|
||||||
QueryableList<eu.eudat.data.entities.UserInfo> users = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().getWithCriteria(userInfoTableRequestItem.getCriteria()).withHint(HintedModelFactory.getHint(UserListingModel.class));
|
QueryableList<eu.eudat.data.entities.UserInfo> users = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().getWithCriteria(userInfoTableRequestItem.getCriteria()).withHint(HintedModelFactory.getHint(UserListingModel.class));
|
||||||
QueryableList<eu.eudat.data.entities.UserInfo> pagedUsers = PaginationManager.applyPaging(users, userInfoTableRequestItem);
|
QueryableList<eu.eudat.data.entities.UserInfo> pagedUsers = PaginationManager.applyPaging(users, userInfoTableRequestItem);
|
||||||
|
|
||||||
List<UserListingModel> modelUsers = pagedUsers.select(item -> new UserListingModel().fromDataModel(item));
|
List<UserListingModel> modelUsers = pagedUsers.select(item -> new UserListingModel().fromDataModel(item));
|
||||||
return apiContext.getOperationsContext().getBuilderFactory().getBuilder(DataTableDataBuilder.class).totalCount(users.count()).data(modelUsers).build();
|
return apiContext.getOperationsContext().getBuilderFactory().getBuilder(DataTableDataBuilder.class).totalCount(users.count()).data(modelUsers).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserProfile getSingle(UUID userId) throws Exception {
|
public UserProfile getSingle(UUID userId) throws Exception {
|
||||||
eu.eudat.data.entities.UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(userId);
|
eu.eudat.data.entities.UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(userId);
|
||||||
UserProfile profile = new UserProfile().fromDataModel(user);
|
UserProfile profile = new UserProfile().fromDataModel(user);
|
||||||
List<DMP> dmps = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getAuthenticated(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable(), userId).take(5).toList();
|
List<DMP> dmps = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getAuthenticated(apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().asQueryable(), userId).take(5).toList();
|
||||||
profile.setAssociatedDmps(dmps.stream().map(x -> new DataManagementPlan().fromDataModel(x)).collect(Collectors.toList()));
|
profile.setAssociatedDmps(dmps.stream().map(x -> new DataManagementPlan().fromDataModel(x)).collect(Collectors.toList()));
|
||||||
return profile;
|
return profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void editRoles(UserListingModel user) {
|
public void editRoles(UserListingModel user) {
|
||||||
eu.eudat.data.entities.UserInfo userInfo = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(user.getId());
|
eu.eudat.data.entities.UserInfo userInfo = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(user.getId());
|
||||||
userInfo.getUserRoles().stream().forEach(item -> apiContext.getOperationsContext().getDatabaseRepository().getUserRoleDao().delete(item));
|
userInfo.getUserRoles().stream().forEach(item -> apiContext.getOperationsContext().getDatabaseRepository().getUserRoleDao().delete(item));
|
||||||
for (Integer role : user.getAppRoles()) {
|
for (Integer role : user.getAppRoles()) {
|
||||||
UserRole userRole = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserRoleBuilder.class).role(role).userInfo(userInfo).build();
|
UserRole userRole = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserRoleBuilder.class).role(role).userInfo(userInfo).build();
|
||||||
apiContext.getOperationsContext().getDatabaseRepository().getUserRoleDao().createOrUpdate(userRole);
|
apiContext.getOperationsContext().getDatabaseRepository().getUserRoleDao().createOrUpdate(userRole);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateSettings(Map<String, Object> settings, Principal principal) throws IOException {
|
public void updateSettings(Map<String, Object> settings, Principal principal) throws IOException {
|
||||||
eu.eudat.data.entities.UserInfo userInfo = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
eu.eudat.data.entities.UserInfo userInfo = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId());
|
||||||
apiContext.getOperationsContext().getDatabaseRepository().detachEntity(userInfo);
|
apiContext.getOperationsContext().getDatabaseRepository().detachEntity(userInfo);
|
||||||
HashMap<String, Object> result =
|
HashMap<String, Object> result =
|
||||||
new ObjectMapper().readValue(userInfo.getAdditionalinfo(), HashMap.class);
|
new ObjectMapper().readValue(userInfo.getAdditionalinfo(), HashMap.class);
|
||||||
result.putAll(settings);
|
result.putAll(settings);
|
||||||
userInfo.setAdditionalinfo(new JSONObject(result).toString());
|
userInfo.setAdditionalinfo(new JSONObject(result).toString());
|
||||||
apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao()
|
apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao()
|
||||||
.createOrUpdate(userInfo);
|
.createOrUpdate(userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Principal authenticate(AuthenticationServiceImpl authenticationServiceImpl, Credentials credentials) {
|
public Principal authenticate(AuthenticationServiceImpl authenticationServiceImpl, Credentials credentials) {
|
||||||
Principal principal = authenticationServiceImpl.Touch(credentials);
|
Principal principal = authenticationServiceImpl.Touch(credentials);
|
||||||
if (principal == null) throw new UnauthorisedException("Could not Sign In User");
|
if (principal == null) throw new UnauthorisedException("Could not Sign In User");
|
||||||
return principal;
|
return principal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DataTableData<UserListingModel> getCollaboratorsPaged(UserInfoTableRequestItem userInfoTableRequestItem, Principal principal) throws Exception {
|
public DataTableData<UserListingModel> getCollaboratorsPaged(UserInfoTableRequestItem userInfoTableRequestItem, Principal principal) throws Exception {
|
||||||
//UserInfoDao userInfoDao = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao();
|
UserInfoDao userInfoDao = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao();
|
||||||
DMPDao dmpDao = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao();
|
|
||||||
|
|
||||||
// Gets all the DMPs the user is associated.
|
QueryableList<UserInfo> users = userInfoDao.asQueryable();
|
||||||
DataManagementPlanCriteria dataManagementPlanCriteria = new DataManagementPlanCriteria();
|
|
||||||
QueryableList<DMP> dmpItmes = dmpDao.getWithCriteria(dataManagementPlanCriteria);
|
|
||||||
QueryableList<DMP> dmpAuthItems = dmpDao.getAuthenticated(dmpItmes, principal.getId());
|
|
||||||
List<DMP> dmpList = dmpAuthItems.toList();
|
|
||||||
|
|
||||||
// List to place the associated users.
|
List<UserListingModel> colaborators = userInfoDao.getAuthenticated(users, principal.getId())
|
||||||
List<UserListingModel> associatedUsersList = new LinkedList<>();
|
.withHint(HintedModelFactory.getHint(UserListingModel.class))
|
||||||
|
.select(colaborator -> new UserListingModel().fromDataModel(colaborator));
|
||||||
|
|
||||||
// Iterate through the DMP list and get the users associated.
|
DataTableData<UserListingModel> dataTableData = new DataTableData<>();
|
||||||
for (DMP dmp : dmpList) {
|
dataTableData.setData(colaborators);
|
||||||
for (UserDMP userDMP : dmp.getUsers()) {
|
dataTableData.setTotalCount((long) colaborators.size());
|
||||||
if (userDMP.getUser().getId() != principal.getId()) {
|
return dataTableData;
|
||||||
associatedUsersList.add(new UserListingModel().fromDataModel(userDMP.getUser()));
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove duplicates from the associated users list.
|
|
||||||
associatedUsersList = associatedUsersList.stream().distinct().collect(Collectors.toList());
|
|
||||||
|
|
||||||
// Filter using the criteria.
|
|
||||||
List<UserListingModel> associatedUsersListFiltered = new LinkedList<>();
|
|
||||||
associatedUsersList.stream()
|
|
||||||
.filter(item -> item.getName().toLowerCase().contains(userInfoTableRequestItem.getCriteria().getCollaboratorLike().toLowerCase())).forEach(
|
|
||||||
o -> {
|
|
||||||
associatedUsersListFiltered.add(o);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/*QueryableList<eu.eudat.data.entities.UserInfo> users = userInfoDao.getWithCriteria(userInfoTableRequestItem.getCriteria()).withHint(HintedModelFactory.getHint(UserListingModel.class));
|
|
||||||
QueryableList<eu.eudat.data.entities.UserInfo> pagedUsers = PaginationManager.applyPaging(users, userInfoTableRequestItem);*/
|
|
||||||
|
|
||||||
//List<UserListingModel> modelUsers = pagedUsers.select(item -> new listing model().fromDataModel(item));
|
|
||||||
|
|
||||||
DataTableData<UserListingModel> dataTableData = new DataTableData<>();
|
|
||||||
dataTableData.setData(associatedUsersListFiltered);
|
|
||||||
dataTableData.setTotalCount((long)associatedUsersListFiltered.size());
|
|
||||||
return dataTableData;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue