diff --git a/visa-business/src/main/java/eu/ill/visa/business/services/CloudClientService.java b/visa-business/src/main/java/eu/ill/visa/business/services/CloudClientService.java index d1efd6bb..07f3e465 100644 --- a/visa-business/src/main/java/eu/ill/visa/business/services/CloudClientService.java +++ b/visa-business/src/main/java/eu/ill/visa/business/services/CloudClientService.java @@ -91,6 +91,10 @@ public CloudClient getCloudClient(Long id) { return cloudClient; } + public List getCloudClients(List ids) { + return ids.stream().map(this::getCloudClient).toList(); + } + public CloudProviderConfiguration getCloudProviderConfiguration(Long id) { return this.cloudProviderService.getById(id); } diff --git a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceMemberService.java b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceMemberService.java index e7dfa764..ad19e200 100644 --- a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceMemberService.java +++ b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceMemberService.java @@ -51,6 +51,17 @@ public User getOwnerByInstanceId(Long instanceId) { return null; } + public List getOwnersByInstanceIds(final List instanceIds) { + final List owners = repository.getOwnersByInstanceIds(instanceIds); + return instanceIds.stream().map(instanceId -> owners.stream().filter(owner -> { + return owner.getInstance().getId().equals(instanceId); + }) + .findAny() + .map(InstanceMember::getUser) + .orElse(null)) + .toList(); + } + public void save(@NotNull InstanceMember instanceMember) { this.repository.save(instanceMember); } diff --git a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceService.java b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceService.java index be01f282..f0778bf7 100644 --- a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceService.java +++ b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceService.java @@ -57,7 +57,7 @@ public Long countAll() { return this.repository.countAll(); } - public Long countAll(QueryFilter filter) { + public Long countAll(final InstanceFilter filter) { return this.repository.countAll(filter); } @@ -135,7 +135,7 @@ public void save(Instance instance) { } } - public List getAll(QueryFilter filter, OrderBy orderBy, Pagination pagination) { + public List getAll(final InstanceFilter filter, final OrderBy orderBy, final Pagination pagination) { return this.repository.getAll(filter, orderBy, pagination); } diff --git a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceSessionService.java b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceSessionService.java index 7c4d22d5..597decb8 100644 --- a/visa-business/src/main/java/eu/ill/visa/business/services/InstanceSessionService.java +++ b/visa-business/src/main/java/eu/ill/visa/business/services/InstanceSessionService.java @@ -121,6 +121,13 @@ public List getAllSessionMembersByInstanceId(@NotNull Lon return this.instanceSessionMemberRepository.getAllSessionMembersByInstanceId(instanceId); } + public List> getAllSessionMembersByInstanceIds(@NotNull List instanceIds) { + List ungroupedSessionMembers = this.instanceSessionMemberRepository.getAllSessionMembersByInstanceIds(instanceIds); + return instanceIds.stream().map(id -> { + return ungroupedSessionMembers.stream().filter(sessionMember -> sessionMember.getInstanceSession().getInstance().getId().equals(id)).toList(); + }).toList(); + } + public InstanceSessionMember getSessionMemberBySessionId(String sessionId) { return this.instanceSessionMemberRepository.getBySessionId(sessionId); } diff --git a/visa-core/src/main/java/eu/ill/visa/core/entity/Instance.java b/visa-core/src/main/java/eu/ill/visa/core/entity/Instance.java index bb1dac91..dea66fe9 100644 --- a/visa-core/src/main/java/eu/ill/visa/core/entity/Instance.java +++ b/visa-core/src/main/java/eu/ill/visa/core/entity/Instance.java @@ -235,8 +235,7 @@ public class Instance extends Timestampable { @Column(name = "unrestricted_member_access", nullable = true) private Date unrestrictedMemberAccess = null; - @OneToMany(orphanRemoval = true, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH}) - @JoinColumn(name = "instance_id", foreignKey = @ForeignKey(name = "fk_instance_member_id"), nullable = false) + @OneToMany(mappedBy = "instance", orphanRemoval = true, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.REFRESH}, fetch = FetchType.LAZY) private List members = new ArrayList<>(); @ManyToMany() diff --git a/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceMember.java b/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceMember.java index 7106fbd8..22c293cf 100644 --- a/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceMember.java +++ b/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceMember.java @@ -1,5 +1,6 @@ package eu.ill.visa.core.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; import eu.ill.visa.core.entity.enumerations.InstanceMemberRole; import jakarta.persistence.*; import org.apache.commons.lang3.builder.EqualsBuilder; @@ -33,6 +34,14 @@ where i.id = :instanceId and m.role = 'OWNER' """), + @NamedQuery(name = "instanceMember.getOwnersByInstanceIds", query = """ + SELECT m FROM InstanceMember m + LEFT JOIN FETCH m.instance i + LEFT JOIN FETCH m.user u + LEFT JOIN FETCH u.affiliation a + where i.id IN :instanceIds + and m.role = 'OWNER' + """), }) @Table(name = "instance_member") public class InstanceMember extends Timestampable { @@ -50,6 +59,11 @@ public class InstanceMember extends Timestampable { @Column(name = "role", length = 255, nullable = false) private InstanceMemberRole role; + @JsonIgnore + @ManyToOne(optional = false) + @JoinColumn(name = "instance_id", foreignKey = @ForeignKey(name = "fk_instance_id"), nullable = false) + private Instance instance; + private InstanceMember(Builder builder) { this.user = builder.user; this.role = builder.role; @@ -91,6 +105,14 @@ public void setRole(InstanceMemberRole role) { this.role = role; } + public Instance getInstance() { + return instance; + } + + public void setInstance(Instance instance) { + this.instance = instance; + } + @Override public boolean equals(Object object) { if (object instanceof InstanceMember) { diff --git a/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceSessionMember.java b/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceSessionMember.java index dfacd343..69824337 100644 --- a/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceSessionMember.java +++ b/visa-core/src/main/java/eu/ill/visa/core/entity/InstanceSessionMember.java @@ -38,6 +38,14 @@ AND i.active = true AND instance.deletedAt IS NULL """), + @NamedQuery(name = "instanceSessionMember.getAllForInstanceIds", query = """ + SELECT i FROM InstanceSessionMember i + LEFT JOIN i.instanceSession instanceSession + LEFT JOIN instanceSession.instance instance + WHERE instance.id IN :instanceIds + AND i.active = true + AND instance.deletedAt IS NULL + """), @NamedQuery(name = "instanceSessionMember.getAllByConnectionId", query = """ SELECT i FROM InstanceSessionMember i LEFT JOIN i.instanceSession instanceSession diff --git a/visa-core/src/main/java/eu/ill/visa/core/entity/User.java b/visa-core/src/main/java/eu/ill/visa/core/entity/User.java index c1936e64..f57b2c2b 100644 --- a/visa-core/src/main/java/eu/ill/visa/core/entity/User.java +++ b/visa-core/src/main/java/eu/ill/visa/core/entity/User.java @@ -21,7 +21,9 @@ @NamedQuery(name = "user.getByIdWithRoles", query = """ SELECT u FROM User u - LEFT OUTER JOIN FETCH u.userRoles + LEFT OUTER JOIN FETCH u.userRoles ur + LEFT OUTER JOIN FETCH ur.role r + LEFT OUTER JOIN FETCH u.affiliation a WHERE u.id = :id """), @NamedQuery(name = "user.getAll", query = """ diff --git a/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/FlavourLimitFilterProvider.java b/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/FlavourLimitFilterProvider.java deleted file mode 100644 index 6f874743..00000000 --- a/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/FlavourLimitFilterProvider.java +++ /dev/null @@ -1,21 +0,0 @@ -package eu.ill.visa.persistence.providers; - -import eu.ill.preql.AbstractFilterQueryProvider; -import eu.ill.visa.core.entity.FlavourLimit; - -import jakarta.persistence.EntityManager; - -public class FlavourLimitFilterProvider extends AbstractFilterQueryProvider { - - public FlavourLimitFilterProvider(EntityManager entityManager) { - super(FlavourLimit.class, entityManager); - addFields( - orderableField("id"), - orderableField("objectId"), - orderableField("objectType"), - orderableField("flavour.id"), - orderableField("flavour.name") - ); - } - -} diff --git a/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/InstanceFilterProvider.java b/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/InstanceFilterProvider.java deleted file mode 100644 index ef9661d6..00000000 --- a/visa-persistence/src/main/java/eu/ill/visa/persistence/providers/InstanceFilterProvider.java +++ /dev/null @@ -1,43 +0,0 @@ -package eu.ill.visa.persistence.providers; - -import eu.ill.preql.AbstractFilterQueryProvider; -import eu.ill.preql.parser.FieldValueParser; -import eu.ill.preql.parser.value.StringValueParser; -import eu.ill.visa.core.entity.Instance; -import eu.ill.visa.core.entity.enumerations.InstanceMemberRole; -import eu.ill.visa.core.entity.enumerations.InstanceState; - -import jakarta.persistence.EntityManager; - -public class InstanceFilterProvider extends AbstractFilterQueryProvider { - - public InstanceFilterProvider(EntityManager entityManager) { - super(Instance.class, entityManager); - addFields( - orderableField("id"), - orderableField("name"), - orderableField("comments"), - orderableField("createdAt"), - orderableField("lastSeenAt"), - orderableField("plan.id"), - orderableField("plan.flavour.id"), - orderableField("plan.flavour.name"), - orderableField("plan.image.id"), - orderableField("plan.image.name"), - orderableField("experiments.instrument.id", "instrument.id"), - orderableField("experiments.instrument.name", "instrument.name"), - orderableField("members.user.id", "user.id"), - orderableField("members.user.firstName", "user.firstName"), - orderableField("members.user.lastName", "user.lastName"), - orderableField("state", (FieldValueParser) value -> { - final StringValueParser parser = new StringValueParser(); - return InstanceState.valueOf(parser.parse(value)); - }), - orderableField("members.role", "user.role", (FieldValueParser) value -> { - final StringValueParser parser = new StringValueParser(); - return InstanceMemberRole.valueOf(parser.parse(value)); - }) - ); - } - -} diff --git a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceMemberRepository.java b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceMemberRepository.java index 0d264447..dc2d1114 100644 --- a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceMemberRepository.java +++ b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceMemberRepository.java @@ -65,6 +65,12 @@ public InstanceMember getOwnerByInstanceId(Long instanceId) { } } + public List getOwnersByInstanceIds(List instanceIds) { + final TypedQuery query = getEntityManager().createNamedQuery("instanceMember.getOwnersByInstanceIds", InstanceMember.class); + query.setParameter("instanceIds", instanceIds); + return query.getResultList(); + } + public void save(InstanceMember instanceMember) { if (instanceMember.getId() == null) { persist(instanceMember); diff --git a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceRepository.java b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceRepository.java index d200cbc6..0d1b1470 100644 --- a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceRepository.java +++ b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceRepository.java @@ -1,20 +1,19 @@ package eu.ill.visa.persistence.repositories; -import eu.ill.preql.FilterQuery; import eu.ill.visa.core.domain.*; import eu.ill.visa.core.domain.filters.InstanceFilter; -import eu.ill.visa.core.entity.Instance; -import eu.ill.visa.core.entity.InstanceThumbnail; -import eu.ill.visa.core.entity.User; +import eu.ill.visa.core.entity.*; import eu.ill.visa.core.entity.enumerations.InstanceMemberRole; import eu.ill.visa.core.entity.enumerations.InstanceState; -import eu.ill.visa.persistence.providers.InstanceFilterProvider; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.persistence.EntityManager; import jakarta.persistence.NoResultException; import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.*; import org.apache.commons.lang3.time.DateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; @@ -22,11 +21,11 @@ import java.util.List; import java.util.Map.Entry; -import static java.util.Objects.requireNonNullElseGet; - @Singleton public class InstanceRepository extends AbstractRepository { + private static final Logger logger = LoggerFactory.getLogger(InstanceRepository.class); + @Inject InstanceRepository(final EntityManager entityManager) { super(entityManager); @@ -99,25 +98,121 @@ public Long countAllForState(InstanceState state) { } } - public List getAll(QueryFilter filter, OrderBy orderBy, Pagination pagination) { - final InstanceFilterProvider provider = new InstanceFilterProvider(getEntityManager()); - final FilterQuery query = createFilterQuery(provider, requireNonNullElseGet(filter, QueryFilter::new), orderBy, pagination); - query.addExpression((criteriaBuilder, root) -> - criteriaBuilder.isNull(root.get("deletedAt")) - ); + public List getAll(final InstanceFilter filter, final OrderBy orderBy, final Pagination pagination) { + final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); + final CriteriaQuery cbQuery = cb.createQuery(Instance.class); + final Root root = cbQuery.from(Instance.class); + + Fetch planFetch = root.fetch("plan", JoinType.INNER); + planFetch.fetch("image", JoinType.INNER); + planFetch.fetch("flavour", JoinType.INNER); + + InstanceRequestContext context = new InstanceRequestContext(root); + final List predicates = this.convertFilterToPredicates(filter, cbQuery, cb, context); + cbQuery.where(cb.and(predicates.toArray(new Predicate[0]))).distinct(true); + + if (orderBy != null) { + if (orderBy.getName().equals("id")) { + Path idOrder = root.get("id"); + cbQuery.orderBy(orderBy.getAscending() ? cb.asc(idOrder) : cb.desc(idOrder)); + + } else if (orderBy.getName().equals("createdAt")) { + Path createdAtOrder = root.get("createdAt"); + cbQuery.orderBy(orderBy.getAscending() ? cb.asc(createdAtOrder) : cb.desc(createdAtOrder)); + + } else if (orderBy.getName().equals("name")) { + Path nameOrder = root.get("name"); + cbQuery.orderBy(orderBy.getAscending() ? cb.asc(nameOrder) : cb.desc(nameOrder)); + + } else if (orderBy.getName().equals("state")) { + Path stateOrder = root.get("state"); + cbQuery.orderBy(orderBy.getAscending() ? cb.asc(stateOrder) : cb.desc(stateOrder)); + + } else if (orderBy.getName().equals("terminationDate")) { + Path terminationOrder = root.get("terminationDate"); + cbQuery.orderBy(orderBy.getAscending() ? cb.asc(terminationOrder) : cb.desc(terminationOrder)); + + } else { + logger.warn("Client has requested ordering of instances by unknown field: {}", orderBy.getName()); + } + } + + TypedQuery query = getEntityManager().createQuery(cbQuery); + + + if (pagination != null) { + query.setFirstResult(pagination.getOffset()); + query.setMaxResults(pagination.getLimit()); + } + return query.getResultList(); } - public Long countAll(QueryFilter filter) { - final InstanceFilterProvider provider = new InstanceFilterProvider(getEntityManager()); - final FilterQuery query = createFilterQuery(provider, requireNonNullElseGet(filter, QueryFilter::new), null, null); - query.addExpression((criteriaBuilder, root) -> - criteriaBuilder.isNull(root.get("deletedAt")) - ); - return query.count(); + public Long countAll(final InstanceFilter filter) { + final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); + final CriteriaQuery cbQuery = cb.createQuery(Long.class); + final Root root = cbQuery.from(Instance.class); + + InstanceRequestContext context = new InstanceRequestContext(root); + final List predicates = this.convertFilterToPredicates(filter, cbQuery, cb, context); + + cbQuery.where(cb.and(predicates.toArray(new Predicate[0]))); + + cbQuery.select(cb.countDistinct(root)); + TypedQuery query = getEntityManager().createQuery(cbQuery); + return query.getSingleResult(); } + protected List convertFilterToPredicates(final InstanceFilter filter, + final CriteriaQuery cq, + final CriteriaBuilder cb, + final InstanceRequestContext context) { + + final Root root = context.getRoot(); + + final List predicates = new ArrayList<>(); + + if (filter.getId() != null) { + predicates.add(cb.equal(root.get("id"), filter.getId())); + } + + if (filter.getNameLike() != null) { + String nameLike = String.format("%%%s%%", filter.getNameLike()).toLowerCase(); + predicates.add(cb.like(cb.lower(root.get("name")), nameLike)); + } + + if (filter.getOwnerId() != null) { + Join memberJoin = context.getMemberJoin(); + predicates.add(cb.equal(memberJoin.get("user").get("id"), filter.getOwnerId())); + predicates.add(cb.equal(memberJoin.get("role"), InstanceMemberRole.OWNER)); + } + + if (filter.getInstrumentId() != null) { + Join experimentJoin = context.getExperimentJoin(); + predicates.add(cb.equal(experimentJoin.get("instrument").get("id"), filter.getInstrumentId())); + } + + if (filter.getInstrumentId() != null) { + Join experimentJoin = context.getExperimentJoin(); + predicates.add(cb.equal(experimentJoin.get("instrument").get("id"), filter.getInstrumentId())); + } + + if (filter.getImageId() != null) { + Join planJoin = context.getPlanJoin(); + predicates.add(cb.equal(planJoin.get("image").get("id"), filter.getImageId())); + } + + if (filter.getFlavourId() != null) { + Join planJoin = context.getPlanJoin(); + predicates.add(cb.equal(planJoin.get("flavour").get("id"), filter.getFlavourId())); + } + + predicates.add(cb.isNull(root.get("deletedAt"))); + + return predicates; + } + /** * Get all instances that will soon expire due to inactivity * @@ -456,4 +551,40 @@ public void saveThumbnail(InstanceThumbnail thumbnail) { } } + + protected static final class InstanceRequestContext { + private final Root root; + private Join memberJoin = null; + private Join experimentJoin = null; + private Join planJoin = null; + + public InstanceRequestContext(Root root) { + this.root = root; + } + + public Root getRoot() { + return root; + } + + public Join getMemberJoin() { + if (memberJoin == null) { + memberJoin = root.join("members", JoinType.INNER); + } + return memberJoin; + } + + public Join getExperimentJoin() { + if (experimentJoin == null) { + experimentJoin = root.join("experiments", JoinType.LEFT); + } + return experimentJoin; + } + + public Join getPlanJoin() { + if (planJoin == null) { + planJoin = root.join("plan", JoinType.INNER); + } + return planJoin; + } + } } diff --git a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceSessionMemberRepository.java b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceSessionMemberRepository.java index 14610afc..8805a37f 100644 --- a/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceSessionMemberRepository.java +++ b/visa-persistence/src/main/java/eu/ill/visa/persistence/repositories/InstanceSessionMemberRepository.java @@ -75,6 +75,12 @@ public List getAllSessionMembersByInstanceId(final Long i return query.getResultList(); } + public List getAllSessionMembersByInstanceIds(final List instanceIds) { + final TypedQuery query = getEntityManager().createNamedQuery("instanceSessionMember.getAllForInstanceIds", InstanceSessionMember.class); + query.setParameter("instanceIds", instanceIds); + return query.getResultList(); + } + public List getAllSessionMembersByInstanceSessionId(final Long instanceSessionId) { final TypedQuery query = getEntityManager().createNamedQuery("instanceSessionMember.getAllByInstanceSessionId", InstanceSessionMember.class); query.setParameter("instanceSessionId", instanceSessionId); diff --git a/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resolvers/InstanceResolver.java b/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resolvers/InstanceResolver.java index fb7fc9c4..e2cc0e9c 100644 --- a/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resolvers/InstanceResolver.java +++ b/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resolvers/InstanceResolver.java @@ -5,7 +5,6 @@ import eu.ill.visa.cloud.domain.CloudInstance; import eu.ill.visa.cloud.exceptions.CloudException; import eu.ill.visa.cloud.services.CloudClient; -import eu.ill.visa.core.entity.User; import eu.ill.visa.web.graphql.exceptions.DataFetchingException; import eu.ill.visa.web.graphql.types.*; import jakarta.inject.Inject; @@ -123,15 +122,16 @@ public CloudInstanceType cloudInstance(@Source InstanceType instance) throws Dat } } - public UserType owner(@Source final InstanceType instance) { - User user = this.instanceMemberService.getOwnerByInstanceId(instance.getId()); - return new UserType(user); + public List owner(@Source final List instances) { + return this.instanceMemberService.getOwnersByInstanceIds(instances.stream().map(InstanceType::getId).toList()).stream() + .map(UserType::new) + .toList(); } - public List activeSessions(@Source final InstanceType instance) throws DataFetchingException { + public List> activeSessions(@Source final List instances) throws DataFetchingException { try { - return instanceSessionService.getAllSessionMembersByInstanceId(instance.getId()).stream() - .map(InstanceSessionMemberType::new) + return instanceSessionService.getAllSessionMembersByInstanceIds(instances.stream().map(InstanceType::getId).toList()).stream() + .map(instanceSessionMembers -> instanceSessionMembers.stream().map(InstanceSessionMemberType::new).toList()) .toList(); } catch (InvalidQueryException exception) { throw new DataFetchingException(exception.getMessage()); @@ -148,12 +148,13 @@ public List sessions(@Source final InstanceType insta } } - public @NotNull CloudClientType cloudClient(@Source final InstanceType instance) { - CloudClient cloudClient = this.cloudClientService.getCloudClient(instance.getCloudId()); - if (cloudClient != null) { - return new CloudClientType(cloudClient); - } - return null; + public @NotNull List cloudClient(@Source final List instances) { + List cloudClients = this.cloudClientService.getCloudClients(instances.stream().map(InstanceType::getCloudId).distinct().toList()); + return instances.stream().map(instanceType -> { + return cloudClients.stream().filter(cloudClient -> { + return cloudClient.getId() == -1 ? instanceType.getCloudId() == null : cloudClient.getId().equals(instanceType.getCloudId()); + }).findFirst().orElse(null); + }).map(cloudClient -> cloudClient == null ? null : new CloudClientType(cloudClient)).toList(); } } diff --git a/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resources/InstanceResource.java b/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resources/InstanceResource.java index 30474759..9c57c704 100644 --- a/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resources/InstanceResource.java +++ b/visa-web-graphql/src/main/java/eu/ill/visa/web/graphql/resources/InstanceResource.java @@ -8,7 +8,7 @@ import eu.ill.visa.cloud.services.CloudClient; import eu.ill.visa.core.domain.NumberInstancesByCloudClient; import eu.ill.visa.core.domain.OrderBy; -import eu.ill.visa.core.domain.QueryFilter; +import eu.ill.visa.core.domain.filters.InstanceFilter; import eu.ill.visa.core.entity.Instance; import eu.ill.visa.core.entity.Role; import eu.ill.visa.core.entity.User; @@ -20,7 +20,6 @@ import eu.ill.visa.web.graphql.exceptions.ValidationException; import eu.ill.visa.web.graphql.inputs.OrderByInput; import eu.ill.visa.web.graphql.inputs.PaginationInput; -import eu.ill.visa.web.graphql.inputs.QueryFilterInput; import eu.ill.visa.web.graphql.types.*; import io.quarkus.security.identity.SecurityIdentity; import io.smallrye.graphql.api.AdaptToScalar; @@ -40,7 +39,6 @@ import static eu.ill.visa.web.graphql.inputs.OrderByInput.toOrderBy; import static eu.ill.visa.web.graphql.inputs.PaginationInput.toPagination; -import static eu.ill.visa.web.graphql.inputs.QueryFilterInput.toQueryFilter; import static java.lang.String.format; import static java.util.Arrays.asList; import static java.util.Objects.requireNonNullElse; @@ -80,18 +78,18 @@ public InstanceResource(final InstanceService instanceService, * @throws DataFetchingException thrown if there was an error fetching the results */ @Query - public @NotNull Connection instances(final QueryFilterInput filter, final OrderByInput orderBy, @NotNull final PaginationInput pagination) throws DataFetchingException { + public @NotNull Connection instances(final InstanceFilter filter, final OrderByInput orderBy, @NotNull final PaginationInput pagination) throws DataFetchingException { try { if (!pagination.isLimitBetween(0, 50)) { throw new DataFetchingException(format("Limit must be between %d and %d", 0, 200)); } final List results = instanceService.getAll( - requireNonNullElseGet(toQueryFilter(filter), QueryFilter::new), + requireNonNullElseGet(filter, InstanceFilter::new), requireNonNullElseGet(toOrderBy(orderBy), () -> new OrderBy("name", true)), toPagination(pagination) ).stream() .map(InstanceType::new) .toList(); - final PageInfo pageInfo = new PageInfo(instanceService.countAll(toQueryFilter(filter)), pagination.getLimit(), pagination.getOffset()); + final PageInfo pageInfo = new PageInfo(instanceService.countAll(filter), pagination.getLimit(), pagination.getOffset()); return new Connection<>(pageInfo, results); } catch (InvalidQueryException exception) { throw new DataFetchingException(exception.getMessage()); @@ -106,9 +104,9 @@ public InstanceResource(final InstanceService instanceService, * @throws DataFetchingException thrown if there was an error fetching the result */ @Query - public @NotNull @AdaptToScalar(Scalar.Int.class) Long countInstances(final QueryFilterInput filter) throws DataFetchingException { + public @NotNull @AdaptToScalar(Scalar.Int.class) Long countInstances(final InstanceFilter filter) throws DataFetchingException { try { - return instanceService.countAll(requireNonNullElseGet(toQueryFilter(filter), QueryFilter::new)); + return instanceService.countAll(requireNonNullElseGet(filter, InstanceFilter::new)); } catch (InvalidQueryException exception) { throw new DataFetchingException(exception.getMessage()); } @@ -132,7 +130,7 @@ public InstanceType instance(final @NotNull @AdaptToScalar(Scalar.Int.class) Lon */ @Query public @NotNull Connection recentInstances(final @NotNull PaginationInput pagination) throws DataFetchingException { - return instances(new QueryFilterInput(), new OrderByInput("createdAt", false), pagination); + return instances(new InstanceFilter(), new OrderByInput("createdAt", false), pagination); } /**