Exemple #1
0
  /**
   * Take a node list of communities and build the structure from them, delegating to the relevant
   * methods in this class for sub-communities and collections
   *
   * @param context the context of the request
   * @param communities a nodelist of communities to create along with their sub-structures
   * @param parent the parent community of the nodelist of communities to create
   * @return an element array containing additional information regarding the created communities
   *     (e.g. the handles they have been assigned)
   */
  private static Element[] handleCommunities(
      Context context, NodeList communities, Community parent) throws Exception {
    Element[] elements = new Element[communities.getLength()];

    for (int i = 0; i < communities.getLength(); i++) {
      Community community;
      Element element = new Element("community");

      // create the community or sub community
      if (parent != null) {
        community = parent.createSubcommunity();
      } else {
        community = Community.create(null, context);
      }

      // default the short description to be an empty string
      community.setMetadata("short_description", " ");

      // now update the metadata
      Node tn = communities.item(i);
      for (Map.Entry<String, String> entry : communityMap.entrySet()) {
        NodeList nl = XPathAPI.selectNodeList(tn, entry.getKey());
        if (nl.getLength() == 1) {
          community.setMetadata(entry.getValue(), getStringValue(nl.item(0)));
        }
      }

      // FIXME: at the moment, if the community already exists by name
      // then this will throw a PSQLException on a duplicate key
      // violation
      // Ideally we'd skip this row and continue to create sub
      // communities
      // and so forth where they don't exist, but it's proving
      // difficult
      // to isolate the community that already exists without hitting
      // the database directly.
      community.update();

      // build the element with the handle that identifies the new
      // community
      // along with all the information that we imported here
      // This looks like a lot of repetition of getting information
      // from above
      // but it's here to keep it separate from the create process in
      // case
      // we want to move it or make it switchable later
      element.setAttribute("identifier", community.getHandle());

      Element nameElement = new Element("name");
      nameElement.setText(community.getMetadata("name"));
      element.addContent(nameElement);

      if (community.getMetadata("short_description") != null) {
        Element descriptionElement = new Element("description");
        descriptionElement.setText(community.getMetadata("short_description"));
        element.addContent(descriptionElement);
      }

      if (community.getMetadata("introductory_text") != null) {
        Element introElement = new Element("intro");
        introElement.setText(community.getMetadata("introductory_text"));
        element.addContent(introElement);
      }

      if (community.getMetadata("copyright_text") != null) {
        Element copyrightElement = new Element("copyright");
        copyrightElement.setText(community.getMetadata("copyright_text"));
        element.addContent(copyrightElement);
      }

      if (community.getMetadata("side_bar_text") != null) {
        Element sidebarElement = new Element("sidebar");
        sidebarElement.setText(community.getMetadata("side_bar_text"));
        element.addContent(sidebarElement);
      }

      // handle sub communities
      NodeList subCommunities = XPathAPI.selectNodeList(tn, "community");
      Element[] subCommunityElements = handleCommunities(context, subCommunities, community);

      // handle collections
      NodeList collections = XPathAPI.selectNodeList(tn, "collection");
      Element[] collectionElements = handleCollections(context, collections, community);

      int j;
      for (j = 0; j < subCommunityElements.length; j++) {
        element.addContent(subCommunityElements[j]);
      }
      for (j = 0; j < collectionElements.length; j++) {
        element.addContent(collectionElements[j]);
      }

      elements[i] = element;
    }

    return elements;
  }
  /**
   * Create a new community
   *
   * @param context The current DSpace context.
   * @param communityID The id of the parent community (-1 for a top-level community).
   * @return A process result's object.
   */
  public static FlowResult processCreateCommunity(Context context, int communityID, Request request)
      throws AuthorizeException, IOException, SQLException {
    FlowResult result = new FlowResult();

    Community parent = Community.find(context, communityID);
    Community newCommunity;

    if (parent != null) {
      newCommunity = parent.createSubcommunity();
    } else {
      newCommunity = Community.create(null, context);
    }

    String name = request.getParameter("name");
    String shortDescription = request.getParameter("short_description");
    String introductoryText = request.getParameter("introductory_text");
    String copyrightText = request.getParameter("copyright_text");
    String sideBarText = request.getParameter("side_bar_text");

    // If they don't have a name then make it untitled.
    if (name == null || name.length() == 0) {
      name = "Untitled";
    }

    // If empty, make it null.
    if (shortDescription != null && shortDescription.length() == 0) {
      shortDescription = null;
    }
    if (introductoryText != null && introductoryText.length() == 0) {
      introductoryText = null;
    }
    if (copyrightText != null && copyrightText.length() == 0) {
      copyrightText = null;
    }
    if (sideBarText != null && sideBarText.length() == 0) {
      sideBarText = null;
    }

    newCommunity.setMetadata("name", name);
    newCommunity.setMetadata("short_description", shortDescription);
    newCommunity.setMetadata("introductory_text", introductoryText);
    newCommunity.setMetadata("copyright_text", copyrightText);
    newCommunity.setMetadata("side_bar_text", sideBarText);

    // Upload the logo
    Object object = request.get("logo");
    Part filePart = null;
    if (object instanceof Part) {
      filePart = (Part) object;
    }

    if (filePart != null && filePart.getSize() > 0) {
      InputStream is = filePart.getInputStream();

      newCommunity.setLogo(is);
    }

    // Save everything
    newCommunity.update();
    context.commit();
    // success
    result.setContinue(true);
    result.setOutcome(true);
    result.setMessage(new Message("default", "The community was successfully created."));
    result.setParameter("communityID", newCommunity.getID());

    return result;
  }