* @param id
*/
public void updateBlog(long id){
DataManager dm = DataManagerFactory.getInstance();
PersistenceManager pm = dm.newPersistenceManager();
//find blog
Blog blog = pm.getObjectById(Blog.class,id);
//check if blog has any updates
BlogUpdateResult result = null;
//fetch channel on the web via RSS
Channel chan = FeedManager.loadRss(blog.getRss());
if (chan != null){
//update blog. Returns true if new items were added
result = updateBlog(pm, blog, chan);
//test url and title and correct if worng
if (chan.getLink() != null && !chan.getLink().equalsIgnoreCase(blog.getLink())){
blog.setLink(chan.getLink());
}
if (chan.getTitle() != null && !chan.getTitle().equalsIgnoreCase(blog.getTitle())){
blog.setTitle(chan.getTitle());
}
}
//Set last post entry date to blog
if (result != null && result.isUpdate())
blog.setLatestEntry(result.getLastestEntryDate());
String messageLog = "";
//build nextUpdate
//if no new update : increase interval
int newInterval = blog.getRefreshInterval();
if (result != null && result.isUpdate()){
newInterval = newInterval / 2;
messageLog = "Blog updated [" + blog.getTitle() + "]";
} else {
newInterval = newInterval * 2;
messageLog = "No new update [" + blog.getTitle() + "]";
}
if (newInterval <= MIN_INTERVAL) newInterval = MIN_INTERVAL;
if (newInterval > MAX_INTERVAL) newInterval = MAX_INTERVAL;
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, newInterval);
blog.setRefreshInterval(newInterval);
blog.setNextUpdate(cal.getTime());
pm.flush();
pm.close();
BlogCache.setNextUpdate(blog.getKey().getId(), newInterval);
Logger.getLogger("updateBlog").log(Level.INFO,
messageLog);