Kawai Wiki

Compare Revisions

Ignore whitespace Rev 189 → Rev 190

/trunk/src/main/java/org/kawai/dao/jdbc/JdbcUserDAO.java
6,114 → 6,146
import org.kawai.model.User;
import org.mentabean.BeanSession;
 
/**
* DAO class JdbcUserDAO.
*
* @author Sergio Oliveira
*
*/
public class JdbcUserDAO extends AbstractJdbcDAO implements UserDAO {
 
public JdbcUserDAO(BeanSession session) {
super(session);
}
@Override
public User load(int id) {
User u = new User();
u.setId(id);
try {
/**
* Parametric constructor.
* @param session BeanSession
*/
public JdbcUserDAO(BeanSession session) {
super(session);
}
 
if (session.load(u)) {
return u;
}
return null;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void update(User u) {
try {
session.update(u);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void insert(User u) {
try {
session.insert(u);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
private List<User> find(User u) {
try {
return session.loadList(u);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean insertOrUpdate(User u) {
String username = u.getUsername();
User user = findByUsername(username);
if (user != null) {
u.setId(user.getId());
update(u);
return false;
} else {
insert(u);
return true;
}
}
@Override
public User findByUsername(String username) {
User u = new User();
u.setUsername(username);
List<User> list = find(u);
if (list == null || list.isEmpty()) {
return null;
} else if (list.size() > 1) {
throw new IllegalStateException("More than one user with the same username!");
} else {
return list.get(0);
}
}
}
/**
* Loads the user object by id.
* @return User
*/
@Override
public User load(int id) {
 
User u = new User();
u.setId(id);
 
try {
 
if (session.load(u)) {
 
return u;
}
 
return null;
 
} catch(Exception e) {
 
throw new RuntimeException(e);
}
}
 
/**
* Updates the user object.
*/
@Override
public void update(User u) {
 
try {
 
session.update(u);
 
} catch(Exception e) {
 
throw new RuntimeException(e);
}
}
 
/**
* Persist the user ojbect.
*/
@Override
public void insert(User u) {
 
try {
 
session.insert(u);
 
} catch(Exception e) {
 
throw new RuntimeException(e);
}
}
 
/**
* Load a list of user object.
* @param u User
* @return List < User >
*/
private List<User> find(User u) {
 
try {
 
return session.loadList(u);
 
} catch(Exception e) {
 
throw new RuntimeException(e);
}
}
 
/**
* Persist or update the user object.
*/
@Override
public boolean insertOrUpdate(User u) {
 
String username = u.getUsername();
 
User user = findByUsername(username);
 
if (user != null) {
 
u.setId(user.getId());
 
update(u);
 
return false;
 
} else {
 
insert(u);
 
return true;
}
}
 
/**
* Find user by username.
* @return User
*/
@Override
public User findByUsername(String username) {
 
User u = new User();
u.setUsername(username);
 
List<User> list = find(u);
 
if (list == null || list.isEmpty()) {
 
return null;
 
} else if (list.size() > 1) {
 
throw new IllegalStateException("More than one user with the same username!");
 
} else {
 
return list.get(0);
}
}
 
}