Skip to content

Commit

Permalink
1. Added Model MedicalStatement resource along with extensions.
Browse files Browse the repository at this point in the history
2. Created provider and service classes to Create the resource(for beneficiary medication history details).
3. Added validations for the same resource.
4. Mapped MedicationStatement resource to MedicationHistoryDTO.
5. Exception handling.
6. Change in mapper class -> Reading benRegID from display instead of reference in Immunization resource.
7. Appropriate validation for above change.
  • Loading branch information
shreypatidar-beehyv committed Aug 21, 2023
1 parent 28014fd commit 435d009
Show file tree
Hide file tree
Showing 10 changed files with 326 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.iemr.hwc.fhir.provider.condition.ConditionExtProvider;
import com.iemr.hwc.fhir.provider.encounter.EncounterExtProvider;
import com.iemr.hwc.fhir.provider.immunization.ImmunizationExtProvider;
import com.iemr.hwc.fhir.provider.medicationStatement.MedicationStatementExtProvider;
import com.iemr.hwc.fhir.provider.observation.ObservationExtProvider;
import com.iemr.hwc.fhir.provider.patient.PatientExtProvider;
import org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator;
Expand Down Expand Up @@ -45,6 +46,7 @@ protected void initialize() throws ServletException{
resourceProviders.add(applicationContext.getBean(ConditionExtProvider.class));
resourceProviders.add(applicationContext.getBean(ObservationExtProvider.class));
resourceProviders.add(applicationContext.getBean(ImmunizationExtProvider.class));
resourceProviders.add(applicationContext.getBean(MedicationStatementExtProvider.class));
setResourceProviders(resourceProviders);

//Registering Interceptors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MedicationHistoryDTO {
private String beneficiaryRegID;
private Integer providerServiceMapID;
private String benVisitID;
private String visitCode;
private String createdBy;
private Integer vanID;
private Integer parkingPlaceID;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.iemr.hwc.fhir.model.medicationStatement;

import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.util.ElementUtil;
import org.hl7.fhir.r4.model.MedicationStatement;
import org.hl7.fhir.r4.model.StringType;

@ResourceDef(name = "MedicationStatement")
public class MedicationStatementExt extends MedicationStatement {

@Description(shortDefinition = "Contains providerServiceMapId ")
@Extension(url = "http://hl7.org/fhir/StructureDefinition/MedicationStatement#MedicationStatement.providerServiceMapId", isModifier = false, definedLocally = true)
@Child(name = "providerServiceMapId")
private StringType providerServiceMapId;

@Description(shortDefinition = "Contains vanID ")
@Extension(url = "http://hl7.org/fhir/StructureDefinition/MedicationStatement#MedicationStatement.vanID", isModifier = false, definedLocally = true)
@Child(name = "vanID")
private StringType vanID;

@Description(shortDefinition = "Contains parkingPlaceID ")
@Extension(url = "http://hl7.org/fhir/StructureDefinition/MedicationStatement#MedicationStatement.parkingPlaceID", isModifier = false, definedLocally = true)
@Child(name = "parkingPlaceID")
private StringType parkingPlaceID;

@Description(shortDefinition = "Contains createdBy ")
@Extension(url = "http://hl7.org/fhir/StructureDefinition/MedicationStatement#MedicationStatement.createdBy", isModifier = false, definedLocally = true)
@Child(name = "createdBy")
private StringType createdBy;

@Description(shortDefinition = "Contains benFlowID ")
@Extension(url = "http://hl7.org/fhir/StructureDefinition/MedicationStatement#MedicationStatement.benFlowID", isModifier = false, definedLocally = true)
@Child(name = "benFlowID")
private StringType benFlowID;

public StringType getProviderServiceMapId() {
if (providerServiceMapId == null) {
providerServiceMapId = new StringType();
}
return providerServiceMapId;
}

public void setProviderServiceMapId(StringType providerService_MapId) {
providerServiceMapId = providerService_MapId;
}

public StringType getVanID() {
if (vanID == null) {
vanID = new StringType();
}
return vanID;
}

public void setVanID(StringType van_ID) {
vanID = van_ID;
}

public StringType getParkingPlaceID() {
if (parkingPlaceID == null) {
parkingPlaceID = new StringType();
}
return parkingPlaceID;
}

public void setParkingPlaceID(StringType parking_PlaceID) {
parkingPlaceID = parking_PlaceID;
}

public StringType getCreatedBy() {
if (createdBy == null) {
createdBy = new StringType();
}
return createdBy;
}

public void setCreatedBy(StringType created_By) {
createdBy = created_By;
}

public StringType getBenFlowID() {
if (benFlowID == null) {
benFlowID = new StringType();
}
return benFlowID;
}

public void setBenFlowID(StringType benFlow_ID) {
benFlowID = benFlow_ID;
}

@Override
public boolean isEmpty() {
return super.isEmpty() && ElementUtil.isEmpty(benFlowID , providerServiceMapId ,
vanID , parkingPlaceID , createdBy );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.iemr.hwc.fhir.provider.medicationStatement;

import ca.uhn.fhir.rest.annotation.Create;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.server.IResourceProvider;
import com.iemr.hwc.fhir.model.medicationStatement.MedicationStatementExt;
import com.iemr.hwc.fhir.service.medicationStatement.MedicationService;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.OperationOutcome;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

@Component
public class MedicationStatementExtProvider implements IResourceProvider {

@Autowired
private MedicationService medicationService;

@Override
public Class<? extends IBaseResource> getResourceType() {
return MedicationStatementExt.class;
}

@Create()
public MethodOutcome createMedicationStatement(HttpServletRequest theRequest, @ResourceParam MedicationStatementExt medicationStatementExt) throws Exception{

MethodOutcome method = new MethodOutcome();
method.setCreated(true);
OperationOutcome opOutcome = new OperationOutcome();
method.setOperationOutcome(opOutcome);
method.setResource(medicationService.createMedicationStatement(theRequest,medicationStatementExt));
return method;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.iemr.hwc.fhir.service.medicationStatement;

import com.iemr.hwc.fhir.model.medicationStatement.MedicationStatementExt;
import javax.servlet.http.HttpServletRequest;

public interface MedicationService {
MedicationStatementExt createMedicationStatement(HttpServletRequest theRequest, MedicationStatementExt medicationStatementExt) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.iemr.hwc.fhir.service.medicationStatement;

import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.iemr.hwc.data.anc.WrapperMedicationHistory;
import com.iemr.hwc.data.benFlowStatus.BeneficiaryFlowStatus;
import com.iemr.hwc.fhir.dto.historyDetails.medicationHistory.MedicationHistoryDTO;
import com.iemr.hwc.fhir.dto.historyDetails.medicationHistory.MedicationListDTO;
import com.iemr.hwc.fhir.dto.mandatoryFieldsDTO.MandatoryFieldsDTO;
import com.iemr.hwc.fhir.model.medicationStatement.MedicationStatementExt;
import com.iemr.hwc.fhir.utils.mapper.MapperMethods;
import com.iemr.hwc.fhir.utils.mapper.MapperUtils;
import com.iemr.hwc.fhir.utils.validation.MedicationStatementValidation;
import com.iemr.hwc.repo.benFlowStatus.BeneficiaryFlowStatusRepo;
import com.iemr.hwc.service.common.transaction.CommonNurseServiceImpl;
import com.iemr.hwc.utils.exception.IEMRException;
import com.iemr.hwc.utils.mapper.InputMapper;
import org.mapstruct.factory.Mappers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

@Service
public class MedicationStatementServiceImpl implements MedicationService{

public MapperUtils mapper = Mappers.getMapper(MapperUtils.class);

Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());

@Autowired
private MedicationStatementValidation validation;

@Autowired
private BeneficiaryFlowStatusRepo beneficiaryFlowStatusRepo;

@Autowired
private CommonNurseServiceImpl commonNurseService;

@Override
public MedicationStatementExt createMedicationStatement(HttpServletRequest theRequest, MedicationStatementExt medicationStatementExt) throws Exception {

validation.medicationStatementValidator(medicationStatementExt);

//Todo - Currently implemented considering all relevant IDs(benRegID, benFlowID) are coming in payload.
//Todo - If not, might need to write new APIs to fetch necessary IDs through some sort of logic. And then use those further.
MandatoryFieldsDTO mandatoryFieldsDTO = mapper.medicationStatementResourceToMandatoryFieldsDTO(medicationStatementExt);

BeneficiaryFlowStatus beneficiaryFlowStatus = beneficiaryFlowStatusRepo.getBenDetailsForLeftSidePanel(Long.parseLong(mandatoryFieldsDTO.getBenFlowID()));

if (beneficiaryFlowStatus !=null ) {
mandatoryFieldsDTO.setBenVisitID(beneficiaryFlowStatus.getBenVisitID().toString());
mandatoryFieldsDTO.setVisitCode(beneficiaryFlowStatus.getBenVisitCode().toString());
}
else {
logger.error("No beneficiary flow status record found for the provided benFlowID");
throw new ResourceNotFoundException("No record found for given benFlowID");
}

MedicationHistoryDTO medicationHistoryDTO = mapper.mandatoryFieldsDTOToMedicationHistoryDTO(mandatoryFieldsDTO);

if (medicationStatementExt.getMedicationCodeableConcept().hasCoding()) {
medicationHistoryDTO.setMedicationHistoryList(MapperMethods.medicationToMedicationListDTO(medicationStatementExt));
}
else {
medicationHistoryDTO.setMedicationHistoryList(new ArrayList<MedicationListDTO>());
}

String medicationHistoryDTOGson = new GsonBuilder().create().toJson(medicationHistoryDTO);
JsonObject medicationHistoryDTOJson = new JsonParser().parse(medicationHistoryDTOGson).getAsJsonObject();

try{
WrapperMedicationHistory wrapperMedicationHistory = InputMapper.gson()
.fromJson(medicationHistoryDTOJson, WrapperMedicationHistory.class);

commonNurseService.updateBenMedicationHistory(wrapperMedicationHistory);
}catch (IEMRException e){
logger.error("Encountered custom exception - IEMRException while trying to map Json with WrapperMedicationHistory class using Input Mapper " + e);
throw new InternalErrorException("Error mapping json to WrapperMedicationHistory class " + e);
}

return medicationStatementExt;
}
}
23 changes: 20 additions & 3 deletions src/main/java/com/iemr/hwc/fhir/utils/mapper/MapperMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.iemr.hwc.fhir.dto.beneficiary.benIdentities.GovtIdentitiesDTO;
import com.iemr.hwc.fhir.dto.historyDetails.medicationHistory.MedicationListDTO;
import com.iemr.hwc.fhir.dto.historyDetails.pastHistory.PastIllnessDTO;
import com.iemr.hwc.fhir.dto.historyDetails.pastHistory.PastSurgeryDTO;
import com.iemr.hwc.fhir.dto.vitalDetails.VitalDetailsDTO;
import com.iemr.hwc.fhir.model.encounter.EncounterExt;
import com.iemr.hwc.fhir.model.medicationStatement.MedicationStatementExt;
import com.iemr.hwc.fhir.model.observation.ObservationExt;
import com.iemr.hwc.fhir.model.patient.PatientExt;
import com.iemr.hwc.utils.exception.IEMRException;
import com.iemr.hwc.utils.mapper.InputMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -152,4 +151,22 @@ public static List<PastSurgeryDTO> observationPastHistoryToPastSurgeryDTO(Observ
}
return pastSurgeryDTOList;
}

public static List<MedicationListDTO> medicationToMedicationListDTO(MedicationStatementExt medicationStatementExt) {
List<MedicationListDTO> medicationDTOList = new ArrayList<>();
for (int i=0 ; i < medicationStatementExt.getMedicationCodeableConcept().getCoding().size() ; i++){
MedicationListDTO medicationListDTO = new MedicationListDTO();

medicationListDTO.setCurrentMedication(medicationStatementExt.getMedicationCodeableConcept().getCoding().get(i).getDisplay());

if(medicationStatementExt.getMedicationCodeableConcept().getCoding().get(i).hasCode()) {
String[] arr = medicationStatementExt.getMedicationCodeableConcept().getCoding().get(i).getCode().split(",");
medicationListDTO.setTimePeriodAgo(arr[0]);
medicationListDTO.setTimePeriodUnit(arr[1]);
}

medicationDTOList.add(medicationListDTO);
}
return medicationDTOList;
}
}
15 changes: 14 additions & 1 deletion src/main/java/com/iemr/hwc/fhir/utils/mapper/MapperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.iemr.hwc.fhir.dto.covidVaccineStatus.CovidVaccineStatusDTO;
import com.iemr.hwc.fhir.dto.examinationDetails.ExaminationDetailsMainDTO;
import com.iemr.hwc.fhir.dto.historyDetails.HistoryDetailsMainDTO;
import com.iemr.hwc.fhir.dto.historyDetails.medicationHistory.MedicationHistoryDTO;
import com.iemr.hwc.fhir.dto.historyDetails.pastHistory.PastHistoryDTO;
import com.iemr.hwc.fhir.dto.mandatoryFieldsDTO.MandatoryFieldsDTO;
import com.iemr.hwc.fhir.dto.nurseForm.NurseFormDTO;
Expand All @@ -17,6 +18,7 @@
import com.iemr.hwc.fhir.model.condition.ConditionExt;
import com.iemr.hwc.fhir.model.encounter.EncounterExt;
import com.iemr.hwc.fhir.model.immunization.ImmunizationExt;
import com.iemr.hwc.fhir.model.medicationStatement.MedicationStatementExt;
import com.iemr.hwc.fhir.model.observation.ObservationExt;
import com.iemr.hwc.fhir.model.patient.PatientExt;
import org.mapstruct.Mapper;
Expand Down Expand Up @@ -171,7 +173,7 @@ public interface MapperUtils {
@Mappings({@Mapping(target = "providerServiceMapID", expression = "java(null == immunizationExt.getProviderServiceMapId().asStringValue() ? null : Integer.parseInt(immunizationExt.getProviderServiceMapId().asStringValue()))"),
@Mapping(target = "parkingPlaceID", expression = "java(null == immunizationExt.getParkingPlaceID().asStringValue() ? null : Integer.parseInt(immunizationExt.getParkingPlaceID().asStringValue()))"),
@Mapping(target = "vanID", expression = "java(null == immunizationExt.getVanID().asStringValue() ? null : Integer.parseInt(immunizationExt.getVanID().asStringValue()))"),
@Mapping(target = "beneficiaryRegID", expression = "java(immunizationExt.getPatient().getReference())"),
@Mapping(target = "beneficiaryRegID", expression = "java(immunizationExt.getPatient().getDisplay())"),
@Mapping(target = "createdBy", expression = "java(immunizationExt.getCreatedBy().asStringValue())"),
@Mapping(target = "modifiedBy", expression = "java(immunizationExt.getModifiedBy().asStringValue())")
})
Expand All @@ -191,6 +193,17 @@ public interface MapperUtils {
})
CovidVaccineStatusDTO immunizationResourceToCovidVaccineStatusDTO(ImmunizationExt immunizationExt, MandatoryFieldsDTO mandatoryFieldsDTO);

@Mappings({@Mapping(target = "providerServiceMapID", expression = "java(Integer.parseInt(medicationStatementExt.getProviderServiceMapId().asStringValue()))"),
@Mapping(target = "parkingPlaceID", expression = "java(Integer.parseInt(medicationStatementExt.getParkingPlaceID().asStringValue()))"),
@Mapping(target = "vanID", expression = "java(Integer.parseInt(medicationStatementExt.getVanID().asStringValue()))"),
@Mapping(target = "benFlowID", expression = "java(medicationStatementExt.getBenFlowID().asStringValue())"),
@Mapping(target = "beneficiaryRegID", expression = "java(medicationStatementExt.getSubject().getDisplay())"),
@Mapping(target = "createdBy", expression = "java(medicationStatementExt.getCreatedBy().asStringValue())"),
})
MandatoryFieldsDTO medicationStatementResourceToMandatoryFieldsDTO(MedicationStatementExt medicationStatementExt);

MedicationHistoryDTO mandatoryFieldsDTOToMedicationHistoryDTO(MandatoryFieldsDTO mandatoryFieldsDTO);




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void immunizationResourceValidator(ImmunizationExt immunizationExt) throw
errMessages.add("Mandatory extension 'vanId' missing");
}

if (!immunizationExt.getPatient().hasDisplay()) {
logger.error("Error while validating Immunization resource. benRegID is a mandatory field and is MISSING");
errMessages.add("Mandatory field 'display'(benRegID) in 'patient' missing");
}

//If vaccination is done then validations for type of vaccine, type of dose
if (immunizationExt.getStatus().getDisplay().equalsIgnoreCase("completed")) {
if (!immunizationExt.getVaccineCode().hasText()) {
Expand Down
Loading

0 comments on commit 435d009

Please sign in to comment.