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; }
// find topics in site, but organized by forum public List<LessonEntity> getEntitiesInSite(SimplePageBean bean) { List<LessonEntity> ret = new ArrayList<LessonEntity>(); // LSNBLDR-21. If the tool is not in the current site we shouldn't query // for topics owned by the tool. Site site = null; try { site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext()); } catch (Exception impossible) { return ret; } ToolConfiguration tool = site.getToolForCommonId("sakai.forums"); if (tool == null) { // Forums is not in this site. Move on to the next provider. if (nextEntity != null) ret.addAll(nextEntity.getEntitiesInSite()); return ret; } // ForumEntity e = new ForumEntity(TYPE_FORUM_TOPIC, 3L, 2); // e.setGroups(Arrays.asList("1c24287b-b880-43da-8cdd-c6cdc1249c5c", // "75184424-853e-4dd4-9e92-980c851f0580")); // e.setGroups(Arrays.asList("75184424-853e-4dd4-9e92-980c851f0580")); SortedSet<DiscussionForum> forums = new TreeSet<DiscussionForum>(new ForumBySortIndexAscAndCreatedDateDesc()); for (DiscussionForum forum : forumManager.getForumsForMainPage()) forums.add(forum); // security. assume this is only used in places where it's OK, so skip security checks for (DiscussionForum forum : forums) { if (!forum.getDraft()) { ForumEntity entity = new ForumEntity(TYPE_FORUM_FORUM, forum.getId(), 1); entity.forum = forum; ret.add(entity); for (Object o : forum.getTopicsSet()) { DiscussionTopic topic = (DiscussionTopic) o; if (topic.getDraft().equals(Boolean.FALSE)) { entity = new ForumEntity(TYPE_FORUM_TOPIC, topic.getId(), 2); entity.topic = topic; ret.add(entity); } } } } if (nextEntity != null) ret.addAll(nextEntity.getEntitiesInSite(bean)); return ret; }
// returns SakaiId of thing just created public String importObject( String title, String topicTitle, String text, boolean texthtml, String base, String baseDir, String siteId, List<String> attachmentHrefs, boolean hide) { DiscussionForum ourForum = null; DiscussionTopic ourTopic = null; int forumtry = 0; int topictry = 0; for (; ; ) { ourForum = null; SortedSet<DiscussionForum> forums = new TreeSet<DiscussionForum>(new ForumBySortIndexAscAndCreatedDateDesc()); for (DiscussionForum forum : discussionForumManager.getForumsForMainPage()) forums.add(forum); for (DiscussionForum forum : forums) { if (forum.getTitle().equals(title)) { ourForum = forum; break; } } if (ourForum == null) { if (forumtry > 0) { System.out.println("oops, forum still not there the second time"); return null; } forumtry++; // if a new site, may need to create the area or we'll get a backtrace when creating forum areaManager.getDiscussionArea(siteId); ourForum = discussionForumManager.createForum(); ourForum.setTitle(title); discussionForumManager.saveForum(siteId, ourForum); continue; // reread, better be there this time } // forum now exists, and was just reread ourTopic = null; for (Object o : ourForum.getTopicsSet()) { DiscussionTopic topic = (DiscussionTopic) o; if (topic.getTitle().equals(topicTitle)) { ourTopic = topic; break; } } if (ourTopic != null) // ok, forum and topic exist break; if (topictry > 0) { System.out.println("oops, topic still not there the second time"); return null; } topictry++; // create it ourTopic = discussionForumManager.createTopic(ourForum); ourTopic.setTitle(topicTitle); if (attachmentHrefs != null && attachmentHrefs.size() > 0) { for (String href : attachmentHrefs) { // we don't have any real label for attachments. About all we can do is use the filename, // without path String label = href; int slash = label.lastIndexOf("/"); if (slash >= 0) label = label.substring(slash + 1); if (label.equals("")) label = "Attachment"; // basedir is a folder in contenthosting starting with /group/ where our content has been // loaded // discussionForum needs a Sakai content resource ID for the attachment Attachment thisDFAttach = discussionForumManager.createDFAttachment(removeDotDot(baseDir + href), label); ourTopic.addAttachment(thisDFAttach); } } String shortText = null; if (texthtml) { ourTopic.setExtendedDescription(text.replaceAll("\\$IMS-CC-FILEBASE\\$", base)); shortText = FormattedText.convertFormattedTextToPlaintext(text); } else { ourTopic.setExtendedDescription(FormattedText.convertPlaintextToFormattedText(text)); shortText = text; } shortText = org.apache.commons.lang.StringUtils.abbreviate(shortText, 254); ourTopic.setShortDescription(shortText); // there's a better way to do attachments, but it's too complex for now if (hide) discussionForumManager.saveTopicAsDraft(ourTopic); else discussionForumManager.saveTopic(ourTopic); // now go back and mmake sure everything is there } return "/" + FORUM_TOPIC + "/" + ourTopic.getId(); }
public String findObject(String objectid, Map<String, String> objectMap, String siteid) { if (!objectid.startsWith("forum_topic/") && !objectid.startsWith("forum_forum/")) { if (nextEntity != null) { return nextEntity.findObject(objectid, objectMap, siteid); } return null; } // isolate forum_topic/NNN from title int i = 0; if (objectid.startsWith("forum_topic/")) i = objectid.indexOf("/", "forum_topic/".length()); else i = objectid.indexOf("/", "forum_forum/".length()); if (i <= 0) return null; String realobjectid = objectid.substring(0, i); // msgcenter uses forum/ not forum_forum/ if (objectid.startsWith("forum_forum/")) realobjectid = realobjectid.substring("forum_".length()); // now see if it's in the map String newtopic = objectMap.get(realobjectid); if (newtopic != null) { if (objectid.startsWith("forum_forum/")) return "/forum_" + newtopic; // forum/ID >> /forum_forum/ID else return "/" + newtopic; // sakaiid is /forum_topic/ID } // this must be 2.8. Can't find the topic in the map // i is start of title String title = null; String forumtitle = null; if (objectid.startsWith("forum_topic/")) { int j = objectid.indexOf("\n"); title = objectid.substring(i + 1, j); forumtitle = objectid.substring(j + 1); } else { forumtitle = objectid.substring(i + 1); } // unfortunately we have to search the topic tree to find it. SortedSet<DiscussionForum> forums = new TreeSet<DiscussionForum>(new ForumBySortIndexAscAndCreatedDateDesc()); for (DiscussionForum forum : forumManager.getForumsForMainPage()) forums.add(forum); // security. assume this is only used in places where it's OK, so skip security checks // ignore draft status. We want to show drafts. for (DiscussionForum forum : forums) { if (forum.getTitle().equals(forumtitle)) { if (title == null) // object is forum not topic return "/forum_forum/" + forum.getId(); for (Object o : forum.getTopicsSet()) { DiscussionTopic topic = (DiscussionTopic) o; if (topic.getTitle().equals(title)) { return "/forum_topic/" + topic.getId(); } } } } return null; }
/** 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); } }