use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException in project gpconnect-demonstrator by nhsconnect.
the class PatientResourceProvider method getPatientById.
@Read(version = true)
public Patient getPatientById(@IdParam IdType internalId) throws FHIRException {
PatientDetails patientDetails = patientSearch.findPatientByInternalID(internalId.getIdPart());
if (patientDetails == null || patientDetails.isSensitive() || patientDetails.isDeceased() || !patientDetails.isActive()) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No patient details found for patient ID: " + internalId.getIdPart()), SystemCode.PATIENT_NOT_FOUND, IssueType.NOTFOUND);
}
Patient patient = IdentifierValidator.versionComparison(internalId, patientDetailsToPatientResourceConverter(patientDetails));
if (null != patient) {
addPreferredBranchSurgeryExtension(patient);
}
return patient;
}
use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException in project gpconnect-demonstrator by nhsconnect.
the class PatientResourceProvider method StructuredRecordOperation.
@Operation(name = GET_STRUCTURED_RECORD_OPERATION_NAME)
public Bundle StructuredRecordOperation(@ResourceParam Parameters params) throws FHIRException {
Bundle structuredBundle = new Bundle();
Boolean getAllergies = false;
Boolean includeResolved = false;
Boolean getMedications = false;
Boolean includePrescriptionIssues = false;
Period medicationPeriod = null;
String NHS = getNhsNumber(params);
PatientDetails patientDetails = patientSearch.findPatient(NHS);
// see https://nhsconnect.github.io/gpconnect/accessrecord_structured_development_retrieve_patient_record.html#error-handling
if (patientDetails == null || patientDetails.isSensitive() || patientDetails.isDeceased() || !patientDetails.isActive()) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No patient details found for patient ID: " + NHS), SystemCode.PATIENT_NOT_FOUND, IssueType.NOTFOUND);
}
if (NHS.equals(patientNoconsent)) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ForbiddenOperationException("No patient consent to share for patient ID: " + NHS), SystemCode.NO_PATIENT_CONSENT, IssueType.FORBIDDEN);
}
operationOutcome = null;
for (ParametersParameterComponent param : params.getParameter()) {
if (validateParametersName(param.getName())) {
if (param.getName().equals(SystemConstants.INCLUDE_ALLERGIES)) {
getAllergies = true;
if (param.getPart().isEmpty()) {
// addWarningIssue(param, IssueType.REQUIRED, "Miss parameter part : " + SystemConstants.INCLUDE_RESOLVED_ALLERGIES);
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Miss parameter : " + SystemConstants.INCLUDE_RESOLVED_ALLERGIES), SystemCode.INVALID_PARAMETER, IssueType.REQUIRED);
}
boolean includeResolvedParameterPartPresent = false;
for (ParametersParameterComponent paramPart : param.getPart()) {
if (paramPart.getName().equals(SystemConstants.INCLUDE_RESOLVED_ALLERGIES)) {
if (paramPart.getValue() instanceof BooleanType) {
includeResolved = Boolean.valueOf(paramPart.getValue().primitiveValue());
includeResolvedParameterPartPresent = true;
} else {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Miss parameter : " + SystemConstants.INCLUDE_RESOLVED_ALLERGIES), SystemCode.INVALID_PARAMETER, IssueType.REQUIRED);
}
} else {
addWarningIssue(param, paramPart, IssueType.NOTSUPPORTED);
// throw OperationOutcomeFactory.buildOperationOutcomeException(
// new UnprocessableEntityException("Incorrect parameter passed : " + paramPart.getName()),
// SystemCode.INVALID_PARAMETER, IssueType.INVALID);
}
}
if (!includeResolvedParameterPartPresent) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Miss parameter : " + SystemConstants.INCLUDE_RESOLVED_ALLERGIES), SystemCode.INVALID_PARAMETER, IssueType.REQUIRED);
}
}
if (param.getName().equals(SystemConstants.INCLUDE_MEDICATION)) {
getMedications = true;
boolean isIncludedPrescriptionIssuesExist = false;
for (ParametersParameterComponent paramPart : param.getPart()) {
if (paramPart.getName().equals(SystemConstants.INCLUDE_PRESCRIPTION_ISSUES)) {
if (paramPart.getValue() instanceof BooleanType) {
includePrescriptionIssues = Boolean.valueOf(paramPart.getValue().primitiveValue());
isIncludedPrescriptionIssuesExist = true;
}
} else if (paramPart.getName().equals(SystemConstants.MEDICATION_SEARCH_FROM_DATE) && paramPart.getValue() instanceof DateType) {
DateType startDateDt = (DateType) paramPart.getValue();
medicationPeriod = new Period();
medicationPeriod.setStart(startDateDt.getValue());
medicationPeriod.setEnd(null);
String startDate = startDateDt.asStringValue();
if (!validateStartDateParamAndEndDateParam(startDate, null)) {
// addWarningIssue(param, paramPart, IssueType.INVALID, "Invalid date used");
}
} else {
addWarningIssue(param, paramPart, IssueType.NOTSUPPORTED);
// throw OperationOutcomeFactory.buildOperationOutcomeException(
// new UnprocessableEntityException("Incorrect parameter passed : " + paramPart.getName()),
// SystemCode.INVALID_PARAMETER, IssueType.INVALID);
}
}
if (!isIncludedPrescriptionIssuesExist) {
// # 1.2.6 now defaults to true if not provided
includePrescriptionIssues = true;
}
}
} else {
// invalid parameter
addWarningIssue(param, IssueType.NOTSUPPORTED);
}
}
// for parameter
// Add Patient
Patient patient = patientDetailsToPatientResourceConverter(patientDetails);
if (patient.getIdentifierFirstRep().getValue().equals(NHS)) {
structuredBundle.addEntry().setResource(patient);
}
// Organization from patient
Set<String> orgIds = new HashSet<>();
orgIds.add(patientDetails.getManagingOrganization());
// Practitioner from patient
Set<String> practitionerIds = new HashSet<>();
List<Reference> practitionerReferenceList = patient.getGeneralPractitioner();
practitionerReferenceList.forEach(practitionerReference -> {
String[] pracRef = practitionerReference.getReference().split("/");
if (pracRef.length > 1) {
practitionerIds.add(pracRef[1]);
}
});
if (getAllergies) {
structuredBundle = structuredAllergyIntoleranceBuilder.buildStructuredAllergyIntolerence(NHS, practitionerIds, structuredBundle, includeResolved);
}
if (getMedications) {
structuredBundle = populateMedicationBundle.addMedicationBundleEntries(structuredBundle, patientDetails, includePrescriptionIssues, medicationPeriod, practitionerIds, orgIds);
}
// Add all practitioners and practitioner roles
for (String practitionerId : practitionerIds) {
Practitioner pracResource = practitionerResourceProvider.getPractitionerById(new IdType(practitionerId));
structuredBundle.addEntry().setResource(pracResource);
List<PractitionerRole> practitionerRoleList = practitionerRoleResourceProvider.getPractitionerRoleByPracticionerId(new IdType(practitionerId));
for (PractitionerRole role : practitionerRoleList) {
String[] split = role.getOrganization().getReference().split("/");
orgIds.add(split[1]);
structuredBundle.addEntry().setResource(role);
}
}
// Add all organizations
for (String orgId : orgIds) {
OrganizationDetails organizationDetails = organizationSearch.findOrganizationDetails(new Long(orgId));
Organization organization = organizationResourceProvider.convertOrganizationDetailsToOrganization(organizationDetails);
structuredBundle.addEntry().setResource(organization);
}
structuredBundle.setType(BundleType.COLLECTION);
structuredBundle.getMeta().addProfile(SystemURL.SD_GPC_STRUCTURED_BUNDLE);
if (operationOutcome != null) {
structuredBundle.addEntry().setResource(operationOutcome);
} else {
removeDuplicateResources(structuredBundle);
}
return structuredBundle;
}
use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException in project gpconnect-demonstrator by nhsconnect.
the class PatientJwtValidator method getNhsNumber.
private String getNhsNumber(RequestDetails requestDetails) {
ResourceBinding patientResourceBinding = getPatientResourceBinding(requestDetails.getServer());
if (patientResourceBinding != null) {
BaseMethodBinding<?> methodBinding = patientResourceBinding.getMethod(requestDetails);
// the request may not be for the patient resource in which case
// we would not expect a method binding
Object parameterValue = null;
if (methodBinding != null) {
for (IParameter parameter : methodBinding.getParameters()) {
parameterValue = parameter.translateQueryParametersIntoServerArgument(requestDetails, methodBinding);
// the identifier may have been passed in the URL
if (parameterValue == null) {
parameterValue = ParameterUtil.convertIdToType(requestDetails.getId(), IdDt.class);
}
if (parameterValue != null) {
String nhsNumber = patientResourceProvider.getNhsNumber(parameterValue);
if (null != nhsNumber) {
return nhsNumber;
}
}
}
}
if (parameterValue != null) {
if (RequestTypeEnum.GET == requestDetails.getRequestType()) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No patient details found for patient ID: " + parameterValue), SystemCode.PATIENT_NOT_FOUND, IssueType.INVALID);
}
// Otherwise, there should have been an identifier header
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("No NHS number submitted: " + parameterValue), SystemCode.INVALID_NHS_NUMBER, IssueType.INVALID);
}
}
return null;
}
use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException in project gpconnect-demonstrator by nhsconnect.
the class LocationResourceProvider method getLocationById.
@Read(version = true)
public Location getLocationById(@IdParam IdType locationId) {
LocationDetails locationDetails = locationSearch.findLocationById(locationId.getIdPart());
if (locationDetails == null) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new ResourceNotFoundException("No location details found for location ID: " + locationId.getIdPart()), SystemCode.REFERENCE_NOT_FOUND, IssueType.INCOMPLETE);
}
Location location = locationDetailsToLocation(locationDetails);
return location;
}
use of ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException in project gpconnect-demonstrator by nhsconnect.
the class PopulateSlotBundle method populateBundle.
/**
* @param bundle Bundle resource to populate
* @param operationOutcome
* @param planningHorizonStart Date
* @param planningHorizonEnd Date
* @param actorPractitioner boolean
* @param actorLocation boolean
* @param managingOrganisation boolean
* @param bookingOdsCode String
* @param bookingOrgType String eg "urgent-care"
*/
public void populateBundle(Bundle bundle, OperationOutcome operationOutcome, Date planningHorizonStart, Date planningHorizonEnd, boolean actorPractitioner, boolean actorLocation, boolean managingOrganisation, String bookingOdsCode, String bookingOrgType) {
bundle.getMeta().addProfile(SystemURL.SD_GPC_SRCHSET_BUNDLE);
// TODO remove hard coding pick up from providerRouting.json ?
final String OUR_ODS_CODE = "A20047";
// find all locations for this ODS practice code and construct Resources for them
// #144 generalise to handle 1..n locations for a practice
HashMap<String, BundleEntryComponent> locationEntries = new HashMap<>();
for (LocationDetails aLocationDetail : locationSearch.findAllLocations()) {
if (aLocationDetail.getOrgOdsCode().equals(OUR_ODS_CODE)) {
Location aLocationResource = locationResourceProvider.getLocationById(new IdType(aLocationDetail.getId()));
BundleEntryComponent locationEntry = new BundleEntryComponent();
locationEntry.setResource(aLocationResource);
// #202 use full urls
// #215 full url removed completely
// locationEntry.setFullUrl(serverBaseUrl + "Location/" + aLocationResource.getIdElement().getIdPart());
locationEntries.put(aLocationResource.getIdElement().getIdPart(), locationEntry);
}
}
// find the provider organization from the ods code
List<OrganizationDetails> ourOrganizationsDetails = organizationSearch.findOrganizationDetailsByOrgODSCode(OUR_ODS_CODE);
OrganizationDetails ourOrganizationDetails = null;
if (!ourOrganizationsDetails.isEmpty()) {
// at 1.2.2 these are added regardless of the status of the relevant parameter
// Just picks the first organization. There should only be one.
ourOrganizationDetails = ourOrganizationsDetails.get(0);
} else {
// do something here .. should never happen
}
// find the bookng organization from the ods code
List<OrganizationDetails> bookingOrganizationsDetails = organizationSearch.findOrganizationDetailsByOrgODSCode(bookingOdsCode);
OrganizationDetails bookingOrganizationDetails = null;
if (!bookingOrganizationsDetails.isEmpty()) {
// at 1.2.2 these are added regardless of the status of the relevant parameter
// Just picks the first organization. There should only be one.
bookingOrganizationDetails = bookingOrganizationsDetails.get(0);
}
HashSet<BundleEntryComponent> addedSchedule = new HashSet<>();
HashSet<BundleEntryComponent> addedLocation = new HashSet<>();
HashSet<String> addedPractitioner = new HashSet<>();
HashSet<String> addedOrganization = new HashSet<>();
// issue #165 don't add duplicate slots, hashSet contains slot id as String
HashSet<String> addedSlot = new HashSet<>();
// #144 process all locations
for (String locationId : locationEntries.keySet()) {
// process the schedules
for (Schedule schedule : scheduleResourceProvider.getSchedulesForLocationId(locationId, planningHorizonStart, planningHorizonEnd)) {
boolean slotsAdded = false;
schedule.getMeta().addProfile(SystemURL.SD_GPC_SCHEDULE);
BundleEntryComponent scheduleEntry = new BundleEntryComponent();
scheduleEntry.setResource(schedule);
// #202 use full urls
// #215 full url removed completely
// scheduleEntry.setFullUrl(serverBaseUrl + "Schedule/" + schedule.getIdElement().getIdPart());
// This Set does not work as expected because Slot does not implement hashCode
// so the second set call to getSlots returns different objects with the same slot id
Set<Slot> slots = new HashSet<>();
//
if (bookingOrgType.isEmpty() && bookingOdsCode.isEmpty()) {
// OPTION 1 get slots Specfying neither org type nor org code
slots.addAll(slotResourceProvider.getSlotsForScheduleIdNoOrganizationTypeOrODS(schedule.getIdElement().getIdPart(), planningHorizonStart, planningHorizonEnd));
} else if (!bookingOrgType.isEmpty() && bookingOdsCode.isEmpty()) {
// OPTION 2 organisation type only
for (Slot slot : slotResourceProvider.getSlotsForScheduleId(schedule.getIdElement().getIdPart(), planningHorizonStart, planningHorizonEnd)) {
SlotDetail slotDetail = slotSearch.findSlotByID(Long.parseLong(slot.getId()));
if (slotDetail.getOrganizationIds().isEmpty() && (slotDetail.getOrganizationTypes().isEmpty() || slotDetail.getOrganizationTypes().contains(bookingOrgType))) {
slots.add(slot);
}
}
} else if (!bookingOdsCode.isEmpty() && bookingOrgType.isEmpty()) {
// OPTION 3 org code only
for (Slot slot : slotResourceProvider.getSlotsForScheduleId(schedule.getIdElement().getIdPart(), planningHorizonStart, planningHorizonEnd)) {
SlotDetail slotDetail = slotSearch.findSlotByID(Long.parseLong(slot.getId()));
if (slotDetail.getOrganizationTypes().isEmpty() && (slotDetail.getOrganizationIds().isEmpty() || bookingOrganizationDetails != null && slotDetail.getOrganizationIds().contains(bookingOrganizationDetails.getId()))) {
slots.add(slot);
}
}
} else if (!bookingOrgType.isEmpty() && !bookingOdsCode.isEmpty()) {
// OPTION 4 both org code and org type
for (Slot slot : slotResourceProvider.getSlotsForScheduleId(schedule.getIdElement().getIdPart(), planningHorizonStart, planningHorizonEnd)) {
SlotDetail slotDetail = slotSearch.findSlotByID(Long.parseLong(slot.getId()));
if (((slotDetail.getOrganizationTypes().isEmpty() || slotDetail.getOrganizationTypes().contains(bookingOrgType))) && (slotDetail.getOrganizationIds().isEmpty() || bookingOrganizationDetails != null && slotDetail.getOrganizationIds().contains(bookingOrganizationDetails.getId()))) {
slots.add(slot);
}
}
}
// added at 1.2.2 add the organisation but only if there are some slots available
if (slots.size() > 0 && ourOrganizationDetails != null && !addedOrganization.contains(ourOrganizationDetails.getOrgCode())) {
addOrganisation(ourOrganizationDetails, bundle);
addedOrganization.add(ourOrganizationDetails.getOrgCode());
}
String freeBusyType = "FREE";
// process all the slots to be returned
for (Slot slot : slots) {
if (freeBusyType.equalsIgnoreCase(slot.getStatus().toString())) {
String slotId = slot.getIdElement().getIdPart();
if (!addedSlot.contains(slotId)) {
BundleEntryComponent slotEntry = new BundleEntryComponent();
slotEntry.setResource(slot);
// #202 use full urls
// #215 full url removed completely
// slotEntry.setFullUrl(serverBaseUrl + "Slot/" + slotId);
bundle.addEntry(slotEntry);
addedSlot.add(slotId);
slotsAdded = true;
}
if (!addedSchedule.contains(scheduleEntry)) {
// only add a schedule if there's a reference to it and only add it once
bundle.addEntry(scheduleEntry);
addedSchedule.add(scheduleEntry);
}
if (actorLocation == true) {
// only add a unique location once
if (!addedLocation.contains(locationEntries.get(locationId))) {
bundle.addEntry(locationEntries.get(locationId));
addedLocation.add(locationEntries.get(locationId));
}
}
}
// if free/busy status matches
}
// # 193 for each schedule only add a practitioner if there have been some slots added.
if (slotsAdded) {
// practitioners for this schedule
List<Reference> practitionerActors = scheduleResourceProvider.getPractitionerReferences(schedule);
if (!practitionerActors.isEmpty()) {
for (Reference practitionerActor : practitionerActors) {
Practitioner practitioner = practitionerResourceProvider.getPractitionerById((IdType) practitionerActor.getReferenceElement());
if (practitioner == null) {
Coding errorCoding = new Coding().setSystem(SystemURL.VS_GPC_ERROR_WARNING_CODE).setCode(SystemCode.REFERENCE_NOT_FOUND);
CodeableConcept errorCodableConcept = new CodeableConcept().addCoding(errorCoding);
errorCodableConcept.setText("Invalid Reference");
operationOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setCode(IssueType.NOTFOUND).setDetails(errorCodableConcept);
throw new ResourceNotFoundException("Practitioner Reference returning null");
}
if (actorPractitioner) {
if (!addedPractitioner.contains(practitioner.getIdElement().getIdPart())) {
addPractitioner(practitioner, bundle);
addedPractitioner.add(practitioner.getIdElement().getIdPart());
}
}
}
// for practitioner
}
// if non empty practitioner list
}
// if slots added
}
// for schedules
}
// for location
}
Aggregations