diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoObservationDstu3.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoObservationDstu3.java index 4bc9501e025..bae0d8e0be0 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoObservationDstu3.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/dstu3/FhirResourceDaoObservationDstu3.java @@ -21,7 +21,6 @@ package ca.uhn.fhir.jpa.dao.dstu3; */ import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDaoObservation; -import ca.uhn.fhir.jpa.dao.ObservationLastNIndexPersistSvc; import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource; import ca.uhn.fhir.jpa.model.entity.ResourceTable; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; @@ -32,16 +31,12 @@ import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.storage.TransactionDetails; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.dstu3.model.Observation; -import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.http.HttpServletResponse; import java.util.Date; public class FhirResourceDaoObservationDstu3 extends BaseHapiFhirResourceDaoObservation { - @Autowired - ObservationLastNIndexPersistSvc myObservationLastNIndexPersistSvc; - @Override public IBundleProvider observationsLastN(SearchParameterMap theSearchParameterMap, RequestDetails theRequestDetails, HttpServletResponse theServletResponse) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoObservationR4.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoObservationR4.java index e1694b6ef54..ff88863db20 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoObservationR4.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r4/FhirResourceDaoObservationR4.java @@ -21,7 +21,6 @@ package ca.uhn.fhir.jpa.dao.r4; */ import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDaoObservation; -import ca.uhn.fhir.jpa.dao.ObservationLastNIndexPersistSvc; import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource; import ca.uhn.fhir.jpa.model.entity.ResourceTable; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; @@ -32,7 +31,6 @@ import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.storage.TransactionDetails; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r4.model.Observation; -import org.springframework.beans.factory.annotation.Autowired; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -46,9 +44,6 @@ public class FhirResourceDaoObservationR4 extends BaseHapiFhirResourceDaoObserva @PersistenceContext(type = PersistenceContextType.TRANSACTION) protected EntityManager myEntityManager; - @Autowired - ObservationLastNIndexPersistSvc myObservationLastNIndexPersistSvc; - @Override public IBundleProvider observationsLastN(SearchParameterMap theSearchParameterMap, RequestDetails theRequestDetails, HttpServletResponse theServletResponse) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoObservationR5.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoObservationR5.java index b7d03064363..e21682f63d1 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoObservationR5.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/r5/FhirResourceDaoObservationR5.java @@ -21,7 +21,6 @@ package ca.uhn.fhir.jpa.dao.r5; */ import ca.uhn.fhir.jpa.dao.BaseHapiFhirResourceDaoObservation; -import ca.uhn.fhir.jpa.dao.ObservationLastNIndexPersistSvc; import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource; import ca.uhn.fhir.jpa.model.entity.ResourceTable; import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; @@ -32,16 +31,12 @@ import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.storage.TransactionDetails; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.Observation; -import org.springframework.beans.factory.annotation.Autowired; import javax.servlet.http.HttpServletResponse; import java.util.Date; public class FhirResourceDaoObservationR5 extends BaseHapiFhirResourceDaoObservation { - @Autowired - ObservationLastNIndexPersistSvc myObservationLastNIndexPersistSvc; - @Override public IBundleProvider observationsLastN(SearchParameterMap theSearchParameterMap, RequestDetails theRequestDetails, HttpServletResponse theServletResponse) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermDeferredStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermDeferredStorageSvcImpl.java index b4c1a5d86d1..b49bf915318 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermDeferredStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermDeferredStorageSvcImpl.java @@ -22,6 +22,7 @@ package ca.uhn.fhir.jpa.term; import ca.uhn.fhir.jpa.api.config.DaoConfig; import ca.uhn.fhir.jpa.dao.data.ITermCodeSystemDao; +import ca.uhn.fhir.jpa.dao.data.ITermCodeSystemVersionDao; import ca.uhn.fhir.jpa.dao.data.ITermConceptDao; import ca.uhn.fhir.jpa.dao.data.ITermConceptParentChildLinkDao; import ca.uhn.fhir.jpa.entity.TermCodeSystem; @@ -62,7 +63,8 @@ public class TermDeferredStorageSvcImpl implements ITermDeferredStorageSvc { @Autowired protected ITermCodeSystemDao myCodeSystemDao; @Autowired - protected PlatformTransactionManager myTransactionMgr; + protected ITermCodeSystemVersionDao myCodeSystemVersionDao; + @Autowired protected PlatformTransactionManager myTransactionMgr; private boolean myProcessDeferred = true; private List myDefferedCodeSystemsDeletions = Collections.synchronizedList(new ArrayList<>()); private List myDeferredConcepts = Collections.synchronizedList(new ArrayList<>()); @@ -142,7 +144,12 @@ public class TermDeferredStorageSvcImpl implements ITermDeferredStorageSvc { ourLog.info("Saving {} deferred concepts...", count); while (codeCount < count && myDeferredConcepts.size() > 0) { TermConcept next = myDeferredConcepts.remove(0); - codeCount += myCodeSystemStorageSvc.saveConcept(next); + if(myCodeSystemVersionDao.findById(next.getCodeSystemVersion().getPid()).isPresent()) { + codeCount += myCodeSystemStorageSvc.saveConcept(next); + } else { + ourLog.warn("Unable to save deferred TermConcept {} because Code System {} version PID {} is no longer valid. Code system may have since been updated.", + next.getCode(), next.getCodeSystemVersion().getCodeSystemDisplayName(), next.getCodeSystemVersion().getPid()); + } } if (codeCount > 0) { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/PersistObservationIndexedSearchParamLastNR4IT.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/PersistObservationIndexedSearchParamLastNR4IT.java index ab827d39d59..5b7c6606fd4 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/PersistObservationIndexedSearchParamLastNR4IT.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/r4/PersistObservationIndexedSearchParamLastNR4IT.java @@ -203,9 +203,6 @@ public class PersistObservationIndexedSearchParamLastNR4IT { List observationDocuments = elasticsearchSvc.executeLastNWithAllFieldsForTest(searchParameterMap, myFhirCtx); assertEquals(100, observationDocuments.size()); - //List codeDocuments = elasticsearchSvc.queryAllIndexedObservationCodesForTest(); - //assertEquals(2, codeDocuments.size()); - // Check that all observations were indexed. searchParameterMap = new SearchParameterMap(); searchParameterMap.add(Observation.SP_SUBJECT, multiSubjectParams); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcTest.java new file mode 100644 index 00000000000..c4c91051307 --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcTest.java @@ -0,0 +1,72 @@ +package ca.uhn.fhir.jpa.term; + +import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; +import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; +import ca.uhn.fhir.jpa.entity.TermConcept; +import ca.uhn.fhir.jpa.entity.TermConceptParentChildLink; +import ca.uhn.fhir.jpa.model.entity.ResourceTable; +import ca.uhn.fhir.rest.api.server.storage.ResourcePersistentId; +import org.hl7.fhir.instance.model.api.IIdType; +import org.hl7.fhir.r4.model.CodeSystem; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class TermCodeSystemStorageSvcTest extends BaseJpaR4Test { + + public static final String URL_MY_CODE_SYSTEM = "http://example.com/my_code_system"; + + private void createCodeSystemWithMoreThan100Concepts() { + CodeSystem codeSystem = new CodeSystem(); + codeSystem.setUrl(URL_MY_CODE_SYSTEM); + codeSystem.setContent(CodeSystem.CodeSystemContentMode.NOTPRESENT); + + + + IIdType id = myCodeSystemDao.create(codeSystem, mySrd).getId().toUnqualified(); + + ResourceTable table = myResourceTableDao.findById(id.getIdPartAsLong()).orElseThrow(IllegalStateException::new); + + TermCodeSystemVersion cs = new TermCodeSystemVersion(); + cs.setResource(table); + + TermConcept parentA = new TermConcept(cs, "codeA").setDisplay("CodeA"); + cs.getConcepts().add(parentA); + + for (int i = 0; i < 450; i++) { + TermConcept childI = new TermConcept(cs, "subCodeA" + i).setDisplay("Sub-code A" + i); + parentA.addChild(childI, TermConceptParentChildLink.RelationshipTypeEnum.ISA); + } + + TermConcept parentB = new TermConcept(cs, "codeB").setDisplay("CodeB"); + cs.getConcepts().add(parentB); + + for (int i = 0; i < 450; i++) { + TermConcept childI = new TermConcept(cs, "subCodeB" + i).setDisplay("Sub-code B" + i); + parentB.addChild(childI, TermConceptParentChildLink.RelationshipTypeEnum.ISA); + } + + myTermCodeSystemStorageSvc.storeNewCodeSystemVersion(new ResourcePersistentId(table.getId()), URL_MY_CODE_SYSTEM, "SYSTEM NAME", "SYSTEM VERSION", cs, table); + + } + + @Test + public void testStoreNewCodeSystemVersionForExistingCodeSystem() throws IOException { + CodeSystem upload = loadResourceFromClasspath(CodeSystem.class, "/bundles-009_test.json"); + ResourceTable codeSystemResourceEntity = (ResourceTable)myCodeSystemDao.create(upload, mySrd).getEntity(); + + runInTransaction(() -> { + myTermCodeSystemStorageSvc.storeNewCodeSystemVersionIfNeeded(upload, codeSystemResourceEntity); + }); + + myTerminologyDeferredStorageSvc.setProcessDeferred(true); + myTerminologyDeferredStorageSvc.saveDeferred(); + myTerminologyDeferredStorageSvc.saveDeferred(); + + assertEquals(124, myTermConceptDao.count()); + } + + +}