Files
Athou_commafeed/src/main/java/com/commafeed/backend/services/FeedUpdateService.java

75 lines
2.2 KiB
Java
Raw Normal View History

2013-04-11 20:49:08 +02:00
package com.commafeed.backend.services;
2013-03-20 20:33:42 +01:00
import java.util.Calendar;
2013-03-22 09:29:30 +01:00
import java.util.List;
2013-03-20 20:33:42 +01:00
2013-05-22 00:07:13 +02:00
import javax.ejb.Stateless;
2013-03-20 20:33:42 +01:00
import javax.inject.Inject;
2013-05-18 00:10:11 +02:00
import com.commafeed.backend.MetricsBean;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.dao.FeedEntryDAO;
2013-06-23 15:14:14 +02:00
import com.commafeed.backend.dao.FeedEntryDAO.EntryWithFeed;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.dao.FeedEntryStatusDAO;
import com.commafeed.backend.dao.FeedSubscriptionDAO;
import com.commafeed.backend.feeds.FeedUtils;
2013-03-23 16:17:19 +01:00
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryContent;
import com.commafeed.backend.model.FeedEntryStatus;
2013-04-08 07:54:46 +02:00
import com.commafeed.backend.model.FeedSubscription;
2013-03-25 23:33:06 +01:00
import com.google.common.collect.Lists;
2013-03-20 20:33:42 +01:00
2013-05-22 00:07:13 +02:00
@Stateless
2013-04-11 20:49:08 +02:00
public class FeedUpdateService {
2013-03-20 20:33:42 +01:00
2013-04-08 07:54:46 +02:00
@Inject
2013-04-11 20:49:08 +02:00
FeedSubscriptionDAO feedSubscriptionDAO;
@Inject
FeedEntryDAO feedEntryDAO;
@Inject
FeedEntryStatusDAO feedEntryStatusDAO;
2013-04-08 07:54:46 +02:00
2013-05-18 00:10:11 +02:00
@Inject
MetricsBean metricsBean;
2013-05-22 00:07:13 +02:00
public void updateEntry(Feed feed, FeedEntry entry,
List<FeedSubscription> subscriptions) {
2013-06-23 15:14:14 +02:00
EntryWithFeed existing = feedEntryDAO.findExisting(entry.getGuid(),
entry.getUrl(), feed.getId());
2013-05-22 00:07:13 +02:00
2013-05-22 09:38:32 +02:00
FeedEntry update = null;
2013-06-23 15:14:14 +02:00
if (existing == null) {
FeedEntryContent content = entry.getContent();
content.setTitle(FeedUtils.truncate(FeedUtils.handleContent(
content.getTitle(), feed.getLink(), true), 2048));
content.setContent(FeedUtils.handleContent(content.getContent(),
feed.getLink(), false));
2013-05-22 00:07:13 +02:00
entry.setInserted(Calendar.getInstance().getTime());
entry.getFeeds().add(feed);
2013-05-22 09:38:32 +02:00
update = entry;
2013-06-23 15:14:14 +02:00
} else if (existing.feed == null) {
existing.entry.getFeeds().add(feed);
update = existing.entry;
}
2013-05-22 00:07:13 +02:00
2013-05-22 09:38:32 +02:00
if (update != null) {
2013-05-22 00:07:13 +02:00
List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
for (FeedSubscription sub : subscriptions) {
FeedEntryStatus status = new FeedEntryStatus();
2013-05-22 09:38:32 +02:00
status.setEntry(update);
status.setSubscription(sub);
statusUpdateList.add(status);
2013-04-25 14:43:52 +02:00
}
2013-05-22 09:38:32 +02:00
feedEntryDAO.saveOrUpdate(update);
2013-05-22 00:07:13 +02:00
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
metricsBean.entryUpdated(statusUpdateList.size());
}
2013-03-25 23:33:06 +01:00
}
2013-03-20 20:33:42 +01:00
}