package refactoring

This commit is contained in:
Athou
2013-03-23 16:17:19 +01:00
parent 7067646b2d
commit 4df1e7f722
36 changed files with 60 additions and 56 deletions

View File

@@ -0,0 +1,60 @@
package com.commafeed.frontend.rest.resources;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.wicket.Application;
import org.apache.wicket.ThreadContext;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.apache.wicket.protocol.http.servlet.ServletWebResponse;
import org.apache.wicket.request.cycle.RequestCycle;
import com.commafeed.backend.dao.FeedCategoryService;
import com.commafeed.backend.dao.FeedEntryService;
import com.commafeed.backend.dao.FeedEntryStatusService;
import com.commafeed.backend.dao.FeedSubscriptionService;
import com.commafeed.backend.model.User;
import com.commafeed.frontend.CommaFeedSession;
@Produces(MediaType.APPLICATION_JSON)
public abstract class AbstractREST {
@Context
HttpServletRequest request;
@Context
HttpServletResponse response;
@Inject
FeedSubscriptionService feedSubscriptionService;
@Inject
FeedCategoryService feedCategoryService;
@Inject
FeedEntryService feedEntryService;
@Inject
FeedEntryStatusService feedEntryStatusService;
@PostConstruct
public void init() {
ServletWebRequest swreq = new ServletWebRequest(request, "");
ServletWebResponse swresp = new ServletWebResponse(swreq, response);
RequestCycle cycle = Application.get()
.createRequestCycle(swreq, swresp);
ThreadContext.setRequestCycle(cycle);
Application.get().fetchCreateAndSetSession(
Application.get().createRequestCycle(swreq, swresp));
}
protected User getUser() {
return CommaFeedSession.get().getUser();
}
}

View File

@@ -0,0 +1,96 @@
package com.commafeed.frontend.rest.resources;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.apache.commons.lang.ObjectUtils;
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.frontend.model.Entries;
import com.commafeed.frontend.model.Entry;
import com.commafeed.frontend.utils.ModelFactory.MF;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
@Path("entries")
public class EntriesREST extends AbstractREST {
@Path("get/{type}/{id}/{readType}")
@GET
public Entries getEntries(@PathParam("type") String type,
@PathParam("id") String id, @PathParam("readType") String readType) {
Entries entries = new Entries();
if ("feed".equals(type)) {
FeedSubscription subscription = feedSubscriptionService
.findById(Long.valueOf(id));
entries.setName(subscription.getTitle());
entries.getEntries().addAll(buildEntries(subscription));
} else {
FeedCategory feedCategory = "all".equals(id) ? null
: feedCategoryService.findById(Long.valueOf(id));
Collection<FeedSubscription> subscriptions = "all".equals(id) ? feedSubscriptionService
.findAll(getUser()) : feedSubscriptionService
.findWithCategory(getUser(), feedCategory);
entries.setName("all".equals(id) ? "All" : feedCategory.getName());
for (FeedSubscription subscription : subscriptions) {
entries.getEntries().addAll(buildEntries(subscription));
}
}
Collections.sort(entries.getEntries(), new Comparator<Entry>() {
@Override
public int compare(Entry e1, Entry e2) {
return ObjectUtils.compare(e2.getDate(), e1.getDate());
}
});
return entries;
}
private List<Entry> buildEntries(FeedSubscription subscription) {
List<FeedEntry> feedEntries = feedEntryService.getUnreadEntries(
subscription.getFeed(), getUser());
List<Entry> entries = Lists.newArrayList();
for (FeedEntry feedEntry : feedEntries) {
List<FeedEntryStatus> feedEntryStatus = feedEntryStatusService
.findByField(MF.i(MF.p(FeedEntryStatus.class).getEntry()),
feedEntry);
Entry entry = buildEntry(feedEntry);
entry.setFeedName(subscription.getTitle());
entry.setFeedId(String.valueOf(subscription.getId()));
entry.setRead(feedEntryStatus.isEmpty() ? false : Iterables
.getFirst(feedEntryStatus, null).isRead());
entry.setStarred(feedEntryStatus.isEmpty() ? false : Iterables
.getFirst(feedEntryStatus, null).isStarred());
entries.add(entry);
}
return entries;
}
private Entry buildEntry(FeedEntry feedEntry) {
Entry entry = new Entry();
entry.setId(String.valueOf(feedEntry.getId()));
entry.setTitle(feedEntry.getTitle());
entry.setContent(feedEntry.getContent());
entry.setDate(feedEntry.getUpdated());
entry.setUrl(feedEntry.getUrl());
return entry;
}
}

View File

@@ -0,0 +1,66 @@
package com.commafeed.frontend.rest.resources;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import org.apache.commons.lang.ObjectUtils;
import com.commafeed.backend.model.FeedCategory;
import com.commafeed.backend.model.FeedSubscription;
import com.commafeed.frontend.model.Category;
import com.commafeed.frontend.model.Subscription;
@Path("subscriptions")
public class SubscriptionsREST extends AbstractREST {
@GET
public Category getSubscriptions() {
Category root = new Category();
List<FeedCategory> categories = feedCategoryService.findAll(getUser());
addChildren(categories, root);
root.setId("all");
root.setName("All");
for (FeedSubscription subscription : feedSubscriptionService
.findWithoutCategories(getUser())) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
int size = feedEntryService.getUnreadEntries(
subscription.getFeed(), getUser()).size();
sub.setUnread(size);
root.getFeeds().add(sub);
}
return root;
}
private void addChildren(List<FeedCategory> categories, Category current) {
for (FeedCategory category : categories) {
if ((category.getParent() == null && current.getId() == null)
|| (category.getParent() != null && (ObjectUtils.equals(
String.valueOf(category.getParent().getId()),
current.getId())))) {
Category child = new Category();
child.setId(String.valueOf(category.getId()));
child.setName(category.getName());
addChildren(categories, child);
for (FeedSubscription subscription : category
.getSubscriptions()) {
Subscription sub = new Subscription();
sub.setId(subscription.getId());
sub.setName(subscription.getTitle());
int size = feedEntryService.getUnreadEntries(
subscription.getFeed(), getUser()).size();
sub.setUnread(size);
child.getFeeds().add(sub);
}
current.getChildren().add(child);
}
}
}
}