Package com.google.livingstories.client

Examples of com.google.livingstories.client.DisplayContentItemBundle


    if (filterSpec.contributorId != null || filterSpec.playerId != null) {
      throw new IllegalArgumentException(
          "filterSpec.contributorId and filterSpec.playerId should not be set by remote callers."
          + " contributorId = " + filterSpec.contributorId + " playerId = "+ filterSpec.playerId);
    }
    DisplayContentItemBundle result = Caches.getDisplayContentItemBundle(
        livingStoryId, filterSpec, focusedContentItemId, cutoff);
    if (result != null) {
      return result;
    }
   
    FilterSpec localFilterSpec = new FilterSpec(filterSpec);
   
    BaseContentItem focusedContentItem = null;
    if (focusedContentItemId != null) {
      focusedContentItem = getContentItem(focusedContentItemId, false);
      if (focusedContentItem != null) {
        if (adjustFilterSpecForContentItem(localFilterSpec, focusedContentItem)) {
          // If we had to adjust the filter spec to accommodate the focused content item,
          // we'll be switching filter views, so we want to clear the start date
          // and reload the list from the beginning.
          cutoff = null;
        }
      }
    }
   
    // Some preliminaries. Note that the present implementation just filters all content items for
    // a story, which could be a bit expensive if there's a cache miss. By and large, though,
    // we'd expect a lot more cache hits than cache misses, unlike the case with, say,
    // a twitter "following" feed, which is more likely to be unique to that user.
    List<BaseContentItem> allContentItems = getContentItemsForLivingStory(livingStoryId, true);
   
    Map<Long, BaseContentItem> idToContentItemMap = Maps.newHashMap();
    List<BaseContentItem> relevantContentItems = Lists.newArrayList();
    for (BaseContentItem contentItem : allContentItems) {
      idToContentItemMap.put(contentItem.getId(), contentItem);
     
      Date sortKey = contentItem.getDateSortKey();
      boolean matchesStartDate = (cutoff == null) ||
          (localFilterSpec.oldestFirst ? !sortKey.before(cutoff) : !sortKey.after(cutoff));

      if (matchesStartDate && localFilterSpec.doesContentItemMatch(contentItem)) {
        relevantContentItems.add(contentItem);
      }
    }
    sortContentItemList(relevantContentItems, localFilterSpec);

    // Need to get the focused content item from the map instead of using the object directly.
    // This is because we use indexOf() to find the location of the focused content item in the
    // list and the original contentItem isn't the same object instance.
    List<BaseContentItem> coreContentItems = getSublist(relevantContentItems,
        focusedContentItem == null ? null : idToContentItemMap.get(focusedContentItemId), cutoff);
    Set<Long> linkedContentItemIds = Sets.newHashSet();
   
    for (BaseContentItem contentItem : coreContentItems) {
      if (contentItem.displayTopLevel()) {
        // If a content item isn't a top-level display content item, we can get away without
        // returning its linked content items.
        linkedContentItemIds.addAll(contentItem.getLinkedContentItemIds());
      }
    }

    Set<BaseContentItem> linkedContentItems = Sets.newHashSet();
    for (Long id : linkedContentItemIds) {
      BaseContentItem linkedContentItem = idToContentItemMap.get(id);
      if (linkedContentItem == null) {
        System.err.println("Linked content item with id " + id + " is not found.");
      } else {
        linkedContentItems.add(linkedContentItem);
        // For linked narratives, we want to get their own linked content items as well
        if (linkedContentItem.getContentItemType() == ContentItemType.NARRATIVE) {
          for (Long linkedToLinkedContentItemId : linkedContentItem.getLinkedContentItemIds()) {
            BaseContentItem linkedToLinkedContentItem =
                idToContentItemMap.get(linkedToLinkedContentItemId);
            if (linkedToLinkedContentItem != null) {
              linkedContentItems.add(linkedToLinkedContentItem);
            }
          }
        }
      }
    }
   
    Date nextDateInSequence = getNextDateInSequence(coreContentItems, relevantContentItems);

    result = new DisplayContentItemBundle(coreContentItems, linkedContentItems, nextDateInSequence,
        localFilterSpec);
    Caches.setDisplayContentItemBundle(livingStoryId, filterSpec, focusedContentItemId, cutoff,
        result);
    return result;
  }
View Full Code Here


    if (byContribution) {
      filterSpec.contributorId = contentItemId;
    } else {
      filterSpec.playerId = contentItemId;
    }
    DisplayContentItemBundle result =
        Caches.getDisplayContentItemBundle(null, filterSpec, null, cutoff);
    if (result != null) {
      return result;
    }
   
    PersistenceManager pm = PMF.get().getPersistenceManager();

    Query query = pm.newQuery(BaseContentEntity.class);
    String contentItemIdClause = byContribution
        ? "contributorIds == contentItemIdParam" : "linkedContentEntityIds == contentItemIdParam";
    query.setFilter(contentItemIdClause
        + " && publishState == '" + PublishState.PUBLISHED.name() + "'");
    // no need to explicitly set ordering, as we resort by display order.
    query.declareParameters("java.lang.Long contentItemIdParam");

    try {
      List<BaseContentItem> relevantContentItems = new ArrayList<BaseContentItem>();
     
      @SuppressWarnings("unchecked")
      List<BaseContentEntity> contentEntities =
          (List<BaseContentEntity>) query.execute(contentItemId);
      for (BaseContentEntity contentEntity : contentEntities) {
        BaseContentItem contentItem = contentEntity.toClientObject();
        if (cutoff == null || !contentItem.getDateSortKey().after(cutoff)) {
          relevantContentItems.add(contentItem);
        }
      }
     
      // sort and put a window on the list, get the next date in the sequence
      sortContentItemList(relevantContentItems, filterSpec);
      List<BaseContentItem> coreContentItems = getSublist(relevantContentItems, null, cutoff);
      Date nextDateInSequence = getNextDateInSequence(coreContentItems, relevantContentItems);
     
      result = new DisplayContentItemBundle(coreContentItems,
          Collections.<BaseContentItem>emptySet(), nextDateInSequence, filterSpec);
      Caches.setDisplayContentItemBundle(null, filterSpec, null, cutoff, result);
      return result;
    } finally {
      query.closeAll();
View Full Code Here

TOP

Related Classes of com.google.livingstories.client.DisplayContentItemBundle

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.