예제 #1
0
  /**
   * Search for first collection with passed name.
   *
   * @param name Name of collection.
   * @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 It returns null if collection was not found. Otherwise returns first founded
   *     collection.
   * @throws WebApplicationException
   */
  @POST
  @Path("/find-collection")
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Collection findCollectionByName(String name, @Context HttpHeaders headers)
      throws WebApplicationException {
    log.info("Searching for first collection with name=" + name + ".");
    org.dspace.core.Context context = null;
    Collection collection = null;

    try {
      context = createContext(getUser(headers));
      org.dspace.content.Collection[] dspaceCollections;

      dspaceCollections = org.dspace.content.Collection.findAll(context);

      for (org.dspace.content.Collection dspaceCollection : dspaceCollections) {
        if (AuthorizeManager.authorizeActionBoolean(
            context, dspaceCollection, org.dspace.core.Constants.READ)) {
          if (dspaceCollection.getName().equals(name)) {
            collection = new Collection(dspaceCollection, "", context, 100, 0);
            break;
          }
        }
      }

      context.complete();

    } catch (SQLException e) {
      processException(
          "Something went wrong while searching for collection(name="
              + name
              + ") from database. Message: "
              + e,
          context);
    } catch (ContextException e) {
      processException(
          "Something went wrong while searching for collection(name="
              + name
              + "), ContextError. Message: "
              + e.getMessage(),
          context);
    } finally {
      processFinally(context);
    }

    if (collection == null) {
      log.info("Collection was not found.");
    } else {
      log.info("Collection was found with id(" + collection.getId() + ").");
    }
    return collection;
  }
예제 #2
0
  /**
   * Update collection. It replace all properties.
   *
   * @param collectionId Id of collection in DSpace.
   * @param collection Collection which will replace properties of actual collection.
   * @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 response 200 if was everything all right. Otherwise 400 when id of community was
   *     incorrect or 401 if was problem with permission to write into collection.
   * @throws WebApplicationException It is thrown when was problem with database reading or writing.
   *     Or problem with authorization to collection. Or problem with creating context.
   */
  @PUT
  @Path("/{collection_id}")
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response updateCollection(
      @PathParam("collection_id") Integer collectionId,
      org.dspace.rest.common.Collection collection,
      @QueryParam("userIP") String user_ip,
      @QueryParam("userAgent") String user_agent,
      @QueryParam("xforwarderfor") String xforwarderfor,
      @Context HttpHeaders headers,
      @Context HttpServletRequest request)
      throws WebApplicationException {

    log.info("Updating collection(id=" + collectionId + ").");
    org.dspace.core.Context context = 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);

      dspaceCollection.setMetadata("name", collection.getName());
      dspaceCollection.setLicense(collection.getLicense());
      // dspaceCollection.setLogo(collection.getLogo()); // TODO Add this option.
      dspaceCollection.setMetadata(
          org.dspace.content.Collection.COPYRIGHT_TEXT, collection.getCopyrightText());
      dspaceCollection.setMetadata(
          org.dspace.content.Collection.INTRODUCTORY_TEXT, collection.getIntroductoryText());
      dspaceCollection.setMetadata(
          org.dspace.content.Collection.SHORT_DESCRIPTION, collection.getShortDescription());
      dspaceCollection.setMetadata(
          org.dspace.content.Collection.SIDEBAR_TEXT, collection.getSidebarText());
      dspaceCollection.update();

      context.complete();

    } catch (ContextException e) {
      processException(
          "Could not update collection(id="
              + collectionId
              + "), ContextException. Message: "
              + e.getMessage(),
          context);
    } catch (SQLException e) {
      processException(
          "Could not update collection(id=" + collectionId + "), SQLException. Message: " + e,
          context);
    } catch (AuthorizeException e) {
      processException(
          "Could not update collection(id=" + collectionId + "), AuthorizeException. Message: " + e,
          context);
    } finally {
      processFinally(context);
    }

    log.info("Collection(id=" + collectionId + ") successfully updated.");
    return Response.ok().build();
  }