Exemplo n.º 1
0
  @GET
  @Path("/get")
  @ApiOperation(
      value = "Get feed categories",
      notes = "Get all categories and subscriptions of the user",
      responseClass = "com.commafeed.frontend.model.Category")
  public Response getSubscriptions() {
    User user = getUser();

    Category root = cache.getUserRootCategory(user);
    if (root == null) {
      log.debug("tree cache miss for {}", user.getId());
      List<FeedCategory> categories = feedCategoryDAO.findAll(user);
      List<FeedSubscription> subscriptions = feedSubscriptionDAO.findAll(user);
      Map<Long, UnreadCount> unreadCount = feedSubscriptionService.getUnreadCount(user);

      root = buildCategory(null, categories, subscriptions, unreadCount);
      root.setId("all");
      root.setName("All");
      cache.setUserRootCategory(user, root);
    }

    return Response.ok(root).build();
  }
Exemplo n.º 2
0
  private Category buildCategory(
      Long id,
      List<FeedCategory> categories,
      List<FeedSubscription> subscriptions,
      Map<Long, UnreadCount> unreadCount) {
    Category category = new Category();
    category.setId(String.valueOf(id));
    category.setExpanded(true);

    for (FeedCategory c : categories) {
      if ((id == null && c.getParent() == null)
          || (c.getParent() != null && ObjectUtils.equals(c.getParent().getId(), id))) {
        Category child = buildCategory(c.getId(), categories, subscriptions, unreadCount);
        child.setId(String.valueOf(c.getId()));
        child.setName(c.getName());
        child.setPosition(c.getPosition());
        if (c.getParent() != null && c.getParent().getId() != null) {
          child.setParentId(String.valueOf(c.getParent().getId()));
        }
        child.setExpanded(!c.isCollapsed());
        category.getChildren().add(child);
      }
    }
    Collections.sort(
        category.getChildren(),
        new Comparator<Category>() {
          @Override
          public int compare(Category o1, Category o2) {
            return ObjectUtils.compare(o1.getPosition(), o2.getPosition());
          }
        });

    for (FeedSubscription subscription : subscriptions) {
      if ((id == null && subscription.getCategory() == null)
          || (subscription.getCategory() != null
              && ObjectUtils.equals(subscription.getCategory().getId(), id))) {
        UnreadCount uc = unreadCount.get(subscription.getId());
        Subscription sub =
            Subscription.build(subscription, applicationSettingsService.get().getPublicUrl(), uc);
        category.getFeeds().add(sub);
      }
    }
    Collections.sort(
        category.getFeeds(),
        new Comparator<Subscription>() {
          @Override
          public int compare(Subscription o1, Subscription o2) {
            return ObjectUtils.compare(o1.getPosition(), o2.getPosition());
          }
        });
    return category;
  }