3838import java .util .concurrent .atomic .AtomicBoolean ;
3939import java .util .stream .Collectors ;
4040
41- import com .vaadin .v7 .ui .CheckBox ;
42- import de .symeda .sormas .ui .utils .LayoutUtil ;
4341import org .apache .commons .lang3 .StringUtils ;
4442
4543import com .vaadin .shared .ui .ErrorLevel ;
5048import com .vaadin .v7 .data .Property ;
5149import com .vaadin .v7 .ui .AbstractSelect ;
5250import com .vaadin .v7 .ui .AbstractSelect .ItemCaptionMode ;
51+ import com .vaadin .v7 .ui .CheckBox ;
5352import com .vaadin .v7 .ui .ComboBox ;
5453import com .vaadin .v7 .ui .DateField ;
5554import com .vaadin .v7 .ui .Field ;
9594import de .symeda .sormas .ui .utils .DateComparisonValidator ;
9695import de .symeda .sormas .ui .utils .FieldAccessHelper ;
9796import de .symeda .sormas .ui .utils .FieldHelper ;
97+ import de .symeda .sormas .ui .utils .LayoutUtil ;
9898import de .symeda .sormas .ui .utils .OutbreakFieldVisibilityChecker ;
9999import de .symeda .sormas .ui .utils .ResizableTextAreaWrapper ;
100100import de .symeda .sormas .ui .utils .SormasFieldGroupFieldFactory ;
@@ -405,14 +405,17 @@ protected void addFields() {
405405 entryDateDF .setVisible (false );
406406 // Entry date is only required for foreigners in Luxembourg with the TB+IMI+IPI diseases only.
407407 if (isConfiguredServer (CountryHelper .COUNTRY_CODE_LUXEMBOURG )) {
408- boolean isEntryDateAllowedDisease = Arrays .asList (Disease .INVASIVE_PNEUMOCOCCAL_INFECTION , Disease .INVASIVE_MENINGOCOCCAL_INFECTION , Disease .TUBERCULOSIS ).contains (disease );
408+ boolean isEntryDateAllowedDisease =
409+ Arrays .asList (Disease .INVASIVE_PNEUMOCOCCAL_INFECTION , Disease .INVASIVE_MENINGOCOCCAL_INFECTION , Disease .TUBERCULOSIS )
410+ .contains (disease );
409411 birthCountryCB .addValueChangeListener (e -> {
410412 CountryReferenceDto countryRef = (CountryReferenceDto ) e .getProperty ().getValue ();
411413 boolean isForeigner = false ;
412414 if (countryRef != null ) {
413415 isForeigner = FacadeProvider .getConfigFacade ().isConfiguredCountry (CountryHelper .COUNTRY_CODE_LUXEMBOURG )
414- && Arrays .asList (CountryHelper .COUNTRY_CODE_LUXEMBOURG , "LUX" )
415- .stream ().filter (Objects ::nonNull )
416+ && Arrays .asList (CountryHelper .COUNTRY_CODE_LUXEMBOURG , "LUX" )
417+ .stream ()
418+ .filter (Objects ::nonNull )
416419 .noneMatch (country -> StringUtils .equalsIgnoreCase (country , countryRef .getIsoCode ()));
417420 }
418421 setVisibleClear (isEntryDateAllowedDisease && isForeigner , PersonDto .ENTRY_DATE );
@@ -699,7 +702,7 @@ private int getApproximateAgeInYears() {
699702 Date birthDate = calcBirthDateValue ();
700703 if (birthDate != null ) {
701704 Pair <Integer , ApproximateAgeType > pair =
702- ApproximateAgeHelper .getApproximateAge (birthDate , (Date ) getFieldGroup ().getField (PersonDto .DEATH_DATE ).getValue ());
705+ ApproximateAgeHelper .getApproximateAge (birthDate , (Date ) getFieldGroup ().getField (PersonDto .DEATH_DATE ).getValue ());
703706 if ((pair .getElement0 () != null ) && (pair .getElement1 () == ApproximateAgeType .YEARS )) {
704707 return pair .getElement0 ();
705708 }
@@ -710,7 +713,7 @@ private int getApproximateAgeInYears() {
710713 private void onEmancipatedChange () {
711714 boolean isEmancipatedChecked = (isEmancipated != null ) && (isEmancipated .getValue ());
712715 hasGuardian .setValue (!isEmancipatedChecked );
713- if (isEmancipatedChecked ) {
716+ if (isEmancipatedChecked ) {
714717 nameOfGuardians .setValue ("" );
715718 }
716719 updateHasGuardianCheckBox (isEmancipatedChecked );
@@ -723,7 +726,7 @@ private void updateLegalGuardianSection(boolean isInitialized) {
723726 boolean canBeEmancipated = personCanBeEmancipated (approximateAge , isEmancipatedChecked );
724727 isEmancipated .setVisible (!isIncapacitatedChecked && canBeEmancipated );
725728 hasGuardian .setValue (isIncapacitatedChecked || approximateAge < minimumAdultAge );
726- if (getApproximateAgeInYears () < minimumAdultAge ) {
729+ if (getApproximateAgeInYears () < minimumAdultAge ) {
727730 nameOfGuardians .setValue (getValue ().getNamesOfGuardians ());
728731 }
729732 updateHasGuardianCheckBox (false );
@@ -732,14 +735,14 @@ private void updateLegalGuardianSection(boolean isInitialized) {
732735
733736 private boolean personCanBeEmancipated (int approximateAge , boolean change ) {
734737 boolean canBeEmancipated ;
735- if (approximateAge == -1 && (approximateAgeField ).getValue () != null ) {
738+ if (approximateAge == -1 && (approximateAgeField ).getValue () != null ) {
736739 canBeEmancipated = false ;
737740 } else {
738741 canBeEmancipated = approximateAge >= minimumEmancipatedAge && approximateAge < minimumAdultAge ;
739742 }
740- if (!canBeEmancipated && (approximateAgeField ).getValue () != null ){
741- int age = Integer . parseInt (approximateAgeField .getValue ());
742- if ( approximateAgeTypeField .getValue () == ApproximateAgeType .YEARS ){
743+ if (!canBeEmancipated && (approximateAgeField ).getValue () != null ) {
744+ Integer age = parseApproximateAge (approximateAgeField .getValue ());
745+ if ( age != null && approximateAgeTypeField .getValue () == ApproximateAgeType .YEARS ) {
743746 canBeEmancipated = age >= minimumEmancipatedAge && age < minimumAdultAge ;
744747 if (change ) {
745748 isEmancipated .setValue (canBeEmancipated );
@@ -749,6 +752,19 @@ private boolean personCanBeEmancipated(int approximateAge, boolean change) {
749752 return canBeEmancipated ;
750753 }
751754
755+ private Integer parseApproximateAge (String ageString ) {
756+ if (ageString == null || ageString .trim ().isEmpty ()) {
757+ return null ;
758+ }
759+ try {
760+ // Remove any formatting characters (commas, spaces) and parse only digits
761+ String cleanedAge = ageString .replaceAll ("[^0-9]" , "" );
762+ return cleanedAge .isEmpty () ? null : Integer .parseInt (cleanedAge );
763+ } catch (NumberFormatException e ) {
764+ return null ;
765+ }
766+ }
767+
752768 private void hardResetNameOfGuardians (boolean isInitialized ) {
753769 boolean isIncapacitatedChecked = (isIncapacitated != null ) && (isIncapacitated .getValue ());
754770 if (getApproximateAgeInYears () != -1 && getApproximateAgeInYears () >= minimumAdultAge && !isIncapacitatedChecked && isInitialized ) {
@@ -760,12 +776,12 @@ private void updateHasGuardianCheckBox(boolean onEmancipatedChange) {
760776 boolean isIncapacitatedChecked = (isIncapacitated != null ) && (isIncapacitated .getValue ());
761777 boolean isEmancipatedChecked = (isEmancipated != null ) && (isEmancipated .getValue ());
762778 boolean canBe = personCanBeEmancipated (getApproximateAgeInYears (), onEmancipatedChange );
763- if ((!canBe || isIncapacitatedChecked ) && isEmancipatedChecked ) {
779+ if ((!canBe || isIncapacitatedChecked ) && isEmancipatedChecked ) {
764780 isEmancipatedChecked = false ;
765781 isEmancipated .setValue (Boolean .FALSE );
766782 isIncapacitated .setVisible (true );
767783 }
768- if (isEmancipatedChecked ){
784+ if (isEmancipatedChecked ) {
769785 hasGuardian .setVisible (false );
770786 nameOfGuardians .setVisible (false );
771787 isIncapacitated .setVisible (false );
@@ -778,7 +794,7 @@ private void updateHasGuardianCheckBox(boolean onEmancipatedChange) {
778794 boolean isChildOrUnknownDate = getApproximateAgeInYears () < minimumAdultAge ;
779795 nameOfGuardians .setVisible (isChildOrUnknownDate || isIncapacitatedChecked );
780796 hasGuardian .setVisible (isChildOrUnknownDate || isIncapacitatedChecked );
781- if ((birthDate == null ) || isChildOrUnknownDate ) {
797+ if ((birthDate == null ) || isChildOrUnknownDate ) {
782798 hasGuardian .setValue (Boolean .TRUE );
783799 }
784800 }
0 commit comments