mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
major improvement in the way data is stored
This commit is contained in:
@@ -3,27 +3,19 @@ package com.commafeed.backend.dao;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
import javax.persistence.Query;
|
||||
import javax.persistence.TypedQuery;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
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.FeedSubscription;
|
||||
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;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
@Stateless
|
||||
@SuppressWarnings("serial")
|
||||
@@ -36,9 +28,7 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
public void updateEntries(String url, Collection<FeedEntry> entries) {
|
||||
Feed feed = Iterables.getFirst(
|
||||
feedService.findByField(MF.i(MF.p(Feed.class).getUrl()), url),
|
||||
null);
|
||||
Feed feed = feedService.findByUrl(url);
|
||||
List<String> guids = Lists.newArrayList();
|
||||
for (FeedEntry entry : entries) {
|
||||
guids.add(entry.getGuid());
|
||||
@@ -56,7 +46,6 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
}
|
||||
if (foundEntry == null) {
|
||||
addFeedToEntry(entry, feed);
|
||||
save(entry);
|
||||
} else {
|
||||
boolean foundFeed = false;
|
||||
for (Feed existingFeed : foundEntry.getFeeds()) {
|
||||
@@ -68,7 +57,6 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
|
||||
if (!foundFeed) {
|
||||
addFeedToEntry(foundEntry, feed);
|
||||
update(foundEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,12 +68,13 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
|
||||
private void addFeedToEntry(FeedEntry entry, Feed feed) {
|
||||
entry.getFeeds().add(feed);
|
||||
saveOrUpdate(entry);
|
||||
List<FeedSubscription> subscriptions = feedSubscriptionService
|
||||
.findByFeed(feed);
|
||||
for (FeedSubscription sub : subscriptions) {
|
||||
FeedEntryStatus status = new FeedEntryStatus();
|
||||
status.setEntry(entry);
|
||||
status.setUser(sub.getUser());
|
||||
status.setSubscription(sub);
|
||||
em.persist(status);
|
||||
}
|
||||
|
||||
@@ -98,123 +87,4 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntriesByKeywords(User user,
|
||||
String keywords) {
|
||||
return getEntriesByKeywords(user, keywords, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntriesByKeywords(User user,
|
||||
String keywords, int offset, int limit) {
|
||||
Query query = em.createNamedQuery("Entry.allByKeywords");
|
||||
query.setParameter("userId", user.getId());
|
||||
query.setParameter("user", user);
|
||||
|
||||
String joinedKeywords = StringUtils.join(
|
||||
keywords.toLowerCase().split(" "), "%");
|
||||
query.setParameter("keywords", "%" + joinedKeywords + "%");
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(User user, boolean unreadOnly) {
|
||||
return getEntries(user, unreadOnly, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(User user, boolean unreadOnly,
|
||||
int offset, int limit) {
|
||||
String queryName = null;
|
||||
if (unreadOnly) {
|
||||
queryName = "Entry.unread";
|
||||
} else {
|
||||
queryName = "Entry.all";
|
||||
}
|
||||
Query query = em.createNamedQuery(queryName);
|
||||
query.setParameter("userId", user.getId());
|
||||
query.setParameter("user", user);
|
||||
if (offset > -1) {
|
||||
query.setFirstResult(offset);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
public Long getUnreadCount(Feed feed, User user) {
|
||||
TypedQuery<Long> query = em.createNamedQuery("Entry.unreadByFeedCount",
|
||||
Long.class);
|
||||
query.setParameter("feed", feed);
|
||||
query.setParameter("userId", user.getId());
|
||||
return query.getSingleResult();
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(Feed feed, User user,
|
||||
boolean unreadOnly) {
|
||||
return getEntries(feed, user, unreadOnly, -1, -1);
|
||||
}
|
||||
|
||||
public List<FeedEntryWithStatus> getEntries(Feed feed, User user,
|
||||
boolean unreadOnly, int offset, int limit) {
|
||||
String queryName = null;
|
||||
if (unreadOnly) {
|
||||
queryName = "Entry.unreadByFeed";
|
||||
} else {
|
||||
queryName = "Entry.allByFeed";
|
||||
}
|
||||
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);
|
||||
}
|
||||
if (limit > -1) {
|
||||
query.setMaxResults(limit);
|
||||
}
|
||||
return buildList(query.getResultList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private List<FeedEntryWithStatus> buildList(List list) {
|
||||
Set<FeedEntryWithStatus> result = Sets.newLinkedHashSet();
|
||||
for (Object object : list) {
|
||||
Object[] array = (Object[]) object;
|
||||
FeedEntry entry = (FeedEntry) array[0];
|
||||
FeedEntryStatus status = (FeedEntryStatus) array[1];
|
||||
FeedEntryWithStatus fews = new FeedEntryWithStatus(entry, status);
|
||||
result.add(fews);
|
||||
}
|
||||
return Lists.newArrayList(result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user