コード例 #1
0
  /**
   * Retrieves a formatted text attribute from an XML element; converts from older forms of
   * formatted text or plaintext, if found. For example, if the baseAttributeName "foo" is
   * specified, the attribute "foo-html" will be looked for first, and then "foo-formatted", and
   * finally just "foo" (plaintext).
   *
   * @param element The XML element from which to retrieve the formatted text attribute
   * @param baseAttributeName The base attribute name of the formatted text attribute
   */
  public static String decodeFormattedTextAttribute(Element element, String baseAttributeName) {
    String ret;

    // first check if an HTML-encoded attribute exists, for example "foo-html", and use it if
    // available
    ret = StringUtil.trimToNull(Xml.decodeAttribute(element, baseAttributeName + "-html"));
    if (ret != null) return ret;

    // next try the older kind of formatted text like "foo-formatted", and convert it if found
    ret = StringUtil.trimToNull(Xml.decodeAttribute(element, baseAttributeName + "-formatted"));
    ret = FormattedText.convertOldFormattedText(ret);
    if (ret != null) return ret;

    // next try just a plaintext attribute and convert the plaintext to formatted text if found
    // convert from old plaintext instructions to new formatted text instruction
    ret = Xml.decodeAttribute(element, baseAttributeName);
    ret = FormattedText.convertPlaintextToFormattedText(ret);
    return ret;
  }
コード例 #2
0
ファイル: ForumEntity.java プロジェクト: yllyx/sakai
  // 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();
  }