From 1c55d87b103778321766562bd43f44c64a1ad5e1 Mon Sep 17 00:00:00 2001 From: ianmarshall Date: Sun, 30 Aug 2020 17:11:48 -0400 Subject: [PATCH] Problem found in LOINC loader. --- .../ca/uhn/fhir/i18n/hapi-messages.properties | 1 + .../dao/data/ITermCodeSystemVersionDao.java | 2 +- .../term/TermCodeSystemStorageSvcImpl.java | 12 ++- ...rminologyLoaderSvcLoincIntegratedTest.java | 79 +++++++++++++++++++ 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologyLoaderSvcLoincIntegratedTest.java diff --git a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties index c8b2f25d067..b263903392d 100644 --- a/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties +++ b/hapi-fhir-base/src/main/resources/ca/uhn/fhir/i18n/hapi-messages.properties @@ -142,6 +142,7 @@ ca.uhn.fhir.jpa.binstore.BinaryAccessProvider.unknownType=Content in resource of ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.cannotCreateDuplicateCodeSystemUrl=Can not create multiple CodeSystem resources with CodeSystem.url "{0}", already have one with resource ID: {1} ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.cannotCreateDuplicateCodeSystemUrlAndVersion=Can not create multiple CodeSystem resources with CodeSystem.url "{0}" and CodeSystem.version "{1}", already have one with resource ID: {2} +ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.cannotUpdateVersionForExistingCodeSystemVersion=Updated CodeSystem has wrong version id. Existing CodeSystem resource has CodeSystem.url "{0}" and CodeSystem.version "{1}", already have one with resource ID: {2} ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.cannotCreateDuplicateConceptMapUrl=Can not create multiple ConceptMap resources with ConceptMap.url "{0}", already have one with resource ID: {1} ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.cannotCreateDuplicateValueSetUrl=Can not create multiple ValueSet resources with ValueSet.url "{0}", already have one with resource ID: {1} ca.uhn.fhir.jpa.term.BaseTermReadSvcImpl.expansionTooLarge=Expansion of ValueSet produced too many codes (maximum {0}) - Operation aborted! diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/ITermCodeSystemVersionDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/ITermCodeSystemVersionDao.java index fc55636836f..8b56e223d30 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/ITermCodeSystemVersionDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/data/ITermCodeSystemVersionDao.java @@ -49,7 +49,7 @@ public interface ITermCodeSystemVersionDao extends JpaRepository findByCodeSystemResourcePid(@Param("resource_id") Long theCodeSystemResourcePid); + TermCodeSystemVersion findByCodeSystemResourcePid(@Param("resource_id") Long theCodeSystemResourcePid); @Query("SELECT cs FROM TermCodeSystemVersion cs WHERE cs.myCodeSystemHavingThisVersionAsCurrentVersionIfAny.myResource.myId = :resource_id") TermCodeSystemVersion findCurrentVersionForCodeSystemResourcePid(@Param("resource_id") Long theCodeSystemResourcePid); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java index 598c5eccdd4..b37f98235b9 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/term/TermCodeSystemStorageSvcImpl.java @@ -715,14 +715,18 @@ public class TermCodeSystemStorageSvcImpl implements ITermCodeSystemStorageSvc { if (theSystemVersionId == null) { codeSystemVersionEntity = myCodeSystemVersionDao.findByCodeSystemPidVersionIsNull(theCodeSystem.getPid()); if (codeSystemVersionEntity != null) { - msg = myContext.getLocalizer().getMessage(BaseTermReadSvcImpl.class, "cannotCreateDuplicateCodeSystemUrl", theSystemUri, - codeSystemVersionEntity.getResource().getIdDt().toUnqualifiedVersionless().getValue()); + msg = myContext.getLocalizer().getMessage(BaseTermReadSvcImpl.class, "cannotCreateDuplicateCodeSystemUrl", theSystemUri, codeSystemVersionEntity.getResource().getIdDt().toUnqualifiedVersionless().getValue()); } } else { codeSystemVersionEntity = myCodeSystemVersionDao.findByCodeSystemPidAndVersion(theCodeSystem.getPid(), theSystemVersionId); if (codeSystemVersionEntity != null) { - msg = myContext.getLocalizer().getMessage(BaseTermReadSvcImpl.class, "cannotCreateDuplicateCodeSystemUrlAndVersion", theSystemUri, - theSystemVersionId, codeSystemVersionEntity.getResource().getIdDt().toUnqualifiedVersionless().getValue()); + msg = myContext.getLocalizer().getMessage(BaseTermReadSvcImpl.class, "cannotCreateDuplicateCodeSystemUrlAndVersion", theSystemUri, theSystemVersionId, codeSystemVersionEntity.getResource().getIdDt().toUnqualifiedVersionless().getValue()); + } else { + codeSystemVersionEntity = myCodeSystemVersionDao.findByCodeSystemResourcePid(theCodeSystemResourceTable.getId()); + if (codeSystemVersionEntity != null) { + msg = myContext.getLocalizer().getMessage(BaseTermReadSvcImpl.class, "cannotCreateDuplicateCodeSystemUrlAndVersion", theSystemUri, theSystemVersionId, codeSystemVersionEntity.getResource().getIdDt().toUnqualifiedVersionless().getValue()); + throw new UnprocessableEntityException(msg); + } } } // Throw exception if the CodeSystemVersion is being duplicated. diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologyLoaderSvcLoincIntegratedTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologyLoaderSvcLoincIntegratedTest.java new file mode 100644 index 00000000000..5543102a7dc --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/term/TerminologyLoaderSvcLoincIntegratedTest.java @@ -0,0 +1,79 @@ +package ca.uhn.fhir.jpa.term; + +import ca.uhn.fhir.jpa.dao.r4.BaseJpaR4Test; +import ca.uhn.fhir.jpa.entity.TermCodeSystem; +import ca.uhn.fhir.jpa.entity.TermCodeSystemVersion; +import org.eclipse.jetty.http.HttpStatus; +import org.hl7.fhir.r4.model.CodeSystem; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TerminologyLoaderSvcLoincIntegratedTest extends BaseJpaR4Test { + private TermLoaderSvcImpl mySvc; + + private ZipCollectionBuilder myFiles; + + @BeforeEach + public void before() { + mySvc = new TermLoaderSvcImpl(); + mySvc.setTermCodeSystemStorageSvcForUnitTests(myTermCodeSystemStorageSvc); + mySvc.setTermDeferredStorageSvc(myTerminologyDeferredStorageSvc); + + myFiles = new ZipCollectionBuilder(); + } + + @Test + public void testLoadLoincMultipleVersions() throws IOException { + + // Load LOINC marked as version 2.67 + TerminologyLoaderSvcLoincTest.addLoincMandatoryFilesWithPropertiesFileToZip(myFiles, "v267_loincupload.properties"); + mySvc.loadLoinc(myFiles.getFiles(), mySrd); + + runInTransaction(() -> { + assertEquals(1, myTermCodeSystemDao.count()); + assertEquals(1, myTermCodeSystemVersionDao.count()); + assertEquals(1, myResourceTableDao.count()); + TermCodeSystem myTermCodeSystem = myTermCodeSystemDao.findByCodeSystemUri("http://loinc.org"); + + TermCodeSystemVersion myTermCodeSystemVersion = myTermCodeSystemVersionDao.findByCodeSystemPidAndVersion(myTermCodeSystem.getPid(), "2.67"); + assertEquals(myTermCodeSystem.getCurrentVersion().getPid(), myTermCodeSystemVersion.getPid()); + assertEquals(myTermCodeSystem.getResource().getId(), myTermCodeSystemVersion.getResource().getId()); + }); + + // Update LOINC marked as version 2.67 + myFiles = new ZipCollectionBuilder(); + TerminologyLoaderSvcLoincTest.addLoincMandatoryFilesWithPropertiesFileToZip(myFiles, "v267_loincupload.properties"); + mySvc.loadLoinc(myFiles.getFiles(), mySrd); + + runInTransaction(() -> { + assertEquals(1, myTermCodeSystemDao.count()); + assertEquals(1, myTermCodeSystemVersionDao.count()); + TermCodeSystem myTermCodeSystem = myTermCodeSystemDao.findByCodeSystemUri("http://loinc.org"); + + TermCodeSystemVersion myTermCodeSystemVersion = myTermCodeSystemVersionDao.findByCodeSystemPidAndVersion(myTermCodeSystem.getPid(), "2.67"); + assertEquals(myTermCodeSystem.getCurrentVersion().getPid(), myTermCodeSystemVersion.getPid()); + assertEquals(myTermCodeSystem.getResource().getId(), myTermCodeSystemVersion.getResource().getId()); + }); + + + // Load LOINC marked as version 2.68 + myFiles = new ZipCollectionBuilder(); + TerminologyLoaderSvcLoincTest.addLoincMandatoryFilesWithPropertiesFileToZip(myFiles, "v268_loincupload.properties"); + mySvc.loadLoinc(myFiles.getFiles(), mySrd); + + runInTransaction(() -> { + assertEquals(1, myTermCodeSystemDao.count()); + assertEquals(2, myTermCodeSystemVersionDao.count()); + TermCodeSystem myTermCodeSystem = myTermCodeSystemDao.findByCodeSystemUri("http://loinc.org"); + + TermCodeSystemVersion mySecondTermCodeSystemVersion = myTermCodeSystemVersionDao.findByCodeSystemPidAndVersion(myTermCodeSystem.getPid(), "2.68"); + assertEquals(myTermCodeSystem.getCurrentVersion().getPid(), mySecondTermCodeSystemVersion.getPid()); + assertEquals(myTermCodeSystem.getResource().getId(), mySecondTermCodeSystemVersion.getResource().getId()); + }); + + } + +}