Search in sources :

Example 96 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class EZIDIdentifierProvider method delete.

@Override
public void delete(Context context, DSpaceObject dso, String identifier) throws IdentifierException {
    log.debug("delete {} from {}", identifier, dso);
    DSpaceObjectService<DSpaceObject> dsoService = contentServiceFactory.getDSpaceObjectService(dso);
    List<MetadataValue> metadata = dsoService.getMetadata(dso, MD_SCHEMA, DOI_ELEMENT, DOI_QUALIFIER, null);
    List<String> remainder = new ArrayList<>();
    int skipped = 0;
    for (MetadataValue id : metadata) {
        if (!id.getValue().equals(idToDOI(identifier))) {
            remainder.add(id.getValue());
            continue;
        }
        EZIDResponse response;
        try {
            EZIDRequest request = requestFactory.getInstance(loadAuthority(), loadUser(), loadPassword());
            response = request.delete(DOIToId(id.getValue()));
        } catch (URISyntaxException e) {
            log.error("Bad URI in metadata value {}:  {}", id.getValue(), e.getMessage());
            remainder.add(id.getValue());
            skipped++;
            continue;
        } catch (IOException e) {
            log.error("Failed request to EZID:  {}", e.getMessage());
            remainder.add(id.getValue());
            skipped++;
            continue;
        }
        if (!response.isSuccess()) {
            log.error("Unable to delete {} from DataCite:  {}", id.getValue(), response.getEZIDStatusValue());
            remainder.add(id.getValue());
            skipped++;
            continue;
        }
        log.info("Deleted {}", id.getValue());
    }
    // delete from item
    try {
        dsoService.clearMetadata(context, dso, MD_SCHEMA, DOI_ELEMENT, DOI_QUALIFIER, null);
        dsoService.addMetadata(context, dso, MD_SCHEMA, DOI_ELEMENT, DOI_QUALIFIER, null, remainder);
        dsoService.update(context, dso);
    } catch (SQLException | AuthorizeException e) {
        log.error("Failed to re-add identifiers:  {}", e.getMessage());
    }
    if (skipped > 0) {
        throw new IdentifierException(identifier + " could not be deleted.");
    }
}
Also used : MetadataValue(org.dspace.content.MetadataValue) SQLException(java.sql.SQLException) AuthorizeException(org.dspace.authorize.AuthorizeException) ArrayList(java.util.ArrayList) EZIDResponse(org.dspace.identifier.ezid.EZIDResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) EZIDRequest(org.dspace.identifier.ezid.EZIDRequest) DSpaceObject(org.dspace.content.DSpaceObject)

Example 97 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class HandleIdentifierProvider method populateHandleMetadata.

protected void populateHandleMetadata(Context context, DSpaceObject dso, String handle) throws SQLException, IOException, AuthorizeException {
    String handleref = handleService.getCanonicalForm(handle);
    DSpaceObjectService<DSpaceObject> dsoService = contentServiceFactory.getDSpaceObjectService(dso);
    // Add handle as identifier.uri DC value.
    // First check that identifier doesn't already exist.
    boolean identifierExists = false;
    List<MetadataValue> identifiers = dsoService.getMetadata(dso, MetadataSchemaEnum.DC.getName(), "identifier", "uri", Item.ANY);
    for (MetadataValue identifier : identifiers) {
        if (handleref.equals(identifier.getValue())) {
            identifierExists = true;
        }
    }
    if (!identifierExists) {
        dsoService.addMetadata(context, dso, MetadataSchemaEnum.DC.getName(), "identifier", "uri", null, handleref);
    }
}
Also used : MetadataValue(org.dspace.content.MetadataValue) DSpaceObject(org.dspace.content.DSpaceObject)

Example 98 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class DOIIdentifierProvider method getDOIOutOfObject.

/**
 * Loads a DOI out of the metadata of an DSpaceObject.
 *
 * @param dso DSpace object to get DOI metadata from
 * @return The DOI or null if no DOI was found.
 * @throws DOIIdentifierException if identifier error
 */
public String getDOIOutOfObject(DSpaceObject dso) throws DOIIdentifierException {
    // FIXME
    if (!(dso instanceof Item)) {
        throw new IllegalArgumentException("We currently support DOIs for Items only, not for " + contentServiceFactory.getDSpaceObjectService(dso).getTypeText(dso) + ".");
    }
    Item item = (Item) dso;
    List<MetadataValue> metadata = itemService.getMetadata(item, MD_SCHEMA, DOI_ELEMENT, DOI_QUALIFIER, null);
    String leftPart = doiService.getResolver() + SLASH + getPrefix() + SLASH + getNamespaceSeparator();
    for (MetadataValue id : metadata) {
        if (id.getValue().startsWith(leftPart)) {
            return doiService.DOIFromExternalFormat(id.getValue());
        }
    }
    return null;
}
Also used : Item(org.dspace.content.Item) MetadataValue(org.dspace.content.MetadataValue)

Example 99 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class ReportGenerator method getItemInfo.

/**
 * get the information for the item with the given handle
 *
 * @param context the DSpace context we are operating under
 * @param handle  the handle of the item being looked up, in the form
 *                1234/567 and so forth
 * @return a string containing a reference (almost citation) to the
 * article
 * @throws SQLException if database error
 */
public static String getItemInfo(Context context, String handle) throws SQLException {
    Item item = null;
    // ensure that the handle exists
    try {
        item = (Item) handleService.resolveToObject(context, handle);
    } catch (Exception e) {
        return null;
    }
    // if no handle that matches is found then also return null
    if (item == null) {
        return null;
    }
    // build the referece
    // FIXME: here we have blurred the line between content and presentation
    // and it should probably be un-blurred
    List<MetadataValue> title = itemService.getMetadata(item, MetadataSchemaEnum.DC.getName(), "title", null, Item.ANY);
    List<MetadataValue> author = itemService.getMetadata(item, MetadataSchemaEnum.DC.getName(), "contributor", "author", Item.ANY);
    StringBuilder authors = new StringBuilder();
    if (author.size() > 0) {
        authors.append("(").append(author.get(0).getValue());
    }
    if (author.size() > 1) {
        authors.append(" et al");
    }
    if (author.size() > 0) {
        authors.append(")");
    }
    String content = title.get(0).getValue() + " " + authors.toString();
    return content;
}
Also used : Item(org.dspace.content.Item) MetadataValue(org.dspace.content.MetadataValue) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 100 with MetadataValue

use of org.dspace.content.MetadataValue in project DSpace by DSpace.

the class DeleteMetadataAction method execute.

/**
 * Delete metadata from item
 *
 * @param context      DSpace Context
 * @param itarch       Item Archive
 * @param isTest       test flag
 * @param suppressUndo undo flag
 * @throws SQLException       if database error
 * @throws AuthorizeException if authorization error
 * @throws ParseException     if parse error
 */
@Override
public void execute(Context context, ItemArchive itarch, boolean isTest, boolean suppressUndo) throws AuthorizeException, ParseException, SQLException {
    Item item = itarch.getItem();
    for (String f : targetFields) {
        DtoMetadata dummy = DtoMetadata.create(f, Item.ANY, "");
        List<MetadataValue> ardcv = itemService.getMetadataByMetadataString(item, f);
        ItemUpdate.pr("Metadata to be deleted: ");
        for (MetadataValue dcv : ardcv) {
            ItemUpdate.pr("  " + MetadataUtilities.getDCValueString(dcv));
        }
        if (!isTest) {
            if (!suppressUndo) {
                for (MetadataValue dcv : ardcv) {
                    MetadataField metadataField = dcv.getMetadataField();
                    MetadataSchema metadataSchema = metadataField.getMetadataSchema();
                    itarch.addUndoMetadataField(DtoMetadata.create(metadataSchema.getName(), metadataField.getElement(), metadataField.getQualifier(), dcv.getLanguage(), dcv.getValue()));
                }
            }
            itemService.clearMetadata(context, item, dummy.schema, dummy.element, dummy.qualifier, Item.ANY);
        }
    }
}
Also used : Item(org.dspace.content.Item) MetadataValue(org.dspace.content.MetadataValue) MetadataSchema(org.dspace.content.MetadataSchema) MetadataField(org.dspace.content.MetadataField)

Aggregations

MetadataValue (org.dspace.content.MetadataValue)140 Item (org.dspace.content.Item)45 ArrayList (java.util.ArrayList)39 SQLException (java.sql.SQLException)29 MetadataField (org.dspace.content.MetadataField)22 Test (org.junit.Test)21 Date (java.util.Date)16 Collection (org.dspace.content.Collection)16 IOException (java.io.IOException)15 AuthorizeException (org.dspace.authorize.AuthorizeException)13 List (java.util.List)11 Community (org.dspace.content.Community)11 WorkspaceItem (org.dspace.content.WorkspaceItem)11 MetadataValueRest (org.dspace.app.rest.model.MetadataValueRest)10 DCDate (org.dspace.content.DCDate)9 MetadataSchema (org.dspace.content.MetadataSchema)9 EPerson (org.dspace.eperson.EPerson)9 WorkflowItem (org.dspace.workflow.WorkflowItem)9 AbstractUnitTest (org.dspace.AbstractUnitTest)8 AbstractEntityIntegrationTest (org.dspace.app.rest.test.AbstractEntityIntegrationTest)8