diff --git a/src/main/java/com/commafeed/backend/dao/FeedSubscriptionService.java b/src/main/java/com/commafeed/backend/dao/FeedSubscriptionService.java index 7127255b..74a60762 100644 --- a/src/main/java/com/commafeed/backend/dao/FeedSubscriptionService.java +++ b/src/main/java/com/commafeed/backend/dao/FeedSubscriptionService.java @@ -3,26 +3,66 @@ package com.commafeed.backend.dao; import java.util.List; import javax.ejb.Stateless; +import javax.inject.Inject; +import javax.persistence.TypedQuery; + +import org.apache.commons.lang.ObjectUtils; import com.commafeed.frontend.utils.ModelFactory.MF; import com.commafeed.model.FeedCategory; import com.commafeed.model.FeedSubscription; import com.commafeed.model.User; +import com.google.common.collect.Lists; import com.uaihebert.factory.EasyCriteriaFactory; import com.uaihebert.model.EasyCriteria; @Stateless public class FeedSubscriptionService extends GenericDAO { + @Inject + FeedCategoryService feedCategoryService; + public List findAll(User user) { return findByField(MF.i(MF.p(FeedCategory.class).getUser()), user); } - + public List findWithoutCategories(User user) { - EasyCriteria criteria = EasyCriteriaFactory.createQueryCriteria(em, getType()); + EasyCriteria criteria = EasyCriteriaFactory + .createQueryCriteria(em, getType()); criteria.andEquals("user", user); criteria.andEquals("category", null); return criteria.getResultList(); - + } + + public List findWithCategory(User user, + FeedCategory category) { + + List categories = Lists.newArrayList(); + for (FeedCategory c : feedCategoryService.findAll(user)) { + if (isChild(c, category)) { + categories.add(c); + } + } + + String query = "select s from FeedSubscription s where s.user=:user and s.category in :categories"; + TypedQuery typedQuery = em.createQuery(query, + FeedSubscription.class); + typedQuery.setParameter("user", user); + typedQuery.setParameter("categories", categories); + return typedQuery.getResultList(); + } + + private boolean isChild(FeedCategory c, FeedCategory category) { + boolean isChild = false; + while (c != null) { + if (ObjectUtils.equals(c.getId(), category.getId())) { + isChild = true; + break; + } + c = c.getParent(); + } + return isChild; + } + } diff --git a/src/main/java/com/commafeed/frontend/rest/FeedEntriesREST.java b/src/main/java/com/commafeed/frontend/rest/FeedEntriesREST.java index 20cac017..b8962160 100644 --- a/src/main/java/com/commafeed/frontend/rest/FeedEntriesREST.java +++ b/src/main/java/com/commafeed/frontend/rest/FeedEntriesREST.java @@ -1,22 +1,27 @@ package com.commafeed.frontend.rest; -import java.text.DateFormat; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import javax.inject.Inject; import org.apache.wicket.request.mapper.parameter.PageParameters; +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.frontend.rest.model.Entries; import com.commafeed.frontend.rest.model.Entry; import com.commafeed.frontend.utils.ModelFactory.MF; +import com.commafeed.model.FeedCategory; import com.commafeed.model.FeedEntry; import com.commafeed.model.FeedEntryStatus; import com.commafeed.model.FeedSubscription; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; public class FeedEntriesREST extends JSONPage { @@ -33,6 +38,9 @@ public class FeedEntriesREST extends JSONPage { @Inject FeedSubscriptionService feedSubscriptionService; + @Inject + FeedCategoryService feedCategoryService; + public FeedEntriesREST(PageParameters pageParameters) { super(pageParameters); } @@ -50,23 +58,52 @@ public class FeedEntriesREST extends JSONPage { if ("feed".equals(type)) { FeedSubscription subscription = feedSubscriptionService .findById(Long.valueOf(id)); - List feedEntries = feedEntryService.getUnreadEntries( - subscription.getFeed(), getUser()); - entries.setName(subscription.getTitle()); - for (FeedEntry feedEntry : feedEntries) { - - List 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.getEntries().add(entry); + entries.getEntries().addAll(buildEntries(subscription)); + + } else { + FeedCategory feedCategory = feedCategoryService.findById("all" + .equals(id) ? null : Long.valueOf(id)); + Collection 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() { + + @Override + public int compare(Entry e1, Entry e2) { + return e2.getDate().compareTo(e1.getDate()); + } + }); + return entries; + } + + private List buildEntries(FeedSubscription subscription) { + List feedEntries = feedEntryService.getUnreadEntries( + subscription.getFeed(), getUser()); + + List entries = Lists.newArrayList(); + for (FeedEntry feedEntry : feedEntries) { + + List 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; } @@ -75,8 +112,7 @@ public class FeedEntriesREST extends JSONPage { entry.setId(feedEntry.getGuid()); entry.setTitle(feedEntry.getTitle()); entry.setContent(feedEntry.getContent()); - entry.setDate(DateFormat.getDateInstance(DateFormat.SHORT, getLocale()) - .format(feedEntry.getUpdated())); + entry.setDate(feedEntry.getUpdated()); entry.setUrl(feedEntry.getUrl()); return entry; diff --git a/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java b/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java index c6657ad6..8a1f82df 100644 --- a/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java +++ b/src/main/java/com/commafeed/frontend/rest/FeedSubscriptionsREST.java @@ -10,9 +10,10 @@ import org.apache.wicket.request.mapper.parameter.PageParameters; import com.commafeed.backend.dao.FeedCategoryService; import com.commafeed.backend.dao.FeedEntryService; import com.commafeed.backend.dao.FeedSubscriptionService; +import com.commafeed.frontend.rest.model.Category; +import com.commafeed.frontend.rest.model.Subscription; import com.commafeed.model.FeedCategory; import com.commafeed.model.FeedSubscription; -import com.google.common.collect.Lists; @SuppressWarnings("serial") public class FeedSubscriptionsREST extends JSONPage { @@ -28,7 +29,6 @@ public class FeedSubscriptionsREST extends JSONPage { public FeedSubscriptionsREST(PageParameters pageParameters) { super(pageParameters); - // TODO Auto-generated constructor stub } @Override @@ -36,6 +36,8 @@ public class FeedSubscriptionsREST extends JSONPage { List categories = feedCategoryService.findAll(getUser()); Category root = new Category(); addChildren(categories, root); + root.setId("all"); + root.setName("All"); for (FeedSubscription subscription : feedSubscriptionService .findWithoutCategories(getUser())) { Subscription sub = new Subscription(); @@ -53,9 +55,10 @@ public class FeedSubscriptionsREST extends JSONPage { for (FeedCategory category : categories) { if ((category.getParent() == null && current.getId() == null) || (category.getParent() != null && (ObjectUtils.equals( - category.getParent().getId(), current.getId())))) { + String.valueOf(category.getParent().getId()), + current.getId())))) { Category child = new Category(); - child.setId(category.getId()); + child.setId(String.valueOf(category.getId())); child.setName(category.getName()); addChildren(categories, child); for (FeedSubscription subscription : category @@ -72,76 +75,4 @@ public class FeedSubscriptionsREST extends JSONPage { } } } - - public static class Category { - private Long id; - private String name; - private List children = Lists.newArrayList(); - private List feeds = Lists.newArrayList(); - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public List getChildren() { - return children; - } - - public void setChildren(List children) { - this.children = children; - } - - public List getFeeds() { - return feeds; - } - - public void setFeeds(List feeds) { - this.feeds = feeds; - } - - } - - public static class Subscription { - private Long id; - private String name; - private int unread; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getUnread() { - return unread; - } - - public void setUnread(int unread) { - this.unread = unread; - } - - } - } \ No newline at end of file diff --git a/src/main/java/com/commafeed/frontend/rest/JSONPage.java b/src/main/java/com/commafeed/frontend/rest/JSONPage.java index 7a29429a..65358a85 100644 --- a/src/main/java/com/commafeed/frontend/rest/JSONPage.java +++ b/src/main/java/com/commafeed/frontend/rest/JSONPage.java @@ -7,13 +7,15 @@ import org.apache.wicket.request.mapper.parameter.PageParameters; import com.commafeed.frontend.CommaFeedSession; import com.commafeed.model.User; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; @SuppressWarnings("serial") public abstract class JSONPage extends WebPage { public JSONPage(PageParameters pageParameters) { + Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); getRequestCycle().scheduleRequestHandlerAfterCurrent( - new TextRequestHandler("application/json", "UTF-8", new Gson() + new TextRequestHandler("application/json", "UTF-8", gson .toJson(getObject(pageParameters)))); } diff --git a/src/main/java/com/commafeed/frontend/rest/model/Category.java b/src/main/java/com/commafeed/frontend/rest/model/Category.java index e7016c27..307e92a3 100644 --- a/src/main/java/com/commafeed/frontend/rest/model/Category.java +++ b/src/main/java/com/commafeed/frontend/rest/model/Category.java @@ -2,20 +2,19 @@ package com.commafeed.frontend.rest.model; import java.util.List; -import com.commafeed.frontend.rest.FeedSubscriptionsREST.Subscription; import com.google.common.collect.Lists; public class Category { - private Long id; + private String id; private String name; private List children = Lists.newArrayList(); private List feeds = Lists.newArrayList(); - public Long getId() { + public String getId() { return id; } - public void setId(Long id) { + public void setId(String id) { this.id = id; } diff --git a/src/main/java/com/commafeed/frontend/rest/model/Entry.java b/src/main/java/com/commafeed/frontend/rest/model/Entry.java index 338cfd01..72433a2e 100644 --- a/src/main/java/com/commafeed/frontend/rest/model/Entry.java +++ b/src/main/java/com/commafeed/frontend/rest/model/Entry.java @@ -1,10 +1,12 @@ package com.commafeed.frontend.rest.model; +import java.util.Date; + public class Entry { private String id; private String title; private String content; - private String date; + private Date date; private String feedId; private String feedName; private String url; @@ -35,11 +37,11 @@ public class Entry { this.content = content; } - public String getDate() { + public Date getDate() { return date; } - public void setDate(String date) { + public void setDate(Date date) { this.date = date; } diff --git a/src/main/webapp/js/directives.js b/src/main/webapp/js/directives.js index 0512bc8f..ae624e35 100644 --- a/src/main/webapp/js/directives.js +++ b/src/main/webapp/js/directives.js @@ -15,14 +15,12 @@ module.directive('category', function($compile) { replace: true, templateUrl: 'directives/category.html', link: function(scope, element) { - if (scope.node.children) { - var ul = element.find('ul'); - ul.prepend('\ - '); - $compile(ul.contents())(scope); - } + var ul = element.find('ul'); + ul.prepend('\ + '); + $compile(ul.contents())(scope); } }; }); \ No newline at end of file diff --git a/src/main/webapp/templates/feeds.html b/src/main/webapp/templates/feeds.html index 55783376..c92b4f0a 100644 --- a/src/main/webapp/templates/feeds.html +++ b/src/main/webapp/templates/feeds.html @@ -3,23 +3,28 @@
    -
  • - -
  • + + + + + +
- {{entryList.name}}» + {{entryList.name}} »