Files
Athou_commafeed/src/main/java/com/commafeed/backend/dao/FeedEntryService.java

91 lines
2.4 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.dao;
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-03-22 09:29:30 +01:00
import javax.persistence.TypedQuery;
2013-03-20 20:33:42 +01:00
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;
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
@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) {
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());
}
List<FeedEntry> existingEntries = getByGuids(guids);
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-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-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);
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);
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) {
TypedQuery<FeedEntry> query = em.createNamedQuery("Entry.byGuids",
FeedEntry.class);
query.setParameter("guids", guids);
return query.getResultList();
2013-03-20 20:33:42 +01:00
}
}