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

152 lines
4.5 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-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-29 10:33:18 +02:00
private Predicate getUpdatablePredicate(Root<Feed> root) {
2013-04-11 19:11:27 +02:00
Predicate isNull = builder.isNull(root.get(Feed_.disabledUntil));
2013-07-29 10:33:18 +02:00
Predicate lessThan = builder.lessThan(root.get(Feed_.disabledUntil), new Date());
2013-07-05 10:44:53 +02:00
return builder.or(isNull, lessThan);
2013-05-24 12:42:18 +02:00
}
2013-07-29 10:33:18 +02:00
public Long getUpdatableCount() {
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-29 10:33:18 +02:00
query.where(getUpdatablePredicate(root));
2013-05-24 12:42:18 +02:00
TypedQuery<Long> q = em.createQuery(query);
return q.getSingleResult();
}
2013-07-29 10:33:18 +02:00
public List<Feed> findNextUpdatable(int count) {
2013-05-24 12:42:18 +02:00
CriteriaQuery<Feed> query = builder.createQuery(getType());
Root<Feed> root = query.from(getType());
2013-07-29 10:33:18 +02:00
query.where(getUpdatablePredicate(root));
query.orderBy(builder.asc(root.get(Feed_.disabledUntil)));
2013-04-11 19:11:27 +02:00
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-07-02 14:33:53 +02:00
String normalized = FeedUtils.normalizeURL(url);
2013-07-27 15:45:15 +02:00
List<Feed> feeds = findByField(Feed_.normalizedUrlHash, DigestUtils.sha1Hex(normalized));
Feed 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-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 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
2013-07-28 14:35:10 +02:00
delete(list);
2013-06-19 17:01:28 +02:00
return deleted;
}
2013-07-08 16:38:09 +02:00
@XmlRootElement
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
}