private Object getTopic(Long topicId, String siteId, String userId) {

    if (LOG.isDebugEnabled()) {
      LOG.debug("getTopic(" + topicId + "," + siteId + "," + userId + ")");
    }

    // This call gets the attachments for the messages but not the topic. Unexpected, yes. Cool,
    // not.
    DiscussionTopic fatTopic =
        (DiscussionTopic) forumManager.getTopicByIdWithMessagesAndAttachments(topicId);

    if (!uiPermissionsManager.isRead(
        topicId,
        fatTopic.getDraft(),
        false,
        userId,
        forumManager.getContextForTopicById(topicId))) {
      LOG.error("'" + userId + "' is not authorised to read topic '" + topicId + "'.");
      throw new EntityException(
          "You are not authorised to read this topic.", "", HttpServletResponse.SC_UNAUTHORIZED);
    }

    SparseTopic sparseTopic = new SparseTopic(fatTopic);

    // Setup the total and read message counts on the topic
    List<Long> topicIds = new ArrayList<Long>();
    topicIds.add(fatTopic.getId());

    List<Object[]> totalCounts = forumManager.getMessageCountsForMainPage(topicIds);
    if (totalCounts.size() > 0) {
      sparseTopic.setTotalMessages((Integer) totalCounts.get(0)[1]);
    } else {
      sparseTopic.setTotalMessages(0);
    }

    List<Object[]> readCounts = forumManager.getReadMessageCountsForMainPage(topicIds);
    if (readCounts.size() > 0) {
      sparseTopic.setReadMessages((Integer) readCounts.get(0)[1]);
    } else {
      sparseTopic.setReadMessages(0);
    }

    List<SparseMessage> messages = new ArrayList<SparseMessage>();
    for (Message fatMessage : (List<Message>) fatTopic.getMessages()) {
      SparseMessage sparseMessage =
          new SparseMessage(
              fatMessage,
              /* readStatus = */ false,
              /* addAttachments = */ true,
              developerHelperService.getServerURL());
      messages.add(sparseMessage);
    }

    List<SparseThread> threads =
        new MessageUtils().getThreadsWithCounts(messages, forumManager, userId);

    sparseTopic.setThreads(threads);

    return sparseTopic;
  }
  private List<?> getAllForaForSite(String siteId, String userId) {

    if (LOG.isDebugEnabled()) {
      LOG.debug("getAllForaForSite(" + siteId + "," + userId + ")");
    }

    List<SparsestForum> sparseFora = new ArrayList<SparsestForum>();

    List<DiscussionForum> fatFora = forumManager.getDiscussionForumsWithTopics(siteId);

    for (DiscussionForum fatForum : fatFora) {

      if (!checkAccess(fatForum, userId, siteId)) {
        LOG.warn(
            "Access denied for user id '"
                + userId
                + "' to forum '"
                + fatForum.getId()
                + "'. This forum will not be returned.");
        continue;
      }

      List<Long> topicIds = new ArrayList<Long>();
      for (Topic topic : (List<Topic>) fatForum.getTopics()) {
        topicIds.add(topic.getId());
      }

      List<Object[]> topicTotals = forumManager.getMessageCountsForMainPage(topicIds);
      List<Object[]> topicReadTotals = forumManager.getReadMessageCountsForMainPage(topicIds);

      SparsestForum sparseForum = new SparsestForum(fatForum, developerHelperService);

      int totalForumMessages = 0;
      for (Object[] topicTotal : topicTotals) {
        totalForumMessages += (Integer) topicTotal[1];
      }
      sparseForum.setTotalMessages(totalForumMessages);

      int totalForumReadMessages = 0;
      for (Object[] topicReadTotal : topicReadTotals) {
        totalForumReadMessages += (Integer) topicReadTotal[1];
      }
      sparseForum.setReadMessages(totalForumReadMessages);

      sparseFora.add(sparseForum);
    }

    return sparseFora;
  }
  private Object getMessage(Long messageId, String siteId, String userId) {

    if (LOG.isDebugEnabled()) {
      LOG.debug("getMessage(" + messageId + "," + siteId + "," + userId + ")");
    }

    Message fatMessage = forumManager.getMessageById(messageId);

    Topic fatTopic =
        forumManager.getTopicByIdWithMessagesAndAttachments(fatMessage.getTopic().getId());

    // This sets the attachments on the message.We have to do this as
    // getMessageById doesn't populate the attachments.
    setAttachments(fatMessage, fatTopic.getMessages());

    if (!uiPermissionsManager.isRead(
        fatTopic.getId(),
        ((DiscussionTopic) fatTopic).getDraft(),
        false,
        userId,
        forumManager.getContextForTopicById(fatTopic.getId()))) {
      LOG.error("'" + userId + "' is not authorised to read message '" + messageId + "'.");
      throw new EntityException(
          "You are not authorised to read this message.", "", HttpServletResponse.SC_UNAUTHORIZED);
    }

    List<SparseMessage> messages = new ArrayList<SparseMessage>();

    for (Message fm : (List<Message>) fatTopic.getMessages()) {
      messages.add(
          new SparseMessage(
              fm,
              /* readStatus =*/ false,
              /* addAttachments =*/ true,
              developerHelperService.getServerURL()));
    }

    SparseMessage sparseThread =
        new SparseMessage(
            fatMessage, false, /* readStatus =*/ true, developerHelperService.getServerURL());

    new MessageUtils().attachReplies(sparseThread, messages, forumManager, userId);

    return sparseThread;
  }
  /** This will return a SparseForum populated down to the topics with their attachments. */
  private Object getForum(Long forumId, String siteId, String userId) {

    if (LOG.isDebugEnabled()) {
      LOG.debug("getForum(" + forumId + "," + siteId + "," + userId + ")");
    }

    DiscussionForum fatForum = forumManager.getForumByIdWithTopicsAttachmentsAndMessages(forumId);

    if (checkAccess(fatForum, userId, siteId)) {

      SparseForum sparseForum = new SparseForum(fatForum, developerHelperService);

      List<DiscussionTopic> fatTopics = (List<DiscussionTopic>) fatForum.getTopics();

      // Gather all the topic ids so we can make the minimum number
      // of calls for the message counts.
      List<Long> topicIds = new ArrayList<Long>();
      for (DiscussionTopic topic : fatTopics) {
        topicIds.add(topic.getId());
      }

      List<Object[]> topicTotals = forumManager.getMessageCountsForMainPage(topicIds);
      List<Object[]> topicReadTotals = forumManager.getReadMessageCountsForMainPage(topicIds);

      int totalForumMessages = 0;
      for (Object[] topicTotal : topicTotals) {
        totalForumMessages += (Integer) topicTotal[1];
      }
      sparseForum.setTotalMessages(totalForumMessages);

      int totalForumReadMessages = 0;
      for (Object[] topicReadTotal : topicReadTotals) {
        totalForumReadMessages += (Integer) topicReadTotal[1];
      }
      sparseForum.setReadMessages(totalForumReadMessages);

      // Reduce the fat topics to sparse topics while setting the total and read
      // counts. A SparseTopic will only be created if the currrent user has read access.
      List<SparsestTopic> sparseTopics = new ArrayList<SparsestTopic>();
      for (DiscussionTopic fatTopic : fatTopics) {

        // Only add this topic to the list if the current user has read permission
        if (!uiPermissionsManager.isRead(fatTopic, fatForum, userId, siteId)) {
          // No read permission, skip this topic.
          continue;
        }

        SparsestTopic sparseTopic = new SparsestTopic(fatTopic);
        for (Object[] topicTotal : topicTotals) {
          if (topicTotal[0].equals(sparseTopic.getId())) {
            sparseTopic.setTotalMessages((Integer) topicTotal[1]);
          }
        }
        for (Object[] topicReadTotal : topicReadTotals) {
          if (topicReadTotal[0].equals(sparseTopic.getId())) {
            sparseTopic.setReadMessages((Integer) topicReadTotal[1]);
          }
        }

        List<SparseAttachment> attachments = new ArrayList<SparseAttachment>();
        for (Attachment attachment : (List<Attachment>) fatTopic.getAttachments()) {
          String url =
              developerHelperService.getServerURL()
                  + "/access/content"
                  + attachment.getAttachmentId();
          attachments.add(new SparseAttachment(attachment.getAttachmentName(), url));
        }
        sparseTopic.setAttachments(attachments);

        sparseTopics.add(sparseTopic);
      }

      sparseForum.setTopics(sparseTopics);

      return sparseForum;
    } else {
      LOG.error("Not authorised to access forum '" + forumId + "'");
      throw new EntityException(
          "You are not authorised to access this forum.", "", HttpServletResponse.SC_UNAUTHORIZED);
    }
  }