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-10 15:06:04 +02:00
|
|
|
import javax.persistence.metamodel.SingularAttribute;
|
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 {
|
2013-07-10 15:06:04 +02:00
|
|
|
public String value;
|
2013-07-08 16:38:09 +02:00
|
|
|
public List<Feed> feeds;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
private List<Predicate> getUpdatablePredicates(Root<Feed> root, Date threshold) {
|
2013-04-11 19:11:27 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
Predicate hasSubscriptions = builder.isNotEmpty(root.get(Feed_.subscriptions));
|
2013-07-05 10:44:53 +02:00
|
|
|
|
2013-04-11 19:11:27 +02:00
|
|
|
Predicate neverUpdated = builder.isNull(root.get(Feed_.lastUpdated));
|
2013-07-25 09:17:33 +02:00
|
|
|
Predicate updatedBeforeThreshold = builder.lessThan(root.get(Feed_.lastUpdated), threshold);
|
2013-04-11 19:11:27 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
Predicate disabledDateIsNull = builder.isNull(root.get(Feed_.disabledUntil));
|
|
|
|
|
Predicate disabledDateIsInPast = builder.lessThan(root.get(Feed_.disabledUntil), new Date());
|
2013-04-11 19:11:27 +02:00
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
return Lists.newArrayList(hasSubscriptions, builder.or(neverUpdated, updatedBeforeThreshold),
|
2013-05-28 09:21:29 +02:00
|
|
|
builder.or(disabledDateIsNull, disabledDateIsInPast));
|
2013-05-24 12:42:18 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-30 12:18:24 +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-25 09:17:33 +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();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-30 12:18:24 +02:00
|
|
|
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-25 09:17:33 +02:00
|
|
|
query.where(getUpdatablePredicates(root, threshold).toArray(new Predicate[0]));
|
2013-05-31 16:45:09 +02:00
|
|
|
|
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
|
|
|
|
2013-05-31 16:45:09 +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-25 09:17:33 +02:00
|
|
|
feeds = findByField(Feed_.normalizedUrlHash, DigestUtils.sha1Hex(normalized));
|
2013-07-02 14:33:53 +02:00
|
|
|
feed = Iterables.getFirst(feeds, null);
|
2013-07-25 09:17:33 +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-04-08 15:49:34 +02:00
|
|
|
|
2013-06-05 21:50:26 +02:00
|
|
|
public List<Feed> findByTopic(String topic) {
|
2013-06-16 11:48:23 +02:00
|
|
|
return findByField(Feed_.pushTopicHash, DigestUtils.sha1Hex(topic));
|
2013-06-05 21:50:26 +02:00
|
|
|
}
|
2013-06-19 17:01:28 +02:00
|
|
|
|
2013-07-08 07:44:43 +02:00
|
|
|
public void deleteRelationships(Feed feed) {
|
2013-07-25 09:17:33 +02:00
|
|
|
Query relationshipDeleteQuery = em.createNamedQuery("Feed.deleteEntryRelationships");
|
2013-07-08 07:44:43 +02:00
|
|
|
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());
|
|
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
SetJoin<Feed, FeedSubscription> join = root.join(Feed_.subscriptions, JoinType.LEFT);
|
2013-06-19 17:01:28 +02:00
|
|
|
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) {
|
2013-07-08 07:44:43 +02:00
|
|
|
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
|
|
|
|
2013-07-10 15:06:04 +02:00
|
|
|
public static enum DuplicateMode {
|
2013-07-25 09:17:33 +02:00
|
|
|
NORMALIZED_URL(Feed_.normalizedUrlHash), LAST_CONTENT(Feed_.lastContentHash), PUSH_TOPIC(Feed_.pushTopicHash);
|
2013-07-10 15:06:04 +02:00
|
|
|
private SingularAttribute<Feed, String> path;
|
|
|
|
|
|
|
|
|
|
private DuplicateMode(SingularAttribute<Feed, String> path) {
|
|
|
|
|
this.path = path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SingularAttribute<Feed, String> getPath() {
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
public List<FeedCount> findDuplicates(DuplicateMode mode, int offset, int limit, long minCount) {
|
2013-07-08 16:38:09 +02:00
|
|
|
CriteriaQuery<String> query = builder.createQuery(String.class);
|
|
|
|
|
Root<Feed> root = query.from(getType());
|
|
|
|
|
|
2013-07-10 15:06:04 +02:00
|
|
|
Path<String> path = root.get(mode.getPath());
|
|
|
|
|
Expression<Long> count = builder.count(path);
|
2013-07-08 16:38:09 +02:00
|
|
|
|
2013-07-10 15:06:04 +02:00
|
|
|
query.select(path);
|
2013-07-08 16:38:09 +02:00
|
|
|
|
2013-07-10 15:06:04 +02:00
|
|
|
query.groupBy(path);
|
2013-07-08 20:38:06 +02:00
|
|
|
query.having(builder.greaterThan(count, minCount));
|
2013-07-08 16:38:09 +02:00
|
|
|
|
|
|
|
|
TypedQuery<String> q = em.createQuery(query);
|
|
|
|
|
limit(q, offset, limit);
|
2013-07-10 15:06:04 +02:00
|
|
|
List<String> pathValues = q.getResultList();
|
2013-07-08 16:38:09 +02:00
|
|
|
|
|
|
|
|
List<FeedCount> result = Lists.newArrayList();
|
2013-07-10 15:06:04 +02:00
|
|
|
for (String pathValue : pathValues) {
|
2013-07-08 16:38:09 +02:00
|
|
|
FeedCount fc = new FeedCount();
|
2013-07-10 15:06:04 +02:00
|
|
|
fc.value = pathValue;
|
2013-07-08 19:01:08 +02:00
|
|
|
fc.feeds = Lists.newArrayList();
|
2013-07-10 15:06:04 +02:00
|
|
|
for (Feed feed : findByField(mode.getPath(), pathValue)) {
|
2013-07-08 19:01:08 +02:00
|
|
|
Feed f = new Feed();
|
|
|
|
|
f.setId(feed.getId());
|
|
|
|
|
f.setUrl(feed.getUrl());
|
|
|
|
|
fc.feeds.add(f);
|
|
|
|
|
}
|
|
|
|
|
result.add(fc);
|
2013-07-08 16:38:09 +02:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|