]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-3599: Related blob is not being deleted when a Media record is deleted.
authorRichard Millet <richard.millet@berkeley.edu>
Fri, 25 Feb 2011 01:19:27 +0000 (01:19 +0000)
committerRichard Millet <richard.millet@berkeley.edu>
Fri, 25 Feb 2011 01:19:27 +0000 (01:19 +0000)
services/client/src/main/java/org/collectionspace/services/client/PayloadPart.java
services/client/src/main/java/org/collectionspace/services/client/PoxPayload.java
services/media/service/src/main/java/org/collectionspace/services/media/MediaResource.java

index 9cdece30033d6f1906d2077478e219c7b05855e5..810c6ba0b973b6e31732d8a3bb5bcfc16a62b27e 100644 (file)
@@ -60,7 +60,12 @@ public abstract class PayloadPart {
        }\r
        \r
        public Object getBody() {\r
-               return body;\r
+               Object result = body;\r
+               if (result == null) {\r
+                       body = PoxPayload.toObject(this.getElementBody());\r
+                       result = body;\r
+               }\r
+               return result;\r
        }\r
        \r
        public Element getElementBody() {\r
index 32ffddf3e2caf801589e0cf45741ad526b80ed8b..8fc3c58c79e3c2f205b773fc15e6bb5dcc0436a4 100644 (file)
@@ -235,7 +235,7 @@ public abstract class PoxPayload<PT extends PayloadPart> {
     }\r
       \r
     /**\r
-     * Attempts to marshal a DOM4j element into an instance of a JAXB object\r
+     * Attempts to marshal a DOM4j element (for a part) into an instance of a JAXB object\r
      *\r
      * @param elementInput the element input\r
      * @return the object\r
@@ -259,7 +259,7 @@ public abstract class PoxPayload<PT extends PayloadPart> {
     }\r
        \r
     /**\r
-     * Attempts to unmarshal a JAXB object to a DOM4j element.\r
+     * Attempts to unmarshal a JAXB object (for a part) to a DOM4j element.\r
      *\r
      * @param jaxbObject the jaxb object\r
      * @return the element\r
@@ -288,7 +288,7 @@ public abstract class PoxPayload<PT extends PayloadPart> {
     }\r
     \r
     /**\r
-     * Attempts to unmarshal a JAXB object to a DOM4j element.\r
+     * Attempts to unmarshal a JAXB object (for a part) to a DOM4j element.\r
      *\r
      * @param jaxbObject the jaxb object\r
      * @return the element\r
index df74272a5f49a7d8dad63b9111ce156506664938..fcf4f6e73c0a65e378446acb8468bf14999ceb70 100644 (file)
@@ -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<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(BlobUtil.BLOB_RESOURCE_NAME, input);
+               ServiceContext<PoxPayloadIn, PoxPayloadOut> 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<PoxPayloadIn, PoxPayloadOut> 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);
+        }
+    }
+    
 }