2013-04-18 12:50:44 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2013-04-18 12:50:44 +02:00
|
|
|
import javax.ejb.Stateless;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
import com.commafeed.backend.cache.CacheService;
|
2013-07-19 12:02:20 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
2013-04-18 12:50:44 +02:00
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
2013-06-20 18:45:58 +02:00
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
2013-04-18 12:50:44 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryStatus;
|
2013-06-20 18:45:58 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-04-18 12:50:44 +02:00
|
|
|
import com.commafeed.backend.model.User;
|
2013-07-25 10:21:11 +02:00
|
|
|
import com.google.common.collect.Lists;
|
2013-04-18 12:50:44 +02:00
|
|
|
|
|
|
|
|
@Stateless
|
|
|
|
|
public class FeedEntryService {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
2013-07-22 16:31:29 +02:00
|
|
|
|
|
|
|
|
@Inject
|
2013-07-19 12:02:20 +02:00
|
|
|
FeedEntryDAO feedEntryDAO;
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-25 10:21:11 +02:00
|
|
|
@Inject
|
|
|
|
|
CacheService cache;
|
|
|
|
|
|
2013-08-13 09:14:41 +02:00
|
|
|
public void markEntry(User user, Long entryId, boolean read) {
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-19 12:02:20 +02:00
|
|
|
FeedEntry entry = feedEntryDAO.findById(entryId);
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-08-13 09:14:41 +02:00
|
|
|
FeedSubscription sub = feedSubscriptionDAO.findByFeed(user, entry.getFeed());
|
|
|
|
|
if (sub == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
FeedEntryStatus status = feedEntryStatusDAO.getStatus(sub, entry);
|
2013-07-24 12:13:06 +02:00
|
|
|
if (status.isMarkable()) {
|
|
|
|
|
status.setRead(read);
|
|
|
|
|
feedEntryStatusDAO.saveOrUpdate(status);
|
2013-07-25 10:21:11 +02:00
|
|
|
cache.invalidateUnreadCount(sub);
|
2013-07-25 10:38:23 +02:00
|
|
|
cache.invalidateUserRootCategory(user);
|
2013-07-24 12:13:06 +02:00
|
|
|
}
|
2013-04-18 12:50:44 +02:00
|
|
|
}
|
2013-04-29 22:37:26 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
public void starEntry(User user, Long entryId, Long subscriptionId, boolean starred) {
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
FeedSubscription sub = feedSubscriptionDAO.findById(user, subscriptionId);
|
2013-06-20 18:45:58 +02:00
|
|
|
if (sub == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-19 12:02:20 +02:00
|
|
|
FeedEntry entry = feedEntryDAO.findById(entryId);
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
FeedEntryStatus status = feedEntryStatusDAO.getStatus(sub, entry);
|
2013-07-24 15:39:20 +02:00
|
|
|
status.setStarred(starred);
|
|
|
|
|
feedEntryStatusDAO.saveOrUpdate(status);
|
2013-04-29 22:37:26 +02:00
|
|
|
}
|
2013-07-25 10:21:11 +02:00
|
|
|
|
2013-07-25 10:38:23 +02:00
|
|
|
public void markSubscriptionEntries(User user, List<FeedSubscription> subscriptions, Date olderThan) {
|
2013-08-13 09:14:41 +02:00
|
|
|
List<FeedEntryStatus> statuses = feedEntryStatusDAO
|
|
|
|
|
.findBySubscriptions(subscriptions, true, null, null, -1, -1, null, false, false);
|
2013-07-25 10:21:11 +02:00
|
|
|
markList(statuses, olderThan);
|
|
|
|
|
cache.invalidateUnreadCount(subscriptions.toArray(new FeedSubscription[0]));
|
2013-07-25 10:38:23 +02:00
|
|
|
cache.invalidateUserRootCategory(user);
|
2013-07-25 10:21:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void markStarredEntries(User user, Date olderThan) {
|
|
|
|
|
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findStarred(user, null, -1, -1, null, false);
|
|
|
|
|
markList(statuses, olderThan);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void markList(List<FeedEntryStatus> statuses, Date olderThan) {
|
|
|
|
|
List<FeedEntryStatus> list = Lists.newArrayList();
|
|
|
|
|
for (FeedEntryStatus status : statuses) {
|
|
|
|
|
if (!status.isRead()) {
|
|
|
|
|
Date inserted = status.getEntry().getInserted();
|
|
|
|
|
if (olderThan == null || inserted == null || olderThan.after(inserted)) {
|
|
|
|
|
status.setRead(true);
|
|
|
|
|
list.add(status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
feedEntryStatusDAO.saveOrUpdate(list);
|
|
|
|
|
}
|
2013-04-18 12:50:44 +02:00
|
|
|
}
|