2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.backend.dao;
|
|
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
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.Predicate;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
|
|
2013-04-14 18:28:48 +02:00
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
2013-04-11 19:11:27 +02:00
|
|
|
import org.apache.commons.lang.time.DateUtils;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-04-11 19:11:27 +02:00
|
|
|
import com.commafeed.backend.model.Feed_;
|
2013-03-24 13:11:05 +01:00
|
|
|
import com.google.common.collect.Iterables;
|
2013-04-08 15:49:34 +02:00
|
|
|
import com.uaihebert.model.EasyCriteria;
|
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
|
|
|
|
2013-04-09 11:50:21 +02:00
|
|
|
public List<Feed> findNextUpdatable(int count) {
|
2013-04-11 19:11:27 +02:00
|
|
|
CriteriaQuery<Feed> query = builder.createQuery(getType());
|
|
|
|
|
Root<Feed> root = query.from(getType());
|
|
|
|
|
|
|
|
|
|
Date now = Calendar.getInstance().getTime();
|
|
|
|
|
|
|
|
|
|
Predicate hasSubscriptions = builder.isNotEmpty(root
|
|
|
|
|
.get(Feed_.subscriptions));
|
|
|
|
|
|
|
|
|
|
Predicate neverUpdated = builder.isNull(root.get(Feed_.lastUpdated));
|
|
|
|
|
Predicate updatedMoreThanOneMinuteAgo = builder.lessThan(
|
|
|
|
|
root.get(Feed_.lastUpdated), DateUtils.addMinutes(now, -1));
|
|
|
|
|
|
|
|
|
|
Predicate disabledDateIsNull = builder.isNull(root
|
|
|
|
|
.get(Feed_.disabledUntil));
|
|
|
|
|
Predicate DisabledDateIsInPast = builder.lessThan(
|
|
|
|
|
root.get(Feed_.disabledUntil), now);
|
|
|
|
|
|
|
|
|
|
query.where(hasSubscriptions,
|
|
|
|
|
builder.or(neverUpdated, updatedMoreThanOneMinuteAgo),
|
|
|
|
|
builder.or(disabledDateIsNull, DisabledDateIsInPast));
|
|
|
|
|
query.orderBy(builder.asc(root.get(Feed_.lastUpdated)));
|
|
|
|
|
|
|
|
|
|
TypedQuery<Feed> q = em.createQuery(query);
|
|
|
|
|
q.setMaxResults(count);
|
|
|
|
|
|
|
|
|
|
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-04-14 18:28:48 +02:00
|
|
|
List<Feed> feeds = findByField(Feed_.urlHash, DigestUtils.sha1Hex(url));
|
|
|
|
|
Feed feed = Iterables.getFirst(feeds, null);
|
|
|
|
|
if (feed != null && StringUtils.equals(url, feed.getUrl())) {
|
|
|
|
|
return feed;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2013-03-24 13:11:05 +01:00
|
|
|
}
|
2013-04-08 15:49:34 +02:00
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
public Feed findByIdWithEntries(Long feedId, int offset, int limit) {
|
2013-04-08 15:49:34 +02:00
|
|
|
EasyCriteria<Feed> criteria = createCriteria();
|
2013-04-12 10:44:41 +02:00
|
|
|
criteria.andEquals(Feed_.id.getName(), feedId);
|
|
|
|
|
criteria.leftJoinFetch(Feed_.entries.getName());
|
2013-04-11 19:11:27 +02:00
|
|
|
|
2013-04-10 16:45:05 +02:00
|
|
|
criteria.setFirstResult(offset);
|
|
|
|
|
criteria.setMaxResults(limit);
|
2013-04-08 15:49:34 +02:00
|
|
|
return criteria.getSingleResult();
|
|
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|