7.9. Creating the Primary Modules

Now we can start creating modules. The process of creating modules is described here, using the customer module as an example. Creating the product module is similar and, if you are interested, you can examine its source code in the .zip download linked at the end of this chapter.

First, we implement a class for working with jqGrid, inheriting it from our abstract class ru.ibase.fbjavaex.jqgrid.JqGrid. It will be able to search and sort by the NAME field in reversing order. Track the source code below for explanatory comments.

  1. package ru.ibase.fbjavaex.jqgrid;
  2. import org.jooq.*;
  3. import java.util.List;
  4. import java.util.Map;
  5. import static ru.ibase.fbjavaex.exampledb.Tables.CUSTOMER;
  6. /**
  7. * Customer grid
  8. *
  9. * @author Simonov Denis
  10. */
  11. public class JqGridCustomer extends JqGrid {
  12. /**
  13. * Adding a search condition
  14. *
  15. * @param query
  16. */
  17. private void makeSearchCondition(SelectQuery<?> query) {
  18. switch (this.searchOper) {
  19. case "eq":
  20. // CUSTOMER.NAME = ?
  21. query.addConditions(CUSTOMER.NAME.eq(this.searchString));
  22. break;
  23. case "bw":
  24. // CUSTOMER.NAME STARTING WITH ?
  25. query.addConditions(CUSTOMER.NAME.startsWith(this.searchString));
  26. break;
  27. case "cn":
  28. // CUSTOMER.NAME CONTAINING ?
  29. query.addConditions(CUSTOMER.NAME.contains(this.searchString));
  30. break;
  31. }
  32. }
  33. /**
  34. * Returns the total number of records
  35. *
  36. * @return
  37. */
  38. @Override
  39. public int getCountRecord() {
  40. // query that returns the number of records
  41. SelectFinalStep<?> select
  42. = dsl.selectCount()
  43. .from(CUSTOMER);
  44. SelectQuery<?> query = select.getQuery();
  45. // if perform a search, then add the search condition
  46. if (this.searchFlag) {
  47. makeSearchCondition(query);
  48. }
  49. return (int) query.fetch().getValue(0, 0);
  50. }
  51. /**
  52. * Returns the grid records
  53. *
  54. * @return
  55. */
  56. @Override
  57. public List<Map<String, Object>> getRecords() {
  58. // Basic selection query
  59. SelectFinalStep<?> select =
  60. dsl.select()
  61. .from(CUSTOMER);
  62. SelectQuery<?> query = select.getQuery();
  63. // if perform a search, then add the search condition
  64. if (this.searchFlag) {
  65. makeSearchCondition(query);
  66. }
  67. // set the sort order
  68. switch (this.sOrd) {
  69. case "asc":
  70. query.addOrderBy(CUSTOMER.NAME.asc());
  71. break;
  72. case "desc":
  73. query.addOrderBy(CUSTOMER.NAME.desc());
  74. break;
  75. }
  76. // limit the number of records
  77. if (this.limit != 0) {
  78. query.addLimit(this.limit);
  79. }
  80. if (this.offset != 0) {
  81. query.addOffset(this.offset);
  82. }
  83. // return an array of maps
  84. return query.fetchMaps();
  85. }
  86. }