2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2013-04-14 18:12:19 +02:00
|
|
|
import javax.ejb.Lock;
|
|
|
|
|
import javax.ejb.LockType;
|
|
|
|
|
import javax.ejb.Singleton;
|
2013-04-11 20:49:08 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-04-14 18:28:48 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.dao.FeedDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
|
|
|
|
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-14 18:12:19 +02:00
|
|
|
@Singleton
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedSubscriptionService {
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedDAO feedDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryDAO feedEntryDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
2013-04-14 18:12:19 +02:00
|
|
|
@Lock(LockType.WRITE)
|
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) {
|
|
|
|
|
|
|
|
|
|
Feed feed = feedDAO.findByUrl(url);
|
|
|
|
|
if (feed == null) {
|
|
|
|
|
feed = new Feed();
|
|
|
|
|
feed.setUrl(url);
|
2013-04-14 18:28:48 +02:00
|
|
|
feed.setUrlHash(DigestUtils.sha1Hex(url));
|
2013-04-11 20:49:08 +02:00
|
|
|
feedDAO.save(feed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
status.setRead(true);
|
|
|
|
|
status.setSubscription(sub);
|
|
|
|
|
statuses.add(status);
|
|
|
|
|
}
|
|
|
|
|
feedEntryStatusDAO.save(statuses);
|
|
|
|
|
}
|
2013-04-16 07:26:24 +02:00
|
|
|
return feed;
|
2013-04-11 20:49:08 +02:00
|
|
|
}
|
|
|
|
|
}
|