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

164 lines
5.0 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-07-05 17:00:32 +02:00
import javax.persistence.Query;
2013-04-11 19:11:27 +02:00
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
2013-07-08 16:38:09 +02:00
import javax.persistence.criteria.Expression;
2013-06-08 16:15:11 +02:00
import javax.persistence.criteria.JoinType;
2013-07-08 16:38:09 +02:00
import javax.persistence.criteria.Path;
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;
2013-07-08 16:38:09 +02:00
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
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_;
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
2013-07-08 16:38:09 +02:00
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public static class FeedCount {
public String normalizedUrlHash;
public List<Feed> feeds;
}
2013-07-04 15:43:08 +02:00
private List<Predicate> getUpdatablePredicates(Root<Feed> root,
Date threshold) {
2013-04-11 19:11:27 +02:00
2013-07-05 10:44:53 +02:00
Predicate hasSubscriptions = builder.isNotEmpty(root
.get(Feed_.subscriptions));
2013-04-11 19:11:27 +02:00
Predicate neverUpdated = builder.isNull(root.get(Feed_.lastUpdated));
2013-05-20 11:46:01 +02:00
Predicate updatedBeforeThreshold = builder.lessThan(
root.get(Feed_.lastUpdated), threshold);
2013-04-11 19:11:27 +02:00
Predicate disabledDateIsNull = builder.isNull(root
.get(Feed_.disabledUntil));
2013-04-22 11:58:06 +02:00
Predicate disabledDateIsInPast = builder.lessThan(
root.get(Feed_.disabledUntil), new Date());
2013-04-11 19:11:27 +02:00
2013-07-05 10:44:53 +02:00
return Lists.newArrayList(hasSubscriptions,
2013-05-20 11:46:01 +02:00
builder.or(neverUpdated, updatedBeforeThreshold),
2013-05-28 09:21:29 +02:00
builder.or(disabledDateIsNull, disabledDateIsInPast));
2013-05-24 12:42:18 +02:00
}
public Long getUpdatableCount(Date threshold) {
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));
2013-07-04 15:43:08 +02:00
query.where(getUpdatablePredicates(root, threshold).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 threshold) {
2013-05-24 12:42:18 +02:00
CriteriaQuery<Feed> query = builder.createQuery(getType());
Root<Feed> root = query.from(getType());
2013-07-04 15:43:08 +02:00
query.where(getUpdatablePredicates(root, threshold).toArray(
new Predicate[0]));
2013-04-11 19:11:27 +02:00
query.orderBy(builder.asc(root.get(Feed_.lastUpdated)));
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-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;
}
2013-07-02 14:33:53 +02:00
String normalized = FeedUtils.normalizeURL(url);
2013-07-04 15:43:08 +02:00
feeds = findByField(Feed_.normalizedUrlHash,
DigestUtils.sha1Hex(normalized));
2013-07-02 14:33:53 +02:00
feed = Iterables.getFirst(feeds, null);
2013-07-04 15:43:08 +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-06-19 17:01:28 +02:00
public void deleteRelationships(Feed feed) {
Query relationshipDeleteQuery = em
.createNamedQuery("Feed.deleteEntryRelationships");
relationshipDeleteQuery.setParameter("feedId", feed.getId());
relationshipDeleteQuery.executeUpdate();
}
2013-06-19 17:01:28 +02:00
public int deleteWithoutSubscriptions(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);
List<Feed> list = q.getResultList();
int deleted = list.size();
2013-07-05 17:00:32 +02:00
for (Feed feed : list) {
deleteRelationships(feed);
2013-07-05 17:00:32 +02:00
delete(feed);
}
2013-06-19 17:01:28 +02:00
return deleted;
}
2013-07-08 16:38:09 +02:00
public List<FeedCount> findDuplicates(int offset, int limit) {
CriteriaQuery<String> query = builder.createQuery(String.class);
Root<Feed> root = query.from(getType());
Path<String> hashPath = root.get(Feed_.normalizedUrlHash);
Expression<Long> count = builder.count(hashPath);
query.select(hashPath);
query.groupBy(hashPath);
query.having(builder.greaterThan(count, 1l));
TypedQuery<String> q = em.createQuery(query);
limit(q, offset, limit);
List<String> normalizedUrlHashes = q.getResultList();
List<FeedCount> result = Lists.newArrayList();
for (String hash : normalizedUrlHashes) {
FeedCount fc = new FeedCount();
fc.normalizedUrlHash = hash;
fc.feeds = findByField(Feed_.normalizedUrlHash, hash);
}
return result;
}
2013-03-20 20:33:42 +01:00
}