use of ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException in project gpconnect-demonstrator by nhsconnect.
the class AppointmentResourceProvider method updateAppointment.
@Update
public MethodOutcome updateAppointment(@IdParam IdType appointmentId, @ResourceParam Appointment appointment) {
MethodOutcome methodOutcome = new MethodOutcome();
OperationOutcome operationalOutcome = new OperationOutcome();
AppointmentDetail appointmentDetail = appointmentResourceConverterToAppointmentDetail(appointment);
// URL ID and Resource ID must be the same
if (!Objects.equals(appointmentId.getIdPartAsLong(), appointmentDetail.getId())) {
operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("Id in URL (" + appointmentId.getIdPart() + ") should match Id in Resource (" + appointmentDetail.getId() + ")");
methodOutcome.setOperationOutcome(operationalOutcome);
return methodOutcome;
}
// Make sure there is an existing appointment to be amended
AppointmentDetail oldAppointmentDetail = appointmentSearch.findAppointmentByID(appointmentId.getIdPartAsLong());
if (oldAppointmentDetail == null) {
operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("No appointment details found for ID: " + appointmentId.getIdPart());
methodOutcome.setOperationOutcome(operationalOutcome);
return methodOutcome;
}
String oldAppointmentVersionId = String.valueOf(oldAppointmentDetail.getLastUpdated().getTime());
String newAppointmentVersionId = appointmentId.getVersionIdPart();
if (newAppointmentVersionId != null && !newAppointmentVersionId.equalsIgnoreCase(oldAppointmentVersionId)) {
throw new ResourceVersionConflictException("The specified version (" + newAppointmentVersionId + ") did not match the current resource version (" + oldAppointmentVersionId + ")");
}
// Determin if it is a cancel or an amend
if (appointmentDetail.getCancellationReason() != null) {
if (appointmentDetail.getCancellationReason().isEmpty()) {
operationalOutcome.addIssue().setSeverity(IssueSeverity.ERROR).setDiagnostics("The cancellation reason can not be blank");
methodOutcome.setOperationOutcome(operationalOutcome);
return methodOutcome;
}
// This is a Cancellation - so copy across fields which can be
// altered
boolean cancelComparisonResult = compareAppointmentsForInvalidPropertyCancel(oldAppointmentDetail, appointmentDetail);
if (cancelComparisonResult) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(400, "Invalid Appointment property has been amended (cancellation)"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
}
oldAppointmentDetail.setCancellationReason(appointmentDetail.getCancellationReason());
String oldStatus = oldAppointmentDetail.getStatus();
appointmentDetail = oldAppointmentDetail;
appointmentDetail.setStatus("cancelled");
if (!"cancelled".equalsIgnoreCase(oldStatus)) {
for (Long slotId : appointmentDetail.getSlotIds()) {
SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
// slotDetail.setAppointmentId(null);
slotDetail.setFreeBusyType("FREE");
slotDetail.setLastUpdated(new Date());
slotStore.saveSlot(slotDetail);
}
}
} else {
if (appointment.getStatus().equals("cancelled")) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(400, "Appointment has been cancelled and cannot be amended"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
}
boolean amendComparisonResult = compareAppointmentsForInvalidPropertyAmend(appointmentDetail, oldAppointmentDetail);
if (amendComparisonResult) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(403, "Invalid Appointment property has been amended"), SystemCode.BAD_REQUEST, IssueType.FORBIDDEN);
}
// This is an Amend
oldAppointmentDetail.setComment(appointmentDetail.getComment());
oldAppointmentDetail.setReasonCode(appointmentDetail.getReasonCode());
oldAppointmentDetail.setDescription(appointmentDetail.getDescription());
oldAppointmentDetail.setReasonDisplay(appointmentDetail.getReasonDisplay());
oldAppointmentDetail.setTypeCode(appointmentDetail.getTypeCode());
oldAppointmentDetail.setTypeDisplay(appointmentDetail.getTypeDisplay());
appointmentDetail = oldAppointmentDetail;
}
List<SlotDetail> slots = new ArrayList<>();
for (Long slotId : appointmentDetail.getSlotIds()) {
SlotDetail slotDetail = slotSearch.findSlotByID(slotId);
if (slotDetail == null) {
throw new UnprocessableEntityException("Slot resource reference is not a valid resource");
}
slots.add(slotDetail);
}
// Update version and
appointmentDetail.setLastUpdated(new Date());
// lastUpdated timestamp
appointmentDetail = appointmentStore.saveAppointment(appointmentDetail, slots);
methodOutcome.setId(new IdDt("Appointment", appointmentDetail.getId()));
methodOutcome.setResource(appointmentDetailToAppointmentResourceConverter(appointmentDetail));
return methodOutcome;
}
use of ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException in project gpconnect-demonstrator by nhsconnect.
the class AppointmentValidation method validateParticipantStatus.
public void validateParticipantStatus(ParticipationStatus participationStatus, Enumeration<ParticipationStatus> enumeration, Enumeration<ParticipationStatus> enumeration2) {
Boolean validStatus = false;
String participantStatusErr = "is a requirement but is missing.";
if (participationStatus != null) {
participantStatusErr = String.format("%s is not a valid ParticipationStatus code", participationStatus);
EnumSet<ParticipationStatus> statusList = EnumSet.allOf(ParticipationStatus.class);
validStatus = statusList.contains(enumeration.getValue());
}
if (!validStatus) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException(String.format("Appointment Participant %s Status %s", enumeration2, participantStatusErr)), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
}
}
use of ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException in project gpconnect-demonstrator by nhsconnect.
the class WebTokenValidator method verifyRequestedResourceValues.
private static void verifyRequestedResourceValues(WebToken webToken) {
// Checking the reason for request is directcare
if (!"directcare".equals(webToken.getReasonForRequest())) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("Reason for request is not directcare"), SystemCode.BAD_REQUEST, IssueType.INVALID);
}
RequestingDevice requestingDevice = webToken.getRequestingDevice();
if (null == requestingDevice) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException("No requesting_device"), SystemCode.BAD_REQUEST, IssueType.INVALID);
}
String deviceType = requestingDevice.getResourceType();
String organizationType = webToken.getRequestingOrganization().getResourceType();
String practitionerType = webToken.getRequestingPractitioner().getResourceType();
if (!deviceType.equals("Device") || !organizationType.equals("Organization") || !practitionerType.equals("Practitioner")) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Invalid resource type"), SystemCode.BAD_REQUEST, IssueType.INVALID);
}
}
use of ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException in project gpconnect-demonstrator by nhsconnect.
the class PatientResourceProvider method checkValidExtensions.
private void checkValidExtensions(List<Extension> undeclaredExtensions) {
List<String> extensionURLs = undeclaredExtensions.stream().map(Extension::getUrl).collect(Collectors.toList());
extensionURLs.remove(SystemURL.SD_EXTENSION_CC_REG_DETAILS);
extensionURLs.remove(SystemURL.SD_CC_EXT_ETHNIC_CATEGORY);
extensionURLs.remove(SystemURL.SD_CC_EXT_RELIGIOUS_AFFILI);
extensionURLs.remove(SystemURL.SD_PATIENT_CADAVERIC_DON);
extensionURLs.remove(SystemURL.SD_CC_EXT_RESIDENTIAL_STATUS);
extensionURLs.remove(SystemURL.SD_CC_EXT_TREATMENT_CAT);
extensionURLs.remove(SystemURL.SD_CC_EXT_NHS_COMMUNICATION);
if (!extensionURLs.isEmpty()) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Invalid/multiple patient extensions found. The following are in excess or invalid: " + extensionURLs.stream().collect(Collectors.joining(", "))), SystemCode.INVALID_RESOURCE, IssueType.INVALID);
}
}
use of ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException in project gpconnect-demonstrator by nhsconnect.
the class PatientResourceProvider method registerPatient.
@Operation(name = REGISTER_PATIENT_OPERATION_NAME)
public Bundle registerPatient(@ResourceParam Parameters params) {
Patient registeredPatient = null;
validateParameterNames(params, registerPatientParams);
Patient unregisteredPatient = params.getParameter().stream().filter(param -> "registerPatient".equalsIgnoreCase(param.getName())).map(ParametersParameterComponent::getResource).map(Patient.class::cast).findFirst().orElse(null);
String nnn = nhsNumber.fromPatientResource(unregisteredPatient);
// if its patient 14 spoof not on PDS and return the required error
if (nnn.equals(patientNotOnSpine)) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) not present on PDS", nnn)), SystemCode.INVALID_PATIENT_DEMOGRAPHICS, IssueType.INVALID);
} else if (nnn.equals(patientSuperseded)) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) is superseded", nnn)), SystemCode.INVALID_NHS_NUMBER, IssueType.INVALID);
}
if (unregisteredPatient != null) {
validatePatient(unregisteredPatient);
// check if the patient already exists
PatientDetails patientDetails = patientSearch.findPatient(nhsNumber.fromPatientResource(unregisteredPatient));
if (patientDetails == null || IsInactiveTemporaryPatient(patientDetails)) {
if (patientDetails == null) {
patientDetails = registerPatientResourceConverterToPatientDetail(unregisteredPatient);
patientStore.create(patientDetails);
} else {
// reactivate inactive non temporary patient
patientDetails.setRegistrationStatus(ACTIVE_REGISTRATION_STATUS);
updateAddressAndTelecom(unregisteredPatient, patientDetails);
patientStore.update(patientDetails);
}
try {
registeredPatient = patientDetailsToRegisterPatientResourceConverter(patientSearch.findPatient(unregisteredPatient.getIdentifierFirstRep().getValue()));
addPreferredBranchSurgeryExtension(registeredPatient);
} catch (FHIRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (patientDetails.isDeceased() || patientDetails.isSensitive()) {
throw OperationOutcomeFactory.buildOperationOutcomeException(new InvalidRequestException(String.format("Patient (NHS number - %s) has invalid demographics", nnn)), SystemCode.INVALID_PATIENT_DEMOGRAPHICS, IssueType.INVALID);
} else {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnclassifiedServerFailureException(409, String.format("Patient (NHS number - %s) already exists", nnn)), SystemCode.DUPLICATE_REJECTED, IssueType.INVALID);
}
} else {
throw OperationOutcomeFactory.buildOperationOutcomeException(new UnprocessableEntityException("Patient record not found"), SystemCode.INVALID_PARAMETER, IssueType.INVALID);
}
Bundle bundle = new Bundle().setType(BundleType.SEARCHSET);
bundle.getMeta().addProfile(SystemURL.SD_GPC_SRCHSET_BUNDLE);
bundle.addEntry().setResource(registeredPatient);
return bundle;
}
Aggregations