2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-06-25 16:57:48 +02:00
|
|
|
import java.util.Date;
|
2013-03-22 09:29:30 +01:00
|
|
|
import java.util.List;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
import javax.ejb.Stateless;
|
2013-03-20 20:33:42 +01:00
|
|
|
import javax.inject.Inject;
|
2013-07-04 23:40:25 +02:00
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
|
import javax.persistence.PersistenceContext;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-07-24 15:39:20 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
|
2013-05-18 00:10:11 +02:00
|
|
|
import com.commafeed.backend.MetricsBean;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.commafeed.backend.cache.CacheService;
|
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-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
2013-05-29 16:42:45 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryContent;
|
2013-04-08 07:54:46 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.commafeed.backend.model.User;
|
2013-03-25 23:33:06 +01:00
|
|
|
import com.google.common.collect.Lists;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-05-22 00:07:13 +02:00
|
|
|
@Stateless
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedUpdateService {
|
2013-07-25 09:17:33 +02:00
|
|
|
|
2013-07-04 23:40:25 +02:00
|
|
|
@PersistenceContext
|
|
|
|
|
protected EntityManager em;
|
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-07-02 18:07:08 +02:00
|
|
|
@Inject
|
|
|
|
|
CacheService cache;
|
2013-07-25 09:17:33 +02:00
|
|
|
|
2013-07-24 15:39:20 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedEntryContentService feedEntryContentService;
|
2013-07-02 18:07:08 +02:00
|
|
|
|
2013-07-24 15:39:20 +02:00
|
|
|
/**
|
|
|
|
|
* this is NOT thread-safe
|
|
|
|
|
*/
|
2013-07-25 09:17:33 +02:00
|
|
|
public void updateEntry(Feed feed, FeedEntry entry, List<FeedSubscription> subscriptions) {
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
FeedEntry existing = feedEntryDAO.findExisting(entry.getGuid(), entry.getUrl(), feed.getId());
|
2013-07-24 15:39:20 +02:00
|
|
|
if (existing != null) {
|
|
|
|
|
return;
|
2013-04-26 18:53:48 +02:00
|
|
|
}
|
2013-05-22 00:07:13 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
FeedEntryContent content = feedEntryContentService.findOrCreate(entry.getContent(), feed.getLink());
|
2013-07-24 15:39:20 +02:00
|
|
|
entry.setGuidHash(DigestUtils.sha1Hex(entry.getGuid()));
|
|
|
|
|
entry.setContent(content);
|
|
|
|
|
entry.setInserted(new Date());
|
|
|
|
|
entry.setFeed(feed);
|
|
|
|
|
|
|
|
|
|
List<User> users = Lists.newArrayList();
|
|
|
|
|
for (FeedSubscription sub : subscriptions) {
|
|
|
|
|
User user = sub.getUser();
|
|
|
|
|
users.add(user);
|
2013-04-25 17:26:07 +02:00
|
|
|
}
|
2013-07-24 15:39:20 +02:00
|
|
|
feedEntryDAO.saveOrUpdate(entry);
|
2013-07-24 15:53:21 +02:00
|
|
|
metricsBean.entryInserted();
|
2013-07-25 10:21:11 +02:00
|
|
|
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
|
2013-07-25 10:38:23 +02:00
|
|
|
cache.invalidateUserRootCategory(users.toArray(new User[0]));
|
2013-03-25 23:33:06 +01:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|