Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfux/issue 9.2 #16

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.ill.visa.business.services;

import eu.ill.visa.business.InvalidTokenException;
import eu.ill.visa.core.domain.fetches.InstanceFetch;
import eu.ill.visa.core.entity.Instance;
import eu.ill.visa.core.entity.InstanceAuthenticationToken;
import eu.ill.visa.core.entity.User;
Expand All @@ -17,18 +18,24 @@
public class InstanceAuthenticationTokenService {

private final InstanceAuthenticationTokenRepository repository;
final InstanceService instanceService;

@Inject
public InstanceAuthenticationTokenService(InstanceAuthenticationTokenRepository repository) {
public InstanceAuthenticationTokenService(final InstanceAuthenticationTokenRepository repository,
final InstanceService instanceService) {
this.repository = repository;
this.instanceService = instanceService;
}

public List<InstanceAuthenticationToken> getAll() {
return this.repository.getAll();
}

public InstanceAuthenticationToken getByToken(String token) {
return this.repository.getByToken(token).lazyLoadInit();
InstanceAuthenticationToken instanceAuthenticationToken = this.repository.getByToken(token);
Instance instance = instanceAuthenticationToken.getInstance();
instanceAuthenticationToken.setInstance(this.instanceService.handleFetches(instance, List.of(InstanceFetch.members)));
return instanceAuthenticationToken;
}

public InstanceAuthenticationToken create(User user, Instance instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.ill.visa.business.gateway.events.InstanceThumbnailUpdatedEvent;
import eu.ill.visa.cloud.services.CloudClient;
import eu.ill.visa.core.domain.*;
import eu.ill.visa.core.domain.fetches.InstanceFetch;
import eu.ill.visa.core.domain.filters.InstanceFilter;
import eu.ill.visa.core.entity.*;
import eu.ill.visa.core.entity.enumerations.InstanceMemberRole;
Expand All @@ -15,6 +16,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -65,10 +67,10 @@ public Long countAllForState(InstanceState state) {
return this.repository.countAllForState(state);
}

public List<Instance> getAllForUser(User user) {
return this.repository.getAllForUser(user).stream()
.map(Instance::lazyLoadInit)
.toList();
public List<Instance> getAllForUser(User user, @NotNull List<InstanceFetch> fetches) {
List<Instance> instances = this.repository.getAllForUser(user);

return this.handleFetches(instances, fetches);
}

public Long countAllForUser(User user) {
Expand All @@ -84,15 +86,17 @@ public Instance getById(Long id) {
}

public Instance getFullById(Long id) {
return this.repository.getById(id).lazyLoadInit();
Instance instance = this.repository.getById(id);
return this.handleFetches(instance, List.of(InstanceFetch.members, InstanceFetch.experiments, InstanceFetch.attributes));
}

public Instance getByUID(String uid) {
return this.repository.getByUID(uid);
}

public Instance getFullByUID(String uid) {
return this.repository.getByUID(uid).lazyLoadInit();
Instance instance = this.repository.getByUID(uid);
return this.handleFetches(instance, List.of(InstanceFetch.members, InstanceFetch.experiments, InstanceFetch.attributes));
}

public Instance create(Instance.Builder instanceBuilder) {
Expand Down Expand Up @@ -139,18 +143,12 @@ public List<Instance> getAll(final InstanceFilter filter, final OrderBy orderBy,
return this.repository.getAll(filter, orderBy, pagination);
}

public List<Instance> getAllFull() {
return this.repository.getAll().stream()
.map(Instance::lazyLoadInit)
.toList();
}

public List<Instance> getAll() {
return this.repository.getAll();
public List<Instance> getAll(List<InstanceFetch> fetches) {
return this.handleFetches(this.repository.getAll(), fetches);
}

public Map<CloudClient, List<Instance>> getAllByCloud() {
List<Instance> instances = this.getAll();
List<Instance> instances = this.getAll(List.of());

List<CloudClient> cloudClients = this.cloudClientService.getAll();
Map<CloudClient, List<Instance>> cloudInstances = new HashMap<>();
Expand Down Expand Up @@ -195,9 +193,7 @@ public List<Instance> getAllNewTerminations(final Integer terminationInHours) {
}

public List<Instance> getAllToDelete() {
return this.repository.getAllToDelete().stream()
.map(Instance::lazyLoadInit)
.toList();
return this.handleFetches(this.repository.getAllToDelete(), List.of(InstanceFetch.members, InstanceFetch.experiments, InstanceFetch.attributes));
}

public Long countAllForInstrumentScientist(User user, InstanceFilter filter) {
Expand All @@ -224,6 +220,10 @@ public List<Instance> getAllForITSupport(InstanceFilter filter, OrderBy orderBy,
return this.repository.getAllForITSupport(filter, orderBy, pagination);
}

public Instance getByUidForOwner(User user, String instanceUid) {
return this.repository.getByIdForOwner(user, instanceUid);
}

public Instance getByIdForInstrumentScientist(User user, Long instanceId) {
return this.repository.getByIdForInstrumentScientist(user, instanceId);
}
Expand Down Expand Up @@ -258,6 +258,13 @@ public boolean isOwnerOrAdmin(User user, Instance instance) {
return instance.isOwner(user) || user.hasRole(Role.ADMIN_ROLE);
}

public boolean isOwnerOrAdmin(User user, String instanceUid) {
if (user.hasRole(Role.ADMIN_ROLE)) {
return true;
}
return this.getByUidForOwner(user, instanceUid) != null;
}

public boolean isAuthorisedForInstance(User user, Instance instance) {
if (instance.isMember(user)) {
return true;
Expand Down Expand Up @@ -326,17 +333,15 @@ private List<Instance> getAllForSupportByUserRole(User user, InstanceFilter filt
}

public List<Instance> getAllForSupportUser(User user, InstanceFilter filter, OrderBy orderBy, Pagination pagination) {
return this.getAllForSupportByUserRole(user, filter, orderBy, pagination).stream()
.map(Instance::lazyLoadInit)
.toList();
return this.handleFetches(this.getAllForSupportByUserRole(user, filter, orderBy, pagination), List.of(InstanceFetch.members, InstanceFetch.experiments, InstanceFetch.attributes));
}

public Long countAllForUserAndRole(User user, InstanceMemberRole role) {
return repository.countAllForUserAndRole(user, role);
}

public InstanceThumbnail createOrUpdateThumbnail(Instance instance, byte[] data) {
final InstanceThumbnail thumbnail = requireNonNullElse(repository.getThumbnailForInstance(instance), new InstanceThumbnail());
final InstanceThumbnail thumbnail = requireNonNullElse(repository.getThumbnailForInstanceUid(instance.getUid()), new InstanceThumbnail());
if (thumbnail.getInstance() == null) {
thumbnail.setInstance(instance);
}
Expand All @@ -349,7 +354,11 @@ public InstanceThumbnail createOrUpdateThumbnail(Instance instance, byte[] data)
}

public InstanceThumbnail getThumbnailForInstance(Instance instance) {
return repository.getThumbnailForInstance(instance);
return this.getThumbnailForInstanceUid(instance.getUid());
}

public InstanceThumbnail getThumbnailForInstanceUid(String instanceUid) {
return repository.getThumbnailForInstanceUid(instanceUid);
}

public String getUID() {
Expand All @@ -367,4 +376,32 @@ public String getUID() {
}
} while (true);
}

public Instance handleFetches(Instance instance, List<InstanceFetch> fetches) {
if (instance != null) {
return this.handleFetches(List.of(instance), fetches)
.stream()
.findAny()
.orElse(null);
}
return null;
}

public List<Instance> handleFetches(List<Instance> instances, List<InstanceFetch> fetches) {
if (fetches != null) {
if (fetches.contains(InstanceFetch.members)) {
instances = this.repository.getAllWithMembersForInstances(instances);
}

if (fetches.contains(InstanceFetch.experiments)) {
instances = this.repository.getAllWithExperimentsForInstances(instances);
}

if (fetches.contains(InstanceFetch.attributes)) {
instances = this.repository.getAllWithAttributesForInstances(instances);
}
}

return instances;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public void cleanupSession() {

public boolean canConnectWhileOwnerAway(Instance instance, String userId) {
final User user = this.userService.getById(userId);
return this.canConnectWhileOwnerAway(instance, user);
}

public boolean canConnectWhileOwnerAway(Instance instance, User user) {
boolean userIsOwner = instance.isOwner(user);

// The user is the owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ void testCreateAndExpire() throws InterruptedException {
@DisplayName("Delete expired instances")
void testDeleteInstanceExpiration() {
List<InstanceExpiration> expirations1 = instanceExpirationService.getAllExpired();
List<Instance> instances1 = instanceService.getAll();
List<Instance> instances1 = instanceService.getAll(List.of());
for (InstanceExpiration instanceExpiration : expirations1) {
instanceExpirationService.delete(instanceExpiration);
}
List<InstanceExpiration> expirations2 = instanceExpirationService.getAllExpired();
List<Instance> instances2 = instanceService.getAll();
List<Instance> instances2 = instanceService.getAll(List.of());

assertEquals(0, expirations2.size());
assertEquals(instances1.size(), instances2.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class InstanceServiceTest {
@Test
@DisplayName("Get all instances")
void testGetAll() {
List<Instance> results = instanceService.getAll();
List<Instance> results = instanceService.getAll(List.of());
assertEquals(11, results.size());
}

Expand Down Expand Up @@ -360,7 +360,7 @@ Instance createInstanceWithExperiments(List<String> experimentIds) {
public void instancesForUser() {
User user = userService.getById("1");
assertNotNull(user);
assertEquals(3, instanceService.getAllForUser(user).size());
assertEquals(3, instanceService.getAllForUser(user, List.of()).size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package eu.ill.visa.core.domain.fetches;


public enum InstanceFetch {
members,
experiments,
attributes,
}
49 changes: 38 additions & 11 deletions visa-core/src/main/java/eu/ill/visa/core/entity/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
WHERE i.id = :id
AND i.deletedAt IS NULL
"""),
@NamedQuery(name = "instance.getByUidForOwner", query = """
SELECT i FROM Instance i
JOIN i.members m
JOIN FETCH i.plan p
JOIN FETCH p.image im
JOIN FETCH p.flavour f
WHERE m.user = :user
AND m.role = 'OWNER'
AND i.uid = :instanceUid
AND i.deletedAt IS NULL
"""),
@NamedQuery(name = "instance.getByUID", query = """
SELECT i FROM Instance i
LEFT JOIN i.members m
Expand All @@ -31,19 +42,19 @@
"""),
@NamedQuery(name = "instance.getAll", query = """
SELECT DISTINCT i FROM Instance i
LEFT JOIN i.members m
JOIN FETCH i.plan p
JOIN FETCH p.image im
JOIN FETCH p.flavour f
WHERE i.deletedAt IS NULL
"""),
@NamedQuery(name = "instance.getAllInactive", query = """
SELECT DISTINCT i FROM Instance i
LEFT JOIN i.members m
WHERE i.deletedAt IS NULL
AND i.terminationDate IS NOT NULL
AND i.lastSeenAt < :date
"""),
@NamedQuery(name = "instance.getAllNewInactive", query = """
SELECT DISTINCT i FROM Instance i
LEFT JOIN i.members m
LEFT OUTER JOIN InstanceExpiration ie on ie.instance = i
WHERE i.deletedAt IS NULL
AND i.terminationDate IS NOT NULL
Expand All @@ -52,7 +63,6 @@
"""),
@NamedQuery(name = "instance.getAllNewTerminations", query = """
SELECT DISTINCT i FROM Instance i
LEFT JOIN i.members m
LEFT OUTER JOIN InstanceExpiration ie on ie.instance = i
WHERE i.deletedAt IS NULL
AND i.terminationDate IS NOT NULL
Expand All @@ -71,10 +81,34 @@ SELECT COUNT(i) FROM Instance i
@NamedQuery(name = "instance.getAllForUser", query = """
SELECT i FROM Instance i
JOIN i.members m
JOIN FETCH i.plan p
JOIN FETCH p.image im
JOIN FETCH p.flavour f
WHERE m.user = :user
AND i.deletedAt IS NULL
ORDER BY i.id DESC
"""),
@NamedQuery(name = "instance.getAllWithMembersForInstances", query = """
SELECT i FROM Instance i
LEFT JOIN FETCH i.members m
LEFT JOIN FETCH m.user u
LEFT JOIN FETCH u.affiliation a
WHERE i IN :instances
ORDER BY i.id DESC
"""),
@NamedQuery(name = "instance.getAllWithExperimentsForInstances", query = """
SELECT i FROM Instance i
LEFT JOIN FETCH i.experiments e
LEFT JOIN FETCH e.instrument instr
WHERE i IN :instances
ORDER BY i.id DESC
"""),
@NamedQuery(name = "instance.getAllWithAttributesForInstances", query = """
SELECT i FROM Instance i
LEFT JOIN FETCH i.attributes a
WHERE i IN :instances
ORDER BY i.id DESC
"""),
@NamedQuery(name = "instance.countAllForUser", query = """
SELECT COUNT(i) FROM Instance i
JOIN i.members m
Expand Down Expand Up @@ -690,13 +724,6 @@ public InstanceMember createMember(User user, InstanceMemberRole role) {
return instanceMember;
}

public Instance lazyLoadInit() {
this.getExperiments().size();
this.getAttributes().size();
this.getMembers().stream().peek(member -> member.getUser().lazyLoadInit()).toList().size();
return this;
}

public static final class Builder {
private Long id;
private String uid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ public void setInstance(Instance instance) {
this.instance = instance;
}

public InstanceAuthenticationToken lazyLoadInit() {
this.instance.lazyLoadInit();
this.user.lazyLoadInit();
return this;
}

public static final class Builder {
private Long id;
private String token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

@Entity
@NamedQueries({
@NamedQuery(name = "instanceThumbnail.getForInstance", query = """
@NamedQuery(name = "instanceThumbnail.getForInstanceUid", query = """
SELECT it FROM InstanceThumbnail it
WHERE it.instance = :instance
WHERE it.instance.uid = :instanceUid
"""),
})
@Table(name = "instance_thumbnail", uniqueConstraints = {
Expand Down
5 changes: 0 additions & 5 deletions visa-core/src/main/java/eu/ill/visa/core/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ public String toString() {
.toString();
}

public User lazyLoadInit() {
this.userRoles.size();
return this;
}

public static final class Builder {
private String id;
private String firstName;
Expand Down
Loading
Loading