7.9.1. CustomerManager Class

The CustomerManager class that is defined next is a kind of business layer between the corresponding controller and the database. We will use it for adding, editing and deleting a customer. All operations in this layer will be performed in a SNAPSHOT-level transaction.

  1. package ru.ibase.fbjavaex.managers;
  2. import org.jooq.DSLContext;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.transaction.annotation.Transactional;
  5. import org.springframework.transaction.annotation.Propagation;
  6. import org.springframework.transaction.annotation.Isolation;
  7. import static ru.ibase.fbjavaex.exampledb.Tables.CUSTOMER;
  8. import static ru.ibase.fbjavaex.exampledb.Sequences.GEN_CUSTOMER_ID;
  9. /**
  10. * Customer manager
  11. *
  12. * @author Simonov Denis
  13. */
  14. public class CustomerManager {
  15. @Autowired(required = true)
  16. private DSLContext dsl;
  17. /**
  18. * Adding a customer
  19. *
  20. * @param name
  21. * @param address
  22. * @param zipcode
  23. * @param phone
  24. */
  25. @Transactional(propagation = Propagation.REQUIRED,
  26. isolation = Isolation.REPEATABLE_READ)
  27. public void create(String name, String address, String zipcode, String phone) {
  28. if (zipcode != null) {
  29. if (zipcode.trim().isEmpty()) {
  30. zipcode = null;
  31. }
  32. }
  33. int customerId = this.dsl.nextval(GEN_CUSTOMER_ID).intValue();
  34. this.dsl
  35. .insertInto(CUSTOMER,
  36. CUSTOMER.CUSTOMER_ID,
  37. CUSTOMER.NAME,
  38. CUSTOMER.ADDRESS,
  39. CUSTOMER.ZIPCODE,
  40. CUSTOMER.PHONE)
  41. .values(
  42. customerId,
  43. name,
  44. address,
  45. zipcode,
  46. phone
  47. )
  48. .execute();
  49. }
  50. /**
  51. * Editing a customer
  52. *
  53. * @param customerId
  54. * @param name
  55. * @param address
  56. * @param zipcode
  57. * @param phone
  58. */
  59. @Transactional(propagation = Propagation.REQUIRED,
  60. isolation = Isolation.REPEATABLE_READ)
  61. public void edit(int customerId, String name, String address,
  62. String zipcode, String phone) {
  63. if (zipcode != null) {
  64. if (zipcode.trim().isEmpty()) {
  65. zipcode = null;
  66. }
  67. }
  68. this.dsl.update(CUSTOMER)
  69. .set(CUSTOMER.NAME, name)
  70. .set(CUSTOMER.ADDRESS, address)
  71. .set(CUSTOMER.ZIPCODE, zipcode)
  72. .set(CUSTOMER.PHONE, phone)
  73. .where(CUSTOMER.CUSTOMER_ID.eq(customerId))
  74. .execute();
  75. }
  76. /**
  77. * Deleting a customer
  78. *
  79. * @param customerId
  80. */
  81. @Transactional(propagation = Propagation.REQUIRED,
  82. isolation = Isolation.REPEATABLE_READ)
  83. public void delete(int customerId) {
  84. this.dsl.deleteFrom(CUSTOMER)
  85. .where(CUSTOMER.CUSTOMER_ID.eq(customerId))
  86. .execute();
  87. }
  88. }