2013-03-22 20:51:22 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
import java.util.Comparator;
|
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-04-08 13:06:53 +02:00
|
|
|
import javax.persistence.Query;
|
|
|
|
|
import javax.persistence.TypedQuery;
|
2013-04-10 10:14:27 +02:00
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
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-03-22 20:51:22 +01:00
|
|
|
|
2013-04-08 13:06:53 +02:00
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-07-19 22:25:43 +02:00
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
2013-07-22 16:31:29 +02:00
|
|
|
import org.hibernate.Criteria;
|
|
|
|
|
import org.hibernate.criterion.Disjunction;
|
|
|
|
|
import org.hibernate.criterion.Order;
|
|
|
|
|
import org.hibernate.criterion.ProjectionList;
|
|
|
|
|
import org.hibernate.criterion.Projections;
|
|
|
|
|
import org.hibernate.criterion.Restrictions;
|
|
|
|
|
import org.hibernate.sql.JoinType;
|
|
|
|
|
import org.hibernate.transform.Transformers;
|
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-07-19 11:17:19 +02:00
|
|
|
import com.commafeed.backend.FixedSizeSortedSet;
|
2013-05-15 16:01:14 +02:00
|
|
|
import com.commafeed.backend.model.FeedEntry;
|
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-07-04 23:40:25 +02:00
|
|
|
import com.commafeed.backend.model.FeedFeedEntry_;
|
2013-05-15 16:01:14 +02:00
|
|
|
import com.commafeed.backend.model.FeedSubscription;
|
2013-07-02 18:07:08 +02:00
|
|
|
import com.commafeed.backend.model.Models;
|
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-07-22 16:31:29 +02:00
|
|
|
import com.google.api.client.util.Maps;
|
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;
|
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-07-19 11:17:19 +02:00
|
|
|
private static final Comparator<FeedEntry> ENTRY_COMPARATOR_DESC = new Comparator<FeedEntry>() {
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(FeedEntry o1, FeedEntry o2) {
|
2013-07-19 22:25:43 +02:00
|
|
|
return ObjectUtils.compare(o2.getUpdated(), o1.getUpdated());
|
2013-07-19 11:17:19 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private static final Comparator<FeedEntry> ENTRY_COMPARATOR_ASC = new Comparator<FeedEntry>() {
|
|
|
|
|
@Override
|
|
|
|
|
public int compare(FeedEntry o1, FeedEntry o2) {
|
2013-07-19 22:25:43 +02:00
|
|
|
return ObjectUtils.compare(o1.getUpdated(), o2.getUpdated());
|
2013-07-19 11:17:19 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2013-06-27 23:30:16 +02:00
|
|
|
@Inject
|
|
|
|
|
ApplicationSettingsService applicationSettingsService;
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
public FeedEntryStatus getStatus(FeedSubscription sub, FeedEntry entry) {
|
2013-06-20 16:49:18 +02:00
|
|
|
|
|
|
|
|
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();
|
2013-07-19 11:17:19 +02:00
|
|
|
FeedEntryStatus status = Iterables.getFirst(statuses, null);
|
|
|
|
|
if (status == null) {
|
|
|
|
|
status = new FeedEntryStatus(sub.getUser(), sub, entry);
|
2013-07-22 16:31:29 +02:00
|
|
|
status.setRead(false);
|
2013-06-21 09:38:49 +02:00
|
|
|
}
|
2013-07-19 11:17:19 +02:00
|
|
|
return status;
|
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-07-19 11:17:19 +02:00
|
|
|
predicates.add(builder.equal(root.get(FeedEntryStatus_.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(
|
2013-07-14 05:48:47 +02:00
|
|
|
root.get(FeedEntryStatus_.entryInserted), newerThan));
|
2013-06-08 14:09:46 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-14 05:48:47 +02:00
|
|
|
orderStatusesBy(query, root, 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-07-22 16:31:29 +02:00
|
|
|
private Criteria buildSearchCriteria(FeedSubscription sub,
|
|
|
|
|
boolean unreadOnly, String keywords, Date newerThan, int offset,
|
|
|
|
|
int limit, ReadingOrder order, boolean includeContent,
|
|
|
|
|
FeedEntry last) {
|
|
|
|
|
Criteria criteria = getSession().createCriteria(FeedEntry.class,
|
|
|
|
|
"entry");
|
|
|
|
|
|
|
|
|
|
Criteria ffeJoin = criteria.createCriteria(
|
|
|
|
|
FeedEntry_.feedRelationships.getName(), "ffe",
|
|
|
|
|
JoinType.INNER_JOIN);
|
|
|
|
|
ffeJoin.add(Restrictions.eq(FeedFeedEntry_.feed.getName(),
|
|
|
|
|
sub.getFeed()));
|
|
|
|
|
|
|
|
|
|
if (newerThan != null) {
|
|
|
|
|
criteria.add(Restrictions.ge(FeedEntry_.inserted.getName(),
|
|
|
|
|
newerThan));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (keywords != null) {
|
|
|
|
|
Criteria contentJoin = criteria.createCriteria(
|
|
|
|
|
FeedEntry_.content.getName(), "content",
|
|
|
|
|
JoinType.INNER_JOIN);
|
|
|
|
|
|
|
|
|
|
String joinedKeywords = StringUtils.join(keywords.toLowerCase()
|
|
|
|
|
.split(" "), "%");
|
|
|
|
|
joinedKeywords = "%" + joinedKeywords + "%";
|
|
|
|
|
|
|
|
|
|
Disjunction or = Restrictions.disjunction();
|
|
|
|
|
or.add(Restrictions.ilike(FeedEntryContent_.content.getName(),
|
|
|
|
|
joinedKeywords));
|
|
|
|
|
or.add(Restrictions.ilike(FeedEntryContent_.title.getName(),
|
|
|
|
|
joinedKeywords));
|
|
|
|
|
contentJoin.add(or);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unreadOnly) {
|
|
|
|
|
|
|
|
|
|
Criteria statusJoin = criteria.createCriteria(FeedEntry_.statuses
|
|
|
|
|
.getName(), "status", JoinType.LEFT_OUTER_JOIN,
|
|
|
|
|
Restrictions.eq(FeedEntryStatus_.subscription.getName(),
|
|
|
|
|
sub));
|
|
|
|
|
|
|
|
|
|
Disjunction or = Restrictions.disjunction();
|
|
|
|
|
or.add(Restrictions.isNull(FeedEntryStatus_.id.getName()));
|
|
|
|
|
or.add(Restrictions.eq(FeedEntryStatus_.read.getName(), false));
|
|
|
|
|
|
|
|
|
|
statusJoin.add(or);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (last != null) {
|
|
|
|
|
if (order == ReadingOrder.desc) {
|
|
|
|
|
ffeJoin.add(Restrictions.gt(
|
|
|
|
|
FeedFeedEntry_.entryUpdated.getName(),
|
|
|
|
|
last.getUpdated()));
|
|
|
|
|
} else {
|
|
|
|
|
ffeJoin.add(Restrictions.lt(
|
|
|
|
|
FeedFeedEntry_.entryUpdated.getName(),
|
|
|
|
|
last.getUpdated()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (order != null) {
|
|
|
|
|
Order o = null;
|
|
|
|
|
if (order == ReadingOrder.asc) {
|
|
|
|
|
o = Order.asc(FeedEntry_.updated.getName());
|
|
|
|
|
} else {
|
|
|
|
|
o = Order.desc(FeedEntry_.updated.getName());
|
|
|
|
|
}
|
|
|
|
|
criteria.addOrder(o);
|
|
|
|
|
}
|
|
|
|
|
if (offset > -1) {
|
|
|
|
|
criteria.setFirstResult(offset);
|
|
|
|
|
}
|
|
|
|
|
if (limit > -1) {
|
|
|
|
|
criteria.setMaxResults(limit);
|
|
|
|
|
}
|
|
|
|
|
int timeout = applicationSettingsService.get().getQueryTimeout();
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
|
criteria.setTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
return criteria;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2013-07-19 11:17:19 +02:00
|
|
|
public List<FeedEntryStatus> findBySubscriptions(
|
2013-07-22 16:31:29 +02:00
|
|
|
List<FeedSubscription> subscriptions, boolean unreadOnly,
|
|
|
|
|
String keywords, Date newerThan, int offset, int limit,
|
|
|
|
|
ReadingOrder order, boolean includeContent) {
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
int capacity = offset + limit;
|
|
|
|
|
Comparator<FeedEntry> comparator = order == ReadingOrder.desc ? ENTRY_COMPARATOR_DESC
|
|
|
|
|
: ENTRY_COMPARATOR_ASC;
|
|
|
|
|
FixedSizeSortedSet<FeedEntry> set = new FixedSizeSortedSet<FeedEntry>(
|
|
|
|
|
capacity < 0 ? Integer.MAX_VALUE : capacity, comparator);
|
|
|
|
|
for (FeedSubscription sub : subscriptions) {
|
2013-07-22 16:31:29 +02:00
|
|
|
FeedEntry last = (order != null && set.isFull()) ? set.last()
|
|
|
|
|
: null;
|
|
|
|
|
Criteria criteria = buildSearchCriteria(sub, unreadOnly, keywords,
|
|
|
|
|
newerThan, -1, limit, order, includeContent, last);
|
2013-06-20 18:45:58 +02:00
|
|
|
|
2013-07-22 16:31:29 +02:00
|
|
|
List<FeedEntry> list = criteria.list();
|
2013-07-19 11:17:19 +02:00
|
|
|
for (FeedEntry entry : list) {
|
|
|
|
|
entry.setSubscription(sub);
|
|
|
|
|
}
|
|
|
|
|
set.addAll(list);
|
2013-06-20 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
List<FeedEntry> entries = set.asList();
|
|
|
|
|
int size = entries.size();
|
|
|
|
|
if (size < offset) {
|
|
|
|
|
return Lists.newArrayList();
|
2013-06-20 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
entries = entries.subList(Math.max(offset, 0), size);
|
2013-06-20 18:45:58 +02:00
|
|
|
|
|
|
|
|
List<FeedEntryStatus> results = Lists.newArrayList();
|
2013-07-19 11:17:19 +02:00
|
|
|
for (FeedEntry entry : entries) {
|
|
|
|
|
FeedSubscription subscription = entry.getSubscription();
|
|
|
|
|
results.add(getStatus(subscription, entry));
|
2013-06-20 18:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lazyLoadContent(includeContent, results);
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-22 16:31:29 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
|
public Long getUnreadCount(FeedSubscription subscription) {
|
|
|
|
|
Long count = null;
|
|
|
|
|
Criteria criteria = buildSearchCriteria(subscription, true, null, null,
|
|
|
|
|
-1, -1, null, false, null);
|
|
|
|
|
ProjectionList projection = Projections.projectionList();
|
|
|
|
|
projection.add(Projections.rowCount(), "count");
|
|
|
|
|
criteria.setProjection(projection);
|
|
|
|
|
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
|
|
|
|
|
List<Map<String, Long>> list = criteria.list();
|
|
|
|
|
for (Map<String, Long> row : list) {
|
|
|
|
|
count = row.get("count");
|
2013-07-19 12:18:10 +02:00
|
|
|
}
|
2013-07-22 16:31:29 +02:00
|
|
|
return count;
|
2013-07-19 12:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2013-05-17 11:25:50 +02:00
|
|
|
/**
|
|
|
|
|
* Map between subscriptionId and unread count
|
|
|
|
|
*/
|
2013-07-22 16:31:29 +02:00
|
|
|
public Map<Long, Long> getUnreadCount(List<FeedSubscription> subscriptions) {
|
2013-05-17 11:25:50 +02:00
|
|
|
Map<Long, Long> map = Maps.newHashMap();
|
2013-07-22 16:31:29 +02:00
|
|
|
for (FeedSubscription sub : subscriptions) {
|
|
|
|
|
map.put(sub.getId(), getUnreadCount(sub));
|
2013-05-17 11:25:50 +02:00
|
|
|
}
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FeedEntryStatus> lazyLoadContent(boolean includeContent,
|
|
|
|
|
List<FeedEntryStatus> results) {
|
|
|
|
|
if (includeContent) {
|
|
|
|
|
for (FeedEntryStatus status : results) {
|
2013-07-02 18:07:08 +02:00
|
|
|
Models.initialize(status.getSubscription().getFeed());
|
|
|
|
|
Models.initialize(status.getEntry().getContent());
|
2013-05-17 11:25:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results;
|
2013-04-10 10:28:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
private void orderStatusesBy(CriteriaQuery<?> query,
|
|
|
|
|
Path<FeedEntryStatus> statusJoin, ReadingOrder order) {
|
2013-07-14 05:48:47 +02:00
|
|
|
orderBy(query, statusJoin.get(FeedEntryStatus_.entryUpdated), order);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void orderBy(CriteriaQuery<?> query, Path<Date> date,
|
2013-06-20 16:49:18 +02:00
|
|
|
ReadingOrder order) {
|
2013-06-18 13:48:07 +02:00
|
|
|
if (order != null) {
|
|
|
|
|
if (order == ReadingOrder.asc) {
|
2013-07-14 05:48:47 +02:00
|
|
|
query.orderBy(builder.asc(date));
|
2013-06-18 13:48:07 +02:00
|
|
|
} else {
|
2013-07-14 05:48:47 +02:00
|
|
|
query.orderBy(builder.desc(date));
|
2013-06-18 13:48:07 +02:00
|
|
|
}
|
2013-04-10 10:28:48 +02:00
|
|
|
}
|
2013-04-08 13:06:53 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-01 21:15:32 +02:00
|
|
|
protected void setTimeout(Query query) {
|
|
|
|
|
setTimeout(query, applicationSettingsService.get().getQueryTimeout());
|
2013-06-12 04:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 11:17:19 +02:00
|
|
|
public void markSubscriptionEntries(List<FeedSubscription> subscriptions,
|
2013-06-13 13:15:46 +02:00
|
|
|
Date olderThan) {
|
2013-07-22 16:31:29 +02:00
|
|
|
List<FeedEntryStatus> statuses = findBySubscriptions(subscriptions,
|
|
|
|
|
true, null, null, -1, -1, null, false);
|
2013-06-20 18:45:58 +02:00
|
|
|
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-07-19 11:17:19 +02:00
|
|
|
List<FeedEntryStatus> statuses = findStarred(user, null, -1, -1, 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-07-22 16:31:29 +02:00
|
|
|
status.setRead(true);
|
|
|
|
|
list.add(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
|
|
|
}
|