Skip to content

Commit

Permalink
DRAFT EPISODE OF CARE
Browse files Browse the repository at this point in the history
  • Loading branch information
slubwama committed May 28, 2024
1 parent 01ccae3 commit 188ce71
Show file tree
Hide file tree
Showing 7 changed files with 458 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.ugandaemrsync.api;

import ca.uhn.fhir.model.api.Include;
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 org.hl7.fhir.r4.model.EpisodeOfCare;
import org.openmrs.module.fhir2.api.FhirService;

import javax.annotation.Nonnull;
import java.util.HashSet;

public interface FhirEpisodeOfCareService extends FhirService<EpisodeOfCare> {
IBundleProvider searchForEpisodeOfCares(ReferenceAndListParam patient, ReferenceAndListParam type, TokenAndListParam id,
DateRangeParam lastUpdated, HashSet<Include> includes, HashSet<Include> revIncludes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.ugandaemrsync.api.dao;

import org.openmrs.PatientProgram;
import org.openmrs.annotation.Authorized;
import org.openmrs.module.fhir2.api.dao.FhirDao;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.util.PrivilegeConstants;

import javax.annotation.Nonnull;
import java.util.List;

public interface FhirEpisodeOfCareDao extends FhirDao<PatientProgram> {

@Override
@Authorized(PrivilegeConstants.GET_PATIENT_PROGRAMS)
PatientProgram get(@Nonnull String uuid);

@Override
@Authorized({ PrivilegeConstants.ADD_PATIENT_PROGRAMS, PrivilegeConstants.EDIT_PATIENT_PROGRAMS })
PatientProgram createOrUpdate(@Nonnull PatientProgram newEntry);

@Override
@Authorized(PrivilegeConstants.DELETE_PATIENT_PROGRAMS)
PatientProgram delete(@Nonnull String uuid);

@Override
@Authorized(PrivilegeConstants.GET_PATIENT_PROGRAMS)
List<PatientProgram> getSearchResults(@Nonnull SearchParameterMap theParams, @Nonnull List<Integer> resourceIds);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.ugandaemrsync.api.dao.impl;

import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.ReferenceAndListParam;
import lombok.AccessLevel;
import lombok.Setter;
import org.hibernate.Criteria;

import org.openmrs.Encounter;
import org.openmrs.PatientProgram;
import org.openmrs.annotation.Authorized;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.dao.FhirEncounterDao;
import org.openmrs.module.fhir2.api.dao.impl.BaseFhirDao;
import org.openmrs.module.fhir2.api.search.param.SearchParameterMap;
import org.openmrs.module.ugandaemrsync.api.dao.FhirEpisodeOfCareDao;
import org.openmrs.util.PrivilegeConstants;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.List;

@Component
@Setter(AccessLevel.PACKAGE)
public class FhirEpisodeOfCareDaoImpl extends BaseFhirDao<PatientProgram> implements FhirEpisodeOfCareDao {
@Override
protected void setupSearchParams(Criteria criteria, SearchParameterMap theParams) {
theParams.getParameters().forEach(entry -> {
switch (entry.getKey()) {
case FhirConstants.DATE_RANGE_SEARCH_HANDLER:
entry.getValue().forEach(param -> handleDateRange("dateEnrolled", (DateRangeParam) param.getParam())
.ifPresent(criteria::add));
break;
case FhirConstants.LOCATION_REFERENCE_SEARCH_HANDLER:
entry.getValue().forEach(param -> handleLocationReference("l", (ReferenceAndListParam) param.getParam())
.ifPresent(l -> criteria.createAlias("location", "l").add(l)));
break;
case FhirConstants.PARTICIPANT_REFERENCE_SEARCH_HANDLER:
entry.getValue().forEach(
param -> handleParticipantReference(criteria, (ReferenceAndListParam) param.getParam()));
break;
case FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER:
entry.getValue()
.forEach(param -> handlePatientReference(criteria, (ReferenceAndListParam) param.getParam()));
break;
case FhirConstants.COMMON_SEARCH_HANDLER:
handleCommonSearchParameters(entry.getValue()).ifPresent(criteria::add);
break;
}
});
}
}
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.ugandaemrsync.api.impl;

import ca.uhn.fhir.model.api.Include;
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.exceptions.ResourceNotFoundException;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.ss.formula.functions.T;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.r4.model.EpisodeOfCare;
import org.openmrs.PatientProgram;
import org.openmrs.api.ProgramWorkflowService;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.dao.FhirDao;
import org.openmrs.module.fhir2.api.impl.BaseFhirService;
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.OpenmrsFhirTranslator;
import org.openmrs.module.ugandaemrsync.api.FhirEpisodeOfCareService;
import org.openmrs.module.ugandaemrsync.api.dao.FhirEpisodeOfCareDao;
import org.openmrs.module.ugandaemrsync.api.translators.EpisodeOfCareTranslator;
import org.openmrs.module.ugandaemrsync.api.translators.impl.EpisodeOfCareTranslatorImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@Transactional
@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PROTECTED)
public class FhirEpisodeOfCareServiceImpl extends BaseFhirService<EpisodeOfCare, PatientProgram> implements FhirEpisodeOfCareService {

@Autowired
private FhirEpisodeOfCareDao dao;

@Autowired
private EpisodeOfCareTranslator<PatientProgram> translator;

@Autowired
private SearchQueryInclude<EpisodeOfCare> searchQueryInclude;

@Autowired
private SearchQuery<PatientProgram, EpisodeOfCare, FhirEpisodeOfCareDao, EpisodeOfCareTranslator<PatientProgram>, SearchQueryInclude<EpisodeOfCare>> searchQuery;

@Override
public EpisodeOfCare get(@Nonnull String uuid) {

EpisodeOfCare result = null;
try {
result = super.get(uuid);
} catch (ResourceNotFoundException e) {

}

return result;
}

@Override
protected FhirDao<PatientProgram> getDao() {
return this.dao;
}


@Override
protected OpenmrsFhirTranslator<PatientProgram, EpisodeOfCare> getTranslator() {
return this.translator;
}


@Override
@Transactional(readOnly = true)
public IBundleProvider searchForEpisodeOfCares(ReferenceAndListParam patient, ReferenceAndListParam type, TokenAndListParam id,
DateRangeParam lastUpdated, HashSet<Include> includes, HashSet<Include> revIncludes) {
SearchParameterMap theParams = new SearchParameterMap().addParameter(FhirConstants.DATE_RANGE_SEARCH_HANDLER, lastUpdated)
.addParameter(FhirConstants.PARTICIPANT_REFERENCE_SEARCH_HANDLER, patient)
.addParameter(FhirConstants.PATIENT_REFERENCE_SEARCH_HANDLER, type)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.ID_PROPERTY, id)
.addParameter(FhirConstants.COMMON_SEARCH_HANDLER, FhirConstants.LAST_UPDATED_PROPERTY, lastUpdated)
.addParameter(FhirConstants.INCLUDE_SEARCH_HANDLER, includes)
.addParameter(FhirConstants.REVERSE_INCLUDE_SEARCH_HANDLER, revIncludes);
return searchQuery.getQueryResults(theParams, dao, translator, searchQueryInclude);
}
}
Original file line number Diff line number Diff line change
@@ -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.ugandaemrsync.api.translators;


import org.hl7.fhir.r4.model.EpisodeOfCare;
import org.openmrs.module.fhir2.api.translators.OpenmrsFhirUpdatableTranslator;

import javax.annotation.Nonnull;

public interface EpisodeOfCareTranslator<T> extends OpenmrsFhirUpdatableTranslator<T, EpisodeOfCare> {

/**
* Maps {@link org.openmrs.PatientProgram} to a {@link org.hl7.fhir.r4.model.EpisodeOfCare} resource
*
* @param patientProgram the OpenMRS patientProgram to translate
* @return the corresponding FHIR EpisodeOfCare resource
*/
@Override
EpisodeOfCare toFhirResource(@Nonnull T patientProgram);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* 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.ugandaemrsync.api.translators.impl;

import lombok.AccessLevel;
import lombok.Setter;
import org.hl7.fhir.r4.model.EpisodeOfCare;
import org.hl7.fhir.r4.model.Period;
import org.hl7.fhir.r4.model.CodeableConcept;
import org.hl7.fhir.r4.model.Reference;
import org.hl7.fhir.r4.model.Identifier;
import org.openmrs.PatientIdentifier;
import org.openmrs.PatientProgram;
import org.openmrs.api.context.Context;
import org.openmrs.module.fhir2.FhirConstants;
import org.openmrs.module.fhir2.api.translators.ConceptTranslator;
import org.openmrs.module.fhir2.api.translators.PatientIdentifierTranslator;
import org.openmrs.module.fhir2.api.translators.PatientReferenceTranslator;
import org.openmrs.module.ugandaemrsync.api.translators.EpisodeOfCareTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

import static org.openmrs.module.ugandaemrsync.server.SyncConstant.GP_DHIS2;

@Component
@Setter(AccessLevel.PACKAGE)
public class EpisodeOfCareTranslatorImpl implements EpisodeOfCareTranslator<PatientProgram> {

@Autowired
private PatientReferenceTranslator patientReferenceTranslator;

@Autowired
private PatientIdentifierTranslator patientIdentifierTranslator;

@Autowired
private ConceptTranslator conceptTranslator;


@Override
public EpisodeOfCare toFhirResource(@Nonnull PatientProgram patientProgram) {

Period period = new Period();
period.setStart(patientProgram.getDateEnrolled());
period.setEnd(patientProgram.getDateCompleted());
List<CodeableConcept> type = new ArrayList<>();
type.add(conceptTranslator.toFhirResource(patientProgram.getProgram().getConcept()));

EpisodeOfCare episodeOfCare = new EpisodeOfCare();
episodeOfCare.setId(patientProgram.getUuid());
episodeOfCare.setStatus(getStatus(patientProgram));
episodeOfCare.setIdentifier(getEpisodeOfCareIdentifer(patientProgram));
episodeOfCare.setManagingOrganization(createOrganizationReference());
episodeOfCare.setPatient(patientReferenceTranslator.toFhirResource(patientProgram.getPatient()));
episodeOfCare.setPeriod(period);
episodeOfCare.setType(type);
return episodeOfCare;
}

@Override
public PatientProgram toOpenmrsType(@Nonnull EpisodeOfCare episodeOfCare) {
return null;
}

@Override
public PatientProgram toOpenmrsType(@Nonnull PatientProgram existingEncounter, @Nonnull EpisodeOfCare episodeOfCare) {
return null;
}

protected Reference createOrganizationReference() {
String healthCenterIdentifier = Context.getAdministrationService().getGlobalProperty(GP_DHIS2);
String healthCenterName = Context.getLocationService().getLocationByUuid("629d78e9-93e5-43b0-ad8a-48313fd99117").getName();

Reference reference = new Reference().setReference(FhirConstants.ORGANIZATION + "/" + healthCenterIdentifier)
.setType(FhirConstants.ORGANIZATION);

Identifier identifier = new Identifier();
identifier.setUse(Identifier.IdentifierUse.OFFICIAL);
identifier.setValue(healthCenterIdentifier);
identifier.setSystem("https://hmis.health.go.ug/");

reference.setDisplay(healthCenterName);
reference.setIdentifier(identifier);
return reference;
}

private List<Identifier> getEpisodeOfCareIdentifer(PatientProgram patientProgram) {
List<Identifier> identifiers = new ArrayList<>();
Identifier identifier = new Identifier();
identifier.setUse(Identifier.IdentifierUse.USUAL);
identifier.setValue(patientProgram.getUuid());
identifier.setSystem("https://ugandaemr/");
identifiers.add(identifier);

return identifiers;
}

private EpisodeOfCare.EpisodeOfCareStatus getStatus(PatientProgram patientProgram) {
if (patientProgram.getActive()) {
return EpisodeOfCare.EpisodeOfCareStatus.ACTIVE;
} else {
return EpisodeOfCare.EpisodeOfCareStatus.FINISHED;
}
}
}
Loading

0 comments on commit 188ce71

Please sign in to comment.