Skip to content

Commit fa77747

Browse files
committed
Added guards against number conversion errors
1 parent 19a3e00 commit fa77747

1 file changed

Lines changed: 56 additions & 22 deletions

File tree

sormas-ui/src/main/java/de/symeda/sormas/ui/samples/PathogenTestForm.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,14 @@
4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
4242

43+
import com.vaadin.server.UserError;
44+
import com.vaadin.ui.AbstractComponent;
4345
import com.vaadin.ui.Label;
4446
import com.vaadin.v7.data.Property;
4547
import com.vaadin.v7.data.fieldgroup.BeanFieldGroup;
4648
import com.vaadin.v7.data.util.BeanItem;
4749
import com.vaadin.v7.data.util.converter.Converter;
50+
import com.vaadin.v7.data.util.converter.Converter.ConversionException;
4851
import com.vaadin.v7.data.util.converter.ConverterUtil;
4952
import com.vaadin.v7.ui.AbstractSelect.ItemCaptionMode;
5053
import com.vaadin.v7.ui.CheckBox;
@@ -64,6 +67,7 @@
6467
import de.symeda.sormas.api.environment.environmentsample.Pathogen;
6568
import de.symeda.sormas.api.i18n.Captions;
6669
import de.symeda.sormas.api.i18n.I18nProperties;
70+
import de.symeda.sormas.api.i18n.Strings;
6771
import de.symeda.sormas.api.i18n.Validations;
6872
import de.symeda.sormas.api.infrastructure.facility.FacilityDto;
6973
import de.symeda.sormas.api.infrastructure.facility.FacilityReferenceDto;
@@ -1007,6 +1011,9 @@ public TuberculosisIGRAInputValueChangeListener(BeanFieldGroup<PathogenTestDto>
10071011
this.fieldGroup = fg;
10081012
}
10091013

1014+
@SuppressWarnings({
1015+
"unchecked",
1016+
"rawtypes" })
10101017
@Override
10111018
public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
10121019

@@ -1016,6 +1023,10 @@ public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
10161023
return;
10171024
}
10181025

1026+
if (igraInputField instanceof AbstractComponent) {
1027+
((AbstractComponent) igraInputField).setComponentError(null);
1028+
}
1029+
10191030
final BeanItem<?> beanItemDataSource = fieldGroup.getItemDataSource();
10201031

10211032
// the input field is always a TextField with a String as value
@@ -1035,13 +1046,21 @@ public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
10351046

10361047
// we need to convert the value to number
10371048
// and we need to do it locale aware and need to finagle with types
1038-
@SuppressWarnings({
1039-
"unchecked",
1040-
"rawtypes" })
1041-
final Number igraNewValue = igraInputField.getValue() == null
1042-
? null
1043-
: (Number) ConverterUtil.getConverter(igraInputField.getType(), (Class) igraValueProp.getType(), null /* current session */)
1044-
.convertToModel(igraInputField.getValue(), igraValueProp.getType(), null /* current locale */);
1049+
1050+
Number igraNewValue = null;
1051+
1052+
try {
1053+
igraNewValue = igraInputField.getValue() == null
1054+
? null
1055+
: (Number) ConverterUtil
1056+
.getConverter(igraInputField.getType(), (Class) igraValueProp.getType(), null /* current session */)
1057+
.convertToModel(igraInputField.getValue(), igraValueProp.getType(), igraInputField.getLocale());
1058+
} catch (ConversionException e) {
1059+
if (igraInputField instanceof AbstractComponent) {
1060+
((AbstractComponent) igraInputField).setComponentError(new UserError(I18nProperties.getString(Strings.errorInvalidValue)));
1061+
}
1062+
return;
1063+
}
10451064

10461065
final Boolean checked = igraNewValue == null ? null : igraNewValue.floatValue() > 10;
10471066

@@ -1067,17 +1086,17 @@ public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
10671086
if (!isCollection) {
10681087
// primitive booleans are easy
10691088
final boolean currentChecked = Boolean.TRUE.equals(igraGT10Field.getValue());
1070-
if (checked != null && checked != currentChecked) {
1089+
if (checked != null && checked.booleanValue() != currentChecked) {
10711090
igraGT10Field.setValue(checked);
10721091
}
10731092
} else {
10741093
// well have to do it the hard way
10751094
final Collection<?> currentSet = (Collection<?>) igraGT10Field.getValue();
10761095
final boolean currentChecked = currentSet != null && !currentSet.isEmpty() && currentSet.contains(Boolean.TRUE);
1077-
if (checked != null && checked != currentChecked) {
1096+
if (checked != null && checked.booleanValue() != currentChecked) {
10781097
final HashSet<Boolean> set = new HashSet<>();
10791098
set.add(checked);
1080-
igraGT10Field.setValue(set);
1099+
igraGT10Field.setValue(Collections.unmodifiableSet(set));
10811100
}
10821101
}
10831102
}
@@ -1103,8 +1122,22 @@ public TuberculosisIGRAGT10InputValueChangeListener(BeanFieldGroup<PathogenTestD
11031122
this.fieldGroup = fg;
11041123
}
11051124

1125+
@SuppressWarnings({
1126+
"rawtypes",
1127+
"unchecked" })
11061128
@Override
11071129
public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
1130+
1131+
// let's try to get the numeric input field and converted value
1132+
final Field<?> igraInputField = fieldGroup.getField(igraInputFieldId);
1133+
if (igraInputField == null) {
1134+
return;
1135+
}
1136+
1137+
if (igraInputField instanceof AbstractComponent) {
1138+
((AbstractComponent) igraInputField).setComponentError(null);
1139+
}
1140+
11081141
final BeanItem<?> beanItemDataSource = fieldGroup.getItemDataSource();
11091142

11101143
final Property<?> igraValueProp = beanItemDataSource.getItemProperty(igraInputFieldId);
@@ -1119,20 +1152,21 @@ public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
11191152
return;
11201153
}
11211154

1122-
// let's try to get the numeric input field and converted value
1123-
final Field<?> igraInputField = fieldGroup.getField(igraInputFieldId);
1124-
if (igraInputField == null) {
1155+
Number igraNewValue = null;
1156+
1157+
try {
1158+
igraNewValue = igraInputField.getValue() == null
1159+
? null
1160+
: (Number) ConverterUtil
1161+
.getConverter(igraInputField.getType(), (Class) igraValueProp.getType(), null /* current session */)
1162+
.convertToModel(igraInputField.getValue(), igraValueProp.getType(), igraInputField.getLocale() /* current locale */);
1163+
} catch (ConversionException e) {
1164+
if (igraInputField instanceof AbstractComponent) {
1165+
((AbstractComponent) igraInputField).setComponentError(new UserError(I18nProperties.getString(Strings.errorInvalidValue)));
1166+
}
11251167
return;
11261168
}
11271169

1128-
@SuppressWarnings({
1129-
"unchecked",
1130-
"rawtypes" })
1131-
final Number igraNewValue = igraInputField.getValue() == null
1132-
? null
1133-
: (Number) ConverterUtil.getConverter(igraInputField.getType(), (Class) igraValueProp.getType(), null /* current session */)
1134-
.convertToModel(igraInputField.getValue(), igraValueProp.getType(), null /* current locale */);
1135-
11361170
// now let's try to determine if the checkbox is checked (we know it's a boolean)
11371171
@SuppressWarnings("unchecked")
11381172
final Field<Object> igraGT10Field = (Field<Object>) fieldGroup.getField(igraGT10FieldId);
@@ -1164,7 +1198,7 @@ public void valueChange(com.vaadin.v7.data.Property.ValueChangeEvent event) {
11641198
} else {
11651199
final HashSet<Boolean> set = new HashSet<>();
11661200
set.add(checked);
1167-
igraGT10Field.setValue(set);
1201+
igraGT10Field.setValue(Collections.unmodifiableSet(set));
11681202
}
11691203

11701204
// don't need to clear anything else because there was no check/uncheck before

0 commit comments

Comments
 (0)