Subversion Repositories SmartDukaan

Rev

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