// adds the metadata element from one <field>
  private void applyDimField(Element field, Item item) {
    String schema = field.getAttributeValue("mdschema");
    String element = field.getAttributeValue("element");
    String qualifier = field.getAttributeValue("qualifier");
    String lang = field.getAttributeValue("lang");
    String authority = field.getAttributeValue("authority");
    String sconf = field.getAttributeValue("confidence");

    if ((authority != null && authority.length() > 0) || (sconf != null && sconf.length() > 0)) {
      int confidence =
          (sconf != null && sconf.length() > 0)
              ? Choices.getConfidenceValue(sconf)
              : Choices.CF_UNSET;
      item.addMetadata(schema, element, qualifier, lang, field.getText(), authority, confidence);
    } else {
      item.addMetadata(schema, element, qualifier, lang, field.getText());
    }
  }
  /**
   * Add the current date to the item metadata. This looks up the field in which to store this
   * metadata in the configuration sword.updated.field
   *
   * @param item
   * @throws DSpaceSwordException
   */
  protected void setUpdatedDate(Item item, VerboseDescription verboseDescription)
      throws DSpaceSwordException {
    String field = ConfigurationManager.getProperty("swordv2-server", "updated.field");
    if (field == null || "".equals(field)) {
      throw new DSpaceSwordException(
          "No configuration, or configuration is invalid for: sword.updated.field");
    }

    MDValue dc = this.configToDC(field, null);
    item.clearMetadata(dc.getSchema(), dc.getElement(), dc.getQualifier(), MDValue.ANY);
    item.addMetadata(
        dc.getSchema(), dc.getElement(), dc.getQualifier(), null, Utils.asISO8601(new Date()));

    verboseDescription.append("Updated date added to response from item metadata where available");
  }
  /**
   * Store the given slug value (which is used for suggested identifiers, and which DSpace ignores)
   * in the item metadata. This looks up the field in which to store this metadata in the
   * configuration sword.slug.field
   *
   * @param item
   * @param slugVal
   * @throws DSpaceSwordException
   */
  protected void setSlug(Item item, String slugVal, VerboseDescription verboseDescription)
      throws DSpaceSwordException {
    // if there isn't a slug value, don't set it
    if (slugVal == null) {
      return;
    }

    String field = ConfigurationManager.getProperty("swordv2-server", "slug.field");
    if (field == null || "".equals(field)) {
      throw new DSpaceSwordException(
          "No configuration, or configuration is invalid for: sword.slug.field");
    }

    MDValue dc = this.configToDC(field, null);
    item.clearMetadata(dc.getSchema(), dc.getElement(), dc.getQualifier(), MDValue.ANY);
    item.addMetadata(dc.getSchema(), dc.getElement(), dc.getQualifier(), null, slugVal);

    verboseDescription.append("Slug value set in response where available");
  }
Example #4
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;
  }
Example #5
0
  private void addDCValue(Context c, Item i, String schema, Node n)
      throws TransformerException, SQLException, AuthorizeException {
    String value = getStringValue(n); // n.getNodeValue();
    // compensate for empty value getting read as "null", which won't display
    if (value == null) {
      value = "";
    }
    // //getElementData(n, "element");
    String element = getAttributeValue(n, "element");
    String qualifier = getAttributeValue(n, "qualifier"); // NodeValue();
    // //getElementData(n,
    // "qualifier");
    String language = getAttributeValue(n, "language");
    if (language != null) {
      language = language.trim();
    }

    if (!isQuiet) {
      System.out.println(
          "\tSchema: "
              + schema
              + " Element: "
              + element
              + " Qualifier: "
              + qualifier
              + " Value: "
              + value);
    }

    if ("none".equals(qualifier) || "".equals(qualifier)) {
      qualifier = null;
    }

    // if language isn't set, use the system's default value
    if (StringUtils.isEmpty(language)) {
      language = ConfigurationManager.getProperty("default.language");
    }

    // a goofy default, but there it is
    if (language == null) {
      language = "en";
    }

    if (!isTest) {
      i.addMetadata(schema, element, qualifier, language, value);
    } else {
      // If we're just test the import, let's check that the actual metadata field exists.
      MetadataSchema foundSchema = MetadataSchema.find(c, schema);

      if (foundSchema == null) {
        System.out.println("ERROR: schema '" + schema + "' was not found in the registry.");
        return;
      }

      int schemaID = foundSchema.getSchemaID();
      MetadataField foundField = MetadataField.findByElement(c, schemaID, element, qualifier);

      if (foundField == null) {
        System.out.println(
            "ERROR: Metadata field: '"
                + schema
                + "."
                + element
                + "."
                + qualifier
                + "' was not found in the registry.");
        return;
      }
    }
  }