Query jdoQuery = pm.newQuery(UserInfo.class);
jdoQuery.declareParameters("String userIdParam");
jdoQuery.setFilter("userId == userIdParam");
log.info("Querying for user " + currentEmail + " by id " + loggedInUserId);
UserInfo user = queryAndReturnFirst(jdoQuery, loggedInUserId);
if (user != null) {
// Update email address if missing or different.
if (!currentEmail.equals(user.getCurrentEmail())) {
log.info("Adding or updating email for " + currentEmail + " id " + loggedInUserId);
user.setCurrentEmail(currentEmail);
pm.makePersistent(user);
}
} else {
// Try to find by email instead.
log.info("Querying for user " + currentEmail + " by email instead.");
jdoQuery = pm.newQuery(UserInfo.class);
jdoQuery.declareParameters("String currentEmailParam");
jdoQuery.setFilter("currentEmail == currentEmailParam");
user = queryAndReturnFirst(jdoQuery, currentEmail);
if (user != null) {
// Add the user's user ID since we now know it.
if (user.getUserId() == null || user.getUserId().equals("")) {
log.info("Adding user ID for " + currentEmail + ".");
user.setUserId(appEngineUser.getUserId());
pm.makePersistent(user);
} else {
log.severe("Found a user by email but user ID was set to a different ID.");
user = null;
}
}
}
if (user == null) {
log.info("Tried to get info for " + loggedInUserId + " but no entry was found.");
if (createIfMissing) {
log.info("Creating new UserInfo for user: " + loggedInUserId);
user = new UserInfo();
user.setUserId(loggedInUserId);
user.setCurrentEmail(currentEmail);
pm.makePersistent(user);
}
}
return user;
}