diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java index d529b27..df6485b 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java @@ -24,9 +24,6 @@ import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; -import org.springframework.format.annotation.NumberFormat; -import org.springframework.format.annotation.NumberFormat.Style; - import com.fjordtek.bookstore.annotation.CurrentYear; @Entity @@ -143,11 +140,10 @@ public class Book { private String isbn; ////////// - @NumberFormat(style = Style.NUMBER, pattern = "#,###.###") @Column(nullable = false) @Digits( integer = 3, fraction = 2, - message = "Invalid price, possibly too many decimals" + message = "Invalid price value" ) @DecimalMin( value = minPrice, message = "Too low price value. Minimum allowed: " + minPrice diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BigDecimalPropertyEditor.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BigDecimalPropertyEditor.java new file mode 100644 index 0000000..7f7a7b4 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BigDecimalPropertyEditor.java @@ -0,0 +1,28 @@ +// Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.web; + +import java.beans.PropertyEditorSupport; +import java.math.BigDecimal; + +public class BigDecimalPropertyEditor extends PropertyEditorSupport { + + @Override + public void setAsText(String input) { + + String[] decimalSeparators = {",", "."}; + + for (int i = 0; i < decimalSeparators.length; i++) { + + // TODO count of decimal separators must be exactly 1 + if (input.contains(decimalSeparators[i])) { + input = input.replace(decimalSeparators[i], "."); + break; + } + } + + Number number = Double.parseDouble(input); + BigDecimal bigDecimal = BigDecimal.valueOf(number.doubleValue()); + setValue(bigDecimal); + } +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 0e76969..e8086be 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -2,6 +2,7 @@ package com.fjordtek.bookstore.web; +import java.math.BigDecimal; import java.time.Year; import java.util.HashMap; import java.util.Map; @@ -15,6 +16,8 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; +import org.springframework.web.bind.WebDataBinder; +import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -29,6 +32,14 @@ import com.fjordtek.bookstore.model.CategoryRepository; @Controller public class BookController { + /* We allow both comma and dot decimal separators + * for BigDecimal data types used in Book class. + */ + @InitBinder("book") + public void initBinder(WebDataBinder binder) { + binder.registerCustomEditor(BigDecimal.class, new BigDecimalPropertyEditor()); + } + @Autowired private BookRepository bookRepository; diff --git a/bookstore/src/main/resources/messages.properties b/bookstore/src/main/resources/messages.properties index 39abe74..8ca6b61 100644 --- a/bookstore/src/main/resources/messages.properties +++ b/bookstore/src/main/resources/messages.properties @@ -25,7 +25,7 @@ book.desc.example.author = Andrzej Sapkowski book.desc.example.title = The Witcher\:\ The Lady of the Lake book.desc.example.isbn = 1212121-3 or 4545454-67 or 6767676-890 book.desc.example.year = 1854 or 2005 -book.desc.example.price = 17 or 123.5 or 45.67 +book.desc.example.price = 17 or 123.5 or 45.67 or 78,43 book.error.author = Invalid author name book.error.title = Invalid title