Subversion Repositories SmartDukaan

Rev

Rev 34815 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.user;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.DesEncrypter;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.dtr.*;
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
import com.spice.profitmandi.dao.repository.dtr.*;
import com.spice.profitmandi.service.AuthService;
import com.spice.profitmandi.service.mail.MailOutboxService;
import com.spice.profitmandi.thrift.clients.TransactionClient;
import in.shop2020.model.v1.order.TransactionService;
import org.apache.commons.lang.RandomStringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Component
public class UserServiceImpl implements UserService {

        private static final String RESET_PASSWORD_BODY = "Dear %s, \nyour password has been reset to %s. \n\nRegards\nSmartdukaan";
        private static final String RESET_PASSWORD_SUBJECT = "Password Reset request";

        @Autowired
        private SessionFactory sessionFactory;

        @Autowired
        @Qualifier(value = "userRepository")
        private UserRepository userRepository;

        @Autowired
        @Qualifier(value = "userUserRepository")
        private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;

        @Autowired
        private RetailerRepository retailerRepo;
        
        @Autowired
        private FofoStoreRepository fofoStoreRepository;
        
        @Autowired
        private UserAccountRepository userAccountRepository;
        
        @Autowired
        private UserRoleRepository userRoleRepository;
        
        @Autowired
        private RoleRepository roleRepository;

        @Autowired
        JavaMailSender mailSender;

        @Autowired
        MailOutboxService mailOutboxService;

        @Autowired
        AuthService authService;
        
        private static final DesEncrypter encrypter = new DesEncrypter("saholic");

        @SuppressWarnings("deprecation")
        @Override
        public boolean updateActivation(int userId, int retailerId, String activationCode) throws Throwable {
                
                TransactionClient transactionServiceClient = new TransactionClient();
                TransactionService.Client tsc = transactionServiceClient.getClient();
                boolean registered = tsc.registerRsa(retailerId, activationCode);

                Session session = sessionFactory.getCurrentSession();

                if(!registered){
                        Criteria cr = session.createCriteria(ActivationCode.class);
                        cr.add(Restrictions.eq("code", activationCode)).add(Restrictions.eq("status", true));
                        ActivationCode code = (ActivationCode)cr.uniqueResult();
                        if(code==null) {
                                return false;
                        } else {
                                registered=true;
                                code.setStatus(false);
                                session.update(code);
                        }
                }

                if(registered) {
                        User user = userRepository.selectById(userId);
                        user.setActivated(true);
                        user.setActivationTime(LocalDateTime.now());
                        user.setReferrer(activationCode);
                        session.update(user);

                        Retailer retailer = retailerRepo.selectById(retailerId);
                        retailer.setActive(true);
                        session.update(retailer);
                }

                return registered;
        }


        @Override
        public boolean createActivationCode(int userId) throws Throwable {
                // TODO Auto-generated method stub
                return false;
        }
        
        @Override
        public Map<String, String> getEmailsAndFofoStoreCodeByUserId(int userId) throws ProfitMandiBusinessException {
                User user = userRepository.selectById(userId);
                UserAccount userAccount = userAccountRepository.selectByUserIdType(userId, AccountType.saholic);
                Role role = roleRepository.selectByName(RoleType.FOFO.toString());
                Map<String, String> map = new HashMap<>();
                map.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
                map.put(ProfitMandiConstants.SECONDRY_EMAIL_ID, user.getSecondryEmailId());
                userRoleRepository.selectByUserIdAndRoleId(userId, role.getId());
                FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(userAccount.getAccountKey());
                map.put(ProfitMandiConstants.CODE, fofoStore.getCode());
                return map;
        }


        @Override
        public Map<Integer, String> getAllUseUserIdEmailIdMap(List<Integer> userIds) throws ProfitMandiBusinessException {
                 Map<Integer, String> userIdEmailIdMap=new HashMap<>();
                 List<User> users=userRepository.selectAllByIds(new HashSet<>(userIds));
                 for(User user:users)
                 {
                         userIdEmailIdMap.put(user.getId(),user.getEmailId());
                 }
                return userIdEmailIdMap;
        }


        @Override
        public User authenticate(String emailMobile, String password) throws ProfitMandiBusinessException {
                User user = userRepository.selectByEmailIdOrMobileNumber(emailMobile);
                if(!user.getPassword().equals(encrypter.encrypt(password))) {
                        throw new ProfitMandiBusinessException("Username Password", emailMobile, "Invalid Username or Password");
                }
                return user;
        }

        @Override
        public void resetPassword(String emailOrMobile) throws ProfitMandiBusinessException {
                User user = userRepository.selectByEmailId(emailOrMobile);
                String password = getRandomString();
                try {
                        mailOutboxService.queueMail(user.getEmailId(), null, RESET_PASSWORD_SUBJECT, String.format(RESET_PASSWORD_BODY, user.getFirstName(), password), "UserServiceImpl.resetPassword");
                } catch (Exception e) {
                        throw new ProfitMandiBusinessException("Password Reset Email", emailOrMobile, "Could not send password reset mail. Password Could not be reset");
                }
                //user.setPassword(encrypter.encrypt(password));
                authService.resetPassword(emailOrMobile, password);
        }

        @Override
        public boolean resetPassword(String emailMobile, String password) throws ProfitMandiBusinessException {
                User user = userRepository.selectByEmailId(emailMobile);
                user.setPassword(encrypter.encrypt(password));
                return true;
        }

        @Override
        public boolean changePassword(User user, String password) throws ProfitMandiBusinessException {
                authService.resetPassword(user.getEmailId(), password);
                return true;
        }

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