Spring Service Method Logic

I am learning spring and am wondering about this service class's method. Is there a way to split this method up? Is there a way to use spring framework to make this method more readable? Should I move any of it to the controller or the DAO or should I split it into more service methods?

// UserBean has fields: username, password, first name, last name, etc.
public UserBean getUserDetails(UserBean bean) {
UserEntity entity = new UserEntity();
BeanUtils.copyProperties(bean, entity);

entity = this.userDao.getUser(entity);

if (entity == null) {
return null;
}

// Fill in the rest of the UserBean's data from the DB
UserBean userBean = new UserBean();
BeanUtils.copyProperties(entity, userBean);
// calculate time since last login based on DB timestamp

// update DB with new timestamp
this.userDao.updateUser(entity);

return userBean;
}

• Are you shure that you need to use copyProperties twice or at least at once? As fo me It can be ommited. Oct 1 '14 at 5:48
• How do you omit it?
– rys
Oct 1 '14 at 12:38

Perhaps if this method:

this.userDao.getUser(entity);


Accepted just a String as input (the username?) and possibly a password, you could then call it without having to mess around creating the UserEntity beans.

So this:

UserEntity entity = new UserEntity();
BeanUtils.copyProperties(bean, entity);

entity = this.userDao.getUser(entity);


Would become

UserEntity entity = this.userDao.getUser(bean.getUserName());


Maybe rename the entity/bean variable too to some thing that makes it a bit clearer what they are.

Also in the second half of the method, there seems to be no need to copy the properties to a UserBean and then back again. Just update the time since last login on the UserEntity directly.

• Can you add code for the last paragraph? Not following it exactly.
– rys
Oct 1 '14 at 12:39