Subversion Repositories SmartDukaan

Rev

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