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

66 lines
2.2 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;
2014-08-17 14:16:30 +02:00
import javax.inject.Inject;
import javax.inject.Singleton;
2013-04-14 18:28:48 +02:00
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
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;
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;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.hibernate.HibernateQuery;
2013-03-20 20:33:42 +01:00
2014-08-17 14:16:30 +02:00
@Singleton
2013-04-11 20:49:08 +02:00
public class FeedDAO extends GenericDAO<Feed> {
2013-03-20 20:33:42 +01:00
private QFeed feed = QFeed.feed;
2014-08-17 14:16:30 +02:00
@Inject
public FeedDAO(SessionFactory sessionFactory) {
super(sessionFactory);
2013-05-24 12:42:18 +02:00
}
public List<Feed> findNextUpdatable(int count, Date lastLoginThreshold) {
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);
if (lastLoginThreshold != null) {
2014-08-12 16:32:03 +02:00
QFeedSubscription subs = QFeedSubscription.feedSubscription;
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);
}
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
}
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);
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-06-05 21:50:26 +02:00
public List<Feed> findByTopic(String topic) {
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
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
}