1
|
|
|
package com.hltech.judged.server.infrastructure.persistence.contracts; |
2
|
|
|
|
3
|
|
|
import com.hltech.judged.server.domain.ServiceId; |
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(ServiceId serviceId) { |
31
|
|
|
return serviceContractsTupleRepository |
32
|
|
|
.findById_NameAndId_Version(serviceId.getName(), serviceId.getVersion()) |
33
|
|
|
.map(this::toServiceContracts); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
@Override |
37
|
|
|
public Optional<Contract> findCapabilityByServiceIdProtocol(ServiceId serviceId, String protocol) { |
38
|
|
|
return serviceContractsTupleRepository |
39
|
|
|
.findById_NameAndId_Version(serviceId.getName(), serviceId.getVersion()) |
40
|
|
|
.map(sc -> sc.getCapabilitiesPerProtocol().get(protocol)) |
41
|
|
|
.map(tuple -> new Contract(tuple.getValue(), tuple.getMimeType())); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
@Override |
45
|
|
|
public String getService(String name) { |
46
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
47
|
|
|
.map(ServiceContractsTuple::getName) |
48
|
|
|
.findFirst() |
49
|
|
|
.orElseThrow(() -> new NoResultException("No service for specified name")); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public List<ServiceContracts> findAllByServiceName(String name) { |
54
|
|
|
return serviceContractsTupleRepository.findById_Name(name).stream() |
55
|
|
|
.map(this::toServiceContracts) |
56
|
|
|
.collect(Collectors.toList()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
@Override |
60
|
|
|
public List<String> getServiceNames() { |
61
|
|
|
return serviceContractsTupleRepository.findId_NameDistinct(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private ServiceContractsTuple toServiceContractsTuple(ServiceContracts serviceContracts) { |
65
|
|
|
return new ServiceContractsTuple( |
66
|
|
|
new ServiceVersion(serviceContracts.getId().getName(), serviceContracts.getId().getVersion()), |
67
|
|
|
toCapabilities(serviceContracts), |
68
|
|
|
toExpectations(serviceContracts), |
69
|
|
|
serviceContracts.getPublicationTime() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private Map<ServiceContractsTuple.ProviderProtocolTuple, ServiceContractsTuple.ContractTuple> |
74
|
|
|
toExpectations(ServiceContracts serviceContracts) { |
75
|
|
|
return serviceContracts.getExpectations().stream() |
76
|
|
|
.collect(Collectors.toMap( |
77
|
|
|
expectation -> new ServiceContractsTuple.ProviderProtocolTuple( |
78
|
|
|
expectation.getProvider(), |
79
|
|
|
expectation.getProtocol() |
80
|
|
|
), |
81
|
|
|
expectation -> new ServiceContractsTuple.ContractTuple( |
82
|
|
|
expectation.getContract().getValue(), |
83
|
|
|
expectation.getContract().getMimeType() |
84
|
|
|
) |
85
|
|
|
)); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private Map<String, ServiceContractsTuple.ContractTuple> toCapabilities(ServiceContracts serviceContracts) { |
89
|
|
|
return serviceContracts.getCapabilities().stream() |
90
|
|
|
.collect(Collectors.toMap( |
91
|
|
|
Capability::getProtocol, |
92
|
|
|
capability -> new ServiceContractsTuple.ContractTuple( |
93
|
|
|
capability.getContract().getValue(), |
94
|
|
|
capability.getContract().getMimeType() |
95
|
|
|
) |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private ServiceContracts toServiceContracts(ServiceContractsTuple serviceContractsTuple) { |
100
|
|
|
return new ServiceContracts( |
101
|
|
|
new ServiceId(serviceContractsTuple.getId().getName(), serviceContractsTuple.getId().getVersion()), |
102
|
|
|
toCapabilities(serviceContractsTuple), |
103
|
|
|
toExpectations(serviceContractsTuple), |
104
|
|
|
serviceContractsTuple.getPublicationTime() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private List<Expectation> toExpectations(ServiceContractsTuple serviceContractsTuple) { |
109
|
|
|
return serviceContractsTuple.getExpectations().keySet().stream() |
110
|
|
|
.map(providerProtocol -> new Expectation( |
111
|
|
|
providerProtocol.getProvider(), |
112
|
|
|
providerProtocol.getProtocol(), |
113
|
|
|
new Contract( |
114
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getValue(), |
115
|
|
|
serviceContractsTuple.getExpectations().get(providerProtocol).getMimeType()))) |
116
|
|
|
.collect(Collectors.toList()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private List<Capability> toCapabilities(ServiceContractsTuple serviceContractsTuple) { |
120
|
|
|
return serviceContractsTuple.getCapabilitiesPerProtocol().keySet().stream() |
121
|
|
|
.map(protocol -> new Capability( |
122
|
|
|
protocol, |
123
|
|
|
new Contract( |
124
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getValue(), |
125
|
|
|
serviceContractsTuple.getCapabilitiesPerProtocol().get(protocol).getMimeType()))) |
126
|
|
|
.collect(Collectors.toList()); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|