package com.commafeed.backend.feeds;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import javax.ejb.AccessTimeout;
import javax.ejb.Asynchronous;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.commafeed.backend.dao.FeedEntryService;
import com.commafeed.backend.dao.FeedService;
import com.commafeed.backend.model.Feed;

@Stateless
public class FeedUpdater {

	private static Logger log = LoggerFactory.getLogger(FeedTimer.class);

	@Inject
	FeedFetcher fetcher;

	@Inject
	FeedService feedService;

	@Inject
	FeedEntryService feedEntryService;

	@Asynchronous
	@Lock(LockType.READ)
	@AccessTimeout(value = 4, unit = TimeUnit.SECONDS)
	public void update(Feed feed) {
		try {
			Feed fetchedFeed = fetcher.fetch(feed.getUrl());
			if (feed.getLink() == null) {
				feed.setLink(fetchedFeed.getLink());
				feedService.update(feed);
			}
			feedEntryService.updateEntries(feed.getUrl(),
					fetchedFeed.getEntries());

			feed.setMessage(null);
			feed.setErrorCount(0);
		} catch (Exception e) {
			log.info("Unable to refresh feed " + feed.getUrl() + " : "
					+ e.getMessage());
			feed.setMessage("Unable to refresh feed: " + e.getMessage());
			feed.setErrorCount(feed.getErrorCount() + 1);
		} finally {
			feed.setLastUpdated(Calendar.getInstance().getTime());
			feedService.update(feed);
		}
	}
}

@@ -1196,17 +1196,25 @@
 %0A%09%09%09
-log.info(
+String message = 
 %22Una
@@ -1285,63 +1285,64 @@
 ge()
-)
 ;%0D%0A%09%09%09
-feed.setMessage(%22Unable to refresh feed: %22 + e.g
+log.info(e.getClass() + %22 %22 + message);%0D%0A%09%09%09feed.s
 etMe
@@ -1347,17 +1347,23 @@
 Message(
-)
+message
 );%0D%0A%09%09%09f

format message