Database Reference
In-Depth Information
In the UserImpl class, you will notice several implemented methods to manage the User object. To add the User
object to the database, use the save method, which will first check to see if a username has already been added to the
database. If no user exists, then the user is saved to the database.
The UserImpl class, shown in Listing 11-21, will also use the @Service annotation to set the interface name as
well as provide its scope through the @Scope annotation. To save the user to the database, the application calls the
save method from UserRepository .
Listing 11-21. UserImpl
@Service("userInterface")
@Scope("prototype")
public class UserImpl extends GraphStoryService implements UserInterface {
static Logger log = Logger.getLogger(UserImpl.class);
private User tempUser;
public GraphStory save (GraphStory graphStory) throws Exception {
// trim and lower case the username
graphStory.getUser().setUsername(graphStory.getUser().getUsername()
.toLowerCase().trim());
// check to see if the username has already been taken
if (!userExists(graphStory.getUser())) {
graphStory.setUser( userRepository.save (graphStory.getUser()));
} else {
addErrorMsg (graphStory, "The username you entered already exists.");
}
return graphStory ;
}
private boolean userExists (User user) throws Exception {
boolean userFound = false;
if ( getByUserName (user.getUsername()) != null) {
userFound = true;
}
return userFound;
}
public User getByUserName (String username) throws Exception {
User u = userRepository.findByUsername(username);
return u;
}
}
 
Search WWH ::




Custom Search