store only 10 entries for new users

This commit is contained in:
Athou
2013-04-10 16:45:05 +02:00
parent edfafeb7b0
commit 581c7bc070
5 changed files with 41 additions and 16 deletions

View File

@@ -7,6 +7,9 @@ import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@@ -14,6 +17,7 @@ import org.apache.commons.lang.StringUtils;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryStatus;
import com.commafeed.backend.model.FeedEntry_;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.collect.Lists;
@@ -37,7 +41,7 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
}
List<FeedEntry> existingEntries = guids.isEmpty() ? new ArrayList<FeedEntry>()
: getByGuids(guids);
: findByGuids(guids);
for (FeedEntry entry : entries) {
FeedEntry foundEntry = null;
for (FeedEntry existingEntry : existingEntries) {
@@ -80,7 +84,7 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
}
public List<FeedEntry> getByGuids(List<String> guids) {
public List<FeedEntry> findByGuids(List<String> guids) {
EasyCriteria<FeedEntry> criteria = createCriteria();
criteria.setDistinctTrue();
criteria.andStringIn(MF.i(proxy().getGuid()), guids);
@@ -88,4 +92,14 @@ public class FeedEntryService extends GenericDAO<FeedEntry> {
return criteria.getResultList();
}
public List<FeedEntry> findByFeed(Feed feed, int offset, int limit) {
CriteriaQuery<FeedEntry> query = builder.createQuery(getType());
Root<FeedEntry> root = query.from(getType());
query.where(builder.isMember(feed, root.get(FeedEntry_.feeds)));
query.orderBy(builder.desc(root.get(FeedEntry_.updated)));
TypedQuery<FeedEntry> q = em.createQuery(query);
q.setFirstResult(offset);
q.setMaxResults(limit);
return q.getResultList();
}
}

View File

@@ -25,10 +25,13 @@ public class FeedService extends GenericDAO<Feed> {
return Iterables.getFirst(feeds, null);
}
public Feed getByIdWithEntries(Long feedId) {
public Feed getByIdWithEntries(Long feedId, int offset, int limit) {
EasyCriteria<Feed> criteria = createCriteria();
criteria.andEquals(MF.i(proxy().getId()), feedId);
criteria.leftJoinFetch(MF.i(proxy().getEntries()));
criteria.setFirstResult(offset);
criteria.setMaxResults(limit);
return criteria.getSingleResult();
}
}

View File

@@ -1,7 +1,6 @@
package com.commafeed.backend.dao;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.inject.Inject;
@@ -28,6 +27,9 @@ public class FeedSubscriptionService extends GenericDAO<FeedSubscription> {
@Inject
FeedService feedService;
@Inject
FeedEntryService feedEntryService;
@Inject
FeedEntryStatusService feedEntryStatusService;
@@ -42,26 +44,30 @@ public class FeedSubscriptionService extends GenericDAO<FeedSubscription> {
}
FeedSubscription sub = findByFeed(user, feed);
boolean newSubscription = false;
if (sub == null) {
sub = new FeedSubscription();
sub.setFeed(feed);
sub.setUser(user);
newSubscription = true;
}
sub.setCategory(category);
sub.setTitle(title);
saveOrUpdate(sub);
List<FeedEntryStatus> statuses = Lists.newArrayList();
Set<FeedEntry> allEntries = feedService
.getByIdWithEntries(feed.getId()).getEntries();
for (FeedEntry entry : allEntries) {
FeedEntryStatus status = new FeedEntryStatus();
status.setEntry(entry);
status.setRead(true);
status.setSubscription(sub);
statuses.add(status);
if (newSubscription) {
List<FeedEntryStatus> statuses = Lists.newArrayList();
List<FeedEntry> allEntries = feedEntryService.findByFeed(feed, 0,
10);
for (FeedEntry entry : allEntries) {
FeedEntryStatus status = new FeedEntryStatus();
status.setEntry(entry);
status.setRead(true);
status.setSubscription(sub);
statuses.add(status);
}
feedEntryStatusService.save(statuses);
}
feedEntryStatusService.save(statuses);
}
public FeedSubscription findById(User user, Long id) {

View File

@@ -3,6 +3,7 @@ package com.commafeed.frontend.pages;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.Url;
import org.apache.wicket.request.UrlRenderer;
@@ -59,7 +60,8 @@ public class GoogleImportCallbackPage extends WebPage {
String code = responseUrl.getCode();
if (responseUrl.getError() != null) {
throw new DisplayException(responseUrl.getError());
// user declined
throw new RestartResponseException(getApplication().getHomePage());
} else if (code == null) {
throw new DisplayException("Missing authorization code");
} else {

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", "auto");
builder.addParameter("approval_prompt", "force");
builder.addParameter("client_id", clientId);
builder.addParameter("state",
String.valueOf(CommaFeedSession.get().getUser().getId()));