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