Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
23781 ashik.ali 1
package com.spice.profitmandi.service.user;
21525 amit.gupta 2
 
33247 ranu 3
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
4
import com.spice.profitmandi.common.model.ProfitMandiConstants;
5
import com.spice.profitmandi.common.util.DesEncrypter;
34785 vikas.jang 6
import com.spice.profitmandi.common.util.Utils;
33247 ranu 7
import com.spice.profitmandi.dao.entity.dtr.*;
8
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
9
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
10
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
11
import com.spice.profitmandi.dao.repository.dtr.*;
12
import com.spice.profitmandi.thrift.clients.TransactionClient;
13
import in.shop2020.model.v1.order.TransactionService;
34785 vikas.jang 14
import org.apache.commons.lang.RandomStringUtils;
21525 amit.gupta 15
import org.hibernate.Criteria;
16
import org.hibernate.Session;
17
import org.hibernate.SessionFactory;
18
import org.hibernate.criterion.Restrictions;
19
import org.springframework.beans.factory.annotation.Autowired;
23781 ashik.ali 20
import org.springframework.beans.factory.annotation.Qualifier;
34785 vikas.jang 21
import org.springframework.mail.javamail.JavaMailSender;
21525 amit.gupta 22
import org.springframework.stereotype.Component;
23
 
33247 ranu 24
import java.time.LocalDateTime;
25
import java.util.HashMap;
26
import java.util.HashSet;
27
import java.util.List;
28
import java.util.Map;
21525 amit.gupta 29
 
30
@Component
31
public class UserServiceImpl implements UserService {
32
 
34785 vikas.jang 33
	private static final String RESET_PASSWORD_BODY = "Dear %s, \nyour password has been reset to %s. \n\nRegards\nSmartdukaan";
34
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
35
 
21525 amit.gupta 36
	@Autowired
22931 ashik.ali 37
	private SessionFactory sessionFactory;
21525 amit.gupta 38
 
39
	@Autowired
23781 ashik.ali 40
	@Qualifier(value = "userRepository")
23204 ashik.ali 41
	private UserRepository userRepository;
21525 amit.gupta 42
 
43
	@Autowired
24099 amit.gupta 44
	@Qualifier(value = "userUserRepository")
45
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
46
 
47
	@Autowired
23204 ashik.ali 48
	private RetailerRepository retailerRepo;
21525 amit.gupta 49
 
23204 ashik.ali 50
	@Autowired
51
	private FofoStoreRepository fofoStoreRepository;
52
 
53
	@Autowired
54
	private UserAccountRepository userAccountRepository;
55
 
56
	@Autowired
57
	private UserRoleRepository userRoleRepository;
58
 
23781 ashik.ali 59
	@Autowired
60
	private RoleRepository roleRepository;
34785 vikas.jang 61
 
62
	@Autowired
63
	JavaMailSender mailSender;
23781 ashik.ali 64
 
24099 amit.gupta 65
	private static final DesEncrypter encrypter = new DesEncrypter("saholic");
34785 vikas.jang 66
 
23204 ashik.ali 67
	@SuppressWarnings("deprecation")
21525 amit.gupta 68
	@Override
69
	public boolean updateActivation(int userId, int retailerId, String activationCode) throws Throwable {
70
 
71
		TransactionClient transactionServiceClient = new TransactionClient();
72
		TransactionService.Client tsc = transactionServiceClient.getClient();
73
		boolean registered = tsc.registerRsa(retailerId, activationCode);
74
 
75
		Session session = sessionFactory.getCurrentSession();
76
 
77
		if(!registered){
78
			Criteria cr = session.createCriteria(ActivationCode.class);
79
			cr.add(Restrictions.eq("code", activationCode)).add(Restrictions.eq("status", true));
80
			ActivationCode code = (ActivationCode)cr.uniqueResult();
81
			if(code==null) {
82
				return false;
83
			} else {
84
				registered=true;
85
				code.setStatus(false);
86
				session.update(code);
87
			}
88
		}
34785 vikas.jang 89
 
21525 amit.gupta 90
		if(registered) {
23204 ashik.ali 91
			User user = userRepository.selectById(userId);
21525 amit.gupta 92
			user.setActivated(true);
22845 amit.gupta 93
			user.setActivationTime(LocalDateTime.now());
21525 amit.gupta 94
			user.setReferrer(activationCode);
95
			session.update(user);
34785 vikas.jang 96
 
21525 amit.gupta 97
			Retailer retailer = retailerRepo.selectById(retailerId);
98
			retailer.setActive(true);
99
			session.update(retailer);
100
		}
34785 vikas.jang 101
 
21525 amit.gupta 102
		return registered;
103
	}
22845 amit.gupta 104
 
105
 
106
	@Override
107
	public boolean createActivationCode(int userId) throws Throwable {
108
		// TODO Auto-generated method stub
109
		return false;
110
	}
23204 ashik.ali 111
 
112
	@Override
113
	public Map<String, String> getEmailsAndFofoStoreCodeByUserId(int userId) throws ProfitMandiBusinessException {
114
		User user = userRepository.selectById(userId);
23273 ashik.ali 115
		UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
23781 ashik.ali 116
		Role role = roleRepository.selectByName(RoleType.FOFO.toString());
23204 ashik.ali 117
		Map<String, String> map = new HashMap<>();
118
		map.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
119
		map.put(ProfitMandiConstants.SECONDRY_EMAIL_ID, user.getSecondryEmailId());
23781 ashik.ali 120
		userRoleRepository.selectByUserIdAndRoleId(userId, role.getId());
23273 ashik.ali 121
		FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(userAccount.getAccountKey());
23204 ashik.ali 122
		map.put(ProfitMandiConstants.CODE, fofoStore.getCode());
123
		return map;
124
	}
23984 govind 125
 
126
 
127
	@Override
33247 ranu 128
	public Map<Integer, String> getAllUseUserIdEmailIdMap(List<Integer> userIds) throws ProfitMandiBusinessException {
23984 govind 129
		 Map<Integer, String> userIdEmailIdMap=new HashMap<>();
130
		 List<User> users=userRepository.selectAllByIds(new HashSet<>(userIds));
131
		 for(User user:users)
132
		 {
133
			 userIdEmailIdMap.put(user.getId(),user.getEmailId());
134
		 }
135
		return userIdEmailIdMap;
136
	}
24099 amit.gupta 137
 
138
 
139
	@Override
140
	public User authenticate(String emailMobile, String password) throws ProfitMandiBusinessException {
141
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
34785 vikas.jang 142
		if(!user.getPassword().equals(encrypter.encrypt(password))) {
24099 amit.gupta 143
			throw new ProfitMandiBusinessException("Username Password", emailMobile, "Invalid Username or Password");
144
		}
145
		return user;
146
	}
147
 
34785 vikas.jang 148
	@Override
149
	public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
150
		User user = userRepository.selectByEmailIdOrMobileNumber(emailOrMobile);
151
		String password = getRandomString();
152
		try {
153
			Utils.sendMailWithAttachments(mailSender, user.getEmailId(), null, RESET_PASSWORD_SUBJECT, String.format(RESET_PASSWORD_BODY, user.getFirstName(), password), null);
154
		} catch (Exception e) {
155
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile, "Could not send password reset mail. Password Could not be reset");
156
		}
157
		user.setPassword(encrypter.encrypt(password));
158
	}
24099 amit.gupta 159
 
160
	@Override
161
	public boolean resetPassword(String emailMobile, String password) throws ProfitMandiBusinessException {
162
		User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
163
		user.setPassword(encrypter.encrypt(password));
164
		userRepository.persist(user);
165
		return true;
166
	}
34785 vikas.jang 167
 
168
	private String getRandomString() {
169
		int length = 10;
170
		boolean useLetters = true;
171
		boolean useNumbers = false;
172
		return RandomStringUtils.random(length, useLetters, useNumbers);
173
	}
23781 ashik.ali 174
 
21525 amit.gupta 175
}