Search in sources :

Example 1 with IdServiceBean

use of edu.harvard.iq.dataverse.IdServiceBean in project dataverse by IQSS.

the class DestroyDatasetCommand method executeImpl.

@Override
protected void executeImpl(CommandContext ctxt) throws CommandException {
    // first check if dataset is released, and if so, if user is a superuser
    if (doomed.isReleased() && (!(getUser() instanceof AuthenticatedUser) || !getUser().isSuperuser())) {
        throw new PermissionException("Destroy can only be called by superusers.", this, Collections.singleton(Permission.DeleteDatasetDraft), doomed);
    }
    // If there is a dedicated thumbnail DataFile, it needs to be reset
    // explicitly, or we'll get a constraint violation when deleting:
    doomed.setThumbnailFile(null);
    final Dataset managedDoomed = ctxt.em().merge(doomed);
    List<String> datasetAndFileSolrIdsToDelete = new ArrayList<>();
    // files need to iterate through and remove 'by hand' to avoid
    // optimistic lock issues... (plus the physical files need to be
    // deleted too!)
    Iterator<DataFile> dfIt = doomed.getFiles().iterator();
    while (dfIt.hasNext()) {
        DataFile df = dfIt.next();
        // Gather potential Solr IDs of files. As of this writing deaccessioned files are never indexed.
        String solrIdOfPublishedFile = IndexServiceBean.solrDocIdentifierFile + df.getId();
        datasetAndFileSolrIdsToDelete.add(solrIdOfPublishedFile);
        String solrIdOfDraftFile = IndexServiceBean.solrDocIdentifierFile + df.getId() + IndexServiceBean.draftSuffix;
        datasetAndFileSolrIdsToDelete.add(solrIdOfDraftFile);
        ctxt.engine().submit(new DeleteDataFileCommand(df, getRequest(), true));
        dfIt.remove();
    }
    // also, lets delete the uploaded thumbnails!
    deleteDatasetLogo(doomed);
    // ASSIGNMENTS
    for (RoleAssignment ra : ctxt.roles().directRoleAssignments(doomed)) {
        ctxt.em().remove(ra);
    }
    // ROLES
    for (DataverseRole ra : ctxt.roles().findByOwnerId(doomed.getId())) {
        ctxt.em().remove(ra);
    }
    IdServiceBean idServiceBean = IdServiceBean.getBean(ctxt);
    try {
        if (idServiceBean.alreadyExists(doomed)) {
            idServiceBean.deleteIdentifier(doomed);
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Identifier deletion was not successfull:", e.getMessage());
    }
    Dataverse toReIndex = managedDoomed.getOwner();
    // dataset
    ctxt.em().remove(managedDoomed);
    // add potential Solr IDs of datasets to list for deletion
    String solrIdOfPublishedDatasetVersion = IndexServiceBean.solrDocIdentifierDataset + doomed.getId();
    datasetAndFileSolrIdsToDelete.add(solrIdOfPublishedDatasetVersion);
    String solrIdOfDraftDatasetVersion = IndexServiceBean.solrDocIdentifierDataset + doomed.getId() + IndexServiceBean.draftSuffix;
    datasetAndFileSolrIdsToDelete.add(solrIdOfDraftDatasetVersion);
    String solrIdOfDeaccessionedDatasetVersion = IndexServiceBean.solrDocIdentifierDataset + doomed.getId() + IndexServiceBean.deaccessionedSuffix;
    datasetAndFileSolrIdsToDelete.add(solrIdOfDeaccessionedDatasetVersion);
    IndexResponse resultOfSolrDeletionAttempt = ctxt.solrIndex().deleteMultipleSolrIds(datasetAndFileSolrIdsToDelete);
    logger.log(Level.FINE, "Result of attempt to delete dataset and file IDs from the search index: {0}", resultOfSolrDeletionAttempt.getMessage());
    ctxt.index().indexDataverse(toReIndex);
}
Also used : PermissionException(edu.harvard.iq.dataverse.engine.command.exception.PermissionException) Dataset(edu.harvard.iq.dataverse.Dataset) RoleAssignment(edu.harvard.iq.dataverse.RoleAssignment) ArrayList(java.util.ArrayList) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Dataverse(edu.harvard.iq.dataverse.Dataverse) PermissionException(edu.harvard.iq.dataverse.engine.command.exception.PermissionException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) DataverseRole(edu.harvard.iq.dataverse.authorization.DataverseRole) DataFile(edu.harvard.iq.dataverse.DataFile) IndexResponse(edu.harvard.iq.dataverse.search.IndexResponse) IdServiceBean(edu.harvard.iq.dataverse.IdServiceBean)

Example 2 with IdServiceBean

use of edu.harvard.iq.dataverse.IdServiceBean in project dataverse by IQSS.

the class FinalizeDatasetPublicationCommand method registerExternalIdentifier.

/**
 * Whether it's EZID or DataCite, if the registration is
 * refused because the identifier already exists, we'll generate another one
 * and try to register again... but only up to some
 * reasonably high number of times - so that we don't
 * go into an infinite loop here, if EZID is giving us
 * these duplicate messages in error.
 *
 * (and we do want the limit to be a "reasonably high" number!
 * true, if our identifiers are randomly generated strings,
 * then it is highly unlikely that we'll ever run into a
 * duplicate race condition repeatedly; but if they are sequential
 * numeric values, than it is entirely possible that a large
 * enough number of values will be legitimately registered
 * by another entity sharing the same authority...)
 * @param theDataset
 * @param ctxt
 * @param doiProvider
 * @throws CommandException
 */
private void registerExternalIdentifier(Dataset theDataset, CommandContext ctxt) throws CommandException {
    IdServiceBean idServiceBean = IdServiceBean.getBean(theDataset.getProtocol(), ctxt);
    if (theDataset.getGlobalIdCreateTime() == null) {
        if (idServiceBean != null) {
            try {
                if (!idServiceBean.alreadyExists(theDataset)) {
                    idServiceBean.createIdentifier(theDataset);
                    theDataset.setGlobalIdCreateTime(new Timestamp(new Date().getTime()));
                } else {
                    int attempts = 0;
                    while (idServiceBean.alreadyExists(theDataset) && attempts < FOOLPROOF_RETRIAL_ATTEMPTS_LIMIT) {
                        theDataset.setIdentifier(ctxt.datasets().generateDatasetIdentifier(theDataset, idServiceBean));
                        attempts++;
                    }
                    if (idServiceBean.alreadyExists(theDataset)) {
                        throw new IllegalCommandException("This dataset may not be published because its identifier is already in use by another dataset;gave up after " + attempts + " attempts. Current (last requested) identifier: " + theDataset.getIdentifier(), this);
                    }
                    idServiceBean.createIdentifier(theDataset);
                    theDataset.setGlobalIdCreateTime(new Timestamp(new Date().getTime()));
                }
            } catch (Throwable e) {
                throw new CommandException(BundleUtil.getStringFromBundle("dataset.publish.error", idServiceBean.getProviderInformation()), this);
            }
        } else {
            throw new IllegalCommandException("This dataset may not be published because its id registry service is not supported.", this);
        }
    }
}
Also used : IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) IdServiceBean(edu.harvard.iq.dataverse.IdServiceBean) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 3 with IdServiceBean

use of edu.harvard.iq.dataverse.IdServiceBean in project dataverse by IQSS.

the class UpdateDatasetTargetURLCommand method executeImpl.

@Override
protected void executeImpl(CommandContext ctxt) throws CommandException {
    if (!(getUser() instanceof AuthenticatedUser) || !getUser().isSuperuser()) {
        throw new PermissionException("Update Target URL can only be called by superusers.", this, Collections.singleton(Permission.EditDataset), target);
    }
    IdServiceBean idServiceBean = IdServiceBean.getBean(target.getProtocol(), ctxt);
    HashMap<String, String> metadata = idServiceBean.getMetadataFromDatasetForTargetURL(target);
    try {
        String doiRetString = idServiceBean.modifyIdentifier(target, metadata);
        if (doiRetString != null && doiRetString.contains(target.getIdentifier())) {
            target.setGlobalIdCreateTime(new Timestamp(new Date().getTime()));
            ctxt.em().merge(target);
            ctxt.em().flush();
        } else {
        // do nothing - we'll know it failed because the global id create time won't have been updated.
        }
    } catch (Exception e) {
    // do nothing - idem and the problem has been logged
    }
}
Also used : PermissionException(edu.harvard.iq.dataverse.engine.command.exception.PermissionException) IdServiceBean(edu.harvard.iq.dataverse.IdServiceBean) AuthenticatedUser(edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser) Timestamp(java.sql.Timestamp) Date(java.util.Date) PermissionException(edu.harvard.iq.dataverse.engine.command.exception.PermissionException) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException)

Example 4 with IdServiceBean

use of edu.harvard.iq.dataverse.IdServiceBean in project dataverse by IQSS.

the class FinalizeDatasetPublicationCommand method publicizeExternalIdentifier.

private void publicizeExternalIdentifier(Dataset dataset, CommandContext ctxt) throws CommandException {
    String protocol = theDataset.getProtocol();
    IdServiceBean idServiceBean = IdServiceBean.getBean(protocol, ctxt);
    if (idServiceBean != null)
        try {
            idServiceBean.publicizeIdentifier(dataset);
        } catch (Throwable e) {
            throw new CommandException(BundleUtil.getStringFromBundle("dataset.publish.error", idServiceBean.getProviderInformation()), this);
        }
}
Also used : IdServiceBean(edu.harvard.iq.dataverse.IdServiceBean) CommandException(edu.harvard.iq.dataverse.engine.command.exception.CommandException) IllegalCommandException(edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)

Aggregations

IdServiceBean (edu.harvard.iq.dataverse.IdServiceBean)4 CommandException (edu.harvard.iq.dataverse.engine.command.exception.CommandException)4 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)2 IllegalCommandException (edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException)2 PermissionException (edu.harvard.iq.dataverse.engine.command.exception.PermissionException)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 DataFile (edu.harvard.iq.dataverse.DataFile)1 Dataset (edu.harvard.iq.dataverse.Dataset)1 Dataverse (edu.harvard.iq.dataverse.Dataverse)1 RoleAssignment (edu.harvard.iq.dataverse.RoleAssignment)1 DataverseRole (edu.harvard.iq.dataverse.authorization.DataverseRole)1 IndexResponse (edu.harvard.iq.dataverse.search.IndexResponse)1 ArrayList (java.util.ArrayList)1