Subversion Repositories SmartDukaan

Rev

Rev 26840 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import java.nio.charset.StandardCharsets;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import com.google.common.hash.Hashing;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.dao.entity.fofo.Customer;
import com.spice.profitmandi.dao.repository.fofo.CustomerRepository;

@Component
public class CustomerServiceImpl implements CustomerService {
        // private static final String RESET_PASSWORD_BODY = "Dear %s, your password has
        // been reset. Please click this <a href=\"%s\">link</a> to reset your
        // password.\n\nRegards\nSmartdukaan";
        private static final String RESET_PASSWORD_BODY = "Dear %s, your password has been reset to %s. Regards\nSmartdukaan";
        private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";

        private static final Logger LOGGER = LogManager.getLogger(AuthServiceImpl.class);

        @Autowired
        CustomerRepository customerRepository;
        @Autowired
        JavaMailSender mailSender;

        private String getHash256(String originalString) {
                return Hashing.sha256().hashString(originalString, StandardCharsets.UTF_8).toString();
        }

        @Override
        public boolean authenticate(String mobile, String password) {
                return customerRepository.authenticate(mobile, getHash256(password));
        }

        @Override
        public Customer addCustomer(Customer customer) {
                try {
                        customer = customerRepository.selectByMobileNumber(customer.getMobileNumber());
                } catch (ProfitMandiBusinessException pbse) {
                        if(customer.getPassword()!=null) {
                                customer.setPassword(getHash256(customer.getPassword()));
                        } else {
                                customer.setPassword(getHash256(getRandomString()));
                        }
                        try {
                                customerRepository.persist(customer);
                        } catch(Exception e) {
                                e.printStackTrace();
                        }
                }
                return customer;

        }

        private String getRandomString() {
                int length = 10;
                boolean useLetters = true;
                boolean useNumbers = false;
                String generatedString = RandomStringUtils.random(length, useLetters, useNumbers);
                return generatedString;
        }

        @Override
        public void resetPassword(String mobile) throws ProfitMandiBusinessException {
                // TODO Auto-generated method stub
                
        }

        @Override
        public void changePassword(String mobileNumber, String newPassword)
                        throws ProfitMandiBusinessException {
                Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
                customer.setPassword(getHash256(newPassword));
        }

}