Java Code Examples for org.sonatype.nexus.repository.storage.StorageTx#findAsset()

The following examples show how to use org.sonatype.nexus.repository.storage.StorageTx#findAsset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: P2ComponentMaintenance.java    From nexus-repository-p2 with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes the asset and its component if it's the only asset in it.
 */
@Override
@Guarded(by = STARTED)
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }

  final EntityId componentId = asset.componentId();
  if (componentId == null) {
    // Assets without components should be deleted on their own
    return super.deleteAssetTx(assetId, deleteBlob);
  }

  final Component component = tx.findComponent(componentId);
  if (component == null) {
    return Collections.emptySet();
  }
  return deleteComponentTx(componentId, deleteBlob).getAssets();
}
 
Example 2
Source File: HelmComponentMaintenanceFacet.java    From nexus-repository-helm with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 3
Source File: RComponentMaintenance.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes the asset and its component if it's the only asset in it.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }

  final EntityId componentId = asset.componentId();
  if (componentId == null) {
    // Assets without components should be deleted on their own
    return super.deleteAssetTx(assetId, deleteBlob);
  }

  final Component component = tx.findComponent(componentId);
  if (Iterables.size(tx.browseAssets(component)) == 1) {
    // Component with only one asset should be deleted as well with its asset
    return deleteComponentTx(componentId, deleteBlob).getAssets();
  }
  else {
    return super.deleteAssetTx(assetId, deleteBlob);
  }
}
 
Example 4
Source File: CondaComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return emptySet();
  }

  tx.deleteAsset(asset, deleteBlobs);

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }
  return Collections.singleton(asset.name());
}
 
Example 5
Source File: MavenComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@TransactionalDeleteBlob
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = tx.findAsset(assetId);
  if (asset == null) {
    return Collections.emptySet();
  }

  Set<String> deletedAssets = new HashSet<>(super.deleteAssetTx(assetId, deleteBlob));

  final EntityId componentId = asset.componentId();
  if (componentId != null) {
    Component component = tx.findComponent(componentId);
    if (component != null && !tx.browseAssets(component).iterator().hasNext()) {
      deletedAssets.addAll(deleteComponentTx(componentId, deleteBlob).getAssets());
    }
  }

  return deletedAssets;
}
 
Example 6
Source File: NpmHostedComponentMaintenanceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes depending on what it is.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  Asset asset = tx.findAsset(assetId, tx.findBucket(getRepository()));
  if (asset == null) {
    return Collections.emptySet();
  }
  return deleteAssetTx(asset, deleteBlob);
}
 
Example 7
Source File: OrientPyPiComponentMaintenance.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Deletes the asset. If the associated component has no additional assets, then the component is also deleted.
 */
@Override
@TransactionalDeleteBlob
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlob) {
  StorageTx tx = UnitOfWork.currentTx();
  final Bucket bucket = tx.findBucket(getRepository());
  final Asset asset = tx.findAsset(assetId, bucket);
  if (asset == null) {
    return Collections.emptySet();
  }
  Set<String> deletedAssets = new HashSet<>();
  deletedAssets.addAll(super.deleteAssetTx(assetId, deleteBlob));

  final EntityId componentId = asset.componentId();
  if (componentId != null) {
    deleteRootIndex();

    deleteCachedIndexForPackage(asset);

    final Component component = tx.findComponentInBucket(componentId, bucket);
    if (component != null && !tx.browseAssets(component).iterator().hasNext()) {
      deletedAssets.addAll(deleteComponentTx(componentId, deleteBlob).getAssets());
    }
  }

  return deletedAssets;
}
 
Example 8
Source File: AptHostedComponentMaintenanceFacet.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@Transactional(retryOn = ONeedRetryException.class)
@Override
protected Set<String> deleteAssetTx(final EntityId assetId, final boolean deleteBlobs) {
  StorageTx tx = UnitOfWork.currentTx();
  Bucket bucket = tx.findBucket(getRepository());
  Asset asset = tx.findAsset(assetId, bucket);

  if (asset == null) {
    return Collections.emptySet();
  }

  String assetKind = asset.formatAttributes().get(P_ASSET_KIND, String.class);
  Set<String> result = super.deleteAssetTx(assetId, deleteBlobs);
  if ("DEB".equals(assetKind)) {
    try {
      getRepository().facet(AptHostedFacet.class)
          .rebuildIndexes(Collections.singletonList(new AptHostedFacet.AssetChange(AssetAction.REMOVED, asset)));
    }
    catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  if (asset.componentId() != null) {
    Component component = tx.findComponentInBucket(asset.componentId(), bucket);

    if (!tx.browseAssets(component).iterator().hasNext()) {
      log.debug("Deleting component: {}", component);
      tx.deleteComponent(component, deleteBlobs);
    }
  }

  return result;
}
 
Example 9
Source File: Content.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds fresh {@link Asset} instance from passed in TX by entity ID of the {@link Asset} used
 * to create passed in {@link Content} instance. The passed in {@link Content} must have been created with
 * method {@link #extractFromAsset(Asset, Iterable, AttributesMap)} to have proper attributes set for this operation.
 *
 * @see #extractFromAsset(Asset, Iterable, AttributesMap)
 */
@Nullable
public static Asset findAsset(final StorageTx tx, final Bucket bucket, Content content) {
  final Asset contentAsset = content.getAttributes().require(Asset.class);
  if (EntityHelper.hasMetadata(contentAsset)) {
    return tx.findAsset(EntityHelper.id(contentAsset), bucket);
  }
  return null;
}