2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2013-04-21 11:24:45 +02:00
|
|
|
import javax.ejb.Stateless;
|
2013-04-11 20:49:08 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-04-25 14:41:56 +02:00
|
|
|
import com.commafeed.backend.feeds.FeedRefreshTaskGiver;
|
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.FeedEntry;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntryStatus;
|
|
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
|
|
|
|
import com.commafeed.backend.model.User;
|
|
|
|
|
import com.google.api.client.util.Lists;
|
|
|
|
|
|
2013-04-21 11:24:45 +02:00
|
|
|
@Stateless
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedSubscriptionService {
|
|
|
|
|
|
|
|
|
|
@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-04-16 07:26:24 +02:00
|
|
|
public Feed subscribe(User user, String url, String title,
|
2013-04-11 20:49:08 +02:00
|
|
|
FeedCategory category) {
|
|
|
|
|
|
2013-04-30 22:05:38 +02:00
|
|
|
if (url.startsWith(applicationSettingsService.get().getPublicUrl())) {
|
|
|
|
|
throw new RuntimeException(
|
|
|
|
|
"Could not subscribe to a feed from this CommaFeed instance");
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
boolean newSubscription = false;
|
|
|
|
|
if (sub == null) {
|
|
|
|
|
sub = new FeedSubscription();
|
|
|
|
|
sub.setFeed(feed);
|
|
|
|
|
sub.setUser(user);
|
|
|
|
|
newSubscription = true;
|
|
|
|
|
}
|
|
|
|
|
sub.setCategory(category);
|
2013-04-14 22:40:13 +02:00
|
|
|
sub.setTitle(title.substring(0, Math.min(128, title.length())));
|
2013-04-11 20:49:08 +02:00
|
|
|
feedSubscriptionDAO.saveOrUpdate(sub);
|
|
|
|
|
|
|
|
|
|
if (newSubscription) {
|
|
|
|
|
List<FeedEntryStatus> statuses = Lists.newArrayList();
|
2013-04-13 09:17:38 +02:00
|
|
|
List<FeedEntry> allEntries = feedEntryDAO.findByFeed(feed, 0, 10);
|
2013-04-11 20:49:08 +02:00
|
|
|
for (FeedEntry entry : allEntries) {
|
|
|
|
|
FeedEntryStatus status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
2013-04-16 07:30:40 +02:00
|
|
|
status.setRead(false);
|
2013-04-11 20:49:08 +02:00
|
|
|
status.setSubscription(sub);
|
|
|
|
|
statuses.add(status);
|
|
|
|
|
}
|
|
|
|
|
feedEntryStatusDAO.save(statuses);
|
|
|
|
|
}
|
2013-04-25 14:41:56 +02:00
|
|
|
taskGiver.add(feed);
|
2013-04-16 07:26:24 +02:00
|
|
|
return feed;
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
}
|