}
@Override
public void handleSubmission(FormEntrySession session, HttpServletRequest request) {
Patient patient = (Patient) session.getSubmissionActions().getCurrentPerson();
if (patient == null) {
throw new RuntimeException("Programming exception: person shouldn't be null");
}
FormEntryContext context = session.getContext();
if (nameWidget != null) {
PersonName name = (PersonName) nameWidget.getValue(context, request);
if (patient != null) {
if (!name.isPreferred()) {
PersonName currentPreferredName = context.getExistingPatient().getPersonName();
if (currentPreferredName != null){
currentPreferredName.setPreferred(false);
currentPreferredName.setVoided(true);
}
}
}
// HACK: we need to set the date created and uuid here as a hack around a hibernate flushing issue (see saving the Patient in FormEntrySession applyActions())
if (name.getDateCreated() == null) {
name.setDateCreated(new Date());
}
if (name.getUuid() == null) {
name.setUuid(UUID.randomUUID().toString());
}
name.setPreferred(true);
patient.addName(name);
}
if (genderWidget != null) {
String value = (String) genderWidget.getValue(context, request);
patient.setGender(value);
}
if (ageWidget != null) {
Double value = (Double) ageWidget.getValue(context, request);
if (value != null)
calculateBirthDate(patient, null, value);
}
if (birthDateWidget != null) {
Date value = (Date) birthDateWidget.getValue(context, request);
if (value != null) {
calculateBirthDate(patient, value, null);
}
}
if (identifierTypeValueWidget != null && identifierTypeWidget != null) {
String identifier = (String) identifierTypeValueWidget.getValue(context, request);
PatientIdentifierType identifierType = getIdentifierType((String) identifierTypeWidget.getValue(context, request));
// Look for an existing identifier of this type
PatientIdentifier patientIdentifier = patient.getPatientIdentifier(identifierType);
if (StringUtils.hasText(identifier)) {
// No existing identifier of this type, so create new
if (patientIdentifier == null) {
patientIdentifier = new PatientIdentifier();
patientIdentifier.setIdentifierType(identifierType);
// HACK: we need to set the date created and uuid here as a hack around a hibernate flushing issue (see saving the Patient in FormEntrySession applyActions())
patientIdentifier.setDateChanged(new Date());
patientIdentifier.setUuid(UUID.randomUUID().toString());
// For 1.9+ onwards patients require a preferred identifier
if (patient.getPatientId() == null) {
patientIdentifier.setPreferred(true);
}
patient.addIdentifier(patientIdentifier);
}
if (!identifier.equals(patientIdentifier.getIdentifier()) || !identifierType.equals(patientIdentifier.getIdentifierType())) {
validateIdentifier(identifierType.getId(), identifier);
}
patientIdentifier.setIdentifier(identifier);
}
else if (patientIdentifier != null) {
// If this field is not required, then we interpret a blank value as a request to avoid any existing identifier
session.getSubmissionActions().getIdentifiersToVoid().add(patientIdentifier);
}
}
//
// TODO current behavior always updates location of the preferred identifier rather than
// a specific identifier type being edited by the identifier widget. But identifier location
// widget isn't aware of the identifier type widget
//
if (identifierLocationWidget != null) {
PatientIdentifier patientIdentifier = patient.getPatientIdentifier();
if (patientIdentifier == null) {
patientIdentifier = new PatientIdentifier();
patient.addIdentifier(patientIdentifier);
}
Object locationString = identifierLocationWidget.getValue(context, request);
Location location = (Location) HtmlFormEntryUtil.convertToType(locationString.toString().trim(), Location.class);
patientIdentifier.setLocation(location);
patientIdentifier.setPreferred(true);
}
if (addressWidget != null) {
PersonAddress personAddress = (PersonAddress) addressWidget.getValue(context, request);
if (context.getMode() == Mode.EDIT) {
if (!personAddress.isPreferred()) {
PersonAddress currentPreferredAddress = context.getExistingPatient().getPersonAddress();
currentPreferredAddress.setPreferred(false);
currentPreferredAddress.setVoided(true);
}
}
personAddress.setPreferred(true);
patient.addAddress(personAddress);
}
session.getSubmissionActions().setPatientUpdateRequired(true);
}