refactored feed updating

This commit is contained in:
Athou
2013-04-25 14:43:52 +02:00
parent 9ea1278823
commit 50585b3ff9

View File

@@ -5,7 +5,6 @@ import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import org.apache.commons.lang.ObjectUtils;
@@ -23,7 +22,6 @@ import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedSubscription;
import com.google.common.collect.Lists;
@Stateless
public class FeedUpdateService {
@Inject
@@ -39,26 +37,21 @@ public class FeedUpdateService {
FeedEntryStatusDAO feedEntryStatusDAO;
public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
List<String> guids = Lists.newArrayList();
for (FeedEntry entry : entries) {
guids.add(entry.getGuid());
}
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
: feedEntryDAO.findByGuids(guids);
List<FeedEntry> existingEntries = getExistingEntries(entries);
List<FeedSubscription> subscriptions = feedSubscriptionDAO
.findByFeed(feed);
List<FeedEntry> entryUpdateList = Lists.newArrayList();
List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
for (FeedEntry entry : entries) {
FeedEntry foundEntry = null;
for (FeedEntry existingEntry : existingEntries) {
if (StringUtils
.equals(entry.getGuid(), existingEntry.getGuid())) {
foundEntry = existingEntry;
break;
}
}
FeedEntry foundEntry = findEntry(existingEntries, entry);
if (foundEntry == null) {
FeedEntryContent content = entry.getContent();
content.setContent(FeedUtils.handleContent(content.getContent()));
content.setContent(FeedUtils.handleContent(content.getContent()));
String title = FeedUtils.handleContent(content.getTitle());
if (title != null) {
content.setTitle(title.substring(0,
@@ -66,7 +59,8 @@ public class FeedUpdateService {
}
entry.setInserted(Calendar.getInstance().getTime());
addFeedToEntry(entry, feed);
entry.getFeeds().add(feed);
entryUpdateList.add(entry);
} else {
boolean foundFeed = false;
for (Feed existingFeed : foundEntry.getFeeds()) {
@@ -77,24 +71,44 @@ public class FeedUpdateService {
}
if (!foundFeed) {
addFeedToEntry(foundEntry, feed);
foundEntry.getFeeds().add(feed);
entryUpdateList.add(entry);
}
}
}
}
private void addFeedToEntry(FeedEntry entry, Feed feed) {
entry.getFeeds().add(feed);
feedEntryDAO.saveOrUpdate(entry);
List<FeedSubscription> subscriptions = feedSubscriptionDAO
.findByFeed(feed);
for (FeedSubscription sub : subscriptions) {
FeedEntryStatus status = new FeedEntryStatus();
status.setEntry(entry);
status.setSubscription(sub);
feedEntryStatusDAO.save(status);
for (FeedEntry entry : entryUpdateList) {
for (FeedSubscription sub : subscriptions) {
FeedEntryStatus status = new FeedEntryStatus();
status.setEntry(entry);
status.setSubscription(sub);
statusUpdateList.add(status);
}
}
feedEntryDAO.saveOrUpdate(entryUpdateList);
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
}
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
FeedEntry foundEntry = null;
for (FeedEntry existingEntry : existingEntries) {
if (StringUtils.equals(entry.getGuid(), existingEntry.getGuid())) {
foundEntry = existingEntry;
break;
}
}
return foundEntry;
}
private List<FeedEntry> getExistingEntries(Collection<FeedEntry> entries) {
List<String> guids = Lists.newArrayList();
for (FeedEntry entry : entries) {
guids.add(entry.getGuid());
}
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
: feedEntryDAO.findByGuids(guids);
return existingEntries;
}
}