String displayName = profileForm.getDisplayName();
TeeShirtSize teeShirtSize = profileForm.getTeeShirtSize();
// Get the Profile from the datastore if it exists
// otherwise create a new one
Profile profile = ofy().load().key(Key.create(Profile.class, userId))
.now();
if (profile == null) {
// Populate the displayName and teeShirtSize with default values
// if not sent in the request
if (displayName == null) {
displayName = extractDefaultDisplayNameFromEmail(user
.getEmail());
}
if (teeShirtSize == null) {
teeShirtSize = TeeShirtSize.NOT_SPECIFIED;
}
// Now create a new Profile entity
profile = new Profile(userId, displayName, mainEmail, teeShirtSize);
} else {
// The Profile entity already exists
// Update the Profile entity
profile.update(displayName, teeShirtSize);
}
// TODO 3
// Save the entity in the datastore
ofy().save().entity(profile).now();