mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
using hibernate to fetch entries now, able to fetch entries in one go
This commit is contained in:
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
@@ -13,7 +14,9 @@ import org.apache.commons.lang.StringUtils;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.extended.FeedEntryWithStatus;
|
||||
import com.commafeed.frontend.utils.ModelFactory.MF;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -62,47 +65,48 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntry> getEntries(Feed feed, User user, boolean read) {
|
||||
return getEntries(feed, user, read, -1, -1);
|
||||
public List<FeedEntryWithStatus> getEntries(Feed feed, User user,
|
||||
boolean unreadOnly) {
|
||||
return getEntries(feed, user, unreadOnly, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntry> getEntries(Feed feed, User user, boolean read,
|
||||
int offset, int limit) {
|
||||
public List<FeedEntryWithStatus> getEntries(Feed feed, User user,
|
||||
boolean unreadOnly, int offset, int limit) {
|
||||
String queryName = null;
|
||||
if (read) {
|
||||
queryName = "Entry.readByFeed";
|
||||
} else {
|
||||
if (unreadOnly) {
|
||||
queryName = "Entry.unreadByFeed";
|
||||
}
|
||||
TypedQuery<FeedEntry> query = em.createNamedQuery(queryName,
|
||||
FeedEntry.class);
|
||||
query.setParameter("feed", feed);
|
||||
query.setParameter("user", user);
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntry> getEntries(List<FeedCategory> categories, User user,
|
||||
boolean read) {
|
||||
return getEntries(categories, user, read, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntry> getEntries(List<FeedCategory> categories, User user,
|
||||
boolean read, int offset, int limit) {
|
||||
String queryName = null;
|
||||
if (read) {
|
||||
queryName = "Entry.readByCategories";
|
||||
} else {
|
||||
queryName = "Entry.unreadByCategories";
|
||||
queryName = "Entry.allByFeed";
|
||||
}
|
||||
TypedQuery<FeedEntry> query = em.createNamedQuery(queryName,
|
||||
FeedEntry.class);
|
||||
Query query = em.createNamedQuery(queryName);
|
||||
query.setParameter("feed", feed);
|
||||
query.setParameter("userId", user.getId());
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(List<FeedCategory> categories, User user,
|
||||
boolean unreadOnly) {
|
||||
return getEntries(categories, user, unreadOnly, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(List<FeedCategory> categories, User user,
|
||||
boolean unreadOnly, int offset, int limit) {
|
||||
String queryName = null;
|
||||
if (unreadOnly) {
|
||||
queryName = "Entry.unreadByCategories";
|
||||
} else {
|
||||
queryName = "Entry.allByCategories";
|
||||
}
|
||||
Query query = em.createNamedQuery(queryName);
|
||||
query.setParameter("categories", categories);
|
||||
query.setParameter("userId", user.getId());
|
||||
query.setParameter("user", user);
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
@@ -110,6 +114,18 @@ public class FeedEntryService extends GenericDAO<FeedEntry, Long> {
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return query.getResultList();
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private List<FeedEntryWithStatus> buildList(List list){
|
||||
List<FeedEntryWithStatus> result = Lists.newArrayList();
|
||||
for (Object object :list) {
|
||||
Object[] array = (Object[]) object;
|
||||
FeedEntry entry = (FeedEntry) array[0];
|
||||
FeedEntryStatus status = (FeedEntryStatus) array[1];
|
||||
result.add(new FeedEntryWithStatus(entry, status));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package com.commafeed.backend.model;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Lob;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import org.apache.commons.codec.binary.StringUtils;
|
||||
|
||||
@Entity
|
||||
@Table(name = "FEEDENTRIES")
|
||||
@SuppressWarnings("serial")
|
||||
@@ -26,7 +30,7 @@ public class FeedEntry extends AbstractModel {
|
||||
private String title;
|
||||
|
||||
@Lob
|
||||
private String content;
|
||||
private byte[] content;
|
||||
|
||||
@Column(length = 2048)
|
||||
private String url;
|
||||
@@ -34,6 +38,9 @@ public class FeedEntry extends AbstractModel {
|
||||
@Temporal(TemporalType.TIMESTAMP)
|
||||
private Date updated;
|
||||
|
||||
@OneToMany(mappedBy = "entry")
|
||||
private Set<FeedEntryStatus> statuses;
|
||||
|
||||
public String getGuid() {
|
||||
return guid;
|
||||
}
|
||||
@@ -51,11 +58,11 @@ public class FeedEntry extends AbstractModel {
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
return StringUtils.newStringUtf8(content);
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
this.content = StringUtils.getBytesUtf8(content);
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
@@ -82,4 +89,12 @@ public class FeedEntry extends AbstractModel {
|
||||
this.feed = feed;
|
||||
}
|
||||
|
||||
public Set<FeedEntryStatus> getStatuses() {
|
||||
return statuses;
|
||||
}
|
||||
|
||||
public void setStatuses(Set<FeedEntryStatus> statuses) {
|
||||
this.statuses = statuses;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.commafeed.backend.model.extended;
|
||||
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
|
||||
public class FeedEntryWithStatus {
|
||||
private FeedEntry entry;
|
||||
private FeedEntryStatus status;
|
||||
|
||||
public FeedEntryWithStatus(FeedEntry entry, FeedEntryStatus status) {
|
||||
this.entry = entry;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public FeedEntry getEntry() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
public void setEntry(FeedEntry entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
public FeedEntryStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(FeedEntryStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import com.commafeed.backend.model.FeedCategory;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.commafeed.backend.model.FeedEntryStatus;
|
||||
import com.commafeed.backend.model.FeedSubscription;
|
||||
import com.commafeed.backend.model.extended.FeedEntryWithStatus;
|
||||
import com.commafeed.frontend.model.Entries;
|
||||
import com.commafeed.frontend.model.Entry;
|
||||
import com.google.common.base.Function;
|
||||
@@ -85,21 +86,12 @@ public class EntriesREST extends AbstractREST {
|
||||
int limit, boolean unreadOnly) {
|
||||
List<Entry> entries = Lists.newArrayList();
|
||||
|
||||
if (!unreadOnly) {
|
||||
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
|
||||
subscription.getFeed(), getUser(), true, offset, limit);
|
||||
for (FeedEntry feedEntry : unreadEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry), subscription,
|
||||
true));
|
||||
}
|
||||
List<FeedEntryWithStatus> unreadEntries = feedEntryService.getEntries(
|
||||
subscription.getFeed(), getUser(), unreadOnly, offset, limit);
|
||||
for (FeedEntryWithStatus feedEntry : unreadEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry), subscription));
|
||||
}
|
||||
|
||||
List<FeedEntry> readEntries = feedEntryService.getEntries(
|
||||
subscription.getFeed(), getUser(), false, offset, limit);
|
||||
for (FeedEntry feedEntry : readEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry), subscription,
|
||||
false));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
@@ -108,39 +100,35 @@ public class EntriesREST extends AbstractREST {
|
||||
boolean unreadOnly) {
|
||||
List<Entry> entries = Lists.newArrayList();
|
||||
|
||||
if (!unreadOnly) {
|
||||
List<FeedEntry> unreadEntries = feedEntryService.getEntries(
|
||||
categories, getUser(), true, offset, limit);
|
||||
for (FeedEntry feedEntry : unreadEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry),
|
||||
subMapping.get(feedEntry.getFeed().getId()), true));
|
||||
}
|
||||
List<FeedEntryWithStatus> unreadEntries = feedEntryService.getEntries(
|
||||
categories, getUser(), unreadOnly, offset, limit);
|
||||
for (FeedEntryWithStatus feedEntry : unreadEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry),
|
||||
subMapping.get(feedEntry.getEntry().getFeed().getId())));
|
||||
}
|
||||
|
||||
List<FeedEntry> readEntries = feedEntryService.getEntries(categories,
|
||||
getUser(), false, offset, limit);
|
||||
for (FeedEntry feedEntry : readEntries) {
|
||||
entries.add(populateEntry(buildEntry(feedEntry),
|
||||
subMapping.get(feedEntry.getFeed().getId()), false));
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
private Entry buildEntry(FeedEntry feedEntry) {
|
||||
private Entry buildEntry(FeedEntryWithStatus feedEntryWithStatus) {
|
||||
Entry entry = new Entry();
|
||||
|
||||
FeedEntry feedEntry = feedEntryWithStatus.getEntry();
|
||||
entry.setId(String.valueOf(feedEntry.getId()));
|
||||
entry.setTitle(feedEntry.getTitle());
|
||||
entry.setContent(feedEntry.getContent());
|
||||
entry.setDate(feedEntry.getUpdated());
|
||||
entry.setUrl(feedEntry.getUrl());
|
||||
|
||||
FeedEntryStatus status = feedEntryWithStatus.getStatus();
|
||||
entry.setRead(status == null ? false : status.isRead());
|
||||
|
||||
return entry;
|
||||
}
|
||||
|
||||
private Entry populateEntry(Entry entry, FeedSubscription sub, boolean read) {
|
||||
private Entry populateEntry(Entry entry, FeedSubscription sub) {
|
||||
entry.setFeedName(sub.getTitle());
|
||||
entry.setFeedId(String.valueOf(sub.getId()));
|
||||
entry.setRead(read);
|
||||
return entry;
|
||||
}
|
||||
|
||||
@@ -156,16 +144,14 @@ public class EntriesREST extends AbstractREST {
|
||||
FeedEntry entry = feedEntryService.findById(Long.valueOf(id));
|
||||
markEntry(entry, read);
|
||||
} else if (type == Type.feed) {
|
||||
List<FeedEntry> entries = Lists.newArrayList();
|
||||
Feed feed = feedSubscriptionService.findById(Long.valueOf(id))
|
||||
.getFeed();
|
||||
if (read) {
|
||||
entries.addAll(feedEntryService.getEntries(feed, getUser(),
|
||||
false));
|
||||
}
|
||||
entries.addAll(feedEntryService.getEntries(feed, getUser(), true));
|
||||
for (FeedEntry entry : entries) {
|
||||
markEntry(entry, read);
|
||||
List<FeedEntryWithStatus> entries = feedEntryService
|
||||
.getEntries(feed, getUser(), false);
|
||||
for (FeedEntryWithStatus entry : entries) {
|
||||
markEntry(entry.getEntry(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ public class SubscriptionsREST extends AbstractREST {
|
||||
sub.setName(subscription.getTitle());
|
||||
sub.setMessage(subscription.getFeed().getMessage());
|
||||
int size = feedEntryService.getEntries(subscription.getFeed(),
|
||||
getUser(), false).size();
|
||||
getUser(), true).size();
|
||||
sub.setUnread(size);
|
||||
category.getFeeds().add(sub);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user