/** * @see org.openmrs.module.htmlformentry.widget.Widget#generateHtml(org.openmrs.module.htmlformentry.FormEntryContext) */ @Override public String generateHtml(FormEntryContext context) { if (context.getMode() == Mode.VIEW) { if (initialValue != null) return WidgetFactory.displayValue(initialValue.getName()); else return ""; } Collections.sort(providers, new ProviderByPersonNameComparator()); StringBuilder sb = new StringBuilder(); sb.append("<select name=\"" + context.getFieldName(this) + "\">"); sb.append("\n<option value=\"\">"); sb.append(Context.getMessageSourceService().getMessage("htmlformentry.chooseAProvider")); sb.append("</option>"); for (Provider provider : providers) { sb.append("\n<option "); if (initialValue != null && initialValue.equals(provider)) sb.append("selected=\"true\" "); sb.append("value=\"" + provider.getId() + "\">").append(provider.getPerson() != null ? HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(provider.getPerson().getPersonName()) : provider.getName()) .append("</option>"); } sb.append("</select>"); return sb.toString(); }
@Test public void getFullNameWithFamilyNameFirst_shouldNotFailIfProviderHasNoName() { PersonName name = new PersonName(); assertThat(HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(name), is("")); }
@Test public void getFullNameWithFamilyNameFirst_shouldNotFailIfProviderDoesNotHaveFamilyName() { PersonName name = new PersonName(); name.setGivenName("Mark"); assertThat(HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(name), is("Mark")); }
@Test public void getFullNameWithFamilyNameFirst_shouldReturnDeletedIfPersonNameIsNull() { assertThat(HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(null), is("[" + Context.getMessageSourceService().getMessage("htmlformentry.unknownProviderName") + "]")); }
@Test public void getFullNameWithFamilyNameFirst_shouldReturnProperSimpleName() { PersonName name = new PersonName(); name.setGivenName("Mark"); name.setFamilyName("Goodrich"); assertThat(HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(name), is("Goodrich, Mark")); }
@Test public void getFullNameWithFamilyNameFirst_shouldReturnProperFullName() { PersonName name = new PersonName(); name.setPrefix("Mr."); name.setGivenName("Mark"); name.setMiddleName("Brutus"); name.setFamilyNamePrefix("de"); name.setFamilyName("Cameroon"); name.setFamilyName2("Smith"); name.setFamilyNameSuffix("Jr."); name.setDegree("Esq."); assertThat(HtmlFormEntryUtil.getFullNameWithFamilyNameFirst(name), is("de Cameroon Smith Jr., Mr. Mark Brutus Esq.")); }