Subversion Repositories SmartDukaan

Rev

Rev 31483 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
23784 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.util.HashSet;
4
import java.util.List;
5
import java.util.Map;
6
 
7
import javax.servlet.http.HttpServletRequest;
8
 
9
import org.apache.logging.log4j.LogManager;
10
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Qualifier;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.transaction.annotation.Transactional;
15
import org.springframework.ui.Model;
16
import org.springframework.web.bind.annotation.RequestBody;
17
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMethod;
19
import org.springframework.web.bind.annotation.RequestParam;
20
 
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.dao.entity.dtr.Api;
23
import com.spice.profitmandi.dao.entity.dtr.Role;
24
import com.spice.profitmandi.dao.entity.dtr.User;
25
import com.spice.profitmandi.dao.enumuration.dtr.Method;
26
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
27
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
28
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
29
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
31
import com.spice.profitmandi.service.authentication.RoleService;
32
 
33
@Controller
34
@Transactional(rollbackFor=Throwable.class)
35
public class RoleController {
36
 
37
	private static final Logger LOGGER = LogManager.getLogger(RoleController.class);
38
 
39
	@Autowired
40
	@Qualifier("userRepository")
41
	private UserRepository userRepository;
42
 
43
	@Autowired
44
	private UserRoleRepository userRoleRepository;
45
 
46
	@Autowired
47
	private RoleRepository roleRepository;
48
 
49
	@Autowired
50
	private RoleApiRepository roleApiRepository;
51
 
52
	@Autowired
53
	private ApiRepository apiRepository;
54
 
55
	@Autowired
56
	private RoleService roleService;
57
 
58
	@RequestMapping(value = "/createRole", method = RequestMethod.GET)
59
	public String createRole(HttpServletRequest request, Model model)  throws Exception{
60
		return "create-role";
61
	}
62
 
63
	@RequestMapping(value = "/createRole", method = RequestMethod.POST)
64
	public String createRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name, Model model)  throws Exception{
65
		roleService.createRole(name);
66
		return "create-role";
67
	}
68
 
69
	@RequestMapping(value = "/createDuplicateRole", method = RequestMethod.GET)
70
	public String createDuplicateRole(HttpServletRequest request, Model model)  throws Exception{
71
		List<Role> roles = roleRepository.selectAll();
72
		model.addAttribute("roles", roles);
73
		return "create-duplicate-role";
74
	}
75
 
76
	@RequestMapping(value = "/createDuplicateRole", method = RequestMethod.POST)
77
	public String createDuplicateRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, @RequestParam(name = ProfitMandiConstants.NAME) String name, Model model)  throws Exception{
78
		roleService.createDuplicateRole(roleId, name);
79
		List<Role> roles = roleRepository.selectAll();
80
		model.addAttribute("roles", roles);
81
		return "create-duplicate-role";
82
	}
83
 
84
	@RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
85
	public String deleteRole(HttpServletRequest request, Model model)  throws Exception{
86
		List<Role> roles = roleRepository.selectAll();
87
		model.addAttribute("roles", roles);
88
		return "delete-role";
89
	}
90
 
91
	@RequestMapping(value = "/deleteRole", method = RequestMethod.DELETE)
92
	public String deleteRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, Model model)  throws Exception{
93
		roleService.deleteRole(roleId);
94
		List<Role> roles = roleRepository.selectAll();
95
		model.addAttribute("roles", roles);
96
		return "delete-role";
97
	}
98
 
99
	@RequestMapping(value = "/renameRole", method = RequestMethod.GET)
100
	public String renameRole(HttpServletRequest request, Model model)  throws Exception{
101
		List<Role> roles = roleRepository.selectAll();
102
		model.addAttribute("roles", roles);
103
		return "rename-role";
104
	}
105
 
106
	@RequestMapping(value = "/renameRole", method = RequestMethod.PUT)
107
	public String renameRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, @RequestParam(name = ProfitMandiConstants.NAME) String name, Model model)  throws Exception{
108
		roleService.renameRole(roleId, name);
109
		List<Role> roles = roleRepository.selectAll();
110
		model.addAttribute("roles", roles);
111
		return "rename-role";
112
	}
113
 
114
	@RequestMapping(value = "/createApi", method = RequestMethod.GET)
115
	public String createApi(HttpServletRequest request, Model model)  throws Exception{
116
		model.addAttribute("methods", Method.values());
117
		return "create-api";
118
	}
119
 
120
	@RequestMapping(value = "/createApi", method = RequestMethod.POST)
121
	public String createApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name, @RequestParam(name = ProfitMandiConstants.URI) String uri, @RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model)  throws Exception{
122
		roleService.createApi(name, uri, method);
123
		return "create-api";
124
	}
125
 
126
	@RequestMapping(value = "/editApi", method = RequestMethod.GET)
127
	public String editApi(HttpServletRequest request, Model model)  throws Exception{
128
		List<Api> apis = apiRepository.selectAll();
129
		model.addAttribute("apis", apis);
130
		model.addAttribute("methods", Method.values());
131
		return "edit-api";
132
	}
133
 
134
	@RequestMapping(value = "/editApi", method = RequestMethod.PUT)
135
	public String editApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId, @RequestParam(name = ProfitMandiConstants.NAME) String name, @RequestParam(name = ProfitMandiConstants.URI) String uri, @RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model)  throws Exception{
136
		roleService.editApi(apiId, name, uri, method);
137
		List<Api> apis = apiRepository.selectAll();
138
		model.addAttribute("apis", apis);
139
		model.addAttribute("methods", Method.values());
140
		return "edit-api";
141
	}
142
 
143
	@RequestMapping(value = "/deleteApi", method = RequestMethod.GET)
144
	public String deleteApi(HttpServletRequest request, Model model)  throws Exception{
145
		List<Api> apis = apiRepository.selectAll();
146
		model.addAttribute("apis", apis);
147
		model.addAttribute("methods", Method.values());
148
		return "delete-api";
149
	}
150
 
151
	@RequestMapping(value = "/deleteApi", method = RequestMethod.DELETE)
152
	public String deleteApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId, Model model)  throws Exception{
153
		roleService.deleteApi(apiId);
154
		List<Api> apis = apiRepository.selectAll();
155
		model.addAttribute("apis", apis);
156
		model.addAttribute("methods", Method.values());
157
		return "delete-api";
158
	}
159
 
160
	@RequestMapping(value = "/addRemoveRole", method = RequestMethod.GET)
161
	public String addRemoveRole(HttpServletRequest request, Model model)  throws Exception{
162
		return "add-remove-role";
163
	}
164
 
165
	@RequestMapping(value = "/userRoles", method = RequestMethod.GET)
166
	public String userRoles(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber, Model model)  throws Exception{
167
		User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
168
		List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
169
		List<Role> roles = roleRepository.selectAll();
170
		model.addAttribute("userRoleIds", roleIds);
171
		model.addAttribute("roles", roles);
172
		return "user-roles";
173
	}
174
 
175
	@RequestMapping(value = "/addRemoveRoles", method = RequestMethod.POST)
176
	public String addRemoveRoles(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber, @RequestBody List<Integer> roleIds, Model model)  throws Exception{
177
		roleService.addRemoveRoles(emailIdOrMobileNumber, new HashSet<>(roleIds));
178
		return "add-remove-role";
179
	}
180
 
181
	@RequestMapping(value = "/addRemoveApi", method = RequestMethod.GET)
182
	public String addRemoveApi(HttpServletRequest request, Model model)  throws Exception{
183
		List<Role> roles = roleRepository.selectAll();
184
		model.addAttribute("roles", roles);
185
		return "add-remove-api";
186
	}
187
 
188
	@RequestMapping(value = "/roleApis", method = RequestMethod.GET)
189
	public String roleApis(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, Model model)  throws Exception{
190
		Map<String, Object> map = roleService.getApisByRoleId(roleId);
191
		model.addAllAttributes(map);
192
		return "role-apis";
193
	}
194
 
195
	@RequestMapping(value = "/addRemoveApis", method = RequestMethod.POST)
196
	public String addRemoveApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, @RequestBody List<Integer> apiIds, Model model)  throws Exception{
197
		roleService.addRemoveApis(roleId, new HashSet<>(apiIds));
198
		List<Role> roles = roleRepository.selectAll();
199
		model.addAttribute("roles", roles);
200
		return "add-remove-api";
201
	}
202
 
203
}