// create new persistenceManager
this.currentManager = DataManagerFactory.getInstance()
.newPersistenceManager();
//fetch user from his jabber id
User user = null;
if (id >0) {
//fetch by id
user = (User) this.currentManager.getObjectById(User.class, id);
} else {
//find by email
if(email != null){
user = this.getOrCreateUser(email);
}
}
//user error
if (user == null){
throw new IllegalArgumentException("NO user found : id="
+ id + ", email=" +email);
}
// next update
int minuteNextUpdate = user.getInterval();
if (minuteNextUpdate < 10)
minuteNextUpdate = 10;
// test user presence
JID jid = new JID(user.getId());
boolean isAvailable = UserPresence.isUserAvailable(jid.getId());
if (isAvailable) {
//user is present : do update !
// select subscriptions
Query q = this.currentManager.newQuery(Subscription.class);
q.setOrdering("lastProcessDate");
q.setRange(0, NB_SUBSCRIPTIONS_MAX);
q.setFilter("userKey == uk");
q.declareParameters("com.google.appengine.api.datastore.Key uk");
@SuppressWarnings("unchecked")
List<Subscription> subs = (List<Subscription>) q.execute(user
.getKey());
// update
for (Subscription sub : subs) {
// update is done ?
boolean updateDone = false;
// fetch blog
Blog blog = (Blog) this.currentManager.getObjectById(
Blog.class, sub.getBlogKey());
// compare dates bet blog last entry and subscription update
if (blog.getLatestEntry().after(sub.getLatestEntryNotifiedDate())) {
Logger.getLogger("UserService").info(
"user " + user.getId() + " present. Try notify : "
+ blog.getTitle());
// find next entry
BlogEntry nextEntry = this.findNextEntry(sub);
if (nextEntry == null) {
// subscription is up to date
} else {
// notify user
this.sendBlogEntry(jid, nextEntry);
// set modification is done
updateDone = true;
// set current subscription mark to entry date
sub.setLatestEntryNotifiedDate(nextEntry.getPubDate());
}
}
// Update subscription process
// last analyze date
sub.setLastProcessDate(now);
this.currentManager.currentTransaction().begin();
this.currentManager.flush();
this.currentManager.currentTransaction().commit();
// break
if (updateDone) {
// update user
user.setLastUpdate(now);
user.setLastSubscriptionKey(sub.getKey());
this.currentManager.currentTransaction().begin();
this.currentManager.flush();
this.currentManager.currentTransaction().commit();
break;
}else {
//nothing to update
Logger.getLogger("UserService").info(
"user " + user.getId() + " present but nothing to update.");
}
}
q.closeAll();
} else {
/*
* removed for XMPP issues
minuteNextUpdate = 30;
//remove from presence
UserPresence.setPresence(user.getId(), presence);
Logger.getLogger("UserService").info(
"user " + user.getId() + " not present");
*/
}
// next update
// record next update
Calendar nextTime = Calendar.getInstance();
nextTime.add(Calendar.MINUTE, minuteNextUpdate);
user.setNextUpdate(nextTime.getTime());
UserPresence.setNextUpdate(user.getId(), minuteNextUpdate);
// flush
this.currentManager.currentTransaction().begin();
this.currentManager.flush();
this.currentManager.currentTransaction().commit();