Implement JPA transaction deletion operation

This commit is contained in:
James Agnew 2015-01-29 10:17:46 -05:00
parent af8bf42e1d
commit 9d7796f0be
6 changed files with 127 additions and 27 deletions

View File

@ -811,7 +811,7 @@ public abstract class BaseFhirDao implements IDao {
} }
} }
protected ResourceTable updateEntity(final IResource theResource, ResourceTable entity, boolean theUpdateHistory, boolean theDelete) { protected ResourceTable updateEntity(final IResource theResource, ResourceTable entity, boolean theUpdateHistory, Date theDeletedTimestampOrNull) {
if (entity.getPublished() == null) { if (entity.getPublished() == null) {
entity.setPublished(new Date()); entity.setPublished(new Date());
} }
@ -843,7 +843,7 @@ public abstract class BaseFhirDao implements IDao {
final List<ResourceIndexedSearchParamQuantity> quantityParams; final List<ResourceIndexedSearchParamQuantity> quantityParams;
final List<ResourceIndexedSearchParamDate> dateParams; final List<ResourceIndexedSearchParamDate> dateParams;
final List<ResourceLink> links; final List<ResourceLink> links;
if (theDelete) { if (theDeletedTimestampOrNull != null) {
stringParams = Collections.emptyList(); stringParams = Collections.emptyList();
tokenParams = Collections.emptyList(); tokenParams = Collections.emptyList();
@ -851,8 +851,8 @@ public abstract class BaseFhirDao implements IDao {
quantityParams = Collections.emptyList(); quantityParams = Collections.emptyList();
dateParams = Collections.emptyList(); dateParams = Collections.emptyList();
links = Collections.emptyList(); links = Collections.emptyList();
entity.setDeleted(new Date()); entity.setDeleted(theDeletedTimestampOrNull);
entity.setUpdated(new Date()); entity.setUpdated(theDeletedTimestampOrNull);
} else { } else {

View File

@ -1,6 +1,7 @@
package ca.uhn.fhir.jpa.dao; package ca.uhn.fhir.jpa.dao;
import static org.apache.commons.lang3.StringUtils.*; import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
@ -68,9 +69,6 @@ import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.base.composite.BaseIdentifierDt; import ca.uhn.fhir.model.base.composite.BaseIdentifierDt;
import ca.uhn.fhir.model.base.composite.BaseQuantityDt; import ca.uhn.fhir.model.base.composite.BaseQuantityDt;
import ca.uhn.fhir.model.dstu.composite.CodingDt;
import ca.uhn.fhir.model.dstu.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu.composite.QuantityDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome; import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum; import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum;
@ -163,7 +161,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
} }
updateEntity(theResource, entity, false, false); updateEntity(theResource, entity, false, null);
MethodOutcome outcome = toMethodOutcome(entity); MethodOutcome outcome = toMethodOutcome(entity);
notifyWriteCompleted(); notifyWriteCompleted();
@ -179,7 +177,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new InvalidRequestException("Trying to update " + theId + " but this is not the current version"); throw new InvalidRequestException("Trying to update " + theId + " but this is not the current version");
} }
ResourceTable savedEntity = updateEntity(null, entity, true, true); ResourceTable savedEntity = updateEntity(null, entity, true, new Date());
notifyWriteCompleted(); notifyWriteCompleted();
@ -490,7 +488,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
// Execute the query and make sure we return distinct results // Execute the query and make sure we return distinct results
List<IResource> retVal = new ArrayList<IResource>(); List<IResource> retVal = new ArrayList<IResource>();
loadResourcesByPid(pidsSubList, retVal); loadResourcesByPid(pidsSubList, retVal, BundleEntryStatusEnum.MATCH);
// Load _include resources // Load _include resources
if (theParams.getIncludes() != null && theParams.getIncludes().isEmpty() == false) { if (theParams.getIncludes() != null && theParams.getIncludes().isEmpty() == false) {
@ -748,7 +746,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
throw new InvalidRequestException("Trying to update " + theId + " but this is not the current version"); throw new InvalidRequestException("Trying to update " + theId + " but this is not the current version");
} }
ResourceTable savedEntity = updateEntity(theResource, entity, true, false); ResourceTable savedEntity = updateEntity(theResource, entity, true, null);
notifyWriteCompleted(); notifyWriteCompleted();
ourLog.info("Processed update on {} in {}ms", theId.getValue(), w.getMillisAndRestart()); ourLog.info("Processed update on {} in {}ms", theId.getValue(), w.getMillisAndRestart());
@ -1423,7 +1421,7 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
createSort(theBuilder, theFrom, theSort.getChain(), theOrders, thePredicates); createSort(theBuilder, theFrom, theSort.getChain(), theOrders, thePredicates);
} }
private void loadResourcesByPid(Collection<Long> theIncludePids, List<IResource> theResourceListToPopulate) { private void loadResourcesByPid(Collection<Long> theIncludePids, List<IResource> theResourceListToPopulate, BundleEntryStatusEnum theBundleEntryStatus) {
if (theIncludePids.isEmpty()) { if (theIncludePids.isEmpty()) {
return; return;
} }
@ -1450,6 +1448,9 @@ public class FhirResourceDao<T extends IResource> extends BaseFhirDao implements
ourLog.warn("Got back unexpected resource PID {}", next.getId()); ourLog.warn("Got back unexpected resource PID {}", next.getId());
continue; continue;
} }
ResourceMetadataKeyEnum.ENTRY_STATUS.put(resource, theBundleEntryStatus);
theResourceListToPopulate.set(index, resource); theResourceListToPopulate.set(index, resource);
} }
} }

View File

@ -3,8 +3,10 @@ package ca.uhn.fhir.jpa.dao;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
@ -20,12 +22,15 @@ import org.springframework.transaction.annotation.Transactional;
import ca.uhn.fhir.jpa.entity.ResourceTable; import ca.uhn.fhir.jpa.entity.ResourceTable;
import ca.uhn.fhir.jpa.util.StopWatch; import ca.uhn.fhir.jpa.util.StopWatch;
import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu.resource.OperationOutcome; import ca.uhn.fhir.model.dstu.resource.OperationOutcome;
import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum; import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum;
import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import ca.uhn.fhir.util.FhirTerser; import ca.uhn.fhir.util.FhirTerser;
@ -41,11 +46,24 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao {
ourLog.info("Beginning transaction with {} resources", theResources.size()); ourLog.info("Beginning transaction with {} resources", theResources.size());
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Set<IdDt> allIds = new HashSet<IdDt>();
for (int i =0; i <theResources.size();i++) { for (int i =0; i <theResources.size();i++) {
IResource res = theResources.get(i); IResource res = theResources.get(i);
if(res.getId().hasIdPart() && !res.getId().hasResourceType()) { if(res.getId().hasIdPart() && !res.getId().hasResourceType()) {
res.setId(new IdDt(toResourceName(res.getClass()), res.getId().getIdPart())); res.setId(new IdDt(toResourceName(res.getClass()), res.getId().getIdPart()));
} }
/*
* Ensure that the bundle doesn't have any duplicates, since this causes all
* kinds of weirdness
*/
if (res.getId().hasResourceType() && res.getId().hasIdPart()) {
IdDt nextId = res.getId().toUnqualifiedVersionless();
if (!allIds.add(nextId)) {
throw new InvalidRequestException("Transaction bundle contains multiple resources with ID: " + nextId);
}
}
} }
FhirTerser terser = getContext().newTerser(); FhirTerser terser = getContext().newTerser();
@ -65,12 +83,6 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao {
String resourceName = toResourceName(nextResource); String resourceName = toResourceName(nextResource);
// IFhirResourceDao<? extends IResource> dao = getDao(nextResource.getClass());
// if (dao == null) {
// throw new InvalidRequestException("This server is not able to handle resources of type: " +
// nextResource.getResourceId().getResourceType());
// }
ResourceTable entity; ResourceTable entity;
if (nextId.isEmpty()) { if (nextId.isEmpty()) {
entity = null; entity = null;
@ -150,7 +162,9 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao {
for (int i = 0; i < theResources.size(); i++) { for (int i = 0; i < theResources.size(); i++) {
IResource resource = theResources.get(i); IResource resource = theResources.get(i);
ResourceTable table = persistedResources.get(i); ResourceTable table = persistedResources.get(i);
updateEntity(resource, table, table.getId() != null, false); InstantDt deletedInstantOrNull = ResourceMetadataKeyEnum.DELETED_AT.get(resource);
Date deletedTimestampOrNull = deletedInstantOrNull != null ? deletedInstantOrNull.getValue() : null;
updateEntity(resource, table, table.getId() != null, deletedTimestampOrNull);
} }
long delay = System.currentTimeMillis() - start; long delay = System.currentTimeMillis() - start;

View File

@ -1,7 +1,16 @@
package ca.uhn.fhir.jpa.dao; package ca.uhn.fhir.jpa.dao;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*; import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -11,13 +20,11 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import ch.qos.logback.core.pattern.color.BlackCompositeConverter;
import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.IResource;
@ -25,16 +32,16 @@ import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum;
import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.dev.composite.IdentifierDt; import ca.uhn.fhir.model.dev.composite.IdentifierDt;
import ca.uhn.fhir.model.dev.composite.QuantityDt; import ca.uhn.fhir.model.dev.composite.QuantityDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dev.resource.Device; import ca.uhn.fhir.model.dev.resource.Device;
import ca.uhn.fhir.model.dev.resource.DiagnosticReport; import ca.uhn.fhir.model.dev.resource.DiagnosticReport;
import ca.uhn.fhir.model.dev.resource.Location; import ca.uhn.fhir.model.dev.resource.Location;
import ca.uhn.fhir.model.dev.resource.Observation; import ca.uhn.fhir.model.dev.resource.Observation;
import ca.uhn.fhir.model.dev.resource.Organization; import ca.uhn.fhir.model.dev.resource.Organization;
import ca.uhn.fhir.model.dev.resource.Patient; import ca.uhn.fhir.model.dev.resource.Patient;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.primitive.InstantDt;
import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.param.TokenParam;
import ca.uhn.fhir.rest.server.IBundleProvider; import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
@ -214,6 +221,67 @@ public class FhirSystemDaoTest {
} }
@Test
public void testTransactionWithDelete() throws Exception {
/*
* Create 3
*/
List<IResource> res;
res = new ArrayList<IResource>();
Patient p1 = new Patient();
p1.addIdentifier().setSystem("urn:system").setValue("testTransactionWithDelete");
res.add(p1);
Patient p2 = new Patient();
p2.addIdentifier().setSystem("urn:system").setValue("testTransactionWithDelete");
res.add(p2);
Patient p3 = new Patient();
p3.addIdentifier().setSystem("urn:system").setValue("testTransactionWithDelete");
res.add(p3);
ourSystemDao.transaction(res);
/*
* Verify
*/
IBundleProvider results = ourPatientDao.search(Patient.SP_IDENTIFIER, new TokenParam("urn:system", "testTransactionWithDelete"));
assertEquals(3, results.size());
/*
* Now delete 2
*/
res = new ArrayList<IResource>();
List<IResource> existing = results.getResources(0, 3);
p1 = new Patient();
p1.setId(existing.get(0).getId());
ResourceMetadataKeyEnum.DELETED_AT.put(p1, InstantDt.withCurrentTime());
res.add(p1);
p2 = new Patient();
p2.setId(existing.get(1).getId());
ResourceMetadataKeyEnum.DELETED_AT.put(p2, InstantDt.withCurrentTime());
res.add(p2);
ourSystemDao.transaction(res);
/*
* Verify
*/
IBundleProvider results2 = ourPatientDao.search(Patient.SP_IDENTIFIER, new TokenParam("urn:system", "testTransactionWithDelete"));
assertEquals(1, results2.size());
List<IResource> existing2 = results2.getResources(0, 1);
assertEquals(existing2.get(0).getId(), existing.get(2).getId());
}
/** /**
* Issue #55 * Issue #55
*/ */
@ -233,7 +301,7 @@ public class FhirSystemDaoTest {
res.add(o1); res.add(o1);
Observation o2 = new Observation(); Observation o2 = new Observation();
o2.setId("cid:observation1"); o2.setId("cid:observation2");
o2.getIdentifier().setSystem("system").setValue("testTransactionWithCidIds03"); o2.getIdentifier().setSystem("system").setValue("testTransactionWithCidIds03");
o2.setSubject(new ResourceReferenceDt("Patient/cid:patient1")); o2.setSubject(new ResourceReferenceDt("Patient/cid:patient1"));
res.add(o2); res.add(o2);
@ -321,6 +389,19 @@ public class FhirSystemDaoTest {
} }
@Test(expected=InvalidRequestException.class)
public void testTransactionFailsWithDuplicateIds() {
Patient patient1 = new Patient();
patient1.setId(new IdDt("Patient/testTransactionFailsWithDusplicateIds"));
patient1.addIdentifier().setSystem("urn:system").setValue( "testPersistWithSimpleLinkP01");
Patient patient2 = new Patient();
patient2.setId(new IdDt("Patient/testTransactionFailsWithDusplicateIds"));
patient2.addIdentifier().setSystem("urn:system").setValue( "testPersistWithSimpleLinkP02");
ourSystemDao.transaction(Arrays.asList((IResource) patient1, patient2));
}
@Test @Test
public void testGetResourceCounts() { public void testGetResourceCounts() {
Observation obs = new Observation(); Observation obs = new Observation();

View File

@ -396,6 +396,7 @@ public class CompleteResourceProviderTest {
Bundle actual = ourClient.search().forResource(Patient.class).where(Patient.IDENTIFIER.exactly().systemAndCode("urn:system", "testSearchByIdentifier01")).encodedJson().prettyPrint().execute(); Bundle actual = ourClient.search().forResource(Patient.class).where(Patient.IDENTIFIER.exactly().systemAndCode("urn:system", "testSearchByIdentifier01")).encodedJson().prettyPrint().execute();
assertEquals(1, actual.size()); assertEquals(1, actual.size());
assertEquals(p1Id.getIdPart(), actual.getEntries().get(0).getResource().getId().getIdPart()); assertEquals(p1Id.getIdPart(), actual.getEntries().get(0).getResource().getId().getIdPart());
assertEquals(BundleEntryStatusEnum.MATCH, actual.getEntries().get(0).getStatus().getValueAsEnum());
} }
@Test @Test

View File

@ -89,6 +89,9 @@
appropriate configuration on the appropriate configuration on the
RestfulClientConfig. Thanks to Grahame Grieve for the suggestion! RestfulClientConfig. Thanks to Grahame Grieve for the suggestion!
</action> </action>
<action type="add">
JPA module now supports deleting resource via transaction
</action>
</release> </release>
<release version="0.8" date="2014-Dec-17"> <release version="0.8" date="2014-Dec-17">
<action type="add"> <action type="add">