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-04-14 18:28:48 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
2014-08-08 16:49:02 +02:00
|
|
|
import org.hibernate.SessionFactory;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.commafeed.backend.model.QFeed;
|
|
|
|
|
import com.commafeed.backend.model.QFeedSubscription;
|
|
|
|
|
import com.commafeed.backend.model.QUser;
|
2013-03-24 13:11:05 +01:00
|
|
|
import com.google.common.collect.Iterables;
|
2014-08-08 16:49:02 +02:00
|
|
|
import com.mysema.query.BooleanBuilder;
|
|
|
|
|
import com.mysema.query.jpa.hibernate.HibernateQuery;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
public class FeedDAO extends GenericDAO<Feed> {
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
private QFeed feed = QFeed.feed;
|
2013-08-01 21:11:45 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public FeedDAO(SessionFactory sessionFactory) {
|
|
|
|
|
super(sessionFactory);
|
2013-05-24 12:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-01 21:11:45 +02:00
|
|
|
public List<Feed> findNextUpdatable(int count, Date lastLoginThreshold) {
|
2014-08-08 16:49:02 +02:00
|
|
|
BooleanBuilder disabledDatePredicate = new BooleanBuilder();
|
|
|
|
|
disabledDatePredicate.or(feed.disabledUntil.isNull());
|
|
|
|
|
disabledDatePredicate.or(feed.disabledUntil.lt(new Date()));
|
2013-04-11 19:11:27 +02:00
|
|
|
|
2014-08-12 16:32:03 +02:00
|
|
|
HibernateQuery query = newQuery().from(feed);
|
2014-08-08 16:49:02 +02:00
|
|
|
if (lastLoginThreshold != null) {
|
2014-08-12 16:32:03 +02:00
|
|
|
QFeedSubscription subs = QFeedSubscription.feedSubscription;
|
2014-08-08 16:49:02 +02:00
|
|
|
QUser user = QUser.user;
|
2014-08-12 16:32:03 +02:00
|
|
|
query.join(feed.subscriptions, subs).join(subs.user, user).where(disabledDatePredicate, user.lastLogin.gt(lastLoginThreshold));
|
|
|
|
|
} else {
|
|
|
|
|
query.where(disabledDatePredicate);
|
2014-08-08 16:49:02 +02:00
|
|
|
}
|
2013-05-24 12:42:18 +02:00
|
|
|
|
2014-08-12 16:56:18 +02:00
|
|
|
return query.orderBy(feed.disabledUntil.asc()).limit(count).distinct().list(feed);
|
2013-04-09 11:50:21 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public Feed findByUrl(String normalizedUrl) {
|
|
|
|
|
List<Feed> feeds = newQuery().from(feed).where(feed.normalizedUrlHash.eq(DigestUtils.sha1Hex(normalizedUrl))).list(feed);
|
2013-07-27 15:45:15 +02:00
|
|
|
Feed feed = Iterables.getFirst(feeds, null);
|
2014-08-08 16:49:02 +02:00
|
|
|
if (feed != null && StringUtils.equals(normalizedUrl, 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-04-08 15:49:34 +02:00
|
|
|
|
2013-06-05 21:50:26 +02:00
|
|
|
public List<Feed> findByTopic(String topic) {
|
2014-08-08 16:49:02 +02:00
|
|
|
return newQuery().from(feed).where(feed.pushTopicHash.eq(DigestUtils.sha1Hex(topic))).list(feed);
|
2013-06-05 21:50:26 +02:00
|
|
|
}
|
2013-06-19 17:01:28 +02:00
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
public List<Feed> findWithoutSubscriptions(int max) {
|
|
|
|
|
QFeedSubscription sub = QFeedSubscription.feedSubscription;
|
|
|
|
|
return newQuery().from(feed).leftJoin(feed.subscriptions, sub).where(sub.id.isNull()).limit(max).list(feed);
|
2013-06-19 17:01:28 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|