mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
refactored feed updating
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user