Beispiel #1
0
  // 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();
  }