package com.gtp.service;
import java.util.List;
import com.gtp.dao.UserDao;
import com.gtp.domain.User;
import commons.ExistedException;
import commons.NotFoundException;
public class UserServiceImp implements UserService {
UserDao userDao;
public User addUser(User u) throws ExistedException {
if(u==null)
throw new NullPointerException("user should not be null");
User user=userDao.findByEmailName(u.getEmail());
if(user!=null)
throw new ExistedException();
return userDao.addUser(u);
}
@Override
public List<User> findAllUsers() {
return userDao.getUsers();
}
@Override
public User removeUser(User user) throws NotFoundException {
if(user==null)
throw new NullPointerException("user should not be null");
User ua=userDao.findByEmailName(user.getEmail());
if(ua==null)
throw new NotFoundException();
return userDao.removeUser(user);
}
@Override
public User updateUser(User user) throws NotFoundException, ExistedException {
if(user==null)
throw new NullPointerException("user should not be null");
User us=userDao.findByEmailName(user.getEmail());
if( us!=null && user.getId()!=us.getId())
throw new ExistedException();
return userDao.updateUser(user);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao=userDao;
}
@Override
public User findUser(String email) throws NotFoundException {
User user=userDao.findByEmailName(email);
if(user==null)
throw new NotFoundException();
return user;
}
@Override
public User updateUserPassword(User user) throws NotFoundException,ExistedException{
return updateUser(user);
}
}