}
@Override
public synchronized void setEmailSubscription(String userEmail, Long livingStoryId,
boolean subscribe, String localeId) {
UserLivingStoryEntity userLivingStoryEntity =
retrieveUserLivingStoryEntity(userEmail, livingStoryId);
if (userLivingStoryEntity == null) {
// This means the user has not visited this living story before. A new row needs to be added.
// Check if a userEntity exists for this user. If not, we need to create that first.
if (retrieveUserEntity(userEmail) == null) {
createNewUserEntity(userEmail);
}
createNewUserLivingStoryEntity(userEmail, livingStoryId, subscribe, localeId);
} else {
// This means the user has visited this living story before and only the subscription needs
// to be updated.
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
// This object needs to be queried using its primary key from the same
// PersistentManager object as the one that is going to persist it
userLivingStoryEntity = pm.getObjectById(
UserLivingStoryEntity.class, userLivingStoryEntity.getId());
userLivingStoryEntity.setSubscribedToEmails(subscribe);
userLivingStoryEntity.setSubscriptionLocale(localeId);
pm.makePersistent(userLivingStoryEntity);
} finally {
pm.close();
}
}