From 8356a07d9bf9826348d986dd3f223a59ef07cc2f Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Fri, 25 Feb 2011 01:19:27 +0000 Subject: [PATCH] CSPACE-3599: Related blob is not being deleted when a Media record is deleted. --- .../services/client/PayloadPart.java | 7 ++- .../services/client/PoxPayload.java | 6 +-- .../services/media/MediaResource.java | 47 ++++++++++++++++++- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/services/client/src/main/java/org/collectionspace/services/client/PayloadPart.java b/services/client/src/main/java/org/collectionspace/services/client/PayloadPart.java index 9cdece300..810c6ba0b 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/PayloadPart.java +++ b/services/client/src/main/java/org/collectionspace/services/client/PayloadPart.java @@ -60,7 +60,12 @@ public abstract class PayloadPart { } public Object getBody() { - return body; + Object result = body; + if (result == null) { + body = PoxPayload.toObject(this.getElementBody()); + result = body; + } + return result; } public Element getElementBody() { diff --git a/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java b/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java index 32ffddf3e..8fc3c58c7 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java +++ b/services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java @@ -235,7 +235,7 @@ public abstract class PoxPayload { } /** - * Attempts to marshal a DOM4j element into an instance of a JAXB object + * Attempts to marshal a DOM4j element (for a part) into an instance of a JAXB object * * @param elementInput the element input * @return the object @@ -259,7 +259,7 @@ public abstract class PoxPayload { } /** - * Attempts to unmarshal a JAXB object to a DOM4j element. + * Attempts to unmarshal a JAXB object (for a part) to a DOM4j element. * * @param jaxbObject the jaxb object * @return the element @@ -288,7 +288,7 @@ public abstract class PoxPayload { } /** - * Attempts to unmarshal a JAXB object to a DOM4j element. + * Attempts to unmarshal a JAXB object (for a part) to a DOM4j element. * * @param jaxbObject the jaxb object * @return the element diff --git a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java index df74272a5..fcf4f6e73 100644 --- a/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java +++ b/services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java @@ -25,6 +25,7 @@ package org.collectionspace.services.media; import org.collectionspace.services.client.BlobClient; import org.collectionspace.services.client.MediaClient; +import org.collectionspace.services.client.PayloadOutputPart; import org.collectionspace.services.client.PoxPayloadIn; import org.collectionspace.services.client.PoxPayloadOut; import org.collectionspace.services.common.ResourceBase; @@ -36,12 +37,14 @@ import org.collectionspace.services.common.blob.BlobUtil; import org.collectionspace.services.common.context.ServiceContext; import org.collectionspace.services.blob.BlobResource; import org.collectionspace.services.nuxeo.client.java.CommonList; +import org.jboss.resteasy.util.HttpResponseCodes; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -62,12 +65,17 @@ import java.util.List; @Produces("application/xml") public class MediaResource extends ResourceBase { final Logger logger = LoggerFactory.getLogger(MediaResource.class); + final static MediaClient mediaClient = new MediaClient(); @Override public String getServiceName(){ return MediaClient.SERVICE_NAME; } + public String getCommonPartName() { + return mediaClient.getCommonPartName(); + } + private BlobResource blobResource = new BlobResource(); BlobResource getBlobResource() { @@ -125,7 +133,7 @@ public class MediaResource extends ResourceBase { PoxPayloadIn input = null; try { - ServiceContext ctx = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input); + ServiceContext ctx = createServiceContext(BlobClient.SERVICE_NAME, input); BlobInput blobInput = BlobUtil.getBlobInput(ctx); blobInput.createBlobFile(blobUri); response = this.create(input, ctx); @@ -267,4 +275,41 @@ public class MediaResource extends ResourceBase { return result; } + + @DELETE + @Path("{csid}") + @Override + public Response delete(@PathParam("csid") String csid) { + try { + // + // First, the media record so we can find and delete any related + // Blob instances. + // + ServiceContext ctx = createServiceContext(); + PoxPayloadOut mediaPayload = this.get(csid, ctx); + PayloadOutputPart mediaPayloadPart = mediaPayload.getPart(getCommonPartName()); + MediaCommon mediaCommon = (MediaCommon) mediaPayloadPart.getBody(); + String blobCsid = mediaCommon.getBlobCsid(); + // + // Delete the blob if it exists. + // + if (blobCsid != null && !blobCsid.isEmpty()) { + Response response = getBlobResource().delete(blobCsid); + if (logger.isDebugEnabled() == true) { + if (response.getStatus() != HttpResponseCodes.SC_OK) { + logger.debug("Problem deleting related blob record of Media record: " + + "Media CSID=" + csid + " " + + "Blob CSID=" + blobCsid); + } + } + } + // + // Now that we've handled the related Blob record, delete the Media record + // + return super.delete(csid, ctx); + } catch (Exception e) { + throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid); + } + } + } -- 2.47.3