1
|
|
|
package com.hltech.judged.server.infrastructure.persistence.environment; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.environment.Environment; |
4
|
|
|
import com.hltech.judged.server.domain.environment.EnvironmentRepository; |
5
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
6
|
|
|
import com.hltech.judged.server.domain.environment.Space; |
7
|
|
|
import lombok.RequiredArgsConstructor; |
8
|
|
|
import org.springframework.stereotype.Repository; |
9
|
|
|
|
10
|
|
|
import javax.persistence.NoResultException; |
11
|
|
|
import java.util.HashSet; |
12
|
|
|
import java.util.Optional; |
13
|
|
|
import java.util.Set; |
14
|
|
|
import java.util.stream.Collectors; |
15
|
|
|
|
16
|
|
|
@Repository |
17
|
|
|
@RequiredArgsConstructor |
18
|
|
|
public class JPAEnvironmentRepository implements EnvironmentRepository { |
19
|
|
|
|
20
|
|
|
private final SpringDataEnvironmentRepository springDataEnvironmentRepository; |
21
|
|
|
|
22
|
|
|
@Override |
23
|
|
|
public Environment persist(Environment environment) { |
24
|
|
|
EnvironmentTuple environmentTuple = toEnvironmentTuple(environment); |
25
|
|
|
return toEnvironment(springDataEnvironmentRepository.saveAndFlush(environmentTuple)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
@Override |
29
|
|
|
public Set<String> getNames() { |
30
|
|
|
return springDataEnvironmentRepository.getNames(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
@Override |
34
|
|
|
public Optional<Environment> find(String name) { |
35
|
|
|
return springDataEnvironmentRepository.findById(name) |
36
|
|
|
.map(this::toEnvironment); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private EnvironmentTuple toEnvironmentTuple(Environment environment) { |
40
|
|
|
return new EnvironmentTuple(environment.getName(), getSpaceServiceVersions(environment)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private Set<ServiceVersion> getSpaceServiceVersions(Environment environment) { |
44
|
|
|
return environment.getSpaceNames().stream() |
45
|
|
|
.flatMap(space -> environment.getServices(space).stream() |
46
|
|
|
.map(service -> new ServiceVersion(space, service.getName(), service.getVersion()))) |
47
|
|
|
.collect(Collectors.toUnmodifiableSet()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private Environment toEnvironment(EnvironmentTuple environmentTuple) { |
51
|
|
|
Set<Space> spaces = new HashSet<>(); |
52
|
|
|
|
53
|
|
|
environmentTuple.getServiceVersions() |
54
|
|
|
.forEach(serviceVersion -> { |
55
|
|
|
Optional<Space> foundSpace = spaces.stream() |
56
|
|
|
.filter(space -> space.getName().equals(serviceVersion.getSpace())) |
57
|
|
|
.findAny(); |
58
|
|
|
|
59
|
|
|
if (foundSpace.isPresent()) { |
60
|
|
|
spaces.remove(foundSpace.get()); |
61
|
|
|
Set<ServiceId> serviceIds = new HashSet<>(foundSpace.get().getServiceIds()); |
62
|
|
|
serviceIds.add(new ServiceId(serviceVersion.getName(), serviceVersion.getVersion())); |
63
|
|
|
|
64
|
|
|
spaces.add(new Space(foundSpace.get().getName(), serviceIds)); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
spaces.add(new Space(serviceVersion.getSpace(), Set.of(new ServiceId(serviceVersion.getName(), serviceVersion.getVersion())))); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
return new Environment(environmentTuple.getName(), spaces); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|