2013-03-22 20:51:22 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
import java.util.Date;
|
2013-03-30 11:37:57 +01:00
|
|
|
import java.util.List;
|
2013-04-08 13:06:53 +02:00
|
|
|
import java.util.Map;
|
2013-03-30 11:37:57 +01:00
|
|
|
|
2013-03-22 20:51:22 +01:00
|
|
|
import javax.ejb.Stateless;
|
2013-06-27 23:30:16 +02:00
|
|
|
import javax.inject.Inject;
|
2013-03-23 17:17:27 +01:00
|
|
|
import javax.persistence.NoResultException;
|
2013-04-08 13:06:53 +02:00
|
|
|
import javax.persistence.Query;
|
2013-06-20 16:49:18 +02:00
|
|
|
import javax.persistence.Tuple;
|
2013-04-08 13:06:53 +02:00
|
|
|
import javax.persistence.TypedQuery;
|
2013-04-10 10:14:27 +02:00
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
2013-05-15 16:01:14 +02:00
|
|
|
import javax.persistence.criteria.Join;
|
2013-04-10 10:28:48 +02:00
|
|
|
import javax.persistence.criteria.Path;
|
2013-04-10 10:14:27 +02:00
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
2013-06-20 16:49:18 +02:00
|
|
|
import javax.persistence.criteria.Selection;
|
|
|
|
|
import javax.persistence.criteria.SetJoin;
|
2013-03-22 20:51:22 +01:00
|
|
|
|
2013-06-21 09:38:49 +02:00
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
2013-04-08 13:06:53 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-06-13 13:15:46 +02:00
|
|
|
import org.hibernate.Hibernate;
|
2013-06-20 16:49:18 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2013-04-08 13:06:53 +02:00
|
|
|
|
2013-06-20 16:49:18 +02:00
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-04-08 13:06:53 +02:00
|
|
|
import com.commafeed.backend.model.FeedCategory;
|
2013-05-15 16:01:14 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntryContent;
|
2013-04-11 10:27:20 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryContent_;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.FeedEntryStatus;
|
2013-04-10 10:14:27 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntryStatus_;
|
|
|
|
|
import com.commafeed.backend.model.FeedEntry_;
|
2013-05-15 16:01:14 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-04-10 10:14:27 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription_;
|
2013-06-20 16:49:18 +02:00
|
|
|
import com.commafeed.backend.model.Feed_;
|
2013-03-23 17:17:27 +01:00
|
|
|
import com.commafeed.backend.model.User;
|
2013-04-10 10:28:48 +02:00
|
|
|
import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
2013-06-27 23:30:16 +02:00
|
|
|
import com.commafeed.backend.services.ApplicationSettingsService;
|
2013-06-21 09:38:49 +02:00
|
|
|
import com.google.common.base.Function;
|
2013-06-20 16:49:18 +02:00
|
|
|
import com.google.common.collect.Iterables;
|
2013-06-21 09:38:49 +02:00
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
import com.google.common.collect.Maps;
|
2013-03-22 20:51:22 +01:00
|
|
|
|
|
|
|
|
@Stateless
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedEntryStatusDAO extends GenericDAO<FeedEntryStatus> {
|
2013-03-22 20:51:22 +01:00
|
|
|
|
2013-06-21 09:18:40 +02:00
|
|
|
protected static Logger log = LoggerFactory
|
2013-06-20 16:49:18 +02:00
|
|
|
.getLogger(FeedEntryStatusDAO.class);
|
|
|
|
|
|
2013-06-27 23:30:16 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-06-08 16:15:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2013-04-08 13:06:53 +02:00
|
|
|
public FeedEntryStatus findById(User user, Long id) {
|
2013-04-08 14:28:31 +02:00
|
|
|
|
2013-06-08 16:15:11 +02:00
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
Join<FeedEntryStatus, FeedSubscription> join = (Join<FeedEntryStatus, FeedSubscription>) root
|
|
|
|
|
.fetch(FeedEntryStatus_.subscription);
|
|
|
|
|
|
|
|
|
|
Predicate p1 = builder.equal(root.get(FeedEntryStatus_.id), id);
|
|
|
|
|
Predicate p2 = builder.equal(join.get(FeedSubscription_.user), user);
|
2013-04-08 14:28:31 +02:00
|
|
|
|
2013-06-08 16:15:11 +02:00
|
|
|
query.where(p1, p2);
|
2013-03-23 17:17:27 +01:00
|
|
|
|
|
|
|
|
FeedEntryStatus status = null;
|
|
|
|
|
try {
|
2013-06-08 16:15:11 +02:00
|
|
|
status = em.createQuery(query).getSingleResult();
|
2013-03-23 17:17:27 +01:00
|
|
|
} catch (NoResultException e) {
|
|
|
|
|
status = null;
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
2013-04-08 13:06:53 +02:00
|
|
|
|
2013-06-20 16:49:18 +02:00
|
|
|
public FeedEntryStatus findByEntry(FeedEntry entry, FeedSubscription sub) {
|
|
|
|
|
|
|
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
Predicate p1 = builder.equal(root.get(FeedEntryStatus_.entry), entry);
|
|
|
|
|
Predicate p2 = builder.equal(root.get(FeedEntryStatus_.subscription),
|
|
|
|
|
sub);
|
|
|
|
|
|
|
|
|
|
query.where(p1, p2);
|
|
|
|
|
|
|
|
|
|
List<FeedEntryStatus> statuses = em.createQuery(query).getResultList();
|
|
|
|
|
return Iterables.getFirst(statuses, null);
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 09:38:49 +02:00
|
|
|
public List<FeedEntryStatus> findByEntries(List<FeedEntry> entries,
|
|
|
|
|
FeedSubscription sub) {
|
|
|
|
|
List<FeedEntryStatus> results = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(entries)) {
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2013-06-27 23:30:16 +02:00
|
|
|
|
2013-06-21 09:38:49 +02:00
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
Predicate p1 = root.get(FeedEntryStatus_.entry).in(entries);
|
|
|
|
|
Predicate p2 = builder.equal(root.get(FeedEntryStatus_.subscription),
|
|
|
|
|
sub);
|
|
|
|
|
|
|
|
|
|
query.where(p1, p2);
|
|
|
|
|
|
|
|
|
|
Map<Long, FeedEntryStatus> existing = Maps.uniqueIndex(
|
|
|
|
|
em.createQuery(query).getResultList(),
|
|
|
|
|
new Function<FeedEntryStatus, Long>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Long apply(FeedEntryStatus input) {
|
|
|
|
|
return input.getEntry().getId();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (FeedEntry entry : entries) {
|
|
|
|
|
FeedEntryStatus s = existing.get(entry.getId());
|
|
|
|
|
if (s == null) {
|
|
|
|
|
s = new FeedEntryStatus();
|
|
|
|
|
s.setEntry(entry);
|
|
|
|
|
s.setSubscription(sub);
|
|
|
|
|
s.setRead(true);
|
|
|
|
|
}
|
|
|
|
|
results.add(s);
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-12 10:44:41 +02:00
|
|
|
public List<FeedEntryStatus> findByKeywords(User user, String keywords,
|
2013-05-15 16:01:14 +02:00
|
|
|
int offset, int limit) {
|
2013-06-27 23:30:16 +02:00
|
|
|
|
2013-04-08 13:06:53 +02:00
|
|
|
String joinedKeywords = StringUtils.join(
|
|
|
|
|
keywords.toLowerCase().split(" "), "%");
|
2013-04-10 10:14:27 +02:00
|
|
|
joinedKeywords = "%" + joinedKeywords + "%";
|
2013-06-27 23:30:16 +02:00
|
|
|
|
2013-06-22 16:41:35 +02:00
|
|
|
CriteriaQuery<Tuple> query = builder.createTupleQuery();
|
|
|
|
|
Root<FeedEntry> root = query.from(FeedEntry.class);
|
2013-04-10 10:14:27 +02:00
|
|
|
|
2013-06-22 16:41:35 +02:00
|
|
|
SetJoin<FeedEntry, Feed> feedJoin = root.join(FeedEntry_.feeds);
|
|
|
|
|
SetJoin<Feed, FeedSubscription> subJoin = feedJoin
|
|
|
|
|
.join(Feed_.subscriptions);
|
|
|
|
|
Join<FeedEntry, FeedEntryContent> contentJoin = root
|
|
|
|
|
.join(FeedEntry_.content);
|
2013-04-10 10:14:27 +02:00
|
|
|
|
2013-06-22 16:41:35 +02:00
|
|
|
Selection<FeedEntry> entryAlias = root.alias("entry");
|
|
|
|
|
Selection<FeedSubscription> subAlias = subJoin.alias("subscription");
|
|
|
|
|
query.multiselect(entryAlias, subAlias);
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-22 16:41:35 +02:00
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
2013-05-15 16:01:14 +02:00
|
|
|
|
|
|
|
|
predicates
|
|
|
|
|
.add(builder.equal(subJoin.get(FeedSubscription_.user), user));
|
2013-06-27 23:30:16 +02:00
|
|
|
|
2013-05-15 16:01:14 +02:00
|
|
|
Predicate content = builder.like(
|
|
|
|
|
builder.lower(contentJoin.get(FeedEntryContent_.content)),
|
|
|
|
|
joinedKeywords);
|
2013-04-10 10:14:27 +02:00
|
|
|
Predicate title = builder.like(
|
2013-05-15 16:01:14 +02:00
|
|
|
builder.lower(contentJoin.get(FeedEntryContent_.title)),
|
2013-04-11 10:27:20 +02:00
|
|
|
joinedKeywords);
|
2013-04-10 10:14:27 +02:00
|
|
|
predicates.add(builder.or(content, title));
|
|
|
|
|
|
|
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
2013-06-22 16:41:35 +02:00
|
|
|
orderBy(query, root, ReadingOrder.desc);
|
2013-04-10 10:28:48 +02:00
|
|
|
|
2013-06-22 16:41:35 +02:00
|
|
|
TypedQuery<Tuple> q = em.createQuery(query);
|
2013-04-10 10:28:48 +02:00
|
|
|
limit(q, offset, limit);
|
2013-06-12 04:58:19 +02:00
|
|
|
setTimeout(q);
|
2013-06-22 16:41:35 +02:00
|
|
|
|
|
|
|
|
List<Tuple> list = q.getResultList();
|
|
|
|
|
List<FeedEntryStatus> results = Lists.newArrayList();
|
|
|
|
|
for (Tuple tuple : list) {
|
|
|
|
|
FeedEntry entry = tuple.get(entryAlias);
|
|
|
|
|
FeedSubscription subscription = tuple.get(subAlias);
|
|
|
|
|
|
|
|
|
|
FeedEntryStatus status = findByEntry(entry, subscription);
|
|
|
|
|
if (status == null) {
|
|
|
|
|
status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
|
|
|
|
status.setRead(true);
|
|
|
|
|
status.setSubscription(subscription);
|
|
|
|
|
}
|
|
|
|
|
results.add(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lazyLoadContent(true, results);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-30 10:43:49 +02:00
|
|
|
public List<FeedEntryStatus> findStarred(User user, ReadingOrder order,
|
|
|
|
|
boolean includeContent) {
|
2013-06-08 14:09:46 +02:00
|
|
|
return findStarred(user, null, -1, -1, order, includeContent);
|
2013-05-22 17:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-08 14:09:46 +02:00
|
|
|
public List<FeedEntryStatus> findStarred(User user, Date newerThan,
|
|
|
|
|
int offset, int limit, ReadingOrder order, boolean includeContent) {
|
2013-04-29 22:37:26 +02:00
|
|
|
|
|
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-12 16:57:31 +02:00
|
|
|
Join<FeedEntryStatus, FeedEntry> entryJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.entry);
|
2013-05-16 14:34:29 +02:00
|
|
|
|
2013-06-12 16:57:31 +02:00
|
|
|
Join<FeedEntryStatus, FeedSubscription> subJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.subscription);
|
2013-05-15 16:01:14 +02:00
|
|
|
|
|
|
|
|
predicates
|
|
|
|
|
.add(builder.equal(subJoin.get(FeedSubscription_.user), user));
|
2013-04-29 22:40:49 +02:00
|
|
|
predicates.add(builder.equal(root.get(FeedEntryStatus_.starred), true));
|
|
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
2013-04-29 22:37:26 +02:00
|
|
|
|
2013-06-08 14:09:46 +02:00
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
entryJoin.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-15 16:01:14 +02:00
|
|
|
orderBy(query, entryJoin, order);
|
2013-04-29 22:37:26 +02:00
|
|
|
|
|
|
|
|
TypedQuery<FeedEntryStatus> q = em.createQuery(query);
|
|
|
|
|
limit(q, offset, limit);
|
2013-06-12 04:58:19 +02:00
|
|
|
setTimeout(q);
|
2013-05-17 11:25:50 +02:00
|
|
|
return lazyLoadContent(includeContent, q.getResultList());
|
2013-04-29 22:37:26 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 16:49:18 +02:00
|
|
|
public List<FeedEntryStatus> findAll(User user, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent) {
|
|
|
|
|
|
|
|
|
|
CriteriaQuery<Tuple> query = builder.createTupleQuery();
|
|
|
|
|
Root<FeedEntry> root = query.from(FeedEntry.class);
|
|
|
|
|
|
|
|
|
|
SetJoin<FeedEntry, Feed> feedJoin = root.join(FeedEntry_.feeds);
|
|
|
|
|
SetJoin<Feed, FeedSubscription> subJoin = feedJoin
|
|
|
|
|
.join(Feed_.subscriptions);
|
|
|
|
|
|
|
|
|
|
Selection<FeedEntry> entryAlias = root.alias("entry");
|
|
|
|
|
Selection<FeedSubscription> subAlias = subJoin.alias("subscription");
|
|
|
|
|
query.multiselect(entryAlias, subAlias);
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
predicates
|
|
|
|
|
.add(builder.equal(subJoin.get(FeedSubscription_.user), user));
|
|
|
|
|
|
|
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
root.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
|
|
|
|
orderBy(query, root, order);
|
|
|
|
|
|
|
|
|
|
TypedQuery<Tuple> q = em.createQuery(query);
|
|
|
|
|
limit(q, offset, limit);
|
|
|
|
|
setTimeout(q);
|
|
|
|
|
|
|
|
|
|
List<Tuple> list = q.getResultList();
|
|
|
|
|
List<FeedEntryStatus> results = Lists.newArrayList();
|
|
|
|
|
for (Tuple tuple : list) {
|
|
|
|
|
FeedEntry entry = tuple.get(entryAlias);
|
|
|
|
|
FeedSubscription subscription = tuple.get(subAlias);
|
|
|
|
|
|
|
|
|
|
FeedEntryStatus status = findByEntry(entry, subscription);
|
|
|
|
|
if (status == null) {
|
|
|
|
|
status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
2013-06-20 18:45:58 +02:00
|
|
|
status.setRead(true);
|
2013-06-20 16:49:18 +02:00
|
|
|
status.setSubscription(subscription);
|
|
|
|
|
}
|
|
|
|
|
results.add(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lazyLoadContent(includeContent, results);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 16:49:18 +02:00
|
|
|
public List<FeedEntryStatus> findAllUnread(User user, ReadingOrder order,
|
2013-06-08 14:09:46 +02:00
|
|
|
boolean includeContent) {
|
2013-06-20 16:49:18 +02:00
|
|
|
return findAllUnread(user, null, -1, -1, order, includeContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FeedEntryStatus> findAllUnread(User user, Date newerThan,
|
|
|
|
|
int offset, int limit, ReadingOrder order, boolean includeContent) {
|
2013-04-10 10:14:27 +02:00
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-12 16:57:31 +02:00
|
|
|
Join<FeedEntryStatus, FeedEntry> entryJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.entry);
|
|
|
|
|
Join<FeedEntryStatus, FeedSubscription> subJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.subscription);
|
2013-05-15 16:01:14 +02:00
|
|
|
|
|
|
|
|
predicates
|
|
|
|
|
.add(builder.equal(subJoin.get(FeedSubscription_.user), user));
|
2013-06-20 16:49:18 +02:00
|
|
|
predicates.add(builder.isFalse(root.get(FeedEntryStatus_.read)));
|
2013-04-11 10:27:20 +02:00
|
|
|
|
2013-06-08 14:09:46 +02:00
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
entryJoin.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 10:14:27 +02:00
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
2013-05-15 16:01:14 +02:00
|
|
|
orderBy(query, entryJoin, order);
|
2013-04-10 10:14:27 +02:00
|
|
|
|
|
|
|
|
TypedQuery<FeedEntryStatus> q = em.createQuery(query);
|
2013-04-10 10:28:48 +02:00
|
|
|
limit(q, offset, limit);
|
2013-06-12 04:58:19 +02:00
|
|
|
setTimeout(q);
|
2013-05-17 11:25:50 +02:00
|
|
|
return lazyLoadContent(includeContent, q.getResultList());
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-13 13:15:46 +02:00
|
|
|
public List<FeedEntryStatus> findBySubscription(
|
2013-06-20 18:45:58 +02:00
|
|
|
FeedSubscription subscription, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent) {
|
|
|
|
|
|
|
|
|
|
CriteriaQuery<FeedEntry> query = builder.createQuery(FeedEntry.class);
|
|
|
|
|
Root<FeedEntry> root = query.from(FeedEntry.class);
|
|
|
|
|
|
|
|
|
|
SetJoin<FeedEntry, Feed> feedJoin = root.join(FeedEntry_.feeds);
|
|
|
|
|
SetJoin<Feed, FeedSubscription> subJoin = feedJoin
|
|
|
|
|
.join(Feed_.subscriptions);
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
predicates.add(builder.equal(subJoin.get(FeedSubscription_.id),
|
|
|
|
|
subscription.getId()));
|
|
|
|
|
|
|
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
root.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
|
|
|
|
orderBy(query, root, order);
|
|
|
|
|
|
|
|
|
|
TypedQuery<FeedEntry> q = em.createQuery(query);
|
|
|
|
|
limit(q, offset, limit);
|
|
|
|
|
setTimeout(q);
|
|
|
|
|
|
|
|
|
|
List<FeedEntry> list = q.getResultList();
|
2013-06-21 09:38:49 +02:00
|
|
|
return lazyLoadContent(includeContent,
|
|
|
|
|
findByEntries(list, subscription));
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
public List<FeedEntryStatus> findUnreadBySubscription(
|
|
|
|
|
FeedSubscription subscription, ReadingOrder order,
|
|
|
|
|
boolean includeContent) {
|
|
|
|
|
return findUnreadBySubscription(subscription, null, -1, -1, order,
|
|
|
|
|
includeContent);
|
|
|
|
|
}
|
2013-04-10 10:14:27 +02:00
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
public List<FeedEntryStatus> findUnreadBySubscription(
|
|
|
|
|
FeedSubscription subscription, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent) {
|
|
|
|
|
|
2013-04-10 10:14:27 +02:00
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-12 16:57:31 +02:00
|
|
|
Join<FeedEntryStatus, FeedEntry> entryJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.entry);
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-13 13:15:46 +02:00
|
|
|
predicates.add(builder.equal(root.get(FeedEntryStatus_.subscription),
|
|
|
|
|
subscription));
|
2013-06-20 18:45:58 +02:00
|
|
|
predicates.add(builder.isFalse(root.get(FeedEntryStatus_.read)));
|
2013-04-11 10:27:20 +02:00
|
|
|
|
2013-06-08 14:09:46 +02:00
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
entryJoin.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 10:14:27 +02:00
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
2013-04-10 10:28:48 +02:00
|
|
|
|
2013-05-15 16:01:14 +02:00
|
|
|
orderBy(query, entryJoin, order);
|
2013-04-10 10:14:27 +02:00
|
|
|
|
|
|
|
|
TypedQuery<FeedEntryStatus> q = em.createQuery(query);
|
2013-04-10 10:28:48 +02:00
|
|
|
limit(q, offset, limit);
|
2013-06-12 04:58:19 +02:00
|
|
|
setTimeout(q);
|
2013-05-17 11:25:50 +02:00
|
|
|
return lazyLoadContent(includeContent, q.getResultList());
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-12 10:44:41 +02:00
|
|
|
public List<FeedEntryStatus> findByCategories(
|
2013-06-20 18:45:58 +02:00
|
|
|
List<FeedCategory> categories, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent) {
|
|
|
|
|
|
|
|
|
|
CriteriaQuery<Tuple> query = builder.createTupleQuery();
|
|
|
|
|
Root<FeedEntry> root = query.from(FeedEntry.class);
|
|
|
|
|
|
|
|
|
|
SetJoin<FeedEntry, Feed> feedJoin = root.join(FeedEntry_.feeds);
|
|
|
|
|
SetJoin<Feed, FeedSubscription> subJoin = feedJoin
|
|
|
|
|
.join(Feed_.subscriptions);
|
|
|
|
|
|
|
|
|
|
Selection<FeedEntry> entryAlias = root.alias("entry");
|
|
|
|
|
Selection<FeedSubscription> subAlias = subJoin.alias("subscription");
|
|
|
|
|
query.multiselect(entryAlias, subAlias);
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
|
|
|
|
|
|
|
|
|
if (categories.size() == 1) {
|
|
|
|
|
predicates.add(builder.equal(subJoin
|
|
|
|
|
.get(FeedSubscription_.category), categories.iterator()
|
|
|
|
|
.next()));
|
|
|
|
|
} else {
|
|
|
|
|
predicates.add(subJoin.get(FeedSubscription_.category).in(
|
|
|
|
|
categories));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
root.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
|
|
|
|
orderBy(query, root, order);
|
|
|
|
|
|
|
|
|
|
TypedQuery<Tuple> q = em.createQuery(query);
|
|
|
|
|
limit(q, offset, limit);
|
|
|
|
|
setTimeout(q);
|
|
|
|
|
|
|
|
|
|
List<Tuple> list = q.getResultList();
|
|
|
|
|
List<FeedEntryStatus> results = Lists.newArrayList();
|
|
|
|
|
for (Tuple tuple : list) {
|
|
|
|
|
FeedEntry entry = tuple.get(entryAlias);
|
|
|
|
|
FeedSubscription subscription = tuple.get(subAlias);
|
|
|
|
|
|
|
|
|
|
FeedEntryStatus status = findByEntry(entry, subscription);
|
|
|
|
|
if (status == null) {
|
|
|
|
|
status = new FeedEntryStatus();
|
|
|
|
|
status.setEntry(entry);
|
|
|
|
|
status.setSubscription(subscription);
|
|
|
|
|
status.setRead(true);
|
|
|
|
|
}
|
|
|
|
|
results.add(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lazyLoadContent(includeContent, results);
|
|
|
|
|
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
public List<FeedEntryStatus> findUnreadByCategories(
|
|
|
|
|
List<FeedCategory> categories, ReadingOrder order,
|
2013-06-08 14:09:46 +02:00
|
|
|
boolean includeContent) {
|
2013-06-20 18:45:58 +02:00
|
|
|
return findUnreadByCategories(categories, null, -1, -1, order,
|
|
|
|
|
includeContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<FeedEntryStatus> findUnreadByCategories(
|
|
|
|
|
List<FeedCategory> categories, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent) {
|
2013-04-10 10:14:27 +02:00
|
|
|
|
|
|
|
|
CriteriaQuery<FeedEntryStatus> query = builder.createQuery(getType());
|
|
|
|
|
Root<FeedEntryStatus> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
List<Predicate> predicates = Lists.newArrayList();
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-12 16:57:31 +02:00
|
|
|
Join<FeedEntryStatus, FeedEntry> entryJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.entry);
|
|
|
|
|
Join<FeedEntryStatus, FeedSubscription> subJoin = root
|
|
|
|
|
.join(FeedEntryStatus_.subscription);
|
2013-05-15 16:01:14 +02:00
|
|
|
|
2013-06-15 15:40:55 +02:00
|
|
|
if (categories.size() == 1) {
|
|
|
|
|
predicates.add(builder.equal(subJoin
|
|
|
|
|
.get(FeedSubscription_.category), categories.iterator()
|
|
|
|
|
.next()));
|
|
|
|
|
} else {
|
|
|
|
|
predicates.add(subJoin.get(FeedSubscription_.category).in(
|
|
|
|
|
categories));
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
predicates.add(builder.isFalse(root.get(FeedEntryStatus_.read)));
|
2013-04-11 10:27:20 +02:00
|
|
|
|
2013-06-08 14:09:46 +02:00
|
|
|
if (newerThan != null) {
|
|
|
|
|
predicates.add(builder.greaterThanOrEqualTo(
|
|
|
|
|
entryJoin.get(FeedEntry_.inserted), newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-10 10:14:27 +02:00
|
|
|
query.where(predicates.toArray(new Predicate[0]));
|
2013-04-10 10:28:48 +02:00
|
|
|
|
2013-05-15 16:01:14 +02:00
|
|
|
orderBy(query, entryJoin, order);
|
2013-04-10 10:14:27 +02:00
|
|
|
|
|
|
|
|
TypedQuery<FeedEntryStatus> q = em.createQuery(query);
|
2013-04-10 10:28:48 +02:00
|
|
|
limit(q, offset, limit);
|
2013-06-12 04:58:19 +02:00
|
|
|
setTimeout(q);
|
2013-05-17 11:25:50 +02:00
|
|
|
return lazyLoadContent(includeContent, q.getResultList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Map between subscriptionId and unread count
|
|
|
|
|
*/
|
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
|
public Map<Long, Long> getUnreadCount(User user) {
|
|
|
|
|
Map<Long, Long> map = Maps.newHashMap();
|
|
|
|
|
Query query = em.createNamedQuery("EntryStatus.unreadCounts");
|
|
|
|
|
query.setParameter("user", user);
|
2013-06-22 06:21:37 +02:00
|
|
|
setTimeout(query);
|
2013-05-17 11:25:50 +02:00
|
|
|
List resultList = query.getResultList();
|
|
|
|
|
for (Object o : resultList) {
|
|
|
|
|
Object[] array = (Object[]) o;
|
|
|
|
|
map.put((Long) array[0], (Long) array[1]);
|
|
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FeedEntryStatus> lazyLoadContent(boolean includeContent,
|
|
|
|
|
List<FeedEntryStatus> results) {
|
|
|
|
|
if (includeContent) {
|
|
|
|
|
for (FeedEntryStatus status : results) {
|
2013-06-13 13:15:46 +02:00
|
|
|
Hibernate.initialize(status.getSubscription().getFeed());
|
|
|
|
|
Hibernate.initialize(status.getEntry().getContent());
|
2013-05-17 11:25:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results;
|
2013-04-10 10:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 16:49:18 +02:00
|
|
|
private void orderBy(CriteriaQuery<?> query, Path<FeedEntry> entryJoin,
|
|
|
|
|
ReadingOrder order) {
|
2013-06-18 13:48:07 +02:00
|
|
|
if (order != null) {
|
|
|
|
|
Path<Date> orderPath = entryJoin.get(FeedEntry_.updated);
|
|
|
|
|
if (order == ReadingOrder.asc) {
|
|
|
|
|
query.orderBy(builder.asc(orderPath));
|
|
|
|
|
} else {
|
|
|
|
|
query.orderBy(builder.desc(orderPath));
|
|
|
|
|
}
|
2013-04-10 10:28:48 +02:00
|
|
|
}
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-12 04:58:19 +02:00
|
|
|
private void setTimeout(Query query) {
|
2013-06-27 23:30:16 +02:00
|
|
|
int queryTimeout = applicationSettingsService.get().getQueryTimeout();
|
|
|
|
|
if (queryTimeout > 0) {
|
|
|
|
|
query.setHint("javax.persistence.query.timeout", queryTimeout);
|
|
|
|
|
}
|
2013-06-12 04:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-13 13:15:46 +02:00
|
|
|
public void markSubscriptionEntries(FeedSubscription subscription,
|
|
|
|
|
Date olderThan) {
|
2013-06-20 18:45:58 +02:00
|
|
|
List<FeedEntryStatus> statuses = findUnreadBySubscription(subscription,
|
2013-06-18 13:48:07 +02:00
|
|
|
null, false);
|
2013-06-20 18:45:58 +02:00
|
|
|
markList(statuses, olderThan);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
public void markCategoryEntries(User user, List<FeedCategory> categories,
|
|
|
|
|
Date olderThan) {
|
2013-06-20 18:45:58 +02:00
|
|
|
List<FeedEntryStatus> statuses = findUnreadByCategories(categories,
|
|
|
|
|
null, false);
|
|
|
|
|
markList(statuses, olderThan);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-22 17:12:01 +02:00
|
|
|
public void markStarredEntries(User user, Date olderThan) {
|
2013-06-18 13:48:07 +02:00
|
|
|
List<FeedEntryStatus> statuses = findStarred(user, null, false);
|
2013-06-20 18:45:58 +02:00
|
|
|
markList(statuses, olderThan);
|
2013-05-22 17:12:01 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
public void markAllEntries(User user, Date olderThan) {
|
2013-06-20 16:49:18 +02:00
|
|
|
List<FeedEntryStatus> statuses = findAllUnread(user, null, false);
|
2013-06-20 18:45:58 +02:00
|
|
|
markList(statuses, olderThan);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-20 18:45:58 +02:00
|
|
|
private void markList(List<FeedEntryStatus> statuses, Date olderThan) {
|
2013-04-08 13:06:53 +02:00
|
|
|
List<FeedEntryStatus> list = Lists.newArrayList();
|
|
|
|
|
for (FeedEntryStatus status : statuses) {
|
|
|
|
|
if (!status.isRead()) {
|
2013-04-09 11:10:26 +02:00
|
|
|
Date inserted = status.getEntry().getInserted();
|
|
|
|
|
if (olderThan == null || inserted == null
|
|
|
|
|
|| olderThan.after(inserted)) {
|
2013-06-20 18:45:58 +02:00
|
|
|
if (status.isStarred()) {
|
|
|
|
|
status.setRead(true);
|
|
|
|
|
list.add(status);
|
|
|
|
|
} else {
|
|
|
|
|
delete(status);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 11:10:26 +02:00
|
|
|
}
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-06-20 18:45:58 +02:00
|
|
|
saveOrUpdate(list);
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-03-22 20:51:22 +01:00
|
|
|
}
|