Blame | Last modification | View Log | RSS feed
package in.shop2020.user.handler;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.thrift.TException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import in.shop2020.model.v1.user.AuthenticationException;import in.shop2020.model.v1.user.Sex;import in.shop2020.model.v1.user.UserCommunication;import in.shop2020.model.v1.user.UserCommunicationException;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.user.domain.User;import in.shop2020.model.v1.user.UserType;import in.shop2020.user.persistence.UserMapper;import in.shop2020.user.util.Converter;@Servicepublic class UserHandler {@Autowiredprivate UserMapper userMapper;@Autowiredprivate CartHandler cartHandler;@Autowiredprivate AddressHandler addressHandler;private static final Log log = LogFactory.getLog(UserHandler.class);public in.shop2020.user.domain.User getUser(int id) throws TException{try{return userMapper.getUserNameById(id);} catch(Exception e) {log.error("Exception while getting user" + e);throw new TException(e);}}@Transactionalpublic in.shop2020.model.v1.user.User createAnonymousUser(String jsession_id)throws TException{User user = userMapper.getUserByEmail(jsession_id+"@anonymous.com");if(user!=null) {log.warn("User already exists : "+jsession_id+"@anonymous.com");return Converter.toThriftUser(user);}in.shop2020.user.domain.Cart cart = cartHandler.createCart();log.info("Created cart for User : " + jsession_id+"@anonymous.com");user = new User();user.setName("anonymous");user.setPassword("anonymous");user.setEmail(jsession_id+"@anonymous.com");user.setCommunication_email(jsession_id+"@anonymous.com");user.setJsession_id(jsession_id);user.setActive_cart_id(cart.getId());user.setIs_anonymous(true);user.setSex(Sex.WONT_SAY.getValue());user.setTrust_level((double)0);user.setActiveSince(new Date());userMapper.createAnonymousUser(user);log.info("Created user : " + jsession_id+"@anonymous.com");return Converter.toThriftUser(user);}public in.shop2020.model.v1.user.User getUserById(long userId)throws TException{return Converter.toThriftUser(userMapper.getUserById(userId));}public in.shop2020.model.v1.user.User getUserByEmail(String email)throws TException{return Converter.toThriftUser(userMapper.getUserByEmail(email));}public in.shop2020.model.v1.user.User getUserByMobileNumber(String mobileNumber)throws TException{return Converter.toThriftUser(userMapper.getUserByMobileNumber(mobileNumber));}@Transactionalpublic in.shop2020.model.v1.user.User createUser(in.shop2020.model.v1.user.User tUser)throws UserContextException, TException{if(userMapper.getUserByEmail(tUser.getEmail())!=null) {log.error("User already exists : "+tUser);throw new UserContextException(109, "User already exists with this email id.");}in.shop2020.user.domain.Cart cart = cartHandler.createCart();log.info("Created cart for User : " + tUser);tUser.setActiveCartId(cart.getId());in.shop2020.user.domain.User user = Converter.toDBUser(tUser);user.setActiveSince(new Date());user.setIs_anonymous(false);userMapper.createUser(user);log.info("Created user : " + tUser);return Converter.toThriftUser(user);}public in.shop2020.model.v1.user.User updateUser(in.shop2020.model.v1.user.User tUser)throws TException{in.shop2020.user.domain.User user = new User();user = userMapper.getUserById(tUser.getUserId());user.setEmail(tUser.getEmail());user.setPassword(tUser.getPassword());user.setName(tUser.getName());user.setCommunication_email(tUser.getCommunicationEmail());user.setJsession_id(tUser.getJsessionId());user.setIs_anonymous(tUser.isSetIsAnonymous());if(tUser.getSex()!=null) {user.setSex(tUser.getSex().getValue());}user.setDate_of_birth(tUser.getDateOfBirth());user.setMobile_number(tUser.getMobileNumber());userMapper.updateUser(user);return Converter.toThriftUser(user);}public boolean deleteUser(long userId) throws TException{userMapper.deleteUser(userId);return true;}public in.shop2020.model.v1.user.User authenticateUser(String email,String password) throws AuthenticationException, TException{in.shop2020.user.domain.User user = userMapper.getUserByEmail(email);if(user==null) {log.warn("This user is not registered : "+email);throw new AuthenticationException("This email address is not registered.", 102);} else if(password.equals(user.getPassword())){return Converter.toThriftUser(user);} else {log.warn("Wrong username or password for user : "+email);throw new AuthenticationException("Wrong username or password", 102);}}public boolean userExists(String email) throws TException{in.shop2020.user.domain.User user = userMapper.getUserByEmail(email);if(user!=null){return true;}log.info("User does not exist : " + email);return false;}public void setLastLogin(long userId, Date date) throws TException{userMapper.setLastLogin(userId, date);}public void setLastLogout(long userId, Date date) throws TException{userMapper.setLastLogout(userId, date);}public void setDefaultAddress(long userId, long addressId)throws TException{userMapper.setDefaultAddress(userId, addressId);}public List<in.shop2020.user.domain.User> getAllUsers(UserType userType, long startDate, long endDate)throws TException{return userMapper.getAllUsers(userType.getValue(), startDate, endDate);}public void increaseTrustLevel(long userId, double trustLevelDelta)throws TException{userMapper.increaseTrustLevel(userId, trustLevelDelta);}public UserCommunication getUserCommunicationById(long id) throws TException{return Converter.toThriftUserCommunication(userMapper.getUserCommunicationById(id));}public List<UserCommunication> getUserCommunicationByUser(long userId)throws TException{List<UserCommunication> tUserCommunications = new ArrayList<UserCommunication>();List<in.shop2020.user.domain.UserCommunication> userCommunications =new ArrayList<in.shop2020.user.domain.UserCommunication>();userCommunications = userMapper.getUserCommunicationByUser(userId);for(in.shop2020.user.domain.UserCommunication userCommunication : userCommunications) {tUserCommunications.add(Converter.toThriftUserCommunication(userCommunication));}return tUserCommunications;}public List<UserCommunication> getAllUserCommunications()throws TException{List<UserCommunication> tUserCommunications = new ArrayList<UserCommunication>();List<in.shop2020.user.domain.UserCommunication> userCommunications =new ArrayList<in.shop2020.user.domain.UserCommunication>();userCommunications = userMapper.getAllUserCommunications();for(in.shop2020.user.domain.UserCommunication userCommunication : userCommunications) {tUserCommunications.add(Converter.toThriftUserCommunication(userCommunication));}return tUserCommunications;}public boolean updatePassword(long userId, String oldPassword, String newPassword)throws UserContextException, TException{User user = userMapper.getUserById(userId);if(user==null) {log.error("No user found with id : " + userId);throw new UserContextException(101, "no such user in system userId : "+userId);}if(!user.getPassword().equals(oldPassword)){log.error("Old password not correct ");throw new UserContextException(103, "Old Password not correct ");}if(newPassword!=null) {log.info("Updating password for userId : "+userId);userMapper.updatePassword(userId, newPassword);return true;}log.error("New password was null, hence not updating ");return false;}public boolean forgotPassword(String email, String newPassword)throws UserContextException, TException{User user = userMapper.getUserByEmail(email);if(user==null) {log.error("No user found with email : " + email);throw new UserContextException(101, "no such user in system userId : "+email);}if(newPassword != null) {userMapper.updatePassword(user.getId(), newPassword);return true;} else {log.error("Password cannot be null");throw new UserContextException(101, "Password cannot be null : "+email);}}public long getUserCount(UserType userType) throws TException{if(userType==null) {log.info("Getting all users count");return userMapper.getUserCount();} else {log.info("Getting "+userType+" users count");return userMapper.getUserCountByType(userType.getValue());}}public in.shop2020.model.v1.user.User getUserByCartId(long cartId)throws TException{in.shop2020.user.domain.User user = userMapper.getUserByCartId(cartId);if(user!=null) {log.error("No user found with cartId : " + cartId);return Converter.toThriftUser(userMapper.getUserByCartId(cartId));}return null;}public boolean saveUserCommunication(long userId, String replyTo,long communicationType, long orderId, String airwaybillNo,String productName, String subject, String message)throws TException{in.shop2020.user.domain.UserCommunication userCommunication =new in.shop2020.user.domain.UserCommunication();userCommunication.setUser_id(userId);userCommunication.setCommunication_type(communicationType);if(orderId>0) {userCommunication.setOrder_id(orderId);}userCommunication.setAirwaybill_no(airwaybillNo);userCommunication.setReply_to(replyTo);userCommunication.setProduct_name(productName);userCommunication.setSubject(subject);userCommunication.setMessage(message);userCommunication.setCommunication_timestamp(new Date());userMapper.insertUserCommunication(userCommunication);return true;}public boolean setUserAsLoggedIn(long userId, long timestamp) throws UserContextException,TException{if(getUserById(userId)==null) {log.error("No user found with Id : " + userId);throw new UserContextException(103, "User not found for Id : "+userId);}if(timestamp==0) {setLastLogin(userId, new Date());} else {setLastLogin(userId, new Date(timestamp));}return true;}public boolean setUserAsLoggedOut(long userId, long timestamp)throws UserContextException, TException{if(getUserById(userId)==null) {log.error("No user found with Id : " + userId);throw new UserContextException(103, "User not found for Id : "+userId);}if(timestamp==0) {setLastLogout(userId, new Date());} else {setLastLogout(userId, new Date(timestamp));}return true;}public String getDefaultPincode(long userId) throws UserContextException,TException{in.shop2020.model.v1.user.User user = getUserById(userId);if(user!=null) {if(user.getDefaultAddressId()!=0) {return addressHandler.getPincodeForAddress(user.getDefaultAddressId());}} else {log.error("No user found with Id : " + userId);throw new UserContextException(103,"No User found with Id : "+userId);}return "110001";}public void removeUserCommunication(long id) throws UserCommunicationException,TException{in.shop2020.user.domain.UserCommunication userCommunication =userMapper.getUserCommunicationById(id);if(userCommunication == null) {log.error("No UserCommunication found with id "+id+" while deleting");throw new UserCommunicationException(103, "No UserCommunication found" +" with id "+id+" while deleting");}userMapper.removeUserCommunication(id);}public double getTrustLevel(long userId) {return userMapper.getTrustLevel(userId);}}