// An account can also be retrieved from the DataStore,
// like the way we do it to get an Application or Directory object,
// if the account's Rest URL is known to the application.
User sessionUser = (User) session.getAttribute("sessionUser");
Account account = sessionUser.getAccount();
account.setGivenName(user.getFirstName());
account.setSurname(user.getLastName());
account.setEmail(user.getEmail());
account.setUsername(user.getFirstName().toLowerCase() + user.getLastName().toLowerCase());
String existingGroupUrl = null;
if (account.getGroupMemberships().iterator().hasNext()) {
GroupMembership groupMembership = account.getGroupMemberships().iterator().next();
existingGroupUrl = groupMembership.getGroup().getHref();
if (!existingGroupUrl.equals(user.getGroupUrl())) {
groupMembership.delete();
existingGroupUrl = null;
}
}
if (user.getGroupUrl() != null && !user.getGroupUrl().isEmpty() && existingGroupUrl == null) {
account.addGroup(stormpath.getDataStore().getResource(user.getGroupUrl(), Group.class));
}
account.save();
user.setAccount(account);
user.setUserName(sessionUser.getUserName());
user.setTootList(sessionUser.getTootList());