Example #1
0
  /**
   * Create item in collection. Item can be without filled metadata.
   *
   * @param collectionId Id of collection in which will be item created.
   * @param item Item filled only with metadata, other variables are ignored.
   * @param headers If you want to access to collection under logged user into context. In headers
   *     must be set header "rest-dspace-token" with passed token from login method.
   * @return Return status code with item. Return status (OK)200 if item was created. NOT_FOUND(404)
   *     if id of collection does not exists. UNAUTHORIZED(401) if user have not permission to write
   *     items in collection.
   * @throws WebApplicationException It is thrown when was problem with database reading or writing
   *     (SQLException) or problem with creating context(ContextException) or problem with
   *     authorization to collection or IOException or problem with index item into browse index. It
   *     is thrown by NOT_FOUND and UNATHORIZED status codes, too.
   */
  @POST
  @Path("/{collection_id}/items")
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Item addCollectionItem(
      @PathParam("collection_id") Integer collectionId,
      Item item,
      @QueryParam("userIP") String user_ip,
      @QueryParam("userAgent") String user_agent,
      @QueryParam("xforwarderfor") String xforwarderfor,
      @Context HttpHeaders headers,
      @Context HttpServletRequest request)
      throws WebApplicationException {

    log.info("Create item in collection(id=" + collectionId + ").");
    org.dspace.core.Context context = null;
    Item returnItem = null;

    try {
      context = createContext(getUser(headers));
      org.dspace.content.Collection dspaceCollection =
          findCollection(context, collectionId, org.dspace.core.Constants.WRITE);

      writeStats(
          dspaceCollection,
          UsageEvent.Action.UPDATE,
          user_ip,
          user_agent,
          xforwarderfor,
          headers,
          request,
          context);

      log.trace("Creating item in collection(id=" + collectionId + ").");
      org.dspace.content.WorkspaceItem workspaceItem =
          org.dspace.content.WorkspaceItem.create(context, dspaceCollection, false);
      org.dspace.content.Item dspaceItem = workspaceItem.getItem();

      log.trace("Adding metadata to item(id=" + dspaceItem.getID() + ").");
      if (item.getMetadata() != null) {
        for (MetadataEntry entry : item.getMetadata()) {
          String data[] = mySplit(entry.getKey());
          dspaceItem.addMetadata(data[0], data[1], data[2], entry.getLanguage(), entry.getValue());
        }
      }
      workspaceItem.update();

      // Index item to browse.
      org.dspace.browse.IndexBrowse browse = new org.dspace.browse.IndexBrowse();
      browse.indexItem(dspaceItem);

      log.trace("Installing item to collection(id=" + collectionId + ").");
      dspaceItem = org.dspace.content.InstallItem.installItem(context, workspaceItem);

      returnItem = new Item(dspaceItem, "", context);

      context.complete();

    } catch (SQLException e) {
      processException(
          "Could not add item into collection(id="
              + collectionId
              + "), SQLException. Message: "
              + e,
          context);
    } catch (AuthorizeException e) {
      processException(
          "Could not add item into collection(id="
              + collectionId
              + "), AuthorizeException. Message: "
              + e,
          context);
    } catch (IOException e) {
      processException(
          "Could not add item into collection(id=" + collectionId + "), IOException. Message: " + e,
          context);
    } catch (BrowseException e) {
      processException(
          "Could not add item into browse index, BrowseException. Message: " + e, context);
    } catch (ContextException e) {
      processException(
          "Could not add item into collection(id="
              + collectionId
              + "), ContextException. Message: "
              + e.getMessage(),
          context);
    } finally {
      processFinally(context);
    }

    log.info(
        "Item successfully created in collection(id="
            + collectionId
            + "). Item handle="
            + returnItem.getHandle());
    return returnItem;
  }