Files
Athou_commafeed/src/main/java/com/commafeed/backend/services/FeedSubscriptionService.java

118 lines
3.2 KiB
Java
Raw Normal View History

2013-04-11 20:49:08 +02:00
package com.commafeed.backend.services;
import java.util.List;
2013-07-03 12:42:01 +02:00
import java.util.Map;
2013-04-11 20:49:08 +02:00
import javax.ejb.ApplicationException;
2013-04-11 20:49:08 +02:00
import javax.inject.Inject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2013-07-03 12:42:01 +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;
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
import com.commafeed.backend.feeds.FeedUtils;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedSubscription;
2013-07-03 12:42:01 +02:00
import com.commafeed.backend.model.Models;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.model.User;
2013-07-23 16:32:20 +02:00
import com.google.api.client.util.Maps;
2013-04-11 20:49:08 +02:00
public class FeedSubscriptionService {
2013-05-20 21:53:13 +02:00
2013-07-25 09:17:33 +02:00
private static Logger log = LoggerFactory.getLogger(FeedSubscriptionService.class);
2013-05-20 21:53:13 +02:00
@SuppressWarnings("serial")
@ApplicationException
2013-05-20 21:53:13 +02:00
public static class FeedSubscriptionException extends RuntimeException {
public FeedSubscriptionException(String msg) {
super(msg);
}
}
2013-04-11 20:49:08 +02:00
@Inject
2013-04-21 11:24:45 +02:00
FeedService feedService;
2013-04-11 20:49:08 +02:00
@Inject
FeedEntryDAO feedEntryDAO;
@Inject
FeedEntryStatusDAO feedEntryStatusDAO;
@Inject
FeedSubscriptionDAO feedSubscriptionDAO;
2013-04-30 22:05:38 +02:00
@Inject
ApplicationSettingsService applicationSettingsService;
@Inject
FeedRefreshTaskGiver taskGiver;
2013-04-11 20:49:08 +02:00
2013-07-03 12:42:01 +02:00
@Inject
CacheService cache;
2013-07-25 09:17:33 +02:00
public Feed subscribe(User user, String url, String title, FeedCategory category) {
2013-04-11 20:49:08 +02:00
final String pubUrl = applicationSettingsService.get().getPublicUrl();
if (StringUtils.isBlank(pubUrl)) {
2013-07-25 09:17:33 +02:00
throw new FeedSubscriptionException("Public URL of this CommaFeed instance is not set");
}
if (url.startsWith(pubUrl)) {
2013-07-25 09:17:33 +02:00
throw new FeedSubscriptionException("Could not subscribe to a feed from this CommaFeed instance");
2013-04-30 22:05:38 +02:00
}
2013-04-21 11:24:45 +02:00
Feed feed = feedService.findOrCreate(url);
2013-04-11 20:49:08 +02:00
FeedSubscription sub = feedSubscriptionDAO.findByFeed(user, feed);
if (sub == null) {
sub = new FeedSubscription();
sub.setFeed(feed);
sub.setUser(user);
}
sub.setCategory(category);
2013-06-04 10:11:16 +02:00
sub.setPosition(0);
sub.setTitle(FeedUtils.truncate(title, 128));
2013-04-11 20:49:08 +02:00
feedSubscriptionDAO.saveOrUpdate(sub);
taskGiver.add(feed, false);
2013-07-25 10:38:23 +02:00
cache.invalidateUserRootCategory(user);
return feed;
2013-04-11 20:49:08 +02:00
}
2013-07-25 10:38:23 +02:00
public boolean unsubscribe(User user, Long subId) {
2013-07-25 10:21:11 +02:00
FeedSubscription sub = feedSubscriptionDAO.findById(user, subId);
if (sub != null) {
feedSubscriptionDAO.delete(sub);
2013-07-25 10:38:23 +02:00
cache.invalidateUserRootCategory(user);
2013-07-25 10:21:11 +02:00
return true;
} else {
return false;
}
}
public Long getUnreadCount(FeedSubscription sub) {
Long count = cache.getUnreadCount(sub);
if (count == null) {
log.debug("unread count cache miss for {}", Models.getId(sub));
count = feedEntryStatusDAO.getUnreadCount(sub);
cache.setUnreadCount(sub, count);
}
return count;
}
2013-07-03 12:42:01 +02:00
public Map<Long, Long> getUnreadCount(User user) {
2013-07-25 10:21:11 +02:00
Map<Long, Long> map = Maps.newHashMap();
2013-07-25 10:38:23 +02:00
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
2013-07-25 10:21:11 +02:00
for (FeedSubscription sub : subs) {
map.put(sub.getId(), getUnreadCount(sub));
2013-07-03 12:42:01 +02:00
}
return map;
}
2013-07-25 10:21:11 +02:00
2013-04-11 20:49:08 +02:00
}