createElement(context, attributes);
}
public void createElement(FormEntryContext context, Map<String, String> attributes) {
String field = attributes.get("field");
Patient existingPatient = context.getExistingPatient();
// Required attribute defaults to true if not specified
required = ! "false".equalsIgnoreCase(attributes.get("required"));
if (FIELD_PERSON_NAME.equalsIgnoreCase(field)) {
nameWidget = new NameWidget();
nameErrorWidget = new ErrorWidget();
createWidgets(context, nameWidget, nameErrorWidget,
existingPatient != null && existingPatient.getPersonName() != null ? existingPatient.getPersonName() : null);
}
else if (FIELD_GENDER.equalsIgnoreCase(field)) {
MessageSourceService msg = Context.getMessageSourceService();
genderWidget = new DropdownWidget();
genderErrorWidget = new ErrorWidget();
genderWidget.addOption(new Option(msg.getMessage("Patient.gender.male"), "M", false));
genderWidget.addOption(new Option(msg.getMessage("Patient.gender.female"), "F", false));
createWidgets(context, genderWidget, genderErrorWidget, existingPatient != null ? existingPatient.getGender() : null);
}
else if (FIELD_AGE.equalsIgnoreCase(field)) {
ageWidget = new NumberFieldWidget(0d, 200d, false);
ageErrorWidget = new ErrorWidget();
createWidgets(context, ageWidget, ageErrorWidget, existingPatient != null ? existingPatient.getAge() : null);
}
else if (FIELD_BIRTH_DATE.equalsIgnoreCase(field)) {
birthDateWidget = new DateWidget();
birthDateErrorWidget = new ErrorWidget();
createWidgets(context, birthDateWidget, birthDateErrorWidget, existingPatient != null ? existingPatient.getBirthdate() : null);
}
else if (FIELD_BIRTH_DATE_OR_AGE.equalsIgnoreCase(field)) {
ageWidget = new NumberFieldWidget(0d, 200d, false);
ageErrorWidget = new ErrorWidget();
createWidgets(context, ageWidget, ageErrorWidget, existingPatient != null ? existingPatient.getAge() : null);
birthDateWidget = new DateWidget();
birthDateErrorWidget = new ErrorWidget();
createWidgets(context, birthDateWidget, birthDateErrorWidget, existingPatient != null ? existingPatient.getBirthdate() : null);
}
else if (FIELD_IDENTIFIER.equalsIgnoreCase(field)) {
PatientIdentifierType idType = HtmlFormEntryUtil.getPatientIdentifierType(attributes.get("identifierTypeId"));
identifierTypeValueWidget = new TextFieldWidget();
identifierTypeValueErrorWidget = new ErrorWidget();
String initialValue = null;
if (existingPatient != null) {
if (idType == null) {
if (existingPatient.getPatientIdentifier() != null) {
initialValue = existingPatient.getPatientIdentifier().getIdentifier();
}
} else {
if (existingPatient.getPatientIdentifier(idType) != null) {
initialValue = existingPatient.getPatientIdentifier(idType).getIdentifier();
}
}
}
createWidgets(context, identifierTypeValueWidget, identifierTypeValueErrorWidget, initialValue);
if (idType != null) {
identifierTypeWidget = new HiddenFieldWidget();
createWidgets(context, identifierTypeWidget, null, idType.getId().toString());
}
else {
identifierTypeWidget = new DropdownWidget();
List<PatientIdentifierType> patientIdentifierTypes = HtmlFormEntryUtil.getPatientIdentifierTypes();
for (PatientIdentifierType patientIdentifierType : patientIdentifierTypes) {
((DropdownWidget) identifierTypeWidget).addOption(new Option(patientIdentifierType.getName(), patientIdentifierType
.getPatientIdentifierTypeId().toString(), false));
}
createWidgets(context, identifierTypeWidget, null,
existingPatient != null && existingPatient.getPatientIdentifier() != null ? existingPatient.getPatientIdentifier()
.getIdentifierType().getId() : null);
}
}
else if (FIELD_IDENTIFIER_LOCATION.equalsIgnoreCase(field)) {
identifierLocationWidget = new DropdownWidget();
identifierLocationErrorWidget = new ErrorWidget();
Location defaultLocation = existingPatient != null
&& existingPatient.getPatientIdentifier() != null ? existingPatient.getPatientIdentifier().getLocation() : null;
defaultLocation = defaultLocation == null ? context.getDefaultLocation() : defaultLocation;
identifierLocationWidget.setInitialValue(defaultLocation);
List<Option> locationOptions = new ArrayList<Option>();
for(Location location:Context.getLocationService().getAllLocations()) {
Option option = new Option(location.getName(), location.getId().toString(), location.equals(defaultLocation));
locationOptions.add(option);
}
Collections.sort(locationOptions, new OptionComparator());
// if initialValueIsSet=false, no initial/default location, hence this shows the 'select input' field as first option
boolean initialValueIsSet = !(defaultLocation == null);
((DropdownWidget) identifierLocationWidget).addOption(new Option(Context.getMessageSourceService().getMessage("htmlformentry.chooseALocation"), "", !initialValueIsSet));
if (!locationOptions.isEmpty()) {
for(Option option: locationOptions){
((DropdownWidget) identifierLocationWidget).addOption(option);
}
}
createWidgets(context, identifierLocationWidget, identifierLocationErrorWidget, defaultLocation);
}
else if (FIELD_ADDRESS.equalsIgnoreCase(field)) {
addressWidget = new AddressWidget();
createWidgets(context, addressWidget, null, existingPatient != null ? existingPatient.getPersonAddress() : null);
}
}