Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.security

File DefaultRoleService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
14% of files have more coverage

Code metrics

2
8
2
1
47
40
3
0.38
4
2
1.5

Classes

Class Line # Actions
DefaultRoleService 15 8 3
0.7575%
 

Contributing tests

This file is covered by 19 tests. .

Source view

1    package io.sunshower.service.security;
2   
3    import io.sunshower.core.security.RoleService;
4    import io.sunshower.model.core.auth.Role;
5    import java.util.Collection;
6    import java.util.List;
7    import java.util.stream.Collectors;
8    import javax.persistence.EntityManager;
9    import javax.persistence.PersistenceContext;
10    import org.springframework.stereotype.Service;
11    import org.springframework.transaction.annotation.Transactional;
12   
13    @Service
14    @Transactional
 
15    public class DefaultRoleService implements RoleService {
16   
17    @PersistenceContext private EntityManager entityManager;
18   
 
19  32 toggle @Override
20    public Role findOrCreate(Role role) {
21   
22  32 List<Role> authorities =
23    entityManager
24    .createQuery("select r from Role r " + "where r.authority = :authority", Role.class)
25    .setParameter("authority", role.getAuthority())
26    .getResultList();
27  32 if (authorities.size() == 1) {
28  9 return authorities.get(0);
29    } else {
30  23 entityManager.persist(role);
31  23 entityManager.flush();
32  23 return role;
33    }
34    }
35   
 
36  0 toggle @Override
37    public List<Role> findOrCreate(Collection<Role> roles) {
38  0 List<Role> authorities =
39    entityManager
40    .createQuery("select r from Role r " + "where r.authority in :authorities", Role.class)
41    .setParameter(
42    "authorities", roles.stream().map(Role::getAuthority).collect(Collectors.toSet()))
43    .getResultList();
44   
45  0 return authorities;
46    }
47    }