Skip to content
Permalink
Browse files

AssetResource delete now supports multiple assets

  • Loading branch information...
richturner committed Sep 6, 2019
1 parent b7905e3 commit fd255078765ca8a2274623750e66272a5221280d
@@ -340,7 +340,7 @@ public void delete() {
view.setFormBusy(true);
clearViewMessages();
environment.getApp().getRequests().send(
requestParams -> assetResource.delete(requestParams, this.assetId),
requestParams -> assetResource.delete(requestParams, Collections.singletonList(this.assetId)),
204,
() -> {
view.setFormBusy(false);
@@ -289,7 +289,7 @@ public Asset mergeAsset(Asset asset, MergeOptions options) {
@Override
public boolean deleteAsset(String assetId) {
LOG.fine("Deleting protocol-provided: " + assetId);
return assetStorageService.delete(assetId);
return assetStorageService.delete(Collections.singletonList(assetId));
}

@Override
@@ -505,20 +505,27 @@ public Asset create(RequestParams requestParams, Asset asset) {
}

@Override
public void delete(RequestParams requestParams, String assetId) {
public void delete(RequestParams requestParams, List<String> assetIds) {
try {
if (assetIds == null || assetIds.isEmpty()) {
throw new WebApplicationException(BAD_REQUEST);
}

if (isRestrictedUser()) {
throw new WebApplicationException(FORBIDDEN);
}
Asset asset = assetStorageService.find(assetId, true);
if (asset == null)
List<Asset> assets = assetStorageService.findAll(new AssetQuery().ids(assetIds.toArray(new String[0])).select(Select.selectExcludePathAndAttributes()));
if (assets == null || assets.size() != assetIds.size()) {
LOG.fine("Request to delete one or more invalid assets");
return;
}

if (!isTenantActiveAndAccessible(asset)) {
LOG.fine("Forbidden access for user '" + getUsername() + "', can't delete: " + asset);
if (assets.stream().anyMatch(asset -> !isTenantActiveAndAccessible(asset))) {
LOG.fine("Forbidden access for user '" + getUsername() + "', can't delete requested assets");
throw new WebApplicationException(FORBIDDEN);
}
if (!assetStorageService.delete(assetId)) {

if (!assetStorageService.delete(assetIds)) {
throw new WebApplicationException(BAD_REQUEST);
}
} catch (IllegalStateException ex) {
@@ -75,7 +75,6 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.joining;
import static org.openremote.container.persistence.PersistenceEvent.PERSISTENCE_TOPIC;
@@ -472,22 +471,25 @@ public Asset merge(Asset asset, boolean overrideVersion, String userName) {
}

/**
* @return <code>true</code> if the asset was deleted, false if the asset still has children and can't be deleted.
* @return <code>true</code> if the assets were deleted, false if any of the assets still have children and can't be deleted.
*/
public boolean delete(String assetId) {
return persistenceService.doReturningTransaction(em -> {
Asset asset = em.find(Asset.class, assetId);
if (asset != null) {
List<Asset> children = findAll(em, new AssetQuery()
.parents(new ParentPredicate(asset.getId()))
);
if (children.size() > 0)
return false;
LOG.fine("Removing: " + asset);
em.remove(asset);
}
return true;
});
public boolean delete(List<String> assetIds) {
try {
persistenceService.doTransaction(em -> {
LOG.fine("Removing: " + String.join(", ", assetIds));
int count = em
.createQuery("delete from Asset a where not exists(select child.id from Asset child where child.parentId = a.id) and a.id in :ids")
.setParameter("ids", assetIds)
.executeUpdate();
if (assetIds.size() != count) {
throw new IllegalArgumentException("Cannot delete one or more requested assets as they have children");
}
});
} catch (Exception e) {
return false;
}

return true;
}

public boolean isUserAsset(String assetId) {
@@ -30,6 +30,8 @@
import javax.validation.Valid;
import javax.ws.rs.*;

import java.util.List;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

/**
@@ -236,12 +238,11 @@ void deleteUserAsset(@BeanParam RequestParams requestParams,
* has children and therefore can't be deleted.
*/
@DELETE
@Path("{assetId}")
@Produces(APPLICATION_JSON)
@SuccessStatusCode(204)
@RolesAllowed({"write:assets"})
@SuppressWarnings("unusable-by-js")
void delete(@BeanParam RequestParams requestParams, @PathParam("assetId") String assetId);
void delete(@BeanParam RequestParams requestParams, @QueryParam("assetId") List<String> assetIds);

/**
* Retrieve assets using an {@link AssetQuery}.

0 comments on commit fd25507

Please sign in to comment.
You can’t perform that action at this time.