Files
Athou_commafeed/src/main/java/com/commafeed/backend/dao/FeedDAO.java

107 lines
3.7 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.dao;
2013-04-11 19:11:27 +02:00
import java.util.Date;
2013-03-24 13:11:05 +01:00
import java.util.List;
2013-03-20 20:33:42 +01:00
import javax.ejb.Stateless;
2013-04-11 19:11:27 +02:00
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
2013-06-08 16:15:11 +02:00
import javax.persistence.criteria.JoinType;
2013-04-11 19:11:27 +02:00
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
2013-06-19 17:01:28 +02:00
import javax.persistence.criteria.SetJoin;
import javax.persistence.criteria.Subquery;
2013-04-11 19:11:27 +02:00
2013-04-14 18:28:48 +02:00
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
2013-03-20 20:33:42 +01:00
2013-07-02 14:33:53 +02:00
import com.commafeed.backend.feeds.FeedUtils;
2013-03-23 16:17:19 +01:00
import com.commafeed.backend.model.Feed;
2013-06-19 17:01:28 +02:00
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.backend.model.FeedSubscription_;
2013-04-11 19:11:27 +02:00
import com.commafeed.backend.model.Feed_;
import com.commafeed.backend.model.User;
import com.commafeed.backend.model.User_;
2013-03-24 13:11:05 +01:00
import com.google.common.collect.Iterables;
2013-05-28 09:21:29 +02:00
import com.google.common.collect.Lists;
2013-03-20 20:33:42 +01:00
@Stateless
2013-04-11 20:49:08 +02:00
public class FeedDAO extends GenericDAO<Feed> {
2013-03-20 20:33:42 +01:00
private List<Predicate> getUpdatablePredicates(CriteriaQuery<?> query, Root<Feed> root, Date lastLoginThreshold) {
2013-04-11 19:11:27 +02:00
List<Predicate> preds = Lists.newArrayList();
Predicate isNull = builder.isNull(root.get(Feed_.disabledUntil));
2013-07-29 10:33:18 +02:00
Predicate lessThan = builder.lessThan(root.get(Feed_.disabledUntil), new Date());
preds.add(builder.or(isNull, lessThan));
2013-07-05 10:44:53 +02:00
if (lastLoginThreshold != null) {
Subquery<Long> subquery = query.subquery(Long.class);
Root<FeedSubscription> subroot = subquery.from(FeedSubscription.class);
subquery.select(builder.count(subroot.get(FeedSubscription_.id)));
Join<FeedSubscription, User> userJoin = subroot.join(FeedSubscription_.user);
Predicate p1 = builder.equal(subroot.get(FeedSubscription_.feed), root);
Predicate p2 = builder.greaterThanOrEqualTo(userJoin.get(User_.lastLogin), lastLoginThreshold);
subquery.where(p1, p2);
preds.add(builder.exists(subquery));
}
return preds;
2013-05-24 12:42:18 +02:00
}
public Long getUpdatableCount(Date lastLoginThreshold) {
2013-05-24 12:42:18 +02:00
CriteriaQuery<Long> query = builder.createQuery(Long.class);
Root<Feed> root = query.from(getType());
query.select(builder.count(root));
query.where(getUpdatablePredicates(query, root, lastLoginThreshold).toArray(new Predicate[0]));
2013-05-24 12:42:18 +02:00
TypedQuery<Long> q = em.createQuery(query);
return q.getSingleResult();
}
public List<Feed> findNextUpdatable(int count, Date lastLoginThreshold) {
2013-05-24 12:42:18 +02:00
CriteriaQuery<Feed> query = builder.createQuery(getType());
Root<Feed> root = query.from(getType());
query.where(getUpdatablePredicates(query, root, lastLoginThreshold).toArray(new Predicate[0]));
query.orderBy(builder.asc(root.get(Feed_.disabledUntil)));
2013-04-11 19:11:27 +02:00
TypedQuery<Feed> q = em.createQuery(query);
q.setMaxResults(count);
2013-05-24 12:42:18 +02:00
return q.getResultList();
2013-04-09 11:50:21 +02:00
}
2013-03-24 13:11:05 +01:00
public Feed findByUrl(String url) {
2013-07-02 14:33:53 +02:00
String normalized = FeedUtils.normalizeURL(url);
2013-07-27 15:45:15 +02:00
List<Feed> feeds = findByField(Feed_.normalizedUrlHash, DigestUtils.sha1Hex(normalized));
Feed feed = Iterables.getFirst(feeds, null);
2013-07-25 09:17:33 +02:00
if (feed != null && StringUtils.equals(normalized, feed.getNormalizedUrl())) {
2013-07-02 14:33:53 +02:00
return feed;
}
2013-04-14 18:28:48 +02:00
return null;
2013-03-24 13:11:05 +01:00
}
2013-06-05 21:50:26 +02:00
public List<Feed> findByTopic(String topic) {
return findByField(Feed_.pushTopicHash, DigestUtils.sha1Hex(topic));
2013-06-05 21:50:26 +02:00
}
2013-11-16 07:30:01 +01:00
public List<Feed> findWithoutSubscriptions(int max) {
2013-06-19 17:01:28 +02:00
CriteriaQuery<Feed> query = builder.createQuery(getType());
Root<Feed> root = query.from(getType());
2013-07-25 09:17:33 +02:00
SetJoin<Feed, FeedSubscription> join = root.join(Feed_.subscriptions, JoinType.LEFT);
2013-06-19 17:01:28 +02:00
query.where(builder.isNull(join.get(FeedSubscription_.id)));
TypedQuery<Feed> q = em.createQuery(query);
q.setMaxResults(max);
2013-11-16 07:30:01 +01:00
return q.getResultList();
2013-06-19 17:01:28 +02:00
}
2013-03-20 20:33:42 +01:00
}