2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
import javax.ejb.Stateless;
|
|
|
|
|
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-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
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-04-08 14:28:31 +02:00
|
|
|
import com.commafeed.frontend.utils.ModelFactory.MF;
|
2013-03-25 23:33:06 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2013-04-08 14:28:31 +02:00
|
|
|
import com.uaihebert.model.EasyCriteria;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Stateless
|
2013-03-25 12:24:00 +01:00
|
|
|
@SuppressWarnings("serial")
|
2013-03-30 11:37:57 +01:00
|
|
|
public class FeedEntryService extends GenericDAO<FeedEntry> {
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedService feedService;
|
|
|
|
|
|
2013-04-08 07:54:46 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionService feedSubscriptionService;
|
|
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
public void updateEntries(String url, Collection<FeedEntry> entries) {
|
2013-04-08 13:06:53 +02:00
|
|
|
Feed feed = feedService.findByUrl(url);
|
2013-03-25 23:33:06 +01:00
|
|
|
List<String> guids = Lists.newArrayList();
|
2013-03-20 20:33:42 +01:00
|
|
|
for (FeedEntry entry : entries) {
|
2013-03-25 23:33:06 +01:00
|
|
|
guids.add(entry.getGuid());
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 20:41:16 +02:00
|
|
|
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
|
|
|
|
|
: getByGuids(guids);
|
2013-03-25 23:33:06 +01:00
|
|
|
for (FeedEntry entry : entries) {
|
2013-04-07 12:01:50 +02:00
|
|
|
FeedEntry foundEntry = null;
|
2013-03-25 23:33:06 +01:00
|
|
|
for (FeedEntry existingEntry : existingEntries) {
|
|
|
|
|
if (StringUtils
|
|
|
|
|
.equals(entry.getGuid(), existingEntry.getGuid())) {
|
2013-04-07 12:01:50 +02:00
|
|
|
foundEntry = existingEntry;
|
2013-03-25 23:33:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-04-07 12:01:50 +02:00
|
|
|
if (foundEntry == null) {
|
2013-04-09 11:10:26 +02:00
|
|
|
entry.setInserted(Calendar.getInstance().getTime());
|
2013-04-08 07:54:46 +02:00
|
|
|
addFeedToEntry(entry, feed);
|
2013-04-07 12:01:50 +02:00
|
|
|
} else {
|
|
|
|
|
boolean foundFeed = false;
|
|
|
|
|
for (Feed existingFeed : foundEntry.getFeeds()) {
|
|
|
|
|
if (ObjectUtils.equals(existingFeed.getId(), feed.getId())) {
|
|
|
|
|
foundFeed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!foundFeed) {
|
2013-04-08 07:54:46 +02:00
|
|
|
addFeedToEntry(foundEntry, feed);
|
2013-04-07 12:01:50 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-25 23:33:06 +01:00
|
|
|
|
2013-03-20 20:33:42 +01:00
|
|
|
feed.setLastUpdated(Calendar.getInstance().getTime());
|
2013-03-25 12:24:00 +01:00
|
|
|
feed.setMessage(null);
|
2013-04-09 09:03:52 +02:00
|
|
|
feed.setErrorCount(0);
|
2013-03-25 23:33:06 +01:00
|
|
|
feedService.update(feed);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-08 07:54:46 +02:00
|
|
|
private void addFeedToEntry(FeedEntry entry, Feed feed) {
|
|
|
|
|
entry.getFeeds().add(feed);
|
2013-04-08 13:06:53 +02:00
|
|
|
saveOrUpdate(entry);
|
2013-04-08 07:54:46 +02:00
|
|
|
List<FeedSubscription> subscriptions = feedSubscriptionService
|
|
|
|
|
.findByFeed(feed);
|
|
|
|
|
for (FeedSubscription sub : subscriptions) {
|
|
|
|
|
FeedEntryStatus status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
2013-04-08 13:06:53 +02:00
|
|
|
status.setSubscription(sub);
|
2013-04-08 07:54:46 +02:00
|
|
|
em.persist(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 23:33:06 +01:00
|
|
|
public List<FeedEntry> getByGuids(List<String> guids) {
|
2013-04-08 14:28:31 +02:00
|
|
|
EasyCriteria<FeedEntry> criteria = createCriteria();
|
|
|
|
|
criteria.setDistinctTrue();
|
|
|
|
|
criteria.andStringIn(MF.i(proxy().getGuid()), guids);
|
|
|
|
|
criteria.leftJoinFetch(MF.i(proxy().getFeeds()));
|
|
|
|
|
return criteria.getResultList();
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|