Example #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();
  }
Example #2
0
  // seems not to be used anymore
  public boolean removeEntityControl(String siteId, String groupId) throws IOException {

    if (type != TYPE_FORUM_TOPIC) return false;

    setMasks();

    if (topic == null) topic = getTopicById(true, id);
    if (topic == null) return false;

    Set<DBMembershipItem> oldMembershipItemSet =
        uiPermissionsManager.getTopicItemsSet((DiscussionTopic) topic);

    Set membershipItemSet = new HashSet();

    String groupName = null;
    String maintainRole = null;
    try {
      Site site = SiteService.getSite(ToolManager.getCurrentPlacement().getContext());
      groupName = site.getGroup(groupId).getTitle();
      maintainRole = authzGroupService.getAuthzGroup("/site/" + site.getId()).getMaintainRole();
    } catch (Exception e) {
      System.out.println("Unable to get site info for AddEntityControl " + e);
    }

    PermissionLevel ownerLevel =
        permissionLevelManager.createPermissionLevel(
            "Owner", typeManager.getOwnerLevelType(), ownerMask);
    permissionLevelManager.savePermissionLevel(ownerLevel);

    DBMembershipItem membershipItem =
        permissionLevelManager.createDBMembershipItem(
            maintainRole, "Owner", MembershipItem.TYPE_ROLE);
    membershipItem.setPermissionLevel(ownerLevel);
    permissionLevelManager.saveDBMembershipItem(membershipItem);

    membershipItemSet.add(membershipItem);

    // now change any existing ones into null
    for (DBMembershipItem item : oldMembershipItemSet) {
      if (item.getType().equals(MembershipItem.TYPE_ROLE)) {
        if (!maintainRole.equals(item.getName())) { // that was done above, other roles contributor
          PermissionLevel contributorLevel =
              permissionLevelManager.createPermissionLevel(
                  "Contributor", typeManager.getContributorLevelType(), contributorMask);
          permissionLevelManager.savePermissionLevel(contributorLevel);

          membershipItem =
              permissionLevelManager.createDBMembershipItem(
                  item.getName(), "Contributor", item.getType());
          membershipItem.setPermissionLevel(contributorLevel);
          permissionLevelManager.saveDBMembershipItem(membershipItem);
          membershipItemSet.add(membershipItem);
        }
      } else { // everything else off
        PermissionLevel noneLevel =
            permissionLevelManager.createPermissionLevel(
                "None", typeManager.getNoneLevelType(), noneMask);
        permissionLevelManager.savePermissionLevel(noneLevel);

        membershipItem =
            permissionLevelManager.createDBMembershipItem(item.getName(), "None", item.getType());
        membershipItem.setPermissionLevel(noneLevel);
        permissionLevelManager.saveDBMembershipItem(membershipItem);
        membershipItemSet.add(membershipItem);
      }
    }

    permissionLevelManager.deleteMembershipItems(oldMembershipItemSet);

    topic.setMembershipItemSet(membershipItemSet);
    discussionForumManager.saveTopic((DiscussionTopic) topic);

    return true;
  };