/** {@inheritDoc} */
  protected PagingResult<Site> computeFavoriteSites(ListingContext listingContext) {
    List<Site> result = getFavoriteSites();

    if (listingContext != null) {
      Collections.sort(
          result,
          new AlphaComparator(listingContext.isSortAscending(), listingContext.getSortProperty()));
    }

    Boolean hasMoreItems = false;
    if (listingContext != null) {
      int fromIndex =
          (listingContext.getSkipCount() > result.size())
              ? result.size()
              : listingContext.getSkipCount();

      // Case if skipCount > result size
      if (listingContext.getSkipCount() < result.size()) {
        fromIndex = listingContext.getSkipCount();
      }

      // Case if skipCount > result size
      if (listingContext.getMaxItems() + fromIndex >= result.size()) {
        result = result.subList(fromIndex, result.size());
        hasMoreItems = false;
      } else {
        result = result.subList(fromIndex, listingContext.getMaxItems() + fromIndex);
        hasMoreItems = true;
      }
    }
    return new PagingResultImpl<Site>(result, hasMoreItems, result.size());
  }
  /** {@inheritDoc} */
  @SuppressWarnings("unchecked")
  protected PagingResult<Site> computeSites(UrlBuilder url, ListingContext listingContext) {
    HttpUtils.Response resp = read(url, ErrorCodeRegistry.SITE_GENERIC);

    List<Object> json = JsonUtils.parseArray(resp.getStream(), resp.getCharset());
    int size = json.size();

    List<Site> result = new ArrayList<Site>();
    int fromIndex = 0, toIndex = size;
    Boolean hasMoreItems = false;

    // Define Listing Context
    if (listingContext != null) {
      fromIndex = (listingContext.getSkipCount() > size) ? size : listingContext.getSkipCount();

      // Case if skipCount > result size
      if (listingContext.getMaxItems() + fromIndex >= size) {
        toIndex = size;
        hasMoreItems = false;
      } else {
        toIndex = listingContext.getMaxItems() + fromIndex;
        hasMoreItems = true;
      }
    }

    String siteName = null;
    Map<String, Object> mapProperties = null;
    CacheSiteExtraProperties extraProperties = null;
    for (int i = fromIndex; i < toIndex; i++) {
      mapProperties = (Map<String, Object>) json.get(i);
      if (mapProperties != null) {
        siteName = JSONConverter.getString(mapProperties, OnPremiseConstant.SHORTNAME_VALUE);
        if (extraPropertiesCache.get(siteName) != null) {
          extraProperties = extraPropertiesCache.get(siteName);
          mapProperties.put(
              OnPremiseConstant.ISPENDINGMEMBER_VALUE, extraProperties.isPendingMember);
          mapProperties.put(OnPremiseConstant.ISMEMBER_VALUE, extraProperties.isMember);
          mapProperties.put(OnPremiseConstant.ISFAVORITE_VALUE, extraProperties.isFavorite);
        }
        result.add(SiteImpl.parseJson((Map<String, Object>) json.get(i)));
      }
    }

    if (listingContext != null) {
      Collections.sort(
          result,
          new AlphaComparator(listingContext.isSortAscending(), listingContext.getSortProperty()));
    }

    return new PagingResultImpl<Site>(result, hasMoreItems, size);
  }
 @Override
 public PagingResult<Node> getFavoriteNodes(ListingContext listingContext) {
   String link = PublicAPIUrlRegistry.getUserFavouritesUrl(session, session.getPersonIdentifier());
   UrlBuilder url = new UrlBuilder(link);
   if (listingContext != null) {
     url.addParameter(PublicAPIConstant.MAX_ITEMS_VALUE, listingContext.getMaxItems());
     url.addParameter(PublicAPIConstant.SKIP_COUNT_VALUE, listingContext.getSkipCount());
   }
   return computeFavorites(url);
 }
 /** {@inheritDoc} */
 protected UrlBuilder getCommentsUrl(
     Node node, ListingContext listingContext, boolean isReadOperation) {
   String link = CloudUrlRegistry.getCommentsUrl((CloudSession) session, node.getIdentifier());
   UrlBuilder url = new UrlBuilder(link);
   if (listingContext != null) {
     url.addParameter(CloudConstant.SKIP_COUNT_VALUE, listingContext.getSkipCount());
     url.addParameter(CloudConstant.MAX_ITEMS_VALUE, listingContext.getMaxItems());
   }
   return url;
 }
  /**
   * Internal method to compute data from server and transform it as high level object.
   *
   * @param url : Alfresco REST API activity url
   * @param listingContext : listing context to apply to the paging result.
   * @return Paging Result of activity entry.
   */
  @SuppressWarnings("unchecked")
  protected PagingResult<ActivityEntry> computeActivities(
      UrlBuilder url, ListingContext listingContext) {
    try {
      // read and parse
      HttpUtils.Response resp = read(url, ErrorCodeRegistry.ACTIVITISTREAM_GENERIC);

      List<Object> json = JsonUtils.parseArray(resp.getStream(), resp.getCharset());
      int size = json.size();
      ArrayList<ActivityEntry> result = new ArrayList<ActivityEntry>(size);

      Boolean b = false;
      if (listingContext != null) {
        int fromIndex =
            (listingContext.getSkipCount() > size) ? size : listingContext.getSkipCount();

        // Case if skipCount > result size
        if (listingContext.getMaxItems() + fromIndex >= size) {
          json = json.subList(fromIndex, size);
          b = false;
        } else {
          json = json.subList(fromIndex, listingContext.getMaxItems() + fromIndex);
          b = true;
        }
      }

      if (json != null) {
        for (Object obj : json) {
          result.add(ActivityEntryImpl.parseJson((Map<String, Object>) obj));
        }
      }

      return new PagingResultImpl<ActivityEntry>(result, b, size);
    } catch (Exception e) {
      convertException(e);
    }
    return null;
  }