Passed
Push — master ( fa687d...bd5bb4 )
by Tomasz
04:10
created

dev.hltech.dredd.domain.contracts.JpaServiceContractsRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A find(String,String) 0 3 1
A getServiceNames() 0 5 1
A find(String) 0 6 1
A JpaServiceContractsRepository(EntityManager) 0 3 1
A persist(ServiceContracts) 0 3 1
1
package dev.hltech.dredd.domain.contracts;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.stereotype.Component;
5
6
import javax.persistence.EntityManager;
7
import java.util.List;
8
import java.util.Optional;
9
10
import static java.util.Optional.ofNullable;
11
12
@Component
13
public class JpaServiceContractsRepository implements ServiceContractsRepository {
14
15
    private final EntityManager entityManager;
16
17
    @Autowired
18
    public JpaServiceContractsRepository(EntityManager entityManager) {
19
        this.entityManager = entityManager;
20
    }
21
22
    @Override
23
    public ServiceContracts persist(ServiceContracts serviceContracts) {
24
        return entityManager.merge(serviceContracts);
25
    }
26
27
    @Override
28
    public Optional<ServiceContracts> find(String name, String version) {
29
        return ofNullable(entityManager.find(ServiceContracts.class, new ServiceContracts.ServiceContractsId(name, version)));
30
    }
31
32
    @Override
33
    public List<ServiceContracts> find(String name) {
34
        return entityManager
35
            .createQuery("select o from " + ServiceContracts.class.getName() + " o where o.id.name = :name", ServiceContracts.class)
36
            .setParameter("name", name)
37
            .getResultList();
38
    }
39
40
    @Override
41
    public List<String> getServiceNames() {
42
        return entityManager
43
            .createQuery("select o.id.name from " + ServiceContracts.class.getName() + " o", String.class)
44
            .getResultList();
45
    }
46
47
}
48