Projekt

Obecné

Profil

Stáhnout (1.99 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.service;
2

    
3
import lombok.extern.slf4j.Slf4j;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.context.event.ContextRefreshedEvent;
6
import org.springframework.context.event.EventListener;
7
import org.springframework.core.annotation.Order;
8
import org.springframework.dao.InvalidDataAccessResourceUsageException;
9
import org.springframework.stereotype.Service;
10
import vldc.aswi.dao.RoleRepository;
11
import vldc.aswi.domain.Role;
12

    
13
import javax.transaction.Transactional;
14
import java.util.LinkedList;
15
import java.util.List;
16

    
17
/**
18
 * Manager for Role.
19
 */
20
@Service
21
@Slf4j
22
public class RoleManagerImpl implements RoleManager {
23

    
24
    /** Autowired role repository. */
25
    @Autowired
26
    private RoleRepository roleRepository;
27

    
28
    /**
29
     * Initialization setup for role manager.
30
     * Check if table "Role" exists.
31
     */
32
    @EventListener(classes = {
33
            ContextRefreshedEvent.class
34
    })
35
    @Order(1)
36
    @Transactional
37
    public void setup() {
38
        try {
39
            if (this.roleRepository.count() == 0) {
40
                // Just checking if table exists.
41
            }
42
        }
43
        catch (InvalidDataAccessResourceUsageException e) {
44
            log.error("Table \"Role\" did not exists in database!");
45
            System.exit(1);
46
        }
47
    }
48

    
49
    /**
50
     * Add newly created role into database.
51
     * @param name - Name of role.
52
     */
53
    public void addRole(String name) {
54
        Role role = new Role(name);
55
        this.roleRepository.save(role);
56
    }
57

    
58
    /**
59
     * Get all roles from database.
60
     * @return List of roles.
61
     */
62
    @Override
63
    public List<Role> getRoles() {
64
        List<Role> retVal = new LinkedList<>();
65
        this.roleRepository.findAll().forEach(retVal::add);
66
        return retVal;
67
    }
68

    
69
    /**
70
     * Get role from database by name.
71
     * @param name name of the role
72
     * @return List of roles.
73
     */
74
    @Override
75
    public Role getRole(String name) {
76
        return roleRepository.getByName(name);
77
    }
78
}
(14-14/18)