major improvement in the way data is stored

This commit is contained in:
Athou
2013-04-08 13:06:53 +02:00
parent 028982477e
commit cf7d4cce9c
14 changed files with 274 additions and 407 deletions

View File

@@ -38,7 +38,7 @@ public class GoogleImportRedirectPage extends WebPage {
builder.addParameter("redirect_uri", redirectUri);
builder.addParameter("response_type", "code");
builder.addParameter("scope", SCOPE);
builder.addParameter("approval_prompt", "force");
builder.addParameter("approval_prompt", "auto");
builder.addParameter("client_id", clientId);
builder.addParameter("state",
String.valueOf(CommaFeedSession.get().getUser().getId()));

View File

@@ -1,7 +1,6 @@
package com.commafeed.frontend.rest.resources;
import java.lang.reflect.Method;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
@@ -19,6 +18,7 @@ import javax.ws.rs.core.Response.Status;
import org.apache.wicket.ThreadContext;
import org.apache.wicket.authentication.IAuthenticationStrategy;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.protocol.http.servlet.ServletWebResponse;
import org.apache.wicket.request.cycle.RequestCycle;
@@ -94,11 +94,13 @@ public abstract class AbstractREST {
CommaFeedSession session = (CommaFeedSession) app
.fetchCreateAndSetSession(cycle);
IAuthenticationStrategy authenticationStrategy = app
.getSecuritySettings().getAuthenticationStrategy();
String[] data = authenticationStrategy.load();
if (data != null && data.length > 1) {
session.signIn(data[0], data[1]);
if (session.getUser() == null) {
IAuthenticationStrategy authenticationStrategy = app
.getSecuritySettings().getAuthenticationStrategy();
String[] data = authenticationStrategy.load();
if (data != null && data.length > 1) {
session.signIn(data[0], data[1]);
}
}
}
@@ -137,11 +139,10 @@ public abstract class AbstractREST {
}
private boolean checkRole(User user, SecurityCheck annotation) {
Set<Role> roles = userRoleService.getRoles(user);
if (!roles.contains(annotation.value())) {
return false;
}
return true;
Roles roles = CommaFeedSession.get().getRoles();
boolean authorized = roles.hasAnyRole(new Roles(annotation.value()
.name()));
return authorized;
}
}

View File

@@ -134,7 +134,8 @@ public class AdminUsersREST extends AbstractREST {
return Response.status(Status.FORBIDDEN)
.entity("You cannot delete the admin user.").build();
}
feedEntryStatusService.delete(feedEntryStatusService.findAll(user));
feedEntryStatusService.delete(feedEntryStatusService.getStatuses(user,
false));
feedSubscriptionService.delete(feedSubscriptionService.findAll(user));
feedCategoryService.delete(feedCategoryService.findAll(user));
userSettingsService.delete(userSettingsService.findByUser(user));

View File

@@ -1,29 +1,25 @@
package com.commafeed.frontend.rest.resources;
import java.util.List;
import java.util.Map;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
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.extended.FeedEntryWithStatus;
import com.commafeed.frontend.model.Entries;
import com.commafeed.frontend.model.Entry;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Path("entries")
public class EntriesREST extends AbstractREST {
@@ -59,34 +55,37 @@ public class EntriesREST extends AbstractREST {
if (subscription != null) {
entries.setName(subscription.getTitle());
entries.setMessage(subscription.getFeed().getMessage());
entries.getEntries().addAll(
buildEntries(subscription, offset, limit, unreadOnly));
List<FeedEntryStatus> unreadEntries = feedEntryStatusService
.getStatuses(subscription.getFeed(), getUser(),
unreadOnly, offset, limit);
for (FeedEntryStatus status : unreadEntries) {
entries.getEntries().add(buildEntry(status));
}
}
} else {
List<FeedSubscription> subs = feedSubscriptionService
.findAll(getUser());
Map<Long, FeedSubscription> subMapping = Maps.uniqueIndex(subs,
new Function<FeedSubscription, Long>() {
public Long apply(FeedSubscription sub) {
return sub.getFeed().getId();
}
});
if (ALL.equals(id)) {
entries.setName("All");
entries.getEntries().addAll(
buildEntries(subMapping, offset, limit, unreadOnly));
List<FeedEntryStatus> unreadEntries = feedEntryStatusService
.getStatuses(getUser(), unreadOnly, offset, limit);
for (FeedEntryStatus status : unreadEntries) {
entries.getEntries().add(buildEntry(status));
}
} else {
FeedCategory feedCategory = feedCategoryService.findById(
getUser(), Long.valueOf(id));
if (feedCategory != null) {
List<FeedCategory> childrenCategories = feedCategoryService
.findAllChildrenCategories(getUser(), feedCategory);
entries.getEntries().addAll(
buildEntries(childrenCategories, subMapping,
offset, limit, unreadOnly));
List<FeedEntryStatus> unreadEntries = feedEntryStatusService
.getStatuses(childrenCategories, getUser(),
unreadOnly, offset, limit);
for (FeedEntryStatus status : unreadEntries) {
entries.getEntries().add(buildEntry(status));
}
entries.setName(feedCategory.getName());
}
}
@@ -96,79 +95,21 @@ public class EntriesREST extends AbstractREST {
return entries;
}
private List<Entry> buildEntries(Map<Long, FeedSubscription> subMapping,
int offset, int limit, boolean unreadOnly) {
List<Entry> entries = Lists.newArrayList();
List<FeedEntryWithStatus> unreadEntries = feedEntryService.getEntries(
getUser(), unreadOnly, offset, limit);
for (FeedEntryWithStatus feedEntry : unreadEntries) {
FeedSubscription sub = null;
for (Feed feed : feedEntry.getEntry().getFeeds()) {
sub = subMapping.get(feed.getId());
if (sub != null) {
break;
}
}
entries.add(populateEntry(buildEntry(feedEntry), sub));
}
return entries;
}
private List<Entry> buildEntries(FeedSubscription subscription, int offset,
int limit, boolean unreadOnly) {
List<Entry> entries = Lists.newArrayList();
List<FeedEntryWithStatus> unreadEntries = feedEntryService.getEntries(
subscription.getFeed(), getUser(), unreadOnly, offset, limit);
for (FeedEntryWithStatus feedEntry : unreadEntries) {
entries.add(populateEntry(buildEntry(feedEntry), subscription));
}
return entries;
}
private List<Entry> buildEntries(List<FeedCategory> categories,
Map<Long, FeedSubscription> subMapping, int offset, int limit,
boolean unreadOnly) {
List<Entry> entries = Lists.newArrayList();
List<FeedEntryWithStatus> unreadEntries = feedEntryService.getEntries(
categories, getUser(), unreadOnly, offset, limit);
for (FeedEntryWithStatus feedEntry : unreadEntries) {
FeedSubscription sub = null;
for (Feed feed : feedEntry.getEntry().getFeeds()) {
sub = subMapping.get(feed.getId());
if (sub != null) {
break;
}
}
entries.add(populateEntry(buildEntry(feedEntry), sub));
}
return entries;
}
private Entry buildEntry(FeedEntryWithStatus feedEntryWithStatus) {
private Entry buildEntry(FeedEntryStatus status) {
Entry entry = new Entry();
FeedEntry feedEntry = feedEntryWithStatus.getEntry();
entry.setId(String.valueOf(feedEntry.getId()));
FeedEntry feedEntry = status.getEntry();
entry.setId(String.valueOf(status.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());
entry.setRead(status.isRead());
return entry;
}
entry.setFeedName(status.getSubscription().getTitle());
entry.setFeedId(String.valueOf(status.getSubscription().getId()));
private Entry populateEntry(Entry entry, FeedSubscription sub) {
entry.setFeedName(sub.getTitle());
entry.setFeedId(String.valueOf(sub.getId()));
return entry;
}
@@ -181,70 +122,40 @@ public class EntriesREST extends AbstractREST {
Preconditions.checkNotNull(read);
if (type == Type.entry) {
FeedEntry entry = feedEntryService.findById(Long.valueOf(id));
markEntry(entry, read);
FeedEntryStatus status = feedEntryStatusService.findById(getUser(),
Long.valueOf(id));
status.setRead(read);
feedEntryStatusService.update(status);
} else if (type == Type.feed) {
if (read) {
Feed feed = feedSubscriptionService.findById(Long.valueOf(id))
.getFeed();
List<FeedEntryWithStatus> entries = feedEntryService
.getEntries(feed, getUser(), true);
for (FeedEntryWithStatus entry : entries) {
markEntry(entry, read);
}
FeedSubscription subscription = feedSubscriptionService
.findById(getUser(), Long.valueOf(id));
feedEntryStatusService.markFeedEntries(getUser(),
subscription.getFeed());
} else {
return Response.status(Status.FORBIDDEN)
.entity("Operation not supported").build();
throw new WebApplicationException(Response.status(
Status.INTERNAL_SERVER_ERROR).build());
}
} else if (type == Type.category) {
if (read) {
List<FeedEntryWithStatus> entries = null;
if (ALL.equals(id)) {
entries = feedEntryService.getEntries(getUser(), true);
feedEntryStatusService.markAllEntries(getUser());
} else {
FeedCategory feedCategory = feedCategoryService.findById(
getUser(), Long.valueOf(id));
List<FeedCategory> childrenCategories = feedCategoryService
.findAllChildrenCategories(getUser(), feedCategory);
entries = feedEntryService.getEntries(childrenCategories,
getUser(), true);
}
for (FeedEntryWithStatus entry : entries) {
markEntry(entry, read);
List<FeedCategory> categories = feedCategoryService
.findAllChildrenCategories(getUser(),
feedCategoryService.findById(getUser(),
Long.valueOf(id)));
feedEntryStatusService.markCategoryEntries(getUser(),
categories);
}
} else {
return Response.status(Status.FORBIDDEN)
.entity("Operation not supported").build();
throw new WebApplicationException(Response.status(
Status.INTERNAL_SERVER_ERROR).build());
}
}
return Response.ok(Status.OK).build();
}
private void markEntry(FeedEntry entry, boolean read) {
FeedEntryStatus status = feedEntryStatusService.getStatus(getUser(),
entry);
if (status == null) {
status = new FeedEntryStatus();
status.setUser(getUser());
status.setEntry(entry);
}
status.setRead(read);
feedEntryStatusService.saveOrUpdate(status);
}
private void markEntry(FeedEntryWithStatus entryWithStatus, boolean read) {
FeedEntryStatus status = entryWithStatus.getStatus();
if (status == null) {
status = new FeedEntryStatus();
status.setUser(getUser());
status.setEntry(entryWithStatus.getEntry());
}
status.setRead(read);
feedEntryStatusService.saveOrUpdate(status);
}
@Path("search")
@GET
public Entries searchEntries(@QueryParam("keywords") String keywords) {
@@ -252,27 +163,11 @@ public class EntriesREST extends AbstractREST {
Entries entries = new Entries();
List<FeedSubscription> subs = feedSubscriptionService
.findAll(getUser());
Map<Long, FeedSubscription> subMapping = Maps.uniqueIndex(subs,
new Function<FeedSubscription, Long>() {
public Long apply(FeedSubscription sub) {
return sub.getFeed().getId();
}
});
List<Entry> list = Lists.newArrayList();
List<FeedEntryWithStatus> entriesWithStatus = feedEntryService
.getEntriesByKeywords(getUser(), keywords);
for (FeedEntryWithStatus feedEntry : entriesWithStatus) {
FeedSubscription sub = null;
for (Feed feed : feedEntry.getEntry().getFeeds()) {
sub = subMapping.get(feed.getId());
if (sub != null) {
break;
}
}
list.add(populateEntry(buildEntry(feedEntry), sub));
List<FeedEntryStatus> entriesStatus = feedEntryStatusService
.getStatusesByKeywords(getUser(), keywords);
for (FeedEntryStatus status : entriesStatus) {
list.add(buildEntry(status));
}
entries.setName("Search for : " + keywords);

View File

@@ -3,6 +3,7 @@ package com.commafeed.frontend.rest.resources;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -164,16 +165,19 @@ public class SubscriptionsREST extends AbstractREST {
List<FeedCategory> categories = feedCategoryService.findAll(getUser());
List<FeedSubscription> subscriptions = feedSubscriptionService
.findAll(getUser());
Map<Long, Long> unreadCount = feedEntryStatusService
.getUnreadCount(getUser());
Category root = buildCategory(null, categories, subscriptions);
Category root = buildCategory(null, categories, subscriptions,
unreadCount);
root.setId("all");
root.setName("All");
return root;
}
Category buildCategory(Long id, List<FeedCategory> categories,
List<FeedSubscription> subscriptions) {
private Category buildCategory(Long id, List<FeedCategory> categories,
List<FeedSubscription> subscriptions, Map<Long, Long> unreadCount) {
Category category = new Category();
category.setId(String.valueOf(id));
category.setExpanded(true);
@@ -183,7 +187,7 @@ public class SubscriptionsREST extends AbstractREST {
|| (c.getParent() != null && ObjectUtils.equals(c
.getParent().getId(), id))) {
Category child = buildCategory(c.getId(), categories,
subscriptions);
subscriptions, unreadCount);
child.setId(String.valueOf(c.getId()));
child.setName(c.getName());
child.setExpanded(!c.isCollapsed());
@@ -206,9 +210,8 @@ public class SubscriptionsREST extends AbstractREST {
sub.setName(subscription.getTitle());
sub.setMessage(subscription.getFeed().getMessage());
sub.setFeedUrl(subscription.getFeed().getLink());
long size = feedEntryService.getUnreadCount(
subscription.getFeed(), getUser());
sub.setUnread(size);
Long size = unreadCount.get(subscription.getId());
sub.setUnread(size == null ? 0 : size);
category.getFeeds().add(sub);
}
}