3131import com .vaadin .data .converter .StringToIntegerConverter ;
3232import com .vaadin .ui .ComboBox ;
3333import com .vaadin .ui .DateField ;
34+ import com .vaadin .ui .HorizontalLayout ;
3435import com .vaadin .ui .Label ;
3536import com .vaadin .ui .TextField ;
3637import com .vaadin .v7 .data .fieldgroup .FieldGroup .CommitEvent ;
4344import de .symeda .sormas .api .i18n .Captions ;
4445import de .symeda .sormas .api .i18n .I18nProperties ;
4546import de .symeda .sormas .api .i18n .Validations ;
47+ import de .symeda .sormas .api .infrastructure .district .DistrictReferenceDto ;
48+ import de .symeda .sormas .api .infrastructure .region .RegionReferenceDto ;
4649import de .symeda .sormas .api .immunization .ImmunizationDto ;
4750import de .symeda .sormas .api .immunization .MeansOfImmunization ;
4851import de .symeda .sormas .api .utils .Diseases ;
5154import de .symeda .sormas .ui .utils .AbstractEditForm ;
5255import de .symeda .sormas .ui .utils .CssStyles ;
5356import de .symeda .sormas .ui .utils .DateFormatHelper ;
57+ import de .symeda .sormas .ui .utils .FieldHelper ;
5458import de .symeda .sormas .ui .utils .FormComponent ;
5559
5660/**
5761 * Simplified "Quick Immunization Entry" form showing only the 7 core fields (BR0070).
58- * Jurisdiction fields (responsibleRegion, responsibleDistrict) are not shown; they must be
59- * pre-set on the DTO before calling {@link #setValue(ImmunizationDto)}.
62+ * Jurisdiction fields (responsibleRegion, responsibleDistrict) stay hidden when they are already
63+ * pre-set on the DTO before calling {@link #setValue(ImmunizationDto)}. If either value is missing,
64+ * Vaadin 8 jurisdiction inputs are shown so the immunization can still be created.
6065 * <p>
6166 * Extends {@link AbstractEditForm} for compatibility with
6267 * {@link de.symeda.sormas.ui.utils.CommitDiscardWrapperComponent}.
@@ -131,6 +136,9 @@ private static final class FieldsComponent extends FormComponent<ImmunizationDto
131136
132137 private DateField reportDate ;
133138 private ComboBox <MeansOfImmunization > meansOfImmunization ;
139+ private ComboBox <RegionReferenceDto > responsibleRegion ;
140+ private ComboBox <DistrictReferenceDto > responsibleDistrict ;
141+ private HorizontalLayout jurisdictionRow ;
134142 private TextField numberOfDoses ;
135143 private DateField dateOfMostRecentDose ;
136144 private ComboBox <VaccinationInfoSource > vaccinationInfoSource ;
@@ -150,8 +158,12 @@ private static final class FieldsComponent extends FormComponent<ImmunizationDto
150158
151159 @ Override
152160 public void setDto (ImmunizationDto dto ) {
153- super .setDto (dto );
154161 refreshMeansOfImmunizationOptions (dto != null ? dto .getDisease () : null );
162+ // Populate district options before binding so a pre-set district can be displayed immediately.
163+ refreshDistrictOptions (dto != null ? dto .getResponsibleRegion () : null );
164+ super .setDto (dto );
165+ refreshDistrictOptions (responsibleRegion .getValue ());
166+ updateJurisdictionFieldsVisibility (dto );
155167 // binder.setBean() only fires value-change events when the value actually
156168 // changes. If meansOfImmunization was already null (typical for a new DTO),
157169 // no event fires and the listener never runs. Re-apply visibility and
@@ -171,6 +183,14 @@ private void buildLayout() {
171183 meansOfImmunization .setItemCaptionGenerator (I18nProperties ::getEnumCaption );
172184 addRow (reportDate , meansOfImmunization );
173185
186+ responsibleRegion = createComboBox (ImmunizationDto .RESPONSIBLE_REGION , ImmunizationDto .I18N_PREFIX );
187+ responsibleRegion .setItemCaptionGenerator (item -> item != null ? item .buildCaption () : "" );
188+ responsibleRegion .setItems (FacadeProvider .getRegionFacade ().getAllActiveByServerCountry ());
189+
190+ responsibleDistrict = createComboBox (ImmunizationDto .RESPONSIBLE_DISTRICT , ImmunizationDto .I18N_PREFIX );
191+ responsibleDistrict .setItemCaptionGenerator (item -> item != null ? item .buildCaption () : "" );
192+ jurisdictionRow = addRow (responsibleRegion , responsibleDistrict );
193+
174194 numberOfDoses = createTextField (ImmunizationDto .NUMBER_OF_DOSES , ImmunizationDto .I18N_PREFIX );
175195 addRow (numberOfDoses , null );
176196
@@ -213,6 +233,14 @@ private void bindFields() {
213233 .asRequired (I18nProperties .getValidationError (Validations .required , meansOfImmunization .getCaption ()))
214234 .bind (ImmunizationDto ::getMeansOfImmunization , ImmunizationDto ::setMeansOfImmunization );
215235
236+ binder .forField (responsibleRegion )
237+ .asRequired (I18nProperties .getValidationError (Validations .required , responsibleRegion .getCaption ()))
238+ .bind (ImmunizationDto .RESPONSIBLE_REGION );
239+
240+ binder .forField (responsibleDistrict )
241+ .asRequired (I18nProperties .getValidationError (Validations .required , responsibleDistrict .getCaption ()))
242+ .bind (ImmunizationDto .RESPONSIBLE_DISTRICT );
243+
216244 binder .forField (numberOfDoses )
217245 .withNullRepresentation ("" )
218246 .withConverter (new StringToIntegerConverter (I18nProperties .getValidationError (Validations .vaccineDosesFormat )))
@@ -243,6 +271,7 @@ private void bindFields() {
243271 }
244272
245273 private void wireEvents () {
274+ track (responsibleRegion .addValueChangeListener (e -> refreshDistrictOptions (e .getValue ())));
246275 track (meansOfImmunization .addValueChangeListener (e -> {
247276 updateVaccinationFieldsVisibility (e .getValue ());
248277 if (e .getValue () != MeansOfImmunization .MATERNAL_VACCINATION ) {
@@ -266,6 +295,19 @@ private void refreshMeansOfImmunizationOptions(Disease disease) {
266295 }
267296 }
268297
298+ private void refreshDistrictOptions (RegionReferenceDto region ) {
299+ FieldHelper .updateItems (
300+ responsibleDistrict ,
301+ region != null ? FacadeProvider .getDistrictFacade ().getAllActiveByRegion (region .getUuid ()) : null );
302+ }
303+
304+ private void updateJurisdictionFieldsVisibility (ImmunizationDto dto ) {
305+ boolean showJurisdictionFields = dto == null || dto .getResponsibleRegion () == null || dto .getResponsibleDistrict () == null ;
306+ responsibleRegion .setVisible (showJurisdictionFields );
307+ responsibleDistrict .setVisible (showJurisdictionFields );
308+ jurisdictionRow .setVisible (showJurisdictionFields );
309+ }
310+
269311 private void updateVaccinationFieldsVisibility (MeansOfImmunization meansOfImmunization ) {
270312 boolean isVaccination = MeansOfImmunization .isVaccination (meansOfImmunization );
271313 boolean isMaternalVaccination = meansOfImmunization == MeansOfImmunization .MATERNAL_VACCINATION ;
0 commit comments