Skip to content

Commit

Permalink
issue #3145 need to provide resource for afterDelete event
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Arnold <robin.arnold@ibm.com>
  • Loading branch information
punktilious committed Feb 15, 2022
1 parent cbe142d commit f2647b8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import com.ibm.fhir.path.evaluator.FHIRPathEvaluator;
import com.ibm.fhir.path.evaluator.FHIRPathEvaluator.EvaluationContext;
import com.ibm.fhir.persistence.FHIRPersistence;
import com.ibm.fhir.persistence.FHIRPersistenceSupport;
import com.ibm.fhir.persistence.FHIRPersistenceTransaction;
import com.ibm.fhir.persistence.HistorySortOrder;
import com.ibm.fhir.persistence.InteractionStatus;
Expand Down Expand Up @@ -1022,7 +1021,8 @@ private FHIRPersistenceNotSupportedException buildNotSupportedException(String m
}

@Override
public <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId) throws FHIRPersistenceException {
public <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId,
com.ibm.fhir.model.type.Instant lastUpdated) throws FHIRPersistenceException {
final String METHODNAME = "delete";
log.entering(CLASSNAME, METHODNAME);

Expand All @@ -1031,7 +1031,6 @@ public <T extends Resource> void delete(FHIRPersistenceContext context, Class<T>
ResourceDAO resourceDao = makeResourceDAO(connection);

// Create a new Resource DTO instance to represent the deletion marker.
final Instant lastUpdated = FHIRPersistenceSupport.getCurrentInstant();
final int newVersionId = versionId + 1;
com.ibm.fhir.persistence.jdbc.dto.Resource resourceDTO =
createResourceDTO(resourceType, logicalId, newVersionId, lastUpdated, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.ibm.fhir.persistence.jdbc.dao.api.ICommonTokenValuesCache;
import com.ibm.fhir.persistence.jdbc.impl.FHIRPersistenceJDBCImpl;
import com.ibm.fhir.persistence.jdbc.test.util.DerbyInitializer;
import com.ibm.fhir.persistence.util.FHIRPersistenceUtil;
import com.ibm.fhir.search.context.FHIRSearchContext;
import com.ibm.fhir.search.util.SearchUtil;

Expand Down Expand Up @@ -130,7 +131,8 @@ public void teardown() throws Exception {
FHIRSearchContext ctx = SearchUtil.parseQueryParameters(Location.class, Collections.emptyMap(), true, true);
FHIRPersistenceContext persistenceContext =
FHIRPersistenceContextFactory.createPersistenceContext(null, ctx);
persistence.delete(persistenceContext, savedResource.getClass(), savedResource.getId(), FHIRPersistenceSupport.getMetaVersionId(savedResource));
com.ibm.fhir.model.type.Instant lastUpdated = FHIRPersistenceUtil.getUpdateTime();
persistence.delete(persistenceContext, savedResource.getClass(), savedResource.getId(), FHIRPersistenceSupport.getMetaVersionId(savedResource), lastUpdated);
if (persistence.isTransactional()) {
persistence.getTransaction().end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ <T extends Resource> SingleResourceResult<T> vread(FHIRPersistenceContext contex
* @param resourceType the resource type of the Resource instance to be deleted
* @param logicalId the logical id of the Resource instance to be deleted
* @param versionId the current version of the Resource instance to be deleted
* @param lastUpdated the modification timestamp to use for the deletion
* @throws FHIRPersistenceException
*/
default <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId) throws FHIRPersistenceException {
default <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId,
com.ibm.fhir.model.type.Instant lastUpdated) throws FHIRPersistenceException {
throw new FHIRPersistenceNotSupportedException("The 'delete' operation is not supported by this persistence implementation");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void test4() throws Exception {
assertNotNull(persistence);

assertFalse(persistence.isDeleteSupported());
persistence.delete(null, null, null, -1);
persistence.delete(null, null, null, -1, null);
}

@Test(expectedExceptions = {FHIRPersistenceException.class})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public static <T extends Resource> void delete(FHIRPersistence persistence, FHIR
}

final int currentVersionId = FHIRPersistenceSupport.getMetaVersionId(resource);
persistence.delete(context, resource.getClass(), resource.getId(), currentVersionId);
final com.ibm.fhir.model.type.Instant lastUpdated = FHIRPersistenceUtil.getUpdateTime();
persistence.delete(context, resource.getClass(), resource.getId(), currentVersionId, lastUpdated);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,18 +1008,23 @@ public FHIRRestOperationResponse doDelete(String type, String id, String searchQ
FHIRPersistenceContextImpl.builder(event)
.build();

persistence.delete(persistenceContext, resourceType, resourceToDelete.getId(), currentVersionNumber);
final com.ibm.fhir.model.type.Instant lastUpdated = com.ibm.fhir.model.type.Instant.now(ZoneOffset.UTC);
persistence.delete(persistenceContext, resourceType, resourceToDelete.getId(), currentVersionNumber, lastUpdated);

if (responseBundle.getEntry().size() == 1) {
// The response needs to return the version number of the deletion marker
// as the ETag. This was previously obtained by returning the modified resource,
// which we no longer have. So we have to make up a copy of the resource
// to support this
// which we no longer have.
int newVersionNumber = currentVersionNumber + 1;
ior.setVersionForETag(newVersionNumber);
}

// Invoke the 'afterDelete' interceptor methods.
// Invoke the 'afterDelete' interceptor methods. To support the notification service, we
// need to provide a resource with the lastUpdated element set. This is just to simplify
// passing values, even though the resource itself doesn't really exist
final int newVersionNumber = currentVersionNumber + 1;
Resource deletionMarker = FHIRPersistenceUtil.copyAndSetResourceMetaFields(resourceToDelete, resourceToDelete.getId(), newVersionNumber, lastUpdated);
event.setFhirResource(deletionMarker);
getInterceptorMgr().fireAfterDeleteEvent(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public int reindex(FHIRPersistenceContext context, Builder operationOutcomeResul
}

@Override
public <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId) throws FHIRPersistenceException {
public <T extends Resource> void delete(FHIRPersistenceContext context, Class<T> resourceType, String logicalId, int versionId,
com.ibm.fhir.model.type.Instant lastUpdated) throws FHIRPersistenceException {
// NOP. No need to do anything in this very simple mock
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
import com.ibm.fhir.model.type.code.NarrativeStatus;
import com.ibm.fhir.model.type.code.ProcedureStatus;
import com.ibm.fhir.persistence.FHIRPersistence;
import com.ibm.fhir.persistence.FHIRPersistenceSupport;
import com.ibm.fhir.persistence.ResourceResult;
import com.ibm.fhir.persistence.SingleResourceResult;
import com.ibm.fhir.persistence.context.FHIRPersistenceEvent;
import com.ibm.fhir.persistence.exception.FHIRPersistenceException;
import com.ibm.fhir.search.context.FHIRSearchContext;
import com.ibm.fhir.search.context.FHIRSearchContextFactory;
import com.ibm.fhir.server.interceptor.FHIRPersistenceInterceptorMgr;
Expand Down Expand Up @@ -2019,8 +2021,19 @@ public void beforeDelete(FHIRPersistenceEvent event) {

@Override
public void afterDelete(FHIRPersistenceEvent event) {
assertNotNull(event.getPrevFhirResource());
assertNull(event.getFhirResource());
try {
assertNotNull(event.getPrevFhirResource());
int currentVersion = FHIRPersistenceSupport.getMetaVersionId(event.getPrevFhirResource());

// The event contains a version of the resource with the lastUpdated time set
assertNotNull(event.getFhirResource());
assertNotNull(event.getFhirResource().getMeta());
assertNotNull(event.getFhirResource().getMeta().getLastUpdated());
int newVersion = FHIRPersistenceSupport.getMetaVersionId(event.getFhirResource());
assertEquals(newVersion, currentVersion+1);
} catch (FHIRPersistenceException x) {
fail("afterDelete(event)", x);
}
}
};
FHIRPersistenceInterceptorMgr.getInstance().addInterceptor(interceptor);
Expand Down

0 comments on commit f2647b8

Please sign in to comment.