| 5432 |
amar.kumar |
1 |
package in.shop2020.user.handler;
|
|
|
2 |
|
|
|
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.Date;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
|
|
|
7 |
import org.apache.commons.logging.Log;
|
|
|
8 |
import org.apache.commons.logging.LogFactory;
|
|
|
9 |
import org.apache.thrift.TException;
|
|
|
10 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
11 |
import org.springframework.stereotype.Service;
|
|
|
12 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
13 |
|
|
|
14 |
import in.shop2020.model.v1.user.AuthenticationException;
|
|
|
15 |
import in.shop2020.model.v1.user.Sex;
|
|
|
16 |
import in.shop2020.model.v1.user.UserCommunication;
|
|
|
17 |
import in.shop2020.model.v1.user.UserCommunicationException;
|
|
|
18 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
19 |
import in.shop2020.user.domain.User;
|
|
|
20 |
import in.shop2020.model.v1.user.UserType;
|
|
|
21 |
import in.shop2020.user.persistence.UserMapper;
|
|
|
22 |
import in.shop2020.user.util.Converter;
|
|
|
23 |
|
|
|
24 |
@Service
|
|
|
25 |
public class UserHandler {
|
|
|
26 |
|
|
|
27 |
@Autowired
|
|
|
28 |
private UserMapper userMapper;
|
|
|
29 |
@Autowired
|
|
|
30 |
private CartHandler cartHandler;
|
|
|
31 |
@Autowired
|
|
|
32 |
private AddressHandler addressHandler;
|
|
|
33 |
|
|
|
34 |
private static final Log log = LogFactory.getLog(UserHandler.class);
|
|
|
35 |
|
|
|
36 |
public in.shop2020.user.domain.User getUser(int id) throws TException{
|
|
|
37 |
try{
|
|
|
38 |
return userMapper.getUserNameById(id);
|
|
|
39 |
} catch(Exception e) {
|
|
|
40 |
log.error("Exception while getting user" + e);
|
|
|
41 |
throw new TException(e);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
@Transactional
|
|
|
47 |
public in.shop2020.model.v1.user.User createAnonymousUser(String jsession_id)
|
|
|
48 |
throws TException{
|
|
|
49 |
User user = userMapper.getUserByEmail(jsession_id+"@anonymous.com");
|
|
|
50 |
if(user!=null) {
|
|
|
51 |
log.warn("User already exists : "+jsession_id+"@anonymous.com");
|
|
|
52 |
return Converter.toThriftUser(user);
|
|
|
53 |
}
|
|
|
54 |
in.shop2020.user.domain.Cart cart = cartHandler.createCart();
|
|
|
55 |
log.info("Created cart for User : " + jsession_id+"@anonymous.com");
|
|
|
56 |
user = new User();
|
|
|
57 |
user.setName("anonymous");
|
|
|
58 |
user.setPassword("anonymous");
|
|
|
59 |
user.setEmail(jsession_id+"@anonymous.com");
|
|
|
60 |
user.setCommunication_email(jsession_id+"@anonymous.com");
|
|
|
61 |
user.setJsession_id(jsession_id);
|
|
|
62 |
user.setActive_cart_id(cart.getId());
|
|
|
63 |
user.setIs_anonymous(true);
|
|
|
64 |
user.setSex(Sex.WONT_SAY.getValue());
|
|
|
65 |
user.setTrust_level((double)0);
|
|
|
66 |
user.setActiveSince(new Date());
|
|
|
67 |
|
|
|
68 |
userMapper.createAnonymousUser(user);
|
|
|
69 |
log.info("Created user : " + jsession_id+"@anonymous.com");
|
|
|
70 |
return Converter.toThriftUser(user);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public in.shop2020.model.v1.user.User getUserById(long userId)
|
|
|
74 |
throws TException{
|
|
|
75 |
return Converter.toThriftUser(userMapper.getUserById(userId));
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
public in.shop2020.model.v1.user.User getUserByEmail(String email)
|
|
|
79 |
throws TException{
|
|
|
80 |
return Converter.toThriftUser(userMapper.getUserByEmail(email));
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
public in.shop2020.model.v1.user.User getUserByMobileNumber(String mobileNumber)
|
|
|
84 |
throws TException{
|
|
|
85 |
return Converter.toThriftUser(userMapper.getUserByMobileNumber(mobileNumber));
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
@Transactional
|
|
|
89 |
public in.shop2020.model.v1.user.User createUser(in.shop2020.model.v1.user.User tUser)
|
|
|
90 |
throws UserContextException, TException{
|
|
|
91 |
if(userMapper.getUserByEmail(tUser.getEmail())!=null) {
|
|
|
92 |
log.error("User already exists : "+tUser);
|
|
|
93 |
throw new UserContextException(109, "User already exists with this email id.");
|
|
|
94 |
}
|
|
|
95 |
in.shop2020.user.domain.Cart cart = cartHandler.createCart();
|
|
|
96 |
log.info("Created cart for User : " + tUser);
|
|
|
97 |
tUser.setActiveCartId(cart.getId());
|
|
|
98 |
in.shop2020.user.domain.User user = Converter.toDBUser(tUser);
|
|
|
99 |
user.setActiveSince(new Date());
|
|
|
100 |
user.setIs_anonymous(false);
|
|
|
101 |
userMapper.createUser(user);
|
|
|
102 |
log.info("Created user : " + tUser);
|
|
|
103 |
return Converter.toThriftUser(user);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public in.shop2020.model.v1.user.User updateUser(in.shop2020.model.v1.user.User tUser)
|
|
|
107 |
throws TException{
|
|
|
108 |
in.shop2020.user.domain.User user = new User();
|
|
|
109 |
user = userMapper.getUserById(tUser.getUserId());
|
|
|
110 |
user.setEmail(tUser.getEmail());
|
|
|
111 |
user.setPassword(tUser.getPassword());
|
|
|
112 |
user.setName(tUser.getName());
|
|
|
113 |
user.setCommunication_email(tUser.getCommunicationEmail());
|
|
|
114 |
user.setJsession_id(tUser.getJsessionId());
|
|
|
115 |
user.setIs_anonymous(tUser.isSetIsAnonymous());
|
|
|
116 |
if(tUser.getSex()!=null) {
|
|
|
117 |
user.setSex(tUser.getSex().getValue());
|
|
|
118 |
}
|
|
|
119 |
user.setDate_of_birth(tUser.getDateOfBirth());
|
|
|
120 |
user.setMobile_number(tUser.getMobileNumber());
|
|
|
121 |
userMapper.updateUser(user);
|
|
|
122 |
return Converter.toThriftUser(user);
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public boolean deleteUser(long userId) throws TException{
|
|
|
126 |
userMapper.deleteUser(userId);
|
|
|
127 |
return true;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public in.shop2020.model.v1.user.User authenticateUser(String email,
|
|
|
131 |
String password) throws AuthenticationException, TException{
|
|
|
132 |
in.shop2020.user.domain.User user = userMapper.getUserByEmail(email);
|
|
|
133 |
if(user==null) {
|
|
|
134 |
log.warn("This user is not registered : "+email);
|
|
|
135 |
throw new AuthenticationException("This email address is not registered.", 102);
|
|
|
136 |
} else if(password.equals(user.getPassword())){
|
|
|
137 |
return Converter.toThriftUser(user);
|
|
|
138 |
} else {
|
|
|
139 |
log.warn("Wrong username or password for user : "+email);
|
|
|
140 |
throw new AuthenticationException("Wrong username or password", 102);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
public boolean userExists(String email) throws TException{
|
|
|
145 |
in.shop2020.user.domain.User user = userMapper.getUserByEmail(email);
|
|
|
146 |
if(user!=null){
|
|
|
147 |
return true;
|
|
|
148 |
}
|
|
|
149 |
log.info("User does not exist : " + email);
|
|
|
150 |
return false;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public void setLastLogin(long userId, Date date) throws TException{
|
|
|
154 |
userMapper.setLastLogin(userId, date);
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public void setLastLogout(long userId, Date date) throws TException{
|
|
|
158 |
userMapper.setLastLogout(userId, date);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
public void setDefaultAddress(long userId, long addressId)
|
|
|
162 |
throws TException{
|
|
|
163 |
userMapper.setDefaultAddress(userId, addressId);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
public List<in.shop2020.user.domain.User> getAllUsers(UserType userType, long startDate, long endDate)
|
|
|
167 |
throws TException{
|
|
|
168 |
return userMapper.getAllUsers(userType.getValue(), startDate, endDate);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
public void increaseTrustLevel(long userId, double trustLevelDelta)
|
|
|
172 |
throws TException{
|
|
|
173 |
userMapper.increaseTrustLevel(userId, trustLevelDelta);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
public UserCommunication getUserCommunicationById(long id) throws TException{
|
|
|
177 |
return Converter.toThriftUserCommunication(userMapper.getUserCommunicationById(id));
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
public List<UserCommunication> getUserCommunicationByUser(long userId)
|
|
|
181 |
throws TException{
|
|
|
182 |
List<UserCommunication> tUserCommunications = new ArrayList<UserCommunication>();
|
|
|
183 |
List<in.shop2020.user.domain.UserCommunication> userCommunications =
|
|
|
184 |
new ArrayList<in.shop2020.user.domain.UserCommunication>();
|
|
|
185 |
|
|
|
186 |
userCommunications = userMapper.getUserCommunicationByUser(userId);
|
|
|
187 |
|
|
|
188 |
for(in.shop2020.user.domain.UserCommunication userCommunication : userCommunications) {
|
|
|
189 |
tUserCommunications.add(Converter.toThriftUserCommunication(userCommunication));
|
|
|
190 |
}
|
|
|
191 |
return tUserCommunications;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
public List<UserCommunication> getAllUserCommunications()
|
|
|
195 |
throws TException{
|
|
|
196 |
List<UserCommunication> tUserCommunications = new ArrayList<UserCommunication>();
|
|
|
197 |
List<in.shop2020.user.domain.UserCommunication> userCommunications =
|
|
|
198 |
new ArrayList<in.shop2020.user.domain.UserCommunication>();
|
|
|
199 |
|
|
|
200 |
userCommunications = userMapper.getAllUserCommunications();
|
|
|
201 |
|
|
|
202 |
for(in.shop2020.user.domain.UserCommunication userCommunication : userCommunications) {
|
|
|
203 |
tUserCommunications.add(Converter.toThriftUserCommunication(userCommunication));
|
|
|
204 |
}
|
|
|
205 |
return tUserCommunications;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
public boolean updatePassword(long userId, String oldPassword, String newPassword)
|
|
|
209 |
throws UserContextException, TException{
|
|
|
210 |
User user = userMapper.getUserById(userId);
|
|
|
211 |
if(user==null) {
|
|
|
212 |
log.error("No user found with id : " + userId);
|
|
|
213 |
throw new UserContextException(101, "no such user in system userId : "+userId);
|
|
|
214 |
}
|
|
|
215 |
if(!user.getPassword().equals(oldPassword)){
|
|
|
216 |
log.error("Old password not correct ");
|
|
|
217 |
throw new UserContextException(103, "Old Password not correct ");
|
|
|
218 |
}
|
|
|
219 |
if(newPassword!=null) {
|
|
|
220 |
log.info("Updating password for userId : "+userId);
|
|
|
221 |
userMapper.updatePassword(userId, newPassword);
|
|
|
222 |
return true;
|
|
|
223 |
}
|
|
|
224 |
log.error("New password was null, hence not updating ");
|
|
|
225 |
return false;
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
public boolean forgotPassword(String email, String newPassword)
|
|
|
229 |
throws UserContextException, TException{
|
|
|
230 |
User user = userMapper.getUserByEmail(email);
|
|
|
231 |
if(user==null) {
|
|
|
232 |
log.error("No user found with email : " + email);
|
|
|
233 |
throw new UserContextException(101, "no such user in system userId : "+email);
|
|
|
234 |
}
|
|
|
235 |
if(newPassword != null) {
|
|
|
236 |
userMapper.updatePassword(user.getId(), newPassword);
|
|
|
237 |
return true;
|
|
|
238 |
} else {
|
|
|
239 |
log.error("Password cannot be null");
|
|
|
240 |
throw new UserContextException(101, "Password cannot be null : "+email);
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
public long getUserCount(UserType userType) throws TException{
|
|
|
246 |
if(userType==null) {
|
|
|
247 |
log.info("Getting all users count");
|
|
|
248 |
return userMapper.getUserCount();
|
|
|
249 |
} else {
|
|
|
250 |
log.info("Getting "+userType+" users count");
|
|
|
251 |
return userMapper.getUserCountByType(userType.getValue());
|
|
|
252 |
}
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
public in.shop2020.model.v1.user.User getUserByCartId(long cartId)
|
|
|
256 |
throws TException{
|
|
|
257 |
in.shop2020.user.domain.User user = userMapper.getUserByCartId(cartId);
|
|
|
258 |
if(user!=null) {
|
|
|
259 |
log.error("No user found with cartId : " + cartId);
|
|
|
260 |
return Converter.toThriftUser(userMapper.getUserByCartId(cartId));
|
|
|
261 |
}
|
|
|
262 |
return null;
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
public boolean saveUserCommunication(long userId, String replyTo,
|
|
|
266 |
long communicationType, long orderId, String airwaybillNo,
|
|
|
267 |
String productName, String subject, String message)
|
|
|
268 |
throws TException{
|
|
|
269 |
in.shop2020.user.domain.UserCommunication userCommunication =
|
|
|
270 |
new in.shop2020.user.domain.UserCommunication();
|
|
|
271 |
userCommunication.setUser_id(userId);
|
|
|
272 |
userCommunication.setCommunication_type(communicationType);
|
|
|
273 |
if(orderId>0) {
|
|
|
274 |
userCommunication.setOrder_id(orderId);
|
|
|
275 |
}
|
|
|
276 |
userCommunication.setAirwaybill_no(airwaybillNo);
|
|
|
277 |
userCommunication.setReply_to(replyTo);
|
|
|
278 |
userCommunication.setProduct_name(productName);
|
|
|
279 |
userCommunication.setSubject(subject);
|
|
|
280 |
userCommunication.setMessage(message);
|
|
|
281 |
userCommunication.setCommunication_timestamp(new Date());
|
|
|
282 |
userMapper.insertUserCommunication(userCommunication);
|
|
|
283 |
|
|
|
284 |
return true;
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
public boolean setUserAsLoggedIn(long userId, long timestamp) throws UserContextException,
|
|
|
288 |
TException{
|
|
|
289 |
if(getUserById(userId)==null) {
|
|
|
290 |
log.error("No user found with Id : " + userId);
|
|
|
291 |
throw new UserContextException(103, "User not found for Id : "+userId);
|
|
|
292 |
}
|
|
|
293 |
if(timestamp==0) {
|
|
|
294 |
setLastLogin(userId, new Date());
|
|
|
295 |
} else {
|
|
|
296 |
setLastLogin(userId, new Date(timestamp));
|
|
|
297 |
}
|
|
|
298 |
return true;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
public boolean setUserAsLoggedOut(long userId, long timestamp)
|
|
|
302 |
throws UserContextException, TException{
|
|
|
303 |
if(getUserById(userId)==null) {
|
|
|
304 |
log.error("No user found with Id : " + userId);
|
|
|
305 |
throw new UserContextException(103, "User not found for Id : "+userId);
|
|
|
306 |
}
|
|
|
307 |
if(timestamp==0) {
|
|
|
308 |
setLastLogout(userId, new Date());
|
|
|
309 |
} else {
|
|
|
310 |
setLastLogout(userId, new Date(timestamp));
|
|
|
311 |
}
|
|
|
312 |
return true;
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
public String getDefaultPincode(long userId) throws UserContextException,
|
|
|
316 |
TException{
|
|
|
317 |
in.shop2020.model.v1.user.User user = getUserById(userId);
|
|
|
318 |
if(user!=null) {
|
|
|
319 |
if(user.getDefaultAddressId()!=0) {
|
|
|
320 |
return addressHandler.getPincodeForAddress(user.getDefaultAddressId());
|
|
|
321 |
}
|
|
|
322 |
} else {
|
|
|
323 |
log.error("No user found with Id : " + userId);
|
|
|
324 |
throw new UserContextException(103,"No User found with Id : "+userId);
|
|
|
325 |
}
|
|
|
326 |
return "110001";
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
public void removeUserCommunication(long id) throws UserCommunicationException,
|
|
|
330 |
TException{
|
|
|
331 |
in.shop2020.user.domain.UserCommunication userCommunication =
|
|
|
332 |
userMapper.getUserCommunicationById(id);
|
|
|
333 |
if(userCommunication == null) {
|
|
|
334 |
log.error("No UserCommunication found with id "+id+" while deleting");
|
|
|
335 |
throw new UserCommunicationException(103, "No UserCommunication found" +
|
|
|
336 |
" with id "+id+" while deleting");
|
|
|
337 |
}
|
|
|
338 |
userMapper.removeUserCommunication(id);
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
public double getTrustLevel(long userId) {
|
|
|
342 |
return userMapper.getTrustLevel(userId);
|
|
|
343 |
}
|
|
|
344 |
|
|
|
345 |
}
|