}
}
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) {