forked from Archives/Athou_commafeed
using hibernate to fetch entries now, able to fetch entries in one go
This commit is contained in:
27
pom.xml
27
pom.xml
@@ -69,6 +69,17 @@
|
||||
<updateOnlyExtension>.css</updateOnlyExtension>
|
||||
</updateOnlyExtensions>
|
||||
</synchronization>
|
||||
<libs>
|
||||
<lib>org.hibernate:hibernate-entitymanager:4.1.10.Final</lib>
|
||||
<lib>org.hibernate:hibernate-core:4.1.10.Final</lib>
|
||||
<lib>org.hibernate.common:hibernate-commons-annotations:4.0.1.Final</lib>
|
||||
<lib>org.hibernate:hibernate-validator:4.3.1.Final</lib>
|
||||
<lib>org.jboss.logging:jboss-logging:3.1.3.GA</lib>
|
||||
<lib>dom4j:dom4j:1.6.1</lib>
|
||||
<lib>antlr:antlr:2.7.7</lib>
|
||||
<lib>remove:hsqldb</lib>
|
||||
<lib>org.hsqldb:hsqldb:SNAPSHOT</lib>
|
||||
</libs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
@@ -82,6 +93,12 @@
|
||||
<type>pom</type>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-entitymanager</artifactId>
|
||||
<version>4.1.10.Final</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>uaihebert.com</groupId>
|
||||
<artifactId>EasyCriteria</artifactId>
|
||||
@@ -211,6 +228,16 @@
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>hsqldb.snapshots</id>
|
||||
<url>http://www.hsqldb.org/repos/</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@
|
||||
<query>select e from FeedEntry e where e.guid in (:guids) order by e.updated desc</query>
|
||||
</named-query>
|
||||
<named-query name="Entry.unreadByFeed">
|
||||
<query>select e from FeedEntry e where e.feed=:feed and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||
<query>select e, s from FeedEntry e LEFT JOIN e.statuses s WITH (s.user.id=:userId) where e.feed=:feed and not exists (select s2 from FeedEntryStatus s2 where s2.entry=e and s2.user.id=:userId and s2.read=true) order by e.updated desc</query>
|
||||
</named-query>
|
||||
<named-query name="Entry.readByFeed">
|
||||
<query>select e from FeedEntry e where e.feed=:feed and exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||
<named-query name="Entry.allByFeed">
|
||||
<query>select e, s from FeedEntry e LEFT JOIN e.statuses s WITH (s.user.id=:userId) where e.feed=:feed order by e.updated desc</query>
|
||||
</named-query>
|
||||
|
||||
<named-query name="Entry.unreadByCategories">
|
||||
<query>select e from FeedEntry e where exists (select s from FeedSubscription s where s.user=:user and s.feed = e.feed and s.category in (:categories) ) and not exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||
<query>select e, s from FeedEntry e LEFT JOIN e.statuses s WITH (s.user.id=:userId) where exists (select s2 from FeedSubscription s2 where s2.user=:user and s2.feed = e.feed and s2.category in (:categories) ) and not exists (select s3 from FeedEntryStatus s3 where s3.entry = e and s3.user =:user and s3.read=true) order by e.updated desc</query>
|
||||
</named-query>
|
||||
<named-query name="Entry.readByCategories">
|
||||
<query>select e from FeedEntry e where exists (select s from FeedSubscription s where s.user=:user and s.feed = e.feed and s.category in (:categories) ) and exists (select s from FeedEntryStatus s where s.entry = e and s.user =:user and s.read=true) order by e.updated desc</query>
|
||||
<named-query name="Entry.allByCategories">
|
||||
<query>select e, s from FeedEntry e LEFT JOIN e.statuses s WITH (s.user.id=:userId) where exists (select s2 from FeedSubscription s2 where s2.user=:user and s2.feed = e.feed and s2.category in (:categories) ) order by e.updated desc</query>
|
||||
</named-query>
|
||||
</entity-mappings>
|
||||
@@ -5,6 +5,7 @@
|
||||
http://java.sun.com/xml/ns/persistence
|
||||
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
|
||||
<persistence-unit name="primary">
|
||||
<provider>org.hibernate.ejb.HibernatePersistence</provider>
|
||||
<jta-data-source>${jpa.datasource.name}</jta-data-source>
|
||||
<properties>
|
||||
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
|
||||
|
||||
@@ -87,6 +87,8 @@ module.factory('EntryService', [
|
||||
|
||||
module.service('SettingsService', function($resource) {
|
||||
var s = {}
|
||||
s.settings = {};
|
||||
s.settings.readMode = 'unread';
|
||||
s.settings = $resource('rest/settings/get').get();
|
||||
s.save = function() {
|
||||
$resource('rest/settings/save').save(s.settings);
|
||||
|
||||
Reference in New Issue
Block a user