Advertisement
Guest User

Untitled

a guest
Jan 14th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. @Component
  2. public class BranchParser implements Parser<Branch> {
  3. /**
  4. * Convert id as string to Branch entity.
  5. * In templates use th:value with an id.
  6. *
  7. * Example:
  8. * <option th:each="item : ${items}" th:value="${item.getId}"
  9. * th:text="${item}">Value</option>
  10. */
  11.  
  12.  
  13. private final BranchRepo branchRepo;
  14.  
  15. public BranchParser(BranchRepo branchRepo) {
  16. this.branchRepo = branchRepo;
  17. }
  18.  
  19. @Override
  20. public Branch parse(String id, Locale locale) throws ParseException {
  21. Branch branch;
  22. try {
  23. branch = this.branchRepo.findById(Integer.parseInt(id)).get();
  24. } catch (NoSuchElementException | NumberFormatException e) {
  25. throw new ParseException(0, "Branch not found with id: " + id);
  26. }
  27. return branch;
  28. }
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. package org.springframework.format;
  38.  
  39. @FunctionalInterface
  40. public interface Parser<T> {
  41.  
  42. /**
  43. * Parse a text String to produce a T.
  44. * @param text the text string
  45. * @param locale the current user locale
  46. * @return an instance of T
  47. * @throws ParseException when a parse exception occurs in a java.text parsing library
  48. * @throws IllegalArgumentException when a parse exception occurs
  49. */
  50. T parse(String text, Locale locale) throws ParseException;
  51.  
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement