Exemplo n.º 1
0
  /** 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);
    }
  }