-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSpringJpaBootstrap.java
More file actions
128 lines (106 loc) · 4.2 KB
/
SpringJpaBootstrap.java
File metadata and controls
128 lines (106 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package guru.springframework.bootstrap;
import guru.springframework.domain.Product;
import guru.springframework.domain.Role;
import guru.springframework.domain.User;
import guru.springframework.repositories.ProductRepository;
import guru.springframework.services.RoleService;
import guru.springframework.services.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.List;
@Component
public class SpringJpaBootstrap implements ApplicationListener<ContextRefreshedEvent> {
private ProductRepository productRepository;
private UserService userService;
private RoleService roleService;
private Logger log = LoggerFactory.getLogger(SpringJpaBootstrap.class);
@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@Autowired
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
loadProducts();
loadUsers();
loadRoles();
assignUsersToUserRole();
assignUsersToAdminRole();
}
private void loadProducts() {
Product shirt = new Product();
shirt.setDescription("Spring Framework Guru Shirt");
shirt.setPrice(new BigDecimal("18.95"));
shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
shirt.setProductId("235268845711068308");
productRepository.save(shirt);
log.info("Saved Shirt - id: " + shirt.getId());
Product mug = new Product();
mug.setDescription("Spring Framework Guru Mug");
mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
mug.setProductId("168639393495335947");
mug.setPrice(new BigDecimal("11.95"));
productRepository.save(mug);
log.info("Saved Mug - id:" + mug.getId());
}
private void loadUsers() {
User user1 = new User();
user1.setUsername("user");
user1.setPassword("user");
userService.saveOrUpdate(user1);
User user2 = new User();
user2.setUsername("admin");
user2.setPassword("admin");
userService.saveOrUpdate(user2);
}
private void loadRoles() {
Role role = new Role();
role.setRole("USER");
roleService.saveOrUpdate(role);
log.info("Saved role" + role.getRole());
Role adminRole = new Role();
adminRole.setRole("ADMIN");
roleService.saveOrUpdate(adminRole);
log.info("Saved role" + adminRole.getRole());
}
private void assignUsersToUserRole() {
List<Role> roles = (List<Role>) roleService.listAll();
List<User> users = (List<User>) userService.listAll();
roles.forEach(role -> {
if (role.getRole().equalsIgnoreCase("USER")) {
users.forEach(user -> {
if (user.getUsername().equals("user")) {
user.addRole(role);
userService.saveOrUpdate(user);
}
});
}
});
}
private void assignUsersToAdminRole() {
List<Role> roles = (List<Role>) roleService.listAll();
List<User> users = (List<User>) userService.listAll();
roles.forEach(role -> {
if (role.getRole().equalsIgnoreCase("ADMIN")) {
users.forEach(user -> {
if (user.getUsername().equals("admin")) {
user.addRole(role);
userService.saveOrUpdate(user);
}
});
}
});
}
}