Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP_FM2-428: Add support for Batch Bundles #381

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions api/src/main/java/org/openmrs/module/fhir2/FhirConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Bundle> {

IBundleProvider searchBatches(TokenAndListParam identifier, TokenAndListParam batchType);
}
Original file line number Diff line number Diff line change
@@ -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<FhirBatch> {

}
Original file line number Diff line number Diff line change
@@ -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<FhirBatch> 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("bh.uuid", t.getValue()))));

break;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.FhirBatchService;
import org.openmrs.module.fhir2.api.dao.FhirBatchDao;
import org.openmrs.module.fhir2.api.search.SearchQuery;
import org.openmrs.module.fhir2.api.search.SearchQueryInclude;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.fhir2.api.translators.BatchTranslator;
import org.openmrs.module.fhir2.model.FhirBatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PROTECTED)
public class FhirBatchServiceImpl extends BaseFhirService<Bundle, FhirBatch> implements FhirBatchService {

@Autowired
private FhirBatchDao dao;

@Autowired
private SearchQueryInclude<Bundle> searchQueryInclude;

@Autowired
private BatchTranslator translator;

@Autowired
private SearchQuery<FhirBatch, Bundle, FhirBatchDao, BatchTranslator, SearchQueryInclude<Bundle>> searchQuery;


@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 searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.openmrs.module.fhir2.api.translators;

import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.DiagnosticReport;
import org.openmrs.Order;
import org.openmrs.module.fhir2.api.translators.ToFhirTranslator;
import org.openmrs.module.fhir2.model.FhirBatch;
import org.openmrs.module.fhir2.model.FhirDiagnosticReport;

import javax.annotation.Nonnull;

public interface BatchTranslator extends OpenmrsFhirUpdatableTranslator<FhirBatch, Bundle> {

/**
* Maps a {@link Order} to a {@link Bundle}
*
* @param fhirBatch the FhirBatch object to translate
* @return the corresponding FHIR Bundle
*/
@Override
Bundle toFhirResource(@Nonnull FhirBatch fhirBatch);

/**
* Maps {@link Bundle} to {@link FhirBatch}
*
* @param bundle the FHIR batch to translate
* @return the corresponding OpenMRS FhirBatch
*/
@Override
FhirBatch toOpenmrsType(@Nonnull Bundle bundle);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.openmrs.module.fhir2.api.translators.impl;

import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.DiagnosticReport;
import org.openmrs.Concept;
import org.openmrs.Obs;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.BatchTranslator;
import org.openmrs.module.fhir2.model.FhirBatch;
import org.openmrs.module.fhir2.model.FhirDiagnosticReport;

import javax.annotation.Nonnull;

import static org.apache.commons.lang3.Validate.notNull;

public class BatchTranslatorImpl implements BatchTranslator {

@Override
public Bundle toFhirResource(@Nonnull FhirBatch fhirBatch) {
notNull(fhirBatch, "The batch operation should not be null");

Bundle bundle = new Bundle();

bundle.setId(fhirBatch.getUuid());

if (fhirBatch.getDateChanged() != null) {
bundle.getMeta().setLastUpdated(fhirBatch.getDateChanged());
} else {
bundle.getMeta().setLastUpdated(fhirBatch.getDateCreated());
}

return bundle;
}

@Override
public FhirBatch toOpenmrsType(@Nonnull Bundle bundle) {
return toOpenmrsType(new FhirBatch(), bundle);
}

@Override
public FhirBatch toOpenmrsType(@Nonnull FhirBatch existingObject, @Nonnull Bundle resource) {
return null;
}
}
40 changes: 40 additions & 0 deletions api/src/main/java/org/openmrs/module/fhir2/model/FhirBatch.java
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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<? extends IBaseResource> 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));
}
}