Subversion Repositories SmartDukaan

Rev

Rev 34190 | 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 gmailRelaySender;

        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 boolean changePassword(String mobileNumber, String oldPassword, String newPassword) throws ProfitMandiBusinessException {
                if(newPassword==null || newPassword.isEmpty()) {
                        throw new ProfitMandiBusinessException("error","","Password cannot be empty");
                }
                if(oldPassword==null || oldPassword.isEmpty()) {
                        throw new ProfitMandiBusinessException("error", "", "Old Password cannot be empty");
                }
                if(mobileNumber==null || mobileNumber.isEmpty()) {
                        throw new ProfitMandiBusinessException("error", "", "Mobile number cannot be empty");
                }
                if(!customerRepository.authenticate(mobileNumber, getHash256(oldPassword))) {
                        throw new ProfitMandiBusinessException("error", "", "Old Password is not correct");
                }
                if(customerRepository.authenticate(mobileNumber, getHash256(newPassword))) {
                        throw new ProfitMandiBusinessException("error", "", "New Password cannot be same as old password");
                }

                Customer customer = customerRepository.selectByMobileNumber(mobileNumber);
                if(customer==null) {
                        throw new ProfitMandiBusinessException("error", "", "Mobile number does not exist");
                }
                 if(customer.getPassword()==null) {
                        throw new ProfitMandiBusinessException("error", "", "Password cannot be empty");
                }
                LOGGER.info(customer+" customer data is fetched successfully");
                if(customer.getPassword().equals(getHash256(oldPassword))) {
                        customer.setPassword(getHash256(newPassword));
                        LOGGER.info("Password changed successfully");
                        return true;
                }
                return false;
    }

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

}