Example #1
0
    List<DCValue> get(Context c, int resourceId, int resourceTypeId, Logger log)
        throws SQLException {
      if (metadata == null) {
        metadata = new ArrayList<DCValue>();

        // Get Dublin Core metadata
        TableRowIterator tri = retrieveMetadata(resourceId, resourceTypeId);

        if (tri != null) {
          try {
            while (tri.hasNext()) {
              TableRow resultRow = tri.next();

              // Get the associated metadata field and schema information
              int fieldID = resultRow.getIntColumn("metadata_field_id");
              MetadataField field = MetadataField.find(c, fieldID);

              if (field == null) {
                log.error("Loading item - cannot find metadata field " + fieldID);
              } else {
                MetadataSchema schema = MetadataSchema.find(c, field.getSchemaID());
                if (schema == null) {
                  log.error(
                      "Loading item - cannot find metadata schema "
                          + field.getSchemaID()
                          + ", field "
                          + fieldID);
                } else {
                  // Make a DCValue object
                  DCValue dcv = new DCValue();
                  dcv.element = field.getElement();
                  dcv.qualifier = field.getQualifier();
                  dcv.value = resultRow.getStringColumn("text_value");
                  dcv.language = resultRow.getStringColumn("text_lang");
                  // dcv.namespace = schema.getNamespace();
                  dcv.schema = schema.getName();
                  dcv.authority = resultRow.getStringColumn("authority");
                  dcv.confidence = resultRow.getIntColumn("confidence");

                  // Add it to the list
                  metadata.add(dcv);
                }
              }
            }
          } finally {
            // close the TableRowIterator to free up resources
            if (tri != null) {
              tri.close();
            }
          }
        }
      }

      return metadata;
    }
Example #2
0
  /**
   * Get metadata for the DSpace Object in a chosen schema. See <code>MetadataSchema</code> for more
   * information about schemas. Passing in a <code>null</code> value for <code>qualifier</code> or
   * <code>lang</code> only matches metadata fields where that qualifier or languages is actually
   * <code>null</code>. Passing in <code>DSpaceObject.ANY</code> retrieves all metadata fields with
   * any value for the qualifier or language, including <code>null</code>
   *
   * <p>Examples:
   *
   * <p>Return values of the unqualified "title" field, in any language. Qualified title fields
   * (e.g. "title.uniform") are NOT returned:
   *
   * <p><code>dspaceobject.getMetadataByMetadataString("dc", "title", null, DSpaceObject.ANY );
   * </code>
   *
   * <p>Return all US English values of the "title" element, with any qualifier (including
   * unqualified):
   *
   * <p><code>dspaceobject.getMetadataByMetadataString("dc, "title", DSpaceObject.ANY, "en_US" );
   * </code>
   *
   * <p>The ordering of values of a particular element/qualifier/language combination is
   * significant. When retrieving with wildcards, values of a particular element/qualifier/language
   * combinations will be adjacent, but the overall ordering of the combinations is indeterminate.
   *
   * @param schema the schema for the metadata field. <em>Must</em> match the <code>name</code> of
   *     an existing metadata schema.
   * @param element the element name. <code>DSpaceObject.ANY</code> matches any element. <code>null
   *     </code> doesn't really make sense as all metadata must have an element.
   * @param qualifier the qualifier. <code>null</code> means unqualified, and <code>DSpaceObject.ANY
   *     </code> means any qualifier (including unqualified.)
   * @param lang the ISO639 language code, optionally followed by an underscore and the ISO3166
   *     country code. <code>null</code> means only values with no language are returned, and <code>
   *     DSpaceObject.ANY</code> means values with any country code or no country code are returned.
   * @return metadata fields that match the parameters
   */
  public DCValue[] getMetadata(String schema, String element, String qualifier, String lang) {
    // Build up list of matching values
    List<DCValue> values = new ArrayList<DCValue>();
    for (DCValue dcv : getMetadata()) {
      if (match(schema, element, qualifier, lang, dcv)) {
        // We will return a copy of the object in case it is altered
        DCValue copy = new DCValue();
        copy.element = dcv.element;
        copy.qualifier = dcv.qualifier;
        copy.value = dcv.value;
        copy.language = dcv.language;
        copy.schema = dcv.schema;
        copy.authority = dcv.authority;
        copy.confidence = dcv.confidence;
        values.add(copy);
      }
    }

    // Create an array of matching values
    DCValue[] valueArray = new DCValue[values.size()];
    valueArray = (DCValue[]) values.toArray(valueArray);

    return valueArray;
  }
Example #3
0
  /**
   * Add metadata fields. These are appended to existing values. Use <code>clearDC</code> to remove
   * values. The ordering of values passed in is maintained.
   *
   * @param schema the schema for the metadata field. <em>Must</em> match the <code>name</code> of
   *     an existing metadata schema.
   * @param element the metadata element name
   * @param qualifier the metadata qualifier name, or <code>null</code> for unqualified
   * @param lang the ISO639 language code, optionally followed by an underscore and the ISO3166
   *     country code. <code>null</code> means the value has no language (for example, a date).
   * @param values the values to add.
   * @param authorities the external authority key for this value (or null)
   * @param confidences the authority confidence (default 0)
   */
  public void addMetadata(
      String schema,
      String element,
      String qualifier,
      String lang,
      String[] values,
      String authorities[],
      int confidences[]) {
    List<DCValue> dublinCore = getMetadata();
    MetadataAuthorityManager mam = MetadataAuthorityManager.getManager();
    boolean authorityControlled = mam.isAuthorityControlled(schema, element, qualifier);
    boolean authorityRequired = mam.isAuthorityRequired(schema, element, qualifier);
    String fieldName = schema + "." + element + ((qualifier == null) ? "" : "." + qualifier);

    // We will not verify that they are valid entries in the registry
    // until update() is called.
    for (int i = 0; i < values.length; i++) {
      DCValue dcv = new DCValue();
      dcv.schema = schema;
      dcv.element = element;
      dcv.qualifier = qualifier;
      dcv.language = (lang == null ? null : lang.trim());

      // Logic to set Authority and Confidence:
      //  - normalize an empty string for authority to NULL.
      //  - if authority key is present, use given confidence or NOVALUE if not given
      //  - otherwise, preserve confidence if meaningful value was given since it may document a
      // failed authority lookup
      //  - CF_UNSET signifies no authority nor meaningful confidence.
      //  - it's possible to have empty authority & CF_ACCEPTED if e.g. user deletes authority key
      if (authorityControlled) {
        if (authorities != null && authorities[i] != null && authorities[i].length() > 0) {
          dcv.authority = authorities[i];
          dcv.confidence = confidences == null ? Choices.CF_NOVALUE : confidences[i];
        } else {
          dcv.authority = null;
          dcv.confidence = confidences == null ? Choices.CF_UNSET : confidences[i];
        }
        // authority sanity check: if authority is required, was it supplied?
        // XXX FIXME? can't throw a "real" exception here without changing all the callers to expect
        // it, so use a runtime exception
        if (authorityRequired && (dcv.authority == null || dcv.authority.length() == 0)) {
          throw new IllegalArgumentException(
              "The metadata field \""
                  + fieldName
                  + "\" requires an authority key but none was provided. Vaue=\""
                  + dcv.value
                  + "\"");
        }
      }
      if (values[i] != null) {
        // remove control unicode char
        String temp = values[i].trim();
        char[] dcvalue = temp.toCharArray();
        for (int charPos = 0; charPos < dcvalue.length; charPos++) {
          if (Character.isISOControl(dcvalue[charPos])
              && !String.valueOf(dcvalue[charPos]).equals("\u0009")
              && !String.valueOf(dcvalue[charPos]).equals("\n")
              && !String.valueOf(dcvalue[charPos]).equals("\r")) {
            dcvalue[charPos] = ' ';
          }
        }
        dcv.value = String.valueOf(dcvalue);
      } else {
        dcv.value = null;
      }
      dublinCore.add(dcv);
      addDetails(fieldName);
    }

    if (values.length > 0) {
      modifiedMetadata = true;
    }
  }