diff --git a/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java b/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java index 436e3c823b..aafb1c1f88 100644 --- a/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java +++ b/api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java @@ -93,9 +93,11 @@ private FhirConstants() { public static final String OPENMRS_FHIR_EXT_NON_CODED_CONDITION = OPENMRS_FHIR_EXT_PREFIX + "/non-coded-condition"; public static final String OPENMRS_FHIR_EXT_MEDICINE = OPENMRS_FHIR_EXT_PREFIX + "/medicine"; - + public static final String OPENMRS_FHIR_EXT_TASK_IDENTIFIER = OPENMRS_FHIR_EXT_PREFIX + "/task/identifier"; - + + public static final String OPENMRS_FHIR_EXT_BATCH_IDENTIFIER = OPENMRS_FHIR_EXT_PREFIX + "/bundle/identifier"; + public static final String OPENMRS_FHIR_EXT_USER_IDENTIFIER = OPENMRS_FHIR_EXT_PREFIX + "/user/identifier"; public static final String OPENMRS_FHIR_EXT_PROVIDER_IDENTIFIER = OPENMRS_FHIR_EXT_PREFIX + "/provider/identifier"; diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/FhirBatchService.java b/api/src/main/java/org/openmrs/module/fhir2/api/FhirBatchService.java new file mode 100644 index 0000000000..1a8c98de2a --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/FhirBatchService.java @@ -0,0 +1,27 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api; + +import ca.uhn.fhir.model.api.Include; +import ca.uhn.fhir.rest.annotation.Sort; +import ca.uhn.fhir.rest.api.SortSpec; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.rest.param.QuantityAndListParam; +import ca.uhn.fhir.rest.param.ReferenceAndListParam; +import ca.uhn.fhir.rest.param.TokenAndListParam; +import org.hl7.fhir.r4.model.Bundle; + +import java.util.HashSet; + +public interface FhirBatchService extends FhirService { + + IBundleProvider searchBatches(TokenAndListParam identifier, TokenAndListParam batchType); +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirBatchDao.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirBatchDao.java new file mode 100644 index 0000000000..1474c5b8de --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/FhirBatchDao.java @@ -0,0 +1,25 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api.dao; + +import org.openmrs.BaseOpenmrsData; +import org.openmrs.annotation.Authorized; +import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; +import org.openmrs.module.fhir2.model.FhirBatch; +import org.openmrs.module.fhir2.model.FhirDiagnosticReport; +import org.openmrs.util.PrivilegeConstants; + +import javax.annotation.Nonnull; +import java.util.Collection; +import java.util.List; + +public interface FhirBatchDao extends FhirDao { + +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirBatchDaoImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirBatchDaoImpl.java new file mode 100644 index 0000000000..d51a09e1fb --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/dao/impl/FhirBatchDaoImpl.java @@ -0,0 +1,43 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api.dao.impl; + +import ca.uhn.fhir.rest.param.TokenAndListParam; +import org.hibernate.Criteria; +import org.openmrs.module.fhir2.FhirConstants; +import org.openmrs.module.fhir2.api.dao.FhirBatchDao; +import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; +import org.openmrs.module.fhir2.model.FhirBatch; + +import java.util.Optional; + +import static org.hibernate.criterion.Restrictions.eq; + +public class FhirBatchDaoImpl extends BaseFhirDao implements FhirBatchDao { + + + @Override + protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams) { + theParams.getParameters().forEach(entry -> { + switch (entry.getKey()) { + case FhirConstants.OPENMRS_FHIR_EXT_BATCH_IDENTIFIER: + handleCommonSearchParameters(entry.getValue()).ifPresent(criteria::add); + break; + case FhirConstants.ENCOUNTER_TYPE_REFERENCE_SEARCH_HANDLER: + entry.getValue() + .forEach(param -> handleAndListParam((TokenAndListParam) param.getParam(), + t -> Optional.of(eq("et.uuid", t.getValue()))) + .ifPresent(t -> criteria.createAlias("encounterType", "et").add(t))); + break; + } + }); + } + +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirBatchServiceImpl.java b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirBatchServiceImpl.java new file mode 100644 index 0000000000..b399b25216 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/api/impl/FhirBatchServiceImpl.java @@ -0,0 +1,51 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api.impl; + +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.TokenAndListParam; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.hl7.fhir.r4.model.Bundle; +import org.openmrs.BaseOpenmrsData; +import org.openmrs.module.fhir2.FhirConstants; +import org.openmrs.module.fhir2.api.FhirBatchService; +import org.openmrs.module.fhir2.api.dao.FhirDao; +import org.openmrs.module.fhir2.api.search.param.SearchParameterMap; +import org.openmrs.module.fhir2.api.translators.OpenmrsFhirTranslator; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@Transactional +@Setter(AccessLevel.PACKAGE) +@Getter(AccessLevel.PROTECTED) +public class FhirBatchServiceImpl extends BaseFhirService implements FhirBatchService { + + @Override + public IBundleProvider searchBatches(TokenAndListParam identifier, TokenAndListParam batchType) { + SearchParameterMap theParams = new SearchParameterMap() + .addParameter(FhirConstants.OPENMRS_FHIR_EXT_BATCH_IDENTIFIER, identifier) + .addParameter(FhirConstants.ENCOUNTER_TYPE_REFERENCE_SEARCH_HANDLER, batchType); + + return null; + } + + @Override + protected FhirDao getDao() { + return null; + } + + @Override + protected OpenmrsFhirTranslator getTranslator() { + return null; + } +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/model/FhirBatch.java b/api/src/main/java/org/openmrs/module/fhir2/model/FhirBatch.java new file mode 100644 index 0000000000..698c430523 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/model/FhirBatch.java @@ -0,0 +1,40 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.model; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.openmrs.BaseOpenmrsData; +import org.openmrs.BaseOpenmrsMetadata; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +@Data +@NoArgsConstructor +@EqualsAndHashCode(onlyExplicitlyIncluded = true, callSuper = false) +@Entity +@Table(name = "fhir_batch") +public class FhirBatch extends BaseOpenmrsMetadata { + + private static final long serialVersionUID = 1L; + + @EqualsAndHashCode.Include + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "diagnostic_report_id") + private Integer id; + +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r3/BatchFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/BatchFhirResourceProvider.java new file mode 100644 index 0000000000..508e183a3d --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r3/BatchFhirResourceProvider.java @@ -0,0 +1,104 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.providers.r3; + +import static lombok.AccessLevel.PACKAGE; + +import ca.uhn.fhir.rest.annotation.Create; +import ca.uhn.fhir.rest.annotation.Delete; +import ca.uhn.fhir.rest.annotation.IdParam; +import ca.uhn.fhir.rest.annotation.OperationParam; +import ca.uhn.fhir.rest.annotation.OptionalParam; +import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.annotation.ResourceParam; +import ca.uhn.fhir.rest.annotation.Search; +import ca.uhn.fhir.rest.annotation.Update; +import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.TokenAndListParam; +import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import lombok.Setter; +import org.hl7.fhir.convertors.conv30_40.Bundle30_40; +import org.hl7.fhir.convertors.conv30_40.DiagnosticReport30_40; +import org.hl7.fhir.convertors.conv30_40.Observation30_40; +import org.hl7.fhir.dstu3.model.DiagnosticReport; +import org.hl7.fhir.dstu3.model.IdType; +import org.hl7.fhir.dstu3.model.Observation; +import org.hl7.fhir.dstu3.model.OperationOutcome; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.dstu3.model.Bundle; +import org.openmrs.module.fhir2.api.FhirBatchService; +import org.openmrs.module.fhir2.api.annotations.R3Provider; +import org.openmrs.module.fhir2.api.search.SearchQueryBundleProviderR3Wrapper; +import org.openmrs.module.fhir2.providers.util.FhirProviderUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.Nonnull; + +@Component("batchFhirResourceProvider") +@R3Provider +@Setter(PACKAGE) +public class BatchFhirResourceProvider implements IResourceProvider { + + @Autowired + FhirBatchService batchService; + + @Override + public Class getResourceType() {return Bundle.class;} + + @Read + @SuppressWarnings("unused") + public Bundle getBatchById(@IdParam @Nonnull IdType id) { + org.hl7.fhir.r4.model.Bundle batch = batchService.get(id.getIdPart()); + if (batch == null) { + throw new ResourceNotFoundException("Could not find observation with Id " + id.getIdPart()); + } + + return Bundle30_40.convertBundle(batch); + } + + @Create + @SuppressWarnings("unused") + public MethodOutcome createBatch(@ResourceParam Bundle bundle) { + return FhirProviderUtils.buildCreate(Bundle30_40.convertBundle(batchService.create(Bundle30_40.convertBundle(bundle)))); + } + + @Update + @SuppressWarnings("unused") + public MethodOutcome updateBatch(@IdParam IdType id, @ResourceParam Bundle bundle) { + String idPart = null; + + if (id != null) { + idPart = id.getIdPart(); + } + + return FhirProviderUtils.buildUpdate(Bundle30_40.convertBundle(batchService.update(idPart, Bundle30_40.convertBundle(bundle)))); + } + + @Delete + @SuppressWarnings("unused") + public OperationOutcome deleteBatch(@IdParam @Nonnull IdType id) { + org.hl7.fhir.r4.model.Bundle batch = batchService.delete(id.getIdPart()); + if (batch == null) { + throw new ResourceNotFoundException("Could not find medication to delete with id " + id.getIdPart()); + } + + return FhirProviderUtils.buildDelete(Bundle30_40.convertBundle(batch)); + } + + @Search + public IBundleProvider searchForBatch( + @OptionalParam(name = Bundle.SP_IDENTIFIER) TokenAndListParam identifier, + @OptionalParam(name = Bundle.SP_TYPE) TokenAndListParam batchType) { + return new SearchQueryBundleProviderR3Wrapper(batchService.searchBatches(identifier, batchType)); + } +} diff --git a/api/src/main/java/org/openmrs/module/fhir2/providers/r4/BatchFhirResourceProvider.java b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/BatchFhirResourceProvider.java new file mode 100644 index 0000000000..4f23b6abe1 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/fhir2/providers/r4/BatchFhirResourceProvider.java @@ -0,0 +1,101 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.providers.r4; + +import ca.uhn.fhir.model.api.Include; +import ca.uhn.fhir.rest.annotation.Create; +import ca.uhn.fhir.rest.annotation.Delete; +import ca.uhn.fhir.rest.annotation.IdParam; +import ca.uhn.fhir.rest.annotation.IncludeParam; +import ca.uhn.fhir.rest.annotation.OptionalParam; +import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.annotation.ResourceParam; +import ca.uhn.fhir.rest.annotation.Search; +import ca.uhn.fhir.rest.annotation.Sort; +import ca.uhn.fhir.rest.annotation.Update; +import ca.uhn.fhir.rest.api.MethodOutcome; +import ca.uhn.fhir.rest.api.SortSpec; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.rest.param.ReferenceAndListParam; +import ca.uhn.fhir.rest.param.TokenAndListParam; +import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; +import org.apache.commons.collections.CollectionUtils; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Bundle; +import org.hl7.fhir.r4.model.DiagnosticReport; +import org.hl7.fhir.r4.model.Encounter; +import org.hl7.fhir.r4.model.IdType; +import org.hl7.fhir.r4.model.OperationOutcome; +import org.hl7.fhir.r4.model.Patient; +import org.openmrs.module.fhir2.api.FhirBatchService; +import org.openmrs.module.fhir2.api.FhirDiagnosticReportService; +import org.openmrs.module.fhir2.model.FhirBatch; +import org.openmrs.module.fhir2.providers.util.FhirProviderUtils; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.annotation.Nonnull; +import java.util.HashSet; + +public class BatchFhirResourceProvider implements IResourceProvider { + + @Autowired + private FhirBatchService service; + + @Override + public Class getResourceType() { + return Bundle.class; + } + + @Read + public Bundle getBatchById(@IdParam @Nonnull IdType id) { + Bundle bundle = service.get(id.getIdPart()); + + if (bundle == null) { + throw new ResourceNotFoundException("Could not find Diagnostic Report with Id " + id.getIdPart()); + } + + return bundle; + } + + @Create + public MethodOutcome createBatch(@ResourceParam Bundle bundle) { + return FhirProviderUtils.buildCreate(service.create(bundle)); + } + + @Update + public MethodOutcome updateBatch(@IdParam IdType id, @ResourceParam Bundle bundle) { + if (id == null || id.getIdPart() == null) { + throw new InvalidRequestException("id must be specified to update"); + } + + return FhirProviderUtils.buildUpdate(service.update(id.getIdPart(), bundle)); + } + + @Delete + @SuppressWarnings("unused") + public OperationOutcome deleteBatch(@IdParam @Nonnull IdType id) { + Bundle bundle = service.delete(id.getIdPart()); + if (bundle == null) { + throw new ResourceNotFoundException("Could not find medication to delete with id " + id.getIdPart()); + } + return FhirProviderUtils.buildDelete(bundle); + } + + @Search + public IBundleProvider searchForBatch( + @OptionalParam(name = org.hl7.fhir.dstu3.model.Bundle.SP_IDENTIFIER) TokenAndListParam identifier, + @OptionalParam(name = org.hl7.fhir.dstu3.model.Bundle.SP_TYPE) TokenAndListParam batchType) { + + return service.searchBatches(identifier, batchType); + } +} diff --git a/api/src/test/java/org/openmrs/module/fhir2/api/FhirBatchService.java b/api/src/test/java/org/openmrs/module/fhir2/api/FhirBatchService.java new file mode 100644 index 0000000000..5361686080 --- /dev/null +++ b/api/src/test/java/org/openmrs/module/fhir2/api/FhirBatchService.java @@ -0,0 +1,28 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ +package org.openmrs.module.fhir2.api; + +import java.util.HashSet; + +import ca.uhn.fhir.model.api.Include; +import ca.uhn.fhir.rest.annotation.Sort; +import ca.uhn.fhir.rest.api.SortSpec; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.DateRangeParam; +import ca.uhn.fhir.rest.param.QuantityAndListParam; +import ca.uhn.fhir.rest.param.TokenAndListParam; +import org.hl7.fhir.r4.model.Bundle; + +public interface FhirBatchService extends FhirService { + + IBundleProvider searchBatches(TokenAndListParam clinicalStatus, DateRangeParam onsetDate, QuantityAndListParam onsetAge, + DateRangeParam recordedDate, TokenAndListParam id, DateRangeParam lastUpdated, @Sort SortSpec sort, + HashSet includes); +}