mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
removed wicket and tomee, use dropwizard instead. remove wro4j, use gulp instead
This commit is contained in:
@@ -3,104 +3,75 @@ package com.commafeed.backend.dao;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.SetJoin;
|
||||
import javax.persistence.criteria.Subquery;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import com.commafeed.backend.feeds.FeedUtils;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.FeedSubscription_;
|
||||
import com.commafeed.backend.model.Feed_;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.User_;
|
||||
import com.commafeed.backend.model.QFeed;
|
||||
import com.commafeed.backend.model.QFeedSubscription;
|
||||
import com.commafeed.backend.model.QUser;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.mysema.query.BooleanBuilder;
|
||||
import com.mysema.query.jpa.hibernate.HibernateQuery;
|
||||
import com.mysema.query.jpa.hibernate.HibernateSubQuery;
|
||||
|
||||
@Stateless
|
||||
public class FeedDAO extends GenericDAO<Feed> {
|
||||
|
||||
private List<Predicate> getUpdatablePredicates(CriteriaQuery<?> query, Root<Feed> root, Date lastLoginThreshold) {
|
||||
private QFeed feed = QFeed.feed;
|
||||
|
||||
List<Predicate> preds = Lists.newArrayList();
|
||||
Predicate isNull = builder.isNull(root.get(Feed_.disabledUntil));
|
||||
Predicate lessThan = builder.lessThan(root.get(Feed_.disabledUntil), new Date());
|
||||
preds.add(builder.or(isNull, lessThan));
|
||||
|
||||
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;
|
||||
public FeedDAO(SessionFactory sessionFactory) {
|
||||
super(sessionFactory);
|
||||
}
|
||||
|
||||
public Long getUpdatableCount(Date lastLoginThreshold) {
|
||||
CriteriaQuery<Long> query = builder.createQuery(Long.class);
|
||||
Root<Feed> root = query.from(getType());
|
||||
BooleanBuilder disabledDatePredicate = new BooleanBuilder();
|
||||
disabledDatePredicate.or(feed.disabledUntil.isNull());
|
||||
disabledDatePredicate.or(feed.disabledUntil.lt(new Date()));
|
||||
|
||||
query.select(builder.count(root));
|
||||
query.where(getUpdatablePredicates(query, root, lastLoginThreshold).toArray(new Predicate[0]));
|
||||
|
||||
TypedQuery<Long> q = em.createQuery(query);
|
||||
return q.getSingleResult();
|
||||
HibernateQuery query = newQuery().from(feed).where(disabledDatePredicate);
|
||||
if (lastLoginThreshold != null) {
|
||||
QFeedSubscription sub = QFeedSubscription.feedSubscription;
|
||||
QUser user = QUser.user;
|
||||
HibernateSubQuery subquery = new HibernateSubQuery();
|
||||
subquery.from(sub).join(sub.user, user).where(sub.feed.eq(feed), user.lastLogin.gt(lastLoginThreshold));
|
||||
query.where(subquery.exists());
|
||||
}
|
||||
return query.orderBy(feed.disabledUntil.asc()).count();
|
||||
}
|
||||
|
||||
public List<Feed> findNextUpdatable(int count, Date lastLoginThreshold) {
|
||||
CriteriaQuery<Feed> query = builder.createQuery(getType());
|
||||
Root<Feed> root = query.from(getType());
|
||||
BooleanBuilder disabledDatePredicate = new BooleanBuilder();
|
||||
disabledDatePredicate.or(feed.disabledUntil.isNull());
|
||||
disabledDatePredicate.or(feed.disabledUntil.lt(new Date()));
|
||||
|
||||
query.where(getUpdatablePredicates(query, root, lastLoginThreshold).toArray(new Predicate[0]));
|
||||
query.orderBy(builder.asc(root.get(Feed_.disabledUntil)));
|
||||
|
||||
TypedQuery<Feed> q = em.createQuery(query);
|
||||
q.setMaxResults(count);
|
||||
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
public Feed findByUrl(String url) {
|
||||
|
||||
String normalized = FeedUtils.normalizeURL(url);
|
||||
List<Feed> feeds = findByField(Feed_.normalizedUrlHash, DigestUtils.sha1Hex(normalized));
|
||||
Feed feed = Iterables.getFirst(feeds, null);
|
||||
if (feed != null && StringUtils.equals(normalized, feed.getNormalizedUrl())) {
|
||||
return feed;
|
||||
HibernateQuery query = newQuery().from(feed).where(disabledDatePredicate);
|
||||
if (lastLoginThreshold != null) {
|
||||
QFeedSubscription sub = QFeedSubscription.feedSubscription;
|
||||
QUser user = QUser.user;
|
||||
HibernateSubQuery subquery = new HibernateSubQuery();
|
||||
subquery.from(sub).join(sub.user, user).where(sub.feed.eq(feed), user.lastLogin.gt(lastLoginThreshold));
|
||||
query.where(subquery.exists());
|
||||
}
|
||||
|
||||
return query.orderBy(feed.disabledUntil.asc()).limit(count).list(feed);
|
||||
}
|
||||
|
||||
public Feed findByUrl(String normalizedUrl) {
|
||||
List<Feed> feeds = newQuery().from(feed).where(feed.normalizedUrlHash.eq(DigestUtils.sha1Hex(normalizedUrl))).list(feed);
|
||||
Feed feed = Iterables.getFirst(feeds, null);
|
||||
if (feed != null && StringUtils.equals(normalizedUrl, feed.getNormalizedUrl())) {
|
||||
return feed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Feed> findByTopic(String topic) {
|
||||
return findByField(Feed_.pushTopicHash, DigestUtils.sha1Hex(topic));
|
||||
return newQuery().from(feed).where(feed.pushTopicHash.eq(DigestUtils.sha1Hex(topic))).list(feed);
|
||||
}
|
||||
|
||||
|
||||
public List<Feed> findWithoutSubscriptions(int max) {
|
||||
CriteriaQuery<Feed> query = builder.createQuery(getType());
|
||||
Root<Feed> root = query.from(getType());
|
||||
|
||||
SetJoin<Feed, FeedSubscription> join = root.join(Feed_.subscriptions, JoinType.LEFT);
|
||||
query.where(builder.isNull(join.get(FeedSubscription_.id)));
|
||||
TypedQuery<Feed> q = em.createQuery(query);
|
||||
q.setMaxResults(max);
|
||||
|
||||
return q.getResultList();
|
||||
QFeedSubscription sub = QFeedSubscription.feedSubscription;
|
||||
return newQuery().from(feed).leftJoin(feed.subscriptions, sub).where(sub.id.isNull()).limit(max).list(feed);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user