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