Subversion Repositories SmartDukaan

Rev

Rev 21428 | Rev 21440 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21248 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
21428 amit.gupta 4
import java.util.HashMap;
21277 ashik.ali 5
import java.util.Map;
21428 amit.gupta 6
import java.util.Set;
21248 ashik.ali 7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
21278 ashik.ali 12
import org.springframework.beans.factory.annotation.Autowired;
21414 kshitij.so 13
import org.springframework.beans.factory.annotation.Value;
21248 ashik.ali 14
import org.springframework.http.HttpStatus;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.stereotype.Controller;
21366 kshitij.so 17
import org.springframework.web.bind.annotation.RequestBody;
21248 ashik.ali 18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
20
import org.springframework.web.bind.annotation.RequestParam;
21
 
22
import com.spice.profitmandi.common.ResponseCodeHolder;
23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
24
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21282 ashik.ali 25
import com.spice.profitmandi.common.util.JWTUtil;
21426 ashik.ali 26
import com.spice.profitmandi.dao.entity.Permission;
21414 kshitij.so 27
import com.spice.profitmandi.dao.entity.Role;
21278 ashik.ali 28
import com.spice.profitmandi.dao.entity.User;
21426 ashik.ali 29
import com.spice.profitmandi.dao.entity.UserRole;
30
import com.spice.profitmandi.dao.repository.PermissionRepository;
31
import com.spice.profitmandi.dao.repository.RoleRepository;
21248 ashik.ali 32
import com.spice.profitmandi.dao.repository.UserRepository;
21426 ashik.ali 33
import com.spice.profitmandi.dao.repository.UserRoleRepository;
21248 ashik.ali 34
import com.spice.profitmandi.web.model.ProfitMandiResponse;
35
import com.spice.profitmandi.web.model.Response;
36
import com.spice.profitmandi.web.model.ResponseStatus;
21277 ashik.ali 37
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
21426 ashik.ali 38
import com.spice.profitmandi.web.req.UserAddRoleRequest;
21366 kshitij.so 39
import com.spice.profitmandi.web.req.UserRequest;
21248 ashik.ali 40
 
41
/**
42
 * @author ashikali
43
 *
44
 */
45
@Controller
46
public class UserController {
21426 ashik.ali 47
 
21248 ashik.ali 48
	private static final Logger LOGGER=LoggerFactory.getLogger(UserController.class);
49
 
21414 kshitij.so 50
	@Value("${admin.token}")
51
	private String validAdminToken;
21426 ashik.ali 52
 
21278 ashik.ali 53
	@Autowired
54
	UserRepository userRepository;
21426 ashik.ali 55
 
21278 ashik.ali 56
	@Autowired
21426 ashik.ali 57
	RoleRepository roleRepository;
58
 
59
	@Autowired
60
	UserRoleRepository userRoleRepository;
61
 
62
	@Autowired
63
	PermissionRepository permissionRepository;
64
 
65
	@Autowired
21278 ashik.ali 66
	GoogleLoginProcessor googleLoginProcessor;
21426 ashik.ali 67
 
21277 ashik.ali 68
	@SuppressWarnings("unchecked")
69
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN,method=RequestMethod.POST)
70
	public ResponseEntity<?> googleLogin(HttpServletRequest request){
71
		LOGGER.info("requested url : "+request.getRequestURL().toString());
72
		final Map<String, Object> googleLoginMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
73
		request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
74
		try {
75
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, googleLoginProcessor.process(googleLoginMap));
76
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
77
		}catch (ProfitMandiBusinessException pmbe) {
78
			LOGGER.error("ProfitMandi error: ", pmbe);
79
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
80
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
81
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
82
		}
83
	}
21426 ashik.ali 84
 
21282 ashik.ali 85
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method=RequestMethod.GET)
86
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token){
87
		LOGGER.info("requested url : "+request.getRequestURL().toString());
88
		try {
89
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, JWTUtil.isExpired(token));
90
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
91
		}catch (ProfitMandiBusinessException pmbe) {
92
			LOGGER.error("ProfitMandi error: ", pmbe);
93
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
94
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
95
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
96
		}
97
	}
21426 ashik.ali 98
 
99
 
21278 ashik.ali 100
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method=RequestMethod.POST)
21366 kshitij.so 101
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest){
21278 ashik.ali 102
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21368 kshitij.so 103
		User user = new User();
104
		user.setFirstName(userRequest.getFirstName());
105
		user.setLastName(userRequest.getLastName());
106
		user.setCity(userRequest.getCity());
107
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
108
		user.setEmailId(userRequest.getEmailId());
109
		user.setUsername("");
110
		user.setPassword("");
111
		user.setMobile_verified(false);
112
		user.setReferral_url("");
113
		user.setGroup_id(1);
114
		user.setStatus(0);
115
		user.setActivated(false);
21414 kshitij.so 116
 
21278 ashik.ali 117
		try {
118
			user.setCreateTimestamp(LocalDateTime.now());
119
			user.setUpdateTimestamp(LocalDateTime.now());
120
			userRepository.persist(user);
21282 ashik.ali 121
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("USR_OK_1000"));
21278 ashik.ali 122
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
123
		}catch (ProfitMandiBusinessException pmbe) {
124
			LOGGER.error("ProfitMandi error: ", pmbe);
125
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
126
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
127
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
128
		}
129
	}
21426 ashik.ali 130
 
21248 ashik.ali 131
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL,method=RequestMethod.GET)
132
	public ResponseEntity<?> getAll(HttpServletRequest request){
133
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21426 ashik.ali 134
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRepository.selectAll());
135
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 136
	}
21426 ashik.ali 137
 
21248 ashik.ali 138
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID,method=RequestMethod.GET)
21432 ashik.ali 139
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21248 ashik.ali 140
		LOGGER.info("requested url : "+request.getRequestURL().toString());
141
		try {
142
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRepository.selectById(id));
143
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
144
		}catch (ProfitMandiBusinessException pmbe) {
145
			LOGGER.error("ProfitMandi error: ", pmbe);
146
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
147
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
148
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
149
		}
150
	}
21426 ashik.ali 151
 
21248 ashik.ali 152
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER,method=RequestMethod.GET)
153
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request, @RequestParam(name = "mobileNumber") String mobileNumber){
154
		LOGGER.info("requested url : "+request.getRequestURL().toString());
155
		try {
156
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRepository.selectByMobileNumber(mobileNumber));
157
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
158
		}catch (ProfitMandiBusinessException pmbe) {
159
			LOGGER.error("ProfitMandi error: ", pmbe);
160
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
161
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
162
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
163
		}
164
	}
21426 ashik.ali 165
 
166
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
167
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request, @RequestParam(name = "mobileNumber") String mobileNumber){
168
		LOGGER.info("requested url : "+request.getRequestURL().toString());
169
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRepository.isExistByMobileNumber(mobileNumber));
170
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
171
	}
172
 
173
 
21248 ashik.ali 174
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID,method=RequestMethod.GET)
175
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId){
176
		LOGGER.info("requested url : "+request.getRequestURL().toString());
177
		try {
178
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRepository.selectByEmailId(emailId));
179
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
180
		}catch (ProfitMandiBusinessException pmbe) {
181
			LOGGER.error("ProfitMandi error: ", pmbe);
182
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
183
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
184
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
185
		}
186
	}
21426 ashik.ali 187
 
188
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method=RequestMethod.POST)
189
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest){
21248 ashik.ali 190
		LOGGER.info("requested url : "+request.getRequestURL().toString());
191
		try {
21426 ashik.ali 192
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
193
 
194
			Role role = null;
195
			try{
196
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(), userAddRoleRequest.getRole().getType());
197
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
198
				role = new Role();
199
				role.setName(userAddRoleRequest.getRole().getName());
200
				role.setType(userAddRoleRequest.getRole().getType());
201
				roleRepository.persist(role);
202
			}
203
			Permission permission = new Permission();
204
			permission.setType(userAddRoleRequest.getRole().getPermissionType());
205
			permission.setRoleId(role.getId());
206
			permissionRepository.persist(permission);
207
			UserRole userRole = new UserRole();
208
			userRole.setRoleId(role.getId());
209
			userRole.setUserId(user.getId());
210
			userRoleRepository.persist(userRole);
211
			//role.setPermissionType(userAddRoleRequest.getRole().getPermissionType());
212
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, "");
213
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 214
		}catch (ProfitMandiBusinessException pmbe) {
215
			LOGGER.error("ProfitMandi error: ", pmbe);
216
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
217
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
218
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
219
		}
220
	}
21426 ashik.ali 221
 
222
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE,method=RequestMethod.DELETE)
21432 ashik.ali 223
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId, @RequestParam(name = "userId") int userId){
21248 ashik.ali 224
		LOGGER.info("requested url : "+request.getRequestURL().toString());
225
		try {
21426 ashik.ali 226
			roleRepository.selectById(roleId);
227
			userRepository.selectById(userId);
228
			userRoleRepository.deleteByUserAndRoleId(userId, roleId);
229
			permissionRepository.deleteByRoleId(roleId);
230
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, "");
231
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 232
		}catch (ProfitMandiBusinessException pmbe) {
233
			LOGGER.error("ProfitMandi error: ", pmbe);
234
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
235
			final ProfitMandiResponse<Response> profitMandiResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
236
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.BAD_REQUEST);
237
		}
238
	}
21426 ashik.ali 239
 
240
 
241
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL,method=RequestMethod.GET)
21432 ashik.ali 242
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id){
21248 ashik.ali 243
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21426 ashik.ali 244
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, userRoleRepository.selectRolesByUserId(id));
245
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 246
	}
21414 kshitij.so 247
 
248
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
249
	public ResponseEntity<?> getAdminToken(HttpServletRequest request, @RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId){
250
		LOGGER.info("requested url : "+request.getRequestURL().toString());
251
		if (!adminToken.equals(validAdminToken)){
252
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN, ResponseStatus.FAILURE, null);
253
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
254
		}
255
 
256
 
257
		Map<String, Object> responseMap = new HashMap<>(2);
258
		try{
259
			User user = userRepository.selectByEmailId(emailId);
260
			Set<Role> roles = user.getRoles();
261
			String[] roleTypes = new String[roles.size()];
262
			int index = 0;
263
			for(Role role : roles){
264
				roleTypes[index++] = role.getType().toString();
265
			}
266
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
267
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
268
		}catch (ProfitMandiBusinessException pmbe) {
269
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create());
270
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
271
		}
272
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseMap);
273
		return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
274
	}
21248 ashik.ali 275
}