Subversion Repositories SmartDukaan

Rev

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