|
1
|
|
|
package dev.hltech.dredd.domain.environment; |
|
2
|
|
|
|
|
3
|
|
|
import com.google.common.collect.Multimap; |
|
4
|
|
|
import dev.hltech.dredd.domain.ServiceVersion; |
|
5
|
|
|
|
|
6
|
|
|
import javax.persistence.*; |
|
7
|
|
|
import java.util.Set; |
|
8
|
|
|
|
|
9
|
|
|
import static com.google.common.collect.HashMultimap.create; |
|
10
|
|
|
import static com.google.common.collect.Sets.newHashSet; |
|
11
|
|
|
import static java.util.stream.Collectors.toList; |
|
12
|
|
|
import static java.util.stream.Collectors.toSet; |
|
13
|
|
|
|
|
14
|
|
|
@Entity |
|
15
|
|
|
@Table(name = "environments") |
|
16
|
|
|
@Access(AccessType.FIELD) |
|
17
|
|
|
public class EnvironmentAggregate { |
|
18
|
|
|
|
|
19
|
|
|
public static final String DEFAULT_NAMESPACE = "default"; |
|
20
|
|
|
|
|
21
|
|
|
@Id |
|
22
|
|
|
private String name; |
|
23
|
|
|
|
|
24
|
|
|
@ElementCollection(fetch = FetchType.EAGER, targetClass = SpaceServiceVersion.class ) |
|
25
|
|
|
@JoinTable(name = "service_versions", joinColumns = { |
|
26
|
|
|
@JoinColumn(name = "environment_name", referencedColumnName = "name"), |
|
27
|
|
|
}) |
|
28
|
|
|
private Set<SpaceServiceVersion> serviceVersions = newHashSet(); |
|
29
|
|
|
|
|
30
|
|
|
protected EnvironmentAggregate() { |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected EnvironmentAggregate(String name, Set< ServiceVersion> deatulSpaceServiceVersions) { |
|
34
|
|
|
this.name = name; |
|
35
|
|
|
this.serviceVersions.addAll( |
|
36
|
|
|
deatulSpaceServiceVersions |
|
37
|
|
|
.stream() |
|
38
|
|
|
.map(sv -> new SpaceServiceVersion(DEFAULT_NAMESPACE, sv.getName(), sv.getVersion())) |
|
39
|
|
|
.collect(toList()) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private EnvironmentAggregate(String name, Multimap<String, ServiceVersion> serviceVersions) { |
|
44
|
|
|
this.name = name; |
|
45
|
|
|
this.serviceVersions.addAll( |
|
46
|
|
|
serviceVersions.entries() |
|
47
|
|
|
.stream() |
|
48
|
|
|
.map(e -> new SpaceServiceVersion(e.getKey(), e.getValue().getName(), e.getValue().getVersion())) |
|
49
|
|
|
.collect(toList()) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public String getName() { |
|
54
|
|
|
return this.name; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public Set<String> getSpaceNames() { |
|
58
|
|
|
return serviceVersions.stream().map(SpaceServiceVersion::getSpace).collect(toSet()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public Set<ServiceVersion> getServices(String space) { |
|
62
|
|
|
return serviceVersions |
|
63
|
|
|
.stream() |
|
64
|
|
|
.filter(ssv -> ssv.getSpace().equals(space)) |
|
65
|
|
|
.map(ssv -> new ServiceVersion(ssv.getName(), ssv.getVersion())) |
|
66
|
|
|
.collect(toSet()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public Set<ServiceVersion> getAllServices() { |
|
70
|
|
|
return serviceVersions.stream() |
|
71
|
|
|
.map(ssv -> new ServiceVersion(ssv.getName(), ssv.getVersion())) |
|
72
|
|
|
.collect(toSet()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static EnvironmentAggregate empty(String environmentName) { |
|
76
|
|
|
return new EnvironmentAggregate(environmentName, create()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static EnvironmentAggregateBuilder builder(String name) { |
|
80
|
|
|
return new EnvironmentAggregateBuilder(name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public static class EnvironmentAggregateBuilder { |
|
84
|
|
|
|
|
85
|
|
|
private String name; |
|
86
|
|
|
private Multimap<String, ServiceVersion> serviceVersions = create(); |
|
87
|
|
|
|
|
88
|
|
|
private EnvironmentAggregateBuilder(String name) { |
|
89
|
|
|
this.name = name; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public EnvironmentAggregateBuilder withServiceVersion(String name, String version) { |
|
93
|
|
|
this.serviceVersions.put(DEFAULT_NAMESPACE, new ServiceVersion(name, version)); |
|
94
|
|
|
return this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public EnvironmentAggregateBuilder withServiceVersions(String space, Set<ServiceVersion> serviceVersions) { |
|
98
|
|
|
this.serviceVersions.putAll(space, serviceVersions); |
|
99
|
|
|
return this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public EnvironmentAggregate build() { |
|
103
|
|
|
return new EnvironmentAggregate( |
|
104
|
|
|
this.name, |
|
105
|
|
|
this.serviceVersions |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|