Subversion Repositories SmartDukaan

Rev

Rev 36399 | 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;
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
36399 amit 63
	JavaMailSender gmailRelaySender;
34794 vikas.jang 64
 
65
	@Autowired
35956 amit 66
	MailOutboxService mailOutboxService;
67
 
68
	@Autowired
34794 vikas.jang 69
	AuthService authService;
23781 ashik.ali 70
 
24099 amit.gupta 71
	private static final DesEncrypter encrypter = new DesEncrypter("saholic");
34785 vikas.jang 72
 
23204 ashik.ali 73
	@SuppressWarnings("deprecation")
21525 amit.gupta 74
	@Override
75
	public boolean updateActivation(int userId, int retailerId, String activationCode) throws Throwable {
36624 amit 76
		boolean registered = false;
21525 amit.gupta 77
		Session session = sessionFactory.getCurrentSession();
78
 
36624 amit 79
		Criteria cr = session.createCriteria(ActivationCode.class);
80
		cr.add(Restrictions.eq("code", activationCode)).add(Restrictions.eq("status", true));
81
		ActivationCode code = (ActivationCode)cr.uniqueResult();
82
		if(code == null) {
83
			return false;
84
		} else {
85
			registered = true;
86
			code.setStatus(false);
87
			session.update(code);
21525 amit.gupta 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 {
34815 vikas 150
		User user = userRepository.selectByEmailId(emailOrMobile);
34785 vikas.jang 151
		String password = getRandomString();
152
		try {
35956 amit 153
			mailOutboxService.queueMail(user.getEmailId(), null, RESET_PASSWORD_SUBJECT, String.format(RESET_PASSWORD_BODY, user.getFirstName(), password), "UserServiceImpl.resetPassword");
34785 vikas.jang 154
		} catch (Exception e) {
155
			throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile, "Could not send password reset mail. Password Could not be reset");
156
		}
34794 vikas.jang 157
		//user.setPassword(encrypter.encrypt(password));
158
		authService.resetPassword(emailOrMobile, password);
34785 vikas.jang 159
	}
24099 amit.gupta 160
 
161
	@Override
162
	public boolean resetPassword(String emailMobile, String password) throws ProfitMandiBusinessException {
34815 vikas 163
		User user = userRepository.selectByEmailId(emailMobile);
24099 amit.gupta 164
		user.setPassword(encrypter.encrypt(password));
165
		return true;
166
	}
34785 vikas.jang 167
 
34794 vikas.jang 168
	@Override
169
	public boolean changePassword(User user, String password) throws ProfitMandiBusinessException {
170
		authService.resetPassword(user.getEmailId(), password);
171
		return true;
172
	}
173
 
34785 vikas.jang 174
	private String getRandomString() {
175
		int length = 10;
176
		boolean useLetters = true;
177
		boolean useNumbers = false;
178
		return RandomStringUtils.random(length, useLetters, useNumbers);
179
	}
23781 ashik.ali 180
 
21525 amit.gupta 181
}