Skip to content

Commit

Permalink
Applies persistance scale on amounts
Browse files Browse the repository at this point in the history
- when transforming values from user input or import input
- round by the amount property field defined scale so the current field value
  has the same scale as persisted and loaded data

Fixes: SIRI-1041
  • Loading branch information
mkeckmkeck committed Dec 18, 2024
1 parent 538ef8e commit e12083c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/java/sirius/db/mixing/properties/AmountProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void create(EntityDescriptor descriptor,
@Override
public Object transformValue(Value value) {
if (value.isFilled()) {
return NLS.parseUserString(Amount.class, value.asString());
return NLS.parseUserString(Amount.class, value.asString()).round(getPersistanceNumberFormat());
}
if (this.isNullable() || defaultValue.isEmptyString()) {
return Amount.NOTHING;
Expand All @@ -106,7 +106,7 @@ protected Object transformValueFromImport(Value value) {
if (value.getAmount().isEmpty() && !this.isNullable()) {
return defaultValue.getAmount();
}
return value.get();
return ((Amount) value.get()).round(getPersistanceNumberFormat());
}

if (value.isFilled()) {
Expand All @@ -118,10 +118,10 @@ protected Object transformValueFromImport(Value value) {

private Amount parseWithNLS(@Nonnull Value value) {
try {
return Amount.ofMachineString(value.asString());
return Amount.ofMachineString(value.asString()).round(getPersistanceNumberFormat());
} catch (IllegalArgumentException originalFormatException) {
try {
return Amount.ofUserString(value.asString());
return Amount.ofUserString(value.asString()).round(getPersistanceNumberFormat());
} catch (Exception ignored) {
throw originalFormatException;
}
Expand All @@ -144,10 +144,15 @@ public String getColumnDefaultValue() {
}
// the resulting string needs to match the string representation in the DB exactly,
// else a schema change will be issued.
NumberFormat format = getAnnotation(Numeric.class).map(numeric -> {
NumberFormat format = getPersistanceNumberFormat();
return Amount.of((BigDecimal) defaultData).toString(format).asString();
}

@Nonnull
private NumberFormat getPersistanceNumberFormat() {
return getAnnotation(Numeric.class).map(numeric -> {
return new NumberFormat(numeric.scale(), RoundingMode.HALF_UP, NLS.getMachineFormatSymbols(), false, null);
}).orElse(NumberFormat.MACHINE_NO_DECIMAL_PLACES);
return Amount.of((BigDecimal) defaultData).toString(format).asString();
}

@Override
Expand Down

0 comments on commit e12083c

Please sign in to comment.