Subversion Repositories SmartDukaan

Rev

Rev 31497 | Go to most recent revision | Details | Compare with Previous | 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
 
31483 tejbeer 21
import com.mongodb.DBObject;
23784 ashik.ali 22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
31490 tejbeer 23
import com.spice.profitmandi.dao.entity.catalog.BrandCatalog;
31483 tejbeer 24
import com.spice.profitmandi.dao.entity.catalog.BrandCategory;
23784 ashik.ali 25
import com.spice.profitmandi.dao.entity.dtr.Api;
26
import com.spice.profitmandi.dao.entity.dtr.Role;
27
import com.spice.profitmandi.dao.entity.dtr.User;
28
import com.spice.profitmandi.dao.enumuration.dtr.Method;
31483 tejbeer 29
import com.spice.profitmandi.dao.repository.catalog.BrandCategoryRepository;
30
 
31
import com.spice.profitmandi.dao.repository.catalog.BrandsRepository;
23784 ashik.ali 32
import com.spice.profitmandi.dao.repository.dtr.ApiRepository;
31483 tejbeer 33
import com.spice.profitmandi.dao.repository.dtr.Mongo;
23784 ashik.ali 34
import com.spice.profitmandi.dao.repository.dtr.RoleApiRepository;
35
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
36
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
37
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
38
import com.spice.profitmandi.service.authentication.RoleService;
39
 
40
@Controller
31483 tejbeer 41
@Transactional(rollbackFor = Throwable.class)
23784 ashik.ali 42
public class RoleController {
43
 
44
	private static final Logger LOGGER = LogManager.getLogger(RoleController.class);
31483 tejbeer 45
 
23784 ashik.ali 46
	@Autowired
47
	@Qualifier("userRepository")
48
	private UserRepository userRepository;
31483 tejbeer 49
 
23784 ashik.ali 50
	@Autowired
51
	private UserRoleRepository userRoleRepository;
31483 tejbeer 52
 
23784 ashik.ali 53
	@Autowired
54
	private RoleRepository roleRepository;
31483 tejbeer 55
 
23784 ashik.ali 56
	@Autowired
57
	private RoleApiRepository roleApiRepository;
31483 tejbeer 58
 
23784 ashik.ali 59
	@Autowired
60
	private ApiRepository apiRepository;
31483 tejbeer 61
 
23784 ashik.ali 62
	@Autowired
63
	private RoleService roleService;
31483 tejbeer 64
 
23784 ashik.ali 65
	@RequestMapping(value = "/createRole", method = RequestMethod.GET)
31483 tejbeer 66
	public String createRole(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 67
		return "create-role";
68
	}
31483 tejbeer 69
 
23784 ashik.ali 70
	@RequestMapping(value = "/createRole", method = RequestMethod.POST)
31483 tejbeer 71
	public String createRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name,
72
			Model model) throws Exception {
23784 ashik.ali 73
		roleService.createRole(name);
74
		return "create-role";
75
	}
31483 tejbeer 76
 
23784 ashik.ali 77
	@RequestMapping(value = "/createDuplicateRole", method = RequestMethod.GET)
31483 tejbeer 78
	public String createDuplicateRole(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 79
		List<Role> roles = roleRepository.selectAll();
80
		model.addAttribute("roles", roles);
81
		return "create-duplicate-role";
82
	}
31483 tejbeer 83
 
23784 ashik.ali 84
	@RequestMapping(value = "/createDuplicateRole", method = RequestMethod.POST)
31483 tejbeer 85
	public String createDuplicateRole(HttpServletRequest request,
86
			@RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
87
			@RequestParam(name = ProfitMandiConstants.NAME) String name, Model model) throws Exception {
23784 ashik.ali 88
		roleService.createDuplicateRole(roleId, name);
89
		List<Role> roles = roleRepository.selectAll();
90
		model.addAttribute("roles", roles);
91
		return "create-duplicate-role";
92
	}
31483 tejbeer 93
 
23784 ashik.ali 94
	@RequestMapping(value = "/deleteRole", method = RequestMethod.GET)
31483 tejbeer 95
	public String deleteRole(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 96
		List<Role> roles = roleRepository.selectAll();
97
		model.addAttribute("roles", roles);
98
		return "delete-role";
99
	}
31483 tejbeer 100
 
23784 ashik.ali 101
	@RequestMapping(value = "/deleteRole", method = RequestMethod.DELETE)
31483 tejbeer 102
	public String deleteRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
103
			Model model) throws Exception {
23784 ashik.ali 104
		roleService.deleteRole(roleId);
105
		List<Role> roles = roleRepository.selectAll();
106
		model.addAttribute("roles", roles);
107
		return "delete-role";
108
	}
31483 tejbeer 109
 
23784 ashik.ali 110
	@RequestMapping(value = "/renameRole", method = RequestMethod.GET)
31483 tejbeer 111
	public String renameRole(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 112
		List<Role> roles = roleRepository.selectAll();
113
		model.addAttribute("roles", roles);
114
		return "rename-role";
115
	}
31483 tejbeer 116
 
23784 ashik.ali 117
	@RequestMapping(value = "/renameRole", method = RequestMethod.PUT)
31483 tejbeer 118
	public String renameRole(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
119
			@RequestParam(name = ProfitMandiConstants.NAME) String name, Model model) throws Exception {
23784 ashik.ali 120
		roleService.renameRole(roleId, name);
121
		List<Role> roles = roleRepository.selectAll();
122
		model.addAttribute("roles", roles);
123
		return "rename-role";
124
	}
31483 tejbeer 125
 
23784 ashik.ali 126
	@RequestMapping(value = "/createApi", method = RequestMethod.GET)
31483 tejbeer 127
	public String createApi(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 128
		model.addAttribute("methods", Method.values());
129
		return "create-api";
130
	}
31483 tejbeer 131
 
23784 ashik.ali 132
	@RequestMapping(value = "/createApi", method = RequestMethod.POST)
31483 tejbeer 133
	public String createApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.NAME) String name,
134
			@RequestParam(name = ProfitMandiConstants.URI) String uri,
135
			@RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model) throws Exception {
23784 ashik.ali 136
		roleService.createApi(name, uri, method);
137
		return "create-api";
138
	}
31483 tejbeer 139
 
23784 ashik.ali 140
	@RequestMapping(value = "/editApi", method = RequestMethod.GET)
31483 tejbeer 141
	public String editApi(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 142
		List<Api> apis = apiRepository.selectAll();
143
		model.addAttribute("apis", apis);
144
		model.addAttribute("methods", Method.values());
145
		return "edit-api";
146
	}
31483 tejbeer 147
 
23784 ashik.ali 148
	@RequestMapping(value = "/editApi", method = RequestMethod.PUT)
31483 tejbeer 149
	public String editApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId,
150
			@RequestParam(name = ProfitMandiConstants.NAME) String name,
151
			@RequestParam(name = ProfitMandiConstants.URI) String uri,
152
			@RequestParam(name = ProfitMandiConstants.METHOD) Method method, Model model) throws Exception {
23784 ashik.ali 153
		roleService.editApi(apiId, name, uri, method);
154
		List<Api> apis = apiRepository.selectAll();
155
		model.addAttribute("apis", apis);
156
		model.addAttribute("methods", Method.values());
157
		return "edit-api";
158
	}
31483 tejbeer 159
 
23784 ashik.ali 160
	@RequestMapping(value = "/deleteApi", method = RequestMethod.GET)
31483 tejbeer 161
	public String deleteApi(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 162
		List<Api> apis = apiRepository.selectAll();
163
		model.addAttribute("apis", apis);
164
		model.addAttribute("methods", Method.values());
165
		return "delete-api";
166
	}
31483 tejbeer 167
 
23784 ashik.ali 168
	@RequestMapping(value = "/deleteApi", method = RequestMethod.DELETE)
31483 tejbeer 169
	public String deleteApi(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.API_ID) int apiId,
170
			Model model) throws Exception {
23784 ashik.ali 171
		roleService.deleteApi(apiId);
172
		List<Api> apis = apiRepository.selectAll();
173
		model.addAttribute("apis", apis);
174
		model.addAttribute("methods", Method.values());
175
		return "delete-api";
176
	}
31483 tejbeer 177
 
23784 ashik.ali 178
	@RequestMapping(value = "/addRemoveRole", method = RequestMethod.GET)
31483 tejbeer 179
	public String addRemoveRole(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 180
		return "add-remove-role";
181
	}
31483 tejbeer 182
 
23784 ashik.ali 183
	@RequestMapping(value = "/userRoles", method = RequestMethod.GET)
31483 tejbeer 184
	public String userRoles(HttpServletRequest request,
185
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
186
			Model model) throws Exception {
23784 ashik.ali 187
		User user = userRepository.selectByEmailIdOrMobileNumber(emailIdOrMobileNumber);
188
		List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
189
		List<Role> roles = roleRepository.selectAll();
190
		model.addAttribute("userRoleIds", roleIds);
191
		model.addAttribute("roles", roles);
192
		return "user-roles";
193
	}
31483 tejbeer 194
 
23784 ashik.ali 195
	@RequestMapping(value = "/addRemoveRoles", method = RequestMethod.POST)
31483 tejbeer 196
	public String addRemoveRoles(HttpServletRequest request,
197
			@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER) String emailIdOrMobileNumber,
198
			@RequestBody List<Integer> roleIds, Model model) throws Exception {
23784 ashik.ali 199
		roleService.addRemoveRoles(emailIdOrMobileNumber, new HashSet<>(roleIds));
200
		return "add-remove-role";
201
	}
202
 
203
	@RequestMapping(value = "/addRemoveApi", method = RequestMethod.GET)
31483 tejbeer 204
	public String addRemoveApi(HttpServletRequest request, Model model) throws Exception {
23784 ashik.ali 205
		List<Role> roles = roleRepository.selectAll();
206
		model.addAttribute("roles", roles);
207
		return "add-remove-api";
208
	}
31483 tejbeer 209
 
23784 ashik.ali 210
	@RequestMapping(value = "/roleApis", method = RequestMethod.GET)
31483 tejbeer 211
	public String roleApis(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId,
212
			Model model) throws Exception {
23784 ashik.ali 213
		Map<String, Object> map = roleService.getApisByRoleId(roleId);
214
		model.addAllAttributes(map);
215
		return "role-apis";
216
	}
31483 tejbeer 217
 
23784 ashik.ali 218
	@RequestMapping(value = "/addRemoveApis", method = RequestMethod.POST)
31483 tejbeer 219
	public String addRemoveApi(HttpServletRequest request,
220
			@RequestParam(name = ProfitMandiConstants.ROLE_ID) int roleId, @RequestBody List<Integer> apiIds,
221
			Model model) throws Exception {
23784 ashik.ali 222
		roleService.addRemoveApis(roleId, new HashSet<>(apiIds));
223
		List<Role> roles = roleRepository.selectAll();
224
		model.addAttribute("roles", roles);
225
		return "add-remove-api";
226
	}
31483 tejbeer 227
 
228
	@Autowired
229
	private Mongo mongoClient;
230
 
231
	@Autowired
232
	private BrandsRepository brandsRepository;
233
 
234
	@Autowired
235
	private BrandCategoryRepository brandCategoryRepository;
236
 
237
	@RequestMapping(value = "/addBrands", method = RequestMethod.POST)
238
	public String addBrands(HttpServletRequest request, Model model) throws Exception {
239
 
31495 tejbeer 240
		List<DBObject> mobileBrands = mongoClient.getAllBrandsToDisplay(6);
31483 tejbeer 241
		for (DBObject mobileBrand : mobileBrands) {
242
			String brandName = (String) mobileBrand.get("name");
31497 tejbeer 243
			BrandCatalog brand = brandsRepository.selectByBrand(brandName);
31483 tejbeer 244
 
31497 tejbeer 245
			if (brand == null) {
246
				brand = new BrandCatalog();
247
				brand.setName(brandName);
248
				brand.setLogoUrl((String) mobileBrand.get("url"));
249
				brandsRepository.persist(brand);
250
			}
31493 tejbeer 251
 
252
			LOGGER.info("brand {}", brand);
253
 
31488 tejbeer 254
			Double category = (Double) mobileBrand.get("categoryId");
31489 tejbeer 255
 
256
			Double rank = (Double) mobileBrand.get("rank");
31493 tejbeer 257
 
31483 tejbeer 258
			BrandCategory brandCategory = new BrandCategory();
31498 tejbeer 259
			brandCategory.setActive((boolean) mobileBrand.get("active"));
31493 tejbeer 260
			brandCategory.setCategoryId(category.intValue());
31483 tejbeer 261
			brandCategory.setBrandId(brand.getId());
31489 tejbeer 262
			brandCategory.setRank(rank.intValue());
31483 tejbeer 263
			brandCategoryRepository.persist(brandCategory);
264
 
31493 tejbeer 265
			LOGGER.info("brandCategory {}", brandCategory);
266
 
31483 tejbeer 267
		}
268
 
269
		return "add-remove-api";
270
	}
271
 
23784 ashik.ali 272
}