use of org.hl7.fhir.r4.model.CodeableConcept in project gpconnect-demonstrator by nhsconnect.
the class AppointmentResourceProvider method createAppointment.
/**
* Create a new appointment
*
* @param appointment Resource
* @return MethodOutcome
*/
@Create
public MethodOutcome createAppointment(@ResourceParam Appointment appointment) {
Meta meta = appointment.getMeta();
final List<UriType> profiles = meta.getProfile();
// #203 validations
// Uses VC class and lamda functions for brevity and to avoid masses of duplicated code
// first lamda takes no params and returns a Boolean representing the fail condition ie true for fail
// second lamda takes no params and returns a String describing the error condition
// VC.execute evaluates the first lamda and if true theows an InvalidResource exception containg the string from the second lambda
VC.execute(new VC[] { new VC(() -> profiles.isEmpty(), () -> "Meta element must be present in Appointment"), new VC(() -> !profiles.get(0).getValue().equalsIgnoreCase(SD_GPC_APPOINTMENT), () -> "Meta.profile " + profiles.get(0).getValue() + " is not equal to " + SD_GPC_APPOINTMENT), // what to do if > 1 meta profile element?
new VC(() -> appointment.getStatus() == null, () -> "No status supplied"), new VC(() -> appointment.getStatus() != AppointmentStatus.BOOKED, () -> "Status must be \"booked\""), new VC(() -> appointment.getStart() == null || appointment.getEnd() == null, () -> "Both start and end date are required"), new VC(() -> appointment.getSlot().isEmpty(), () -> "At least one slot is required"), new VC(() -> appointment.getCreated() == null, () -> "Created must be populated"), new VC(() -> appointment.getParticipant().isEmpty(), () -> "At least one participant is required"), new VC(() -> !appointment.getIdentifierFirstRep().isEmpty() && appointment.getIdentifierFirstRep().getValue() == null, () -> "Appointment identifier value is required"), new VC(() -> appointment.getId() != null, () -> "Appointment id shouldn't be provided!"), new VC(() -> !appointment.getReason().isEmpty(), () -> "Appointment reason shouldn't be provided!"), // #157 refers to typeCode but means specialty (name was changed)
new VC(() -> !appointment.getSpecialty().isEmpty(), () -> "Appointment speciality shouldn't be provided!"), // new VC(() -> !appointment.getServiceType().isEmpty(), () -> "Appointment service type shouldn't be provided!"),
new VC(() -> !appointment.getAppointmentType().isEmpty(), () -> "Appointment type shouldn't be provided!"), new VC(() -> !appointment.getIndication().isEmpty(), () -> "Appointment indication shouldn't be provided!"), new VC(() -> !appointment.getSupportingInformation().isEmpty(), () -> "Appointment supporting information shouldn't be provided!"), new VC(() -> !appointment.getIncomingReferral().isEmpty(), () -> "Appointment incoming referral shouldn't be provided!") });
boolean patientFound = false;
boolean locationFound = false;
for (AppointmentParticipantComponent participant : appointment.getParticipant()) {
if (participant.getActor().getReference() != null) {
String reference = participant.getActor().getReference();
// check for absolute reference #200
checkReferenceIsRelative(reference);
if (reference.contains("Patient")) {
patientFound = true;
} else if (reference.contains("Location")) {
locationFound = true;
}
} else {
throwUnprocessableEntity422_InvalidResourceException("Appointment resource is not valid as it does not contain a Participant Actor reference");
}
}
if (!patientFound || !locationFound) {
throwUnprocessableEntity422_InvalidResourceException("Appointment resource is not a valid resource required valid Patient and Location");
}
// variable accessed in a lambda must be declared final
final boolean fPatientFound = patientFound;
final boolean fLocationFound = locationFound;
VC.execute(new VC[] { new VC(() -> !fPatientFound, () -> "Appointment resource is not a valid resource required valid Patient"), new VC(() -> !fLocationFound, () -> "Appointment resource is not a valid resource required valid Location") });
String locationId = null;
String practitionerId = null;
for (AppointmentParticipantComponent participant : appointment.getParticipant()) {
Reference participantActor = participant.getActor();
final Boolean searchParticipant = (participantActor != null);
if (searchParticipant) {
appointmentValidation.validateParticipantActor(participantActor);
}
CodeableConcept participantType = participant.getTypeFirstRep();
final Boolean validParticipantType = appointmentValidation.validateParticipantType(participantType);
VC.execute(new VC[] { new VC(() -> !searchParticipant, () -> "Supplied Participant is not valid. Must have an Actor."), new VC(() -> !validParticipantType, () -> "Supplied Participant is not valid. Must have a Type.") });
// gets the logical id as a string
if (participantActor.getReference().startsWith("Location")) {
locationId = appointmentValidation.getId();
} else if (participantActor.getReference().startsWith("Practitioner")) {
practitionerId = appointmentValidation.getId();
}
appointmentValidation.validateParticipantStatus(participant.getStatus(), participant.getStatusElement(), participant.getStatusElement());
}
List<Extension> extensions = validateAppointmentExtensions(appointment, profiles, AppointmentOperation.BOOK);
// Store New Appointment
AppointmentDetail appointmentDetail = appointmentResourceConverterToAppointmentDetail(appointment);
List<SlotDetail> slots = new ArrayList<>();
// we'll get the delivery channel from the slot
String deliveryChannel = null;
String practitionerRoleCode = null;
String practitionerRoleDisplay = null;
ScheduleDetail schedule = null;
String serviceType = null;
for (Long slotId : appointmentDetail.getSlotIds()) {
SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
new VC(() -> slotDetail == null, () -> String.format("Slot resource reference value %s is not a valid resource.", slotId)).execute();
if (slotDetail.getFreeBusyType().equals("BUSY")) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceVersionConflictException(String.format("Slot is already in use.", slotId)), SystemCode.DUPLICATE_REJECTED, IssueType.CONFLICT);
}
if (deliveryChannel == null) {
deliveryChannel = slotDetail.getDeliveryChannelCode();
} else if (!deliveryChannel.equals(slotDetail.getDeliveryChannelCode())) {
// added at 1.2.2
throwUnprocessableEntity422_InvalidResourceException(String.format("Subsequent slot (Slot/%s) delivery channel (%s) is not equal to initial slot delivery channel (%s).", slotId, deliveryChannel, slotDetail.getDeliveryChannelCode()));
}
// 1.2.7 check service type is the same as initial service type
if (serviceType == null) {
serviceType = slotDetail.getTypeDisply();
} else if (!serviceType.equals(slotDetail.getTypeDisply())) {
// added at 1.2.2
throwUnprocessableEntity422_InvalidResourceException(String.format("Subsequent slot (Slot/%s) service type (%s) is not equal to initial slot service type (%s).", slotId, serviceType, slotDetail.getTypeDisply()));
}
if (schedule == null) {
// load the schedule so we can get the Practitioner ID
schedule = scheduleSearch.findScheduleByID(slotDetail.getScheduleReference());
// add practitioner id so we can get the practitioner role
// practitioner is derived but needs to be stored fgr comparison if provided
appointmentDetail.setPractitionerId(schedule.getPractitionerId());
}
if (practitionerRoleDisplay == null) {
practitionerRoleDisplay = schedule.getPractitionerRoleDisplay();
practitionerRoleCode = schedule.getPractitionerRoleCode();
}
slots.add(slotDetail);
}
// for slot
validateUpdateExtensions(deliveryChannel, practitionerRoleDisplay, practitionerRoleCode, extensions);
// #203 check location id matches the one in the schedule
if (locationId != null && !locationId.equals(schedule.getLocationId().toString())) {
throwUnprocessableEntity422_InvalidResourceException(String.format("Provided location id (%s) is not equal to Schedule location id (%s)", locationId, schedule.getLocationId().toString()));
}
// #203 check practitioner id matches the one in the schedule if there is one
if (practitionerId != null) {
if (!practitionerId.equals(schedule.getPractitionerId().toString())) {
throwUnprocessableEntity422_InvalidResourceException(String.format("Provided practitioner id (%s) is not equal to Schedule practitioner id (%s)", practitionerId, schedule.getPractitionerId().toString()));
}
}
// #203
Date firstSlotStart = slots.get(0).getStartDateTime();
Date lastSlotEnd = slots.get(slots.size() - 1).getEndDateTime();
// need to insert a colon in the timezone string
String firstSlotStartStr = TIMESTAMP_FORMAT.format(firstSlotStart).replaceFirst("([0-9]{2})([0-9]{2})$", "$1:$2");
String lastSlotEndStr = TIMESTAMP_FORMAT.format(lastSlotEnd).replaceFirst("([0-9]{2})([0-9]{2})$", "$1:$2");
VC.execute(new VC[] { new VC(() -> appointment.getStart().compareTo(firstSlotStart) != 0, () -> String.format("Start date '%s' must match start date of first slot '%s'", appointment.getStart(), firstSlotStart)), new VC(() -> appointment.getEnd().compareTo(lastSlotEnd) != 0, () -> String.format("End date '%s' must match end date of last slot '%s'", appointment.getEnd(), lastSlotEnd)), // #218 strings should match exactly
new VC(() -> !appointment.getStartElement().getValueAsString().equals(firstSlotStartStr), () -> String.format("Start date '%s' must lexically match start date of first slot '%s'", appointment.getStartElement().getValueAsString(), firstSlotStartStr)), new VC(() -> !appointment.getEndElement().getValueAsString().equals(lastSlotEndStr), () -> String.format("End date '%s' must lexically match end date of last slot '%s'", appointment.getEndElement().getValueAsString(), lastSlotEndStr)), // This probably can never happen under fhir
new VC(() -> appointment.getSlot().isEmpty(), () -> "Slot reference must be populated") });
int durationFromSlots = (int) (lastSlotEnd.getTime() - firstSlotStart.getTime()) / (1000 * 60);
// #203 validate optional minutes duration
if (appointment.hasMinutesDuration()) {
int providedDuration = appointment.getMinutesDuration();
new VC(() -> durationFromSlots != providedDuration, () -> String.format("Provided duration (%d minutes) is not equal to actual appointment duration (%d minutes)", providedDuration, durationFromSlots)).execute();
} else {
// not provided so write the calculated value into the appointmentDetail object
appointmentDetail.setMinutesDuration(durationFromSlots);
}
// add deliveryChannel #157 removed
// appointmentDetail.setDeliveryChannel(deliveryChannel);
appointmentDetail = appointmentStore.saveAppointment(appointmentDetail, slots);
for (SlotDetail slot : slots) {
// slot.setAppointmentId(appointmentDetail.getId());
slot.setFreeBusyType("BUSY");
slot.setLastUpdated(new Date());
slotStore.saveSlot(slot);
}
// Build response containing the new resource id
MethodOutcome methodOutcome = new MethodOutcome();
methodOutcome.setId(new IdDt("Appointment", appointmentDetail.getId()));
methodOutcome.setResource(appointmentDetailToAppointmentResourceConverter(appointmentDetail));
methodOutcome.setCreated(Boolean.TRUE);
return methodOutcome;
}
use of org.hl7.fhir.r4.model.CodeableConcept in project gpconnect-demonstrator by nhsconnect.
the class OperationOutcomeFactory method buildOperationOutcomeException.
/**
* @param exception carries message used for diagnostics
* @param code
* @param issueType
* @param diagnostics may be null but will override exception.message if set
* @return BaseServerResponseException
*/
public static BaseServerResponseException buildOperationOutcomeException(BaseServerResponseException exception, String code, IssueType issueType, String diagnostics) {
CodeableConcept codeableConcept = new CodeableConcept();
Coding coding = new Coding(SystemURL.VS_GPC_ERROR_WARNING_CODE, code, code);
codeableConcept.addCoding(coding);
OperationOutcome operationOutcome = new OperationOutcome();
OperationOutcomeIssueComponent ooIssue = new OperationOutcomeIssueComponent();
ooIssue.setSeverity(IssueSeverity.ERROR).setDetails(codeableConcept).setCode(issueType);
if (diagnostics != null) {
ooIssue.setDiagnostics(diagnostics);
} else {
// #248 move exception.getMessage() from text to diagnostics element
ooIssue.setDiagnostics(exception.getMessage());
}
operationOutcome.addIssue(ooIssue);
operationOutcome.getMeta().addProfile(SystemURL.SD_GPC_OPERATIONOUTCOME);
exception.setOperationOutcome(operationOutcome);
return exception;
}
use of org.hl7.fhir.r4.model.CodeableConcept in project gpconnect-demonstrator by nhsconnect.
the class MedicationDispenseResourceProvider method getMedicationDispensesForPatientId.
@Search
public List<MedicationDispense> getMedicationDispensesForPatientId(@RequiredParam(name = "patient") String patientId) {
ArrayList<MedicationDispense> medicationDispenses = new ArrayList<>();
List<MedicationDispenseDetail> medicationDispenseDetailList = medicationDispenseSearch.findMedicationDispenseForPatient(Long.parseLong(patientId));
if (medicationDispenseDetailList != null && !medicationDispenseDetailList.isEmpty()) {
for (MedicationDispenseDetail medicationDispenseDetail : medicationDispenseDetailList) {
MedicationDispense medicationDispense = new MedicationDispense();
medicationDispense.setId(String.valueOf(medicationDispenseDetail.getId()));
medicationDispense.getMeta().setLastUpdated(medicationDispenseDetail.getLastUpdated());
medicationDispense.getMeta().setVersionId(String.valueOf(medicationDispenseDetail.getLastUpdated().getTime()));
switch(medicationDispenseDetail.getStatus().toLowerCase(Locale.UK)) {
case "completed":
medicationDispense.setStatus(MedicationDispenseStatus.COMPLETED);
break;
case "entered_in_error":
medicationDispense.setStatus(MedicationDispenseStatus.ENTEREDINERROR);
break;
case "in_progress":
medicationDispense.setStatus(MedicationDispenseStatus.INPROGRESS);
break;
case "on_hold":
medicationDispense.setStatus(MedicationDispenseStatus.ONHOLD);
break;
case "stopped":
medicationDispense.setStatus(MedicationDispenseStatus.STOPPED);
break;
}
medicationDispense.setSubject(new Reference("Patient/" + patientId));
medicationDispense.setAuthorizingPrescription(Collections.singletonList(new Reference("MedicationOrder/" + medicationDispenseDetail.getMedicationOrderId())));
Medication medication = new Medication();
Coding coding = new Coding();
coding.setCode(String.valueOf(medicationDispenseDetail.getMedicationId()));
coding.setDisplay(medicationDispenseDetail.getMedicationName());
CodeableConcept codeableConcept = new CodeableConcept();
codeableConcept.setCoding(Collections.singletonList(coding));
medication.setCode(codeableConcept);
medicationDispense.addDosageInstruction().setText(medicationDispenseDetail.getDosageText());
medicationDispenses.add(medicationDispense);
}
}
return medicationDispenses;
}
use of org.hl7.fhir.r4.model.CodeableConcept in project gpconnect-demonstrator by nhsconnect.
the class OrganizationResourceProvider method addAdditionalProperties.
// Adding in additional properties manually for now so we can test in the
// Test Suite
private Organization addAdditionalProperties(Organization organization) {
Coding orgTypeCode = new Coding();
orgTypeCode.setCode("dept");
orgTypeCode.setDisplay("Hospital Department");
orgTypeCode.setSystem(SystemURL.VS_CC_ORGANISATION_TYPE);
organization.addTelecom(getValidTelecom());
organization.addAddress(getValidAddress());
// organization.addContact(getValidContact());
CodeableConcept orgType = new CodeableConcept();
orgType.addCoding(orgTypeCode);
organization.addType(orgType);
organization.addExtension().setUrl(SystemURL.SD_EXTENSION_CC_MAIN_LOCATION);
return organization;
}
use of org.hl7.fhir.r4.model.CodeableConcept in project gpconnect-demonstrator by nhsconnect.
the class PatientResourceProvider method patientDetailsToMinimalPatient.
private Patient patientDetailsToMinimalPatient(PatientDetails patientDetails) throws FHIRException {
Patient patient = new Patient();
Date lastUpdated = patientDetails.getLastUpdated() == null ? new Date() : patientDetails.getLastUpdated();
String resourceId = String.valueOf(patientDetails.getId());
String versionId = String.valueOf(lastUpdated.getTime());
String resourceType = patient.getResourceType().toString();
IdType id = new IdType(resourceType, resourceId, versionId);
patient.setId(id);
patient.getMeta().setVersionId(versionId);
patient.getMeta().setLastUpdated(lastUpdated);
patient.getMeta().addProfile(SystemURL.SD_GPC_PATIENT);
Identifier patientNhsNumber = new Identifier().setSystem(SystemURL.ID_NHS_NUMBER).setValue(patientDetails.getNhsNumber());
Extension extension = createCodingExtension("01", "Number present and verified", SystemURL.CS_CC_NHS_NUMBER_VERIF, SystemURL.SD_CC_EXT_NHS_NUMBER_VERIF);
patientNhsNumber.addExtension(extension);
patient.addIdentifier(patientNhsNumber);
patient.setBirthDate(patientDetails.getDateOfBirth());
String gender = patientDetails.getGender();
if (gender != null) {
patient.setGender(AdministrativeGender.fromCode(gender.toLowerCase(Locale.UK)));
}
Date registrationEndDateTime = patientDetails.getRegistrationEndDateTime();
Date registrationStartDateTime = patientDetails.getRegistrationStartDateTime();
Extension regDetailsExtension = new Extension(SystemURL.SD_EXTENSION_CC_REG_DETAILS);
Period registrationPeriod = new Period().setStart(registrationStartDateTime).setEnd(registrationEndDateTime);
Extension regPeriodExt = new Extension(SystemURL.SD_CC_EXT_REGISTRATION_PERIOD, registrationPeriod);
regDetailsExtension.addExtension(regPeriodExt);
String registrationStatusValue = patientDetails.getRegistrationStatus();
patient.setActive(ACTIVE_REGISTRATION_STATUS.equals(registrationStatusValue) || null == registrationStatusValue);
String registrationTypeValue = patientDetails.getRegistrationType();
if (registrationTypeValue != null) {
Coding regTypeCode = new Coding();
regTypeCode.setCode(registrationTypeValue);
// Should always be Temporary
regTypeCode.setDisplay("Temporary");
regTypeCode.setSystem(SystemURL.CS_REGISTRATION_TYPE);
CodeableConcept regTypeConcept = new CodeableConcept();
regTypeConcept.addCoding(regTypeCode);
Extension regTypeExt = new Extension(SystemURL.SD_CC_EXT_REGISTRATION_TYPE, regTypeConcept);
regDetailsExtension.addExtension(regTypeExt);
}
patient.addExtension(regDetailsExtension);
String maritalStatus = patientDetails.getMaritalStatus();
if (maritalStatus != null) {
CodeableConcept marital = new CodeableConcept();
Coding maritalCoding = new Coding();
maritalCoding.setSystem(SystemURL.VS_CC_MARITAL_STATUS);
maritalCoding.setCode(patientDetails.getMaritalStatus());
maritalCoding.setDisplay("Married");
marital.addCoding(maritalCoding);
patient.setMaritalStatus(marital);
}
patient.setMultipleBirth(patientDetails.isMultipleBirth());
if (patientDetails.isDeceased()) {
DateTimeType decesed = new DateTimeType(patientDetails.getDeceased());
patient.setDeceased(decesed);
}
return patient;
}
Aggregations