Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
26785 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import java.nio.charset.StandardCharsets;
4
 
5
import org.apache.commons.lang3.RandomStringUtils;
6
import org.apache.logging.log4j.LogManager;
7
import org.apache.logging.log4j.Logger;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.mail.javamail.JavaMailSender;
10
import org.springframework.stereotype.Component;
11
 
12
import com.google.common.hash.Hashing;
13
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
14
import com.spice.profitmandi.dao.entity.fofo.Customer;
15
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;
16
 
17
@Component
18
public class CustomerServiceImpl implements CustomerService {
19
	// private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
20
	// been reset. Please click this <a href=\"%s\">link</a> to reset your
21
	// password.\n\nRegards\nSmartdukaan";
22
	private static final String RESET_PASSWORD_BODY = "Dear %s, your password has been reset to %s. Regards\nSmartdukaan";
23
	private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";
24
 
25
	private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);
26
 
27
	@Autowired
28
	CustomerRepository customerRepository;
29
	@Autowired
30
	JavaMailSender mailSender;
31
 
32
	private String getHash256(String originalString) {
33
		return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
34
	}
35
 
36
	@Override
37
	public boolean authenticate(String mobile, String password) {
38
		return customerRepository.authenticate(mobile, getHash256(password));
39
	}
40
 
41
	@Override
26817 amit.gupta 42
	public Customer addCustomer(Customer customer) {
26785 amit.gupta 43
		try {
26817 amit.gupta 44
			customer = customerRepository.selectByMobileNumber(customer.getMobileNumber());
26785 amit.gupta 45
		} catch (ProfitMandiBusinessException pbse) {
26834 amit.gupta 46
			if(customer.getPassword()!=null) {
47
				customer.setPassword(getHash256(customer.getPassword()));
26840 amit.gupta 48
			} else {
49
				customer.setPassword(getHash256(getRandomString()));
26834 amit.gupta 50
			}
26817 amit.gupta 51
			try {
52
				customerRepository.persist(customer);
53
			} catch(Exception e) {
54
				e.printStackTrace();
55
			}
26785 amit.gupta 56
		}
26817 amit.gupta 57
		return customer;
26785 amit.gupta 58
 
59
	}
60
 
61
	private String getRandomString() {
62
		int length = 10;
63
		boolean useLetters = true;
64
		boolean useNumbers = false;
65
		String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
66
		return generatedString;
67
	}
68
 
69
	@Override
70
	public void resetPassword(String mobile) throws ProfitMandiBusinessException {
71
		// TODO Auto-generated method stub
72
 
73
	}
74
 
75
	@Override
76
	public void changePassword(String emailOrMobile, String oldHashedPassword, String hashedPassword)
77
			throws ProfitMandiBusinessException {
78
		// TODO Auto-generated method stub
79
 
80
	}
81
 
82
}