package com.eactive.eai.authserver.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.eactive.eai.authserver.dao.UserDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.util.Logger;

@Service("eLinkUserDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private UserDAO userDAO;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		logger.debug("UserDetailsServiceImpl.loadUserByUsername :::: {}", username);

		UserDetails user = null;
		try {
			user = userDAO.getUserByUserId(username);
		} catch (DAOException e) {
			logger.error(e.getMessage());
		}

		if (user == null) {
			throw new UsernameNotFoundException(username + " is not found.");
		}

		return user;
	}
}
