1
|
|
|
package dev.hltech.dredd.domain.contracts; |
2
|
|
|
|
3
|
|
|
import dev.hltech.dredd.domain.ServiceVersion; |
4
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
5
|
|
|
import org.springframework.stereotype.Component; |
6
|
|
|
|
7
|
|
|
import javax.persistence.EntityManager; |
8
|
|
|
import java.util.List; |
9
|
|
|
import java.util.Optional; |
10
|
|
|
|
11
|
|
|
import static java.util.Optional.ofNullable; |
12
|
|
|
|
13
|
|
|
@Component |
14
|
|
|
public class JpaServiceContractsRepository implements ServiceContractsRepository { |
15
|
|
|
|
16
|
|
|
private final EntityManager entityManager; |
17
|
|
|
|
18
|
|
|
@Autowired |
19
|
|
|
public JpaServiceContractsRepository(EntityManager entityManager) { |
20
|
|
|
this.entityManager = entityManager; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
@Override |
24
|
|
|
public ServiceContracts persist(ServiceContracts serviceContracts) { |
25
|
|
|
return entityManager.merge(serviceContracts); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
@Override |
29
|
|
|
public Optional<ServiceContracts> findOne(ServiceVersion serviceVersion) { |
30
|
|
|
return ofNullable(entityManager.find( |
31
|
|
|
ServiceContracts.class, |
32
|
|
|
new ServiceVersion(serviceVersion.getName(), serviceVersion.getVersion()) |
33
|
|
|
)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
@Override |
37
|
|
|
public String getService(String name) { |
38
|
|
|
return entityManager |
39
|
|
|
.createQuery("select distinct o.id.name from " + ServiceContracts.class.getName() + " o where o.id.name = :name", String.class) |
40
|
|
|
.setParameter("name", name) |
41
|
|
|
.getSingleResult(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
@Override |
45
|
|
|
public List<ServiceContracts> findAllByServiceName(String name) { |
46
|
|
|
return entityManager |
47
|
|
|
.createQuery("select o from " + ServiceContracts.class.getName() + " o where o.id.name = :name", ServiceContracts.class) |
48
|
|
|
.setParameter("name", name) |
49
|
|
|
.getResultList(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
@Override |
53
|
|
|
public List<String> getServiceNames() { |
54
|
|
|
return entityManager |
55
|
|
|
.createQuery("select distinct o.id.name from " + ServiceContracts.class.getName() + " o", String.class) |
56
|
|
|
.getResultList(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
} |
60
|
|
|
|