2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-08 20:41:16 +02:00
|
|
|
import java.util.ArrayList;
|
2013-03-20 20:33:42 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Collection;
|
2013-03-22 09:29:30 +01:00
|
|
|
import java.util.List;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
import javax.ejb.Lock;
|
|
|
|
|
import javax.ejb.LockType;
|
|
|
|
|
import javax.ejb.Singleton;
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-04-07 12:01:50 +02:00
|
|
|
import org.apache.commons.lang.ObjectUtils;
|
2013-03-25 23:33:06 +01:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
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;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-04-22 13:20:17 +02:00
|
|
|
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;
|
2013-04-14 18:51:12 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryContent;
|
2013-03-26 16:02:26 +01:00
|
|
|
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-04-26 18:53:48 +02:00
|
|
|
@Singleton
|
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-04-26 18:53:48 +02:00
|
|
|
@Lock(LockType.WRITE)
|
2013-04-14 18:12:35 +02:00
|
|
|
public void updateEntries(Feed feed, Collection<FeedEntry> entries) {
|
2013-03-25 23:33:06 +01:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
List<FeedEntry> existingEntries = getExistingEntries(entries);
|
|
|
|
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO
|
|
|
|
|
.findByFeed(feed);
|
2013-04-25 14:43:52 +02:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
List<FeedEntry> entryUpdateList = Lists.newArrayList();
|
|
|
|
|
List<FeedEntryStatus> statusUpdateList = Lists.newArrayList();
|
|
|
|
|
for (FeedEntry entry : entries) {
|
2013-04-25 14:43:52 +02:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
FeedEntry foundEntry = findEntry(existingEntries, entry);
|
2013-04-16 12:57:40 +02:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
if (foundEntry == null) {
|
2013-05-17 14:33:21 +02:00
|
|
|
String baseUri = feed.getLink();
|
2013-04-26 18:53:48 +02:00
|
|
|
FeedEntryContent content = entry.getContent();
|
2013-04-14 18:51:12 +02:00
|
|
|
|
2013-05-17 14:33:21 +02:00
|
|
|
content.setContent(FeedUtils.handleContent(
|
|
|
|
|
content.getContent(), baseUri));
|
|
|
|
|
String title = FeedUtils.handleContent(content.getTitle(),
|
|
|
|
|
baseUri);
|
2013-04-26 18:53:48 +02:00
|
|
|
if (title != null) {
|
|
|
|
|
content.setTitle(title.substring(0,
|
|
|
|
|
Math.min(2048, title.length())));
|
|
|
|
|
}
|
2013-04-07 12:01:50 +02:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
entry.setInserted(Calendar.getInstance().getTime());
|
|
|
|
|
entry.getFeeds().add(feed);
|
|
|
|
|
entryUpdateList.add(entry);
|
|
|
|
|
} else {
|
|
|
|
|
boolean foundFeed = false;
|
|
|
|
|
for (Feed existingFeed : foundEntry.getFeeds()) {
|
|
|
|
|
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
|
|
|
|
|
foundFeed = true;
|
|
|
|
|
break;
|
2013-04-25 17:26:07 +02:00
|
|
|
}
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
2013-04-25 17:26:07 +02:00
|
|
|
|
2013-04-26 18:53:48 +02:00
|
|
|
if (!foundFeed) {
|
|
|
|
|
foundEntry.getFeeds().add(feed);
|
|
|
|
|
entryUpdateList.add(foundEntry);
|
2013-04-07 12:01:50 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
|
|
|
|
for (FeedEntry entry : entryUpdateList) {
|
|
|
|
|
for (FeedSubscription sub : subscriptions) {
|
|
|
|
|
FeedEntryStatus status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
|
|
|
|
status.setSubscription(sub);
|
|
|
|
|
statusUpdateList.add(status);
|
2013-04-25 14:43:52 +02:00
|
|
|
}
|
2013-04-25 17:26:07 +02:00
|
|
|
}
|
2013-04-26 18:53:48 +02:00
|
|
|
|
|
|
|
|
feedEntryDAO.saveOrUpdate(entryUpdateList);
|
|
|
|
|
feedEntryStatusDAO.saveOrUpdate(statusUpdateList);
|
2013-05-18 09:38:34 +02:00
|
|
|
metricsBean.feedUpdated(entryUpdateList.size());
|
2013-03-25 23:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
2013-04-25 14:43:52 +02:00
|
|
|
private FeedEntry findEntry(List<FeedEntry> existingEntries, FeedEntry entry) {
|
|
|
|
|
FeedEntry foundEntry = null;
|
|
|
|
|
for (FeedEntry existingEntry : existingEntries) {
|
2013-05-09 06:13:20 +02:00
|
|
|
if (StringUtils.equals(entry.getGuid(), existingEntry.getGuid())
|
|
|
|
|
&& StringUtils.equals(entry.getUrl(),
|
|
|
|
|
existingEntry.getUrl())) {
|
2013-04-25 14:43:52 +02:00
|
|
|
foundEntry = existingEntry;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-04-08 07:54:46 +02:00
|
|
|
}
|
2013-04-25 14:43:52 +02:00
|
|
|
return foundEntry;
|
2013-04-08 07:54:46 +02:00
|
|
|
}
|
2013-04-14 18:51:12 +02:00
|
|
|
|
2013-04-25 14:43:52 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|