1
|
|
|
package com.hltech.judged.server.infrastructure.persistence.contracts; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceVersion; |
4
|
|
|
import com.hltech.judged.server.domain.contracts.Capability; |
5
|
|
|
import com.hltech.judged.server.domain.contracts.Contract; |
6
|
|
|
import com.hltech.judged.server.domain.contracts.Expectation; |
7
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
8
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
9
|
|
|
import lombok.RequiredArgsConstructor; |
10
|
|
|
import org.springframework.stereotype.Component; |
11
|
|
|
|
12
|
|
|
import javax.persistence.NoResultException; |
13
|
|
|
import java.util.List; |
14
|
|
|
import java.util.Map; |
15
|
|
|
import java.util.Optional; |
16
|
|
|
import java.util.stream.Collectors; |
17
|
|
|
|
18
|
|
|
@Component |
19
|
|
|
@RequiredArgsConstructor |
20
|
|
|
public class ServiceContractsRepositoryImpl implements ServiceContractsRepository { |
21
|
|
|
|
22
|
|
|
private final ServiceContractsTupleRepository serviceContractsTupleRepository; |
23
|
|
|
|
24
|
|
|
@Override |
25
|
|
|
public ServiceContracts persist(ServiceContracts serviceContracts) { |
26
|
|
|
return toServiceContracts(serviceContractsTupleRepository.save(toServiceContractsTuple(serviceContracts))); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
@Override |
30
|
|
|
public Optional<ServiceContracts> findOne(ServiceVersion serviceVersion) { |
31
|
|
|
return serviceContractsTupleRepository |
32
|
|
|
.findById_NameAndId_Version(serviceVersion.getName(), serviceVersion.getVersion()) |
33
|
|
|
.map(this::toServiceContracts); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
@Override |
37
|
|
|
public String getService(String name) { |
38
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
39
|
|
|
.map(ServiceContractsTuple::getName) |
40
|
|
|
.findFirst() |
41
|
|
|
.orElseThrow(() -> new NoResultException("No service for specified name")); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
@Override |
45
|
|
|
public List<ServiceContracts> findAllByServiceName(String name) { |
46
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
47
|
|
|
.map(this::toServiceContracts) |
48
|
|
|
.collect(Collectors.toList()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
@Override |
52
|
|
|
public List<String> getServiceNames() { |
53
|
|
|
return serviceContractsTupleRepository.findId_NameDistinct(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private ServiceContractsTuple toServiceContractsTuple(ServiceContracts serviceContracts) { |
57
|
|
|
return new ServiceContractsTuple( |
58
|
|
|
serviceContracts.getId(), |
59
|
|
|
toCapabilities(serviceContracts), |
60
|
|
|
toExpectations(serviceContracts) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private Map<ServiceContractsTuple.ProviderProtocolTuple, ServiceContractsTuple.ContractTuple> |
65
|
|
|
toExpectations(ServiceContracts serviceContracts) { |
66
|
|
|
return serviceContracts.getExpectations().stream() |
67
|
|
|
.collect(Collectors.toMap( |
68
|
|
|
expectation -> new ServiceContractsTuple.ProviderProtocolTuple( |
69
|
|
|
expectation.getProvider(), |
70
|
|
|
expectation.getProtocol() |
71
|
|
|
), |
72
|
|
|
expectation -> new ServiceContractsTuple.ContractTuple( |
73
|
|
|
expectation.getContract().getValue(), |
74
|
|
|
expectation.getContract().getMimeType() |
75
|
|
|
) |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private Map<String, ServiceContractsTuple.ContractTuple> toCapabilities(ServiceContracts serviceContracts) { |
80
|
|
|
return serviceContracts.getCapabilities().stream() |
81
|
|
|
.collect(Collectors.toMap( |
82
|
|
|
Capability::getProtocol, |
83
|
|
|
capability -> new ServiceContractsTuple.ContractTuple( |
84
|
|
|
capability.getContract().getValue(), |
85
|
|
|
capability.getContract().getMimeType() |
86
|
|
|
) |
87
|
|
|
)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private ServiceContracts toServiceContracts(ServiceContractsTuple serviceContractsTuple) { |
91
|
|
|
return new ServiceContracts( |
92
|
|
|
serviceContractsTuple.getId(), |
93
|
|
|
toCapabilities(serviceContractsTuple), |
94
|
|
|
toExpectations(serviceContractsTuple) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private List<Expectation> toExpectations(ServiceContractsTuple serviceContractsTuple) { |
99
|
|
|
return serviceContractsTuple.getExpectations().keySet().stream() |
100
|
|
|
.map(providerProtocol -> new Expectation( |
101
|
|
|
providerProtocol.getProvider(), |
102
|
|
|
providerProtocol.getProtocol(), |
103
|
|
|
new Contract( |
104
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getValue(), |
105
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getMimeType()))) |
106
|
|
|
.collect(Collectors.toList()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private List<Capability> toCapabilities(ServiceContractsTuple serviceContractsTuple) { |
110
|
|
|
return serviceContractsTuple.getCapabilitiesPerProtocol().keySet().stream() |
111
|
|
|
.map(protocol -> new Capability( |
112
|
|
|
protocol, |
113
|
|
|
new Contract( |
114
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getValue(), |
115
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getMimeType()))) |
116
|
|
|
.collect(Collectors.toList()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|