7.8. Writing the Application Code

We will display the data of our application using the JavaScript component jqGrid. Currently, jqGrid is distributed under a commercial licence, but it is free for non-commercial purposes. You can use the free-jqGrid fork instead.

To display data and page-by-page navigation elements in this grid, we need to return data in the JSON format, the structure of which looks like this:

  1. {
  2. total: 100,
  3. page: 3,
  4. records: 3000,
  5. rows: [
  6. {id: 1, name: "Ada"},
  7. {id: 2, name: "Smith"},
  8. ]
  9. }

where

total

the total number of pages

page

the number of the current page

records

the total number of records

rows

the count of records on the current page array

The following code creates a class demonstrating this structure:

  1. package ru.ibase.fbjavaex.jqgrid;
  2. import java.util.List;
  3. import java.util.Map;
  4. /**
  5. * A class describing the structure that is used in jqGrid
  6. * Designed for JSON serialization
  7. *
  8. * @author Simonov Denis
  9. */
  10. public class JqGridData {
  11. /**
  12. * Total number of pages
  13. */
  14. private final int total;
  15. /**
  16. * The current page number
  17. */
  18. private final int page;
  19. /**
  20. * Total number of records
  21. */
  22. private final int records;
  23. /**
  24. * The actual data
  25. */
  26. private final List<Map<String, Object>> rows;
  27. /**
  28. * Constructor
  29. *
  30. * @param total
  31. * @param page
  32. * @param records
  33. * @param rows
  34. */
  35. public JqGridData(int total, int page, int records,
  36. List<Map<String, Object>> rows) {
  37. this.total = total;
  38. this.page = page;
  39. this.records = records;
  40. this.rows = rows;
  41. }
  42. /**
  43. * Returns the total number of pages
  44. *
  45. * @return
  46. */
  47. public int getTotal() {
  48. return total;
  49. }
  50. /**
  51. * Returns the current page
  52. *
  53. * @return
  54. */
  55. public int getPage() {
  56. return page;
  57. }
  58. /**
  59. * Returns the total number of records
  60. *
  61. * @return
  62. */
  63. public int getRecords() {
  64. return records;
  65. }
  66. /**
  67. * Return list of map
  68. * This is an array of data to display in the grid
  69. *
  70. * @return
  71. */
  72. public List<Map<String, Object>> getRows() {
  73. return rows;
  74. }
  75. }

Now we will write an abstract class that will return that structure depending on the search and sorting conditions. It will be a parent class for the entity-specific classes that return similar structures.

  1. /*
  2. * Abstract class for working with JqGrid
  3. */
  4. package ru.ibase.fbjavaex.jqgrid;
  5. import java.util.Map;
  6. import java.util.List;
  7. import org.jooq.DSLContext;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. /**
  10. * Working with JqGrid
  11. *
  12. * @author Simonov Denis
  13. */
  14. public abstract class JqGrid {
  15. @Autowired(required = true)
  16. protected DSLContext dsl;
  17. protected String searchField = "";
  18. protected String searchString = "";
  19. protected String searchOper = "eq";
  20. protected Boolean searchFlag = false;
  21. protected int pageNo = 0;
  22. protected int limit = 0;
  23. protected int offset = 0;
  24. protected String sIdx = "";
  25. protected String sOrd = "asc";
  26. /**
  27. * Returns the total number of records
  28. *
  29. * @return
  30. */
  31. public abstract int getCountRecord();
  32. /**
  33. * Returns the structure for JSON serialization
  34. *
  35. * @return
  36. */
  37. public JqGridData getJqGridData() {
  38. int recordCount = this.getCountRecord();
  39. List<Map<String, Object>> records = this.getRecords();
  40. int total = 0;
  41. if (this.limit > 0) {
  42. total = recordCount / this.limit + 1;
  43. }
  44. JqGridData jqGridData = new JqGridData(
  45. total,
  46. this.pageNo,
  47. recordCount,
  48. records);
  49. return jqGridData;
  50. }
  51. /**
  52. * Returns the number of records per page
  53. *
  54. * @return
  55. */
  56. public int getLimit() {
  57. return this.limit;
  58. }
  59. /**
  60. * Returns the offset to retrieve the first record on the page
  61. *
  62. * @return
  63. */
  64. public int getOffset() {
  65. return this.offset;
  66. }
  67. /**
  68. * Returns field name for sorting
  69. *
  70. * @return
  71. */
  72. public String getIdx() {
  73. return this.sIdx;
  74. }
  75. /**
  76. * Returns the sort order
  77. *
  78. * @return
  79. */
  80. public String getOrd() {
  81. return this.sOrd;
  82. }
  83. /**
  84. * Returns the current page number
  85. *
  86. * @return
  87. */
  88. public int getPageNo() {
  89. return this.pageNo;
  90. }
  91. /**
  92. * Returns an array of records as a list of maps
  93. *
  94. * @return
  95. */
  96. public abstract List<Map<String, Object>> getRecords();
  97. /**
  98. * Returns field name for search
  99. *
  100. * @return
  101. */
  102. public String getSearchField() {
  103. return this.searchField;
  104. }
  105. /**
  106. * Returns value for search
  107. *
  108. * @return
  109. */
  110. public String getSearchString() {
  111. return this.searchString;
  112. }
  113. /**
  114. * Returns the search operation
  115. *
  116. * @return
  117. */
  118. public String getSearchOper() {
  119. return this.searchOper;
  120. }
  121. /**
  122. * Sets the limit on the number of display records
  123. *
  124. * @param limit
  125. */
  126. public void setLimit(int limit) {
  127. this.limit = limit;
  128. }
  129. /**
  130. * Sets the number of records to skip
  131. *
  132. * @param offset
  133. */
  134. public void setOffset(int offset) {
  135. this.offset = offset;
  136. }
  137. /**
  138. * Sets the sorting
  139. *
  140. * @param sIdx
  141. * @param sOrd
  142. */
  143. public void setOrderBy(String sIdx, String sOrd) {
  144. this.sIdx = sIdx;
  145. this.sOrd = sOrd;
  146. }
  147. /**
  148. * Sets the current page number
  149. *
  150. * @param pageNo
  151. */
  152. public void setPageNo(int pageNo) {
  153. this.pageNo = pageNo;
  154. this.offset = (pageNo - 1) * this.limit;
  155. }
  156. /**
  157. * Sets the search condition
  158. *
  159. * @param searchField
  160. * @param searchString
  161. * @param searchOper
  162. */
  163. public void setSearchCondition(String searchField, String searchString,
  164. String searchOper) {
  165. this.searchFlag = true;
  166. this.searchField = searchField;
  167. this.searchString = searchString;
  168. this.searchOper = searchOper;
  169. }
  170. }

Notice that this class contains the DSLContext dsl property that will be used to build jOOQ queries for retrieving data.