Subversion Repositories SmartDukaan

Rev

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