1 | package jquery.datatables.controller;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 |
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collections;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.servlet.ServletConfig;
|
---|
12 | import javax.servlet.ServletException;
|
---|
13 | import javax.servlet.annotation.WebServlet;
|
---|
14 | import javax.servlet.http.HttpServlet;
|
---|
15 | import javax.servlet.http.HttpServletRequest;
|
---|
16 | import javax.servlet.http.HttpServletResponse;
|
---|
17 |
|
---|
18 | import org.json.JSONArray;
|
---|
19 | import org.json.JSONException;
|
---|
20 | import org.json.JSONObject;
|
---|
21 |
|
---|
22 | import jquery.datatables.model.RecordHashmap;
|
---|
23 | import jquery.datatables.model.DataRepository;
|
---|
24 | import jquery.datatables.controller.DataTableRequestParam;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * JDBCAjaxDataSourceServlet provides data to the JQuery DataTables
|
---|
28 | */
|
---|
29 | // The following specified through @annotations was found to not work
|
---|
30 | // when combined with additional settings specified in web.xml
|
---|
31 | // Consequently all the parameter settings were moved to web.xml
|
---|
32 | //
|
---|
33 | // Defunct:
|
---|
34 | // AT WebServlet(name="JDBCAjaxDataSource", urlPatterns={"/JDBCAjaxDataSource"})
|
---|
35 | // AT WebServlet("/JDBCAjaxDataSource")
|
---|
36 | // Handled in web.xml (technically web.xml.in) so init-param can specify
|
---|
37 | // where on the file system the database file is to be found
|
---|
38 |
|
---|
39 | public class JDBCAjaxDataSourceServlet extends HttpServlet
|
---|
40 | {
|
---|
41 | private static final long serialVersionUID = 1L;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @see Servlet#init(ServletConfig)
|
---|
45 | */
|
---|
46 | public void init(ServletConfig config) throws ServletException
|
---|
47 | {
|
---|
48 | super.init(config);
|
---|
49 |
|
---|
50 | String jdbc_uri = config.getInitParameter("jdbc.uri");
|
---|
51 | String jdbc_db_table= config.getInitParameter("jdbc.db.table");
|
---|
52 |
|
---|
53 | System.err.println("*** jdbc_url = " + jdbc_uri);
|
---|
54 | System.err.println("*** jdbc_db_table = " + jdbc_db_table);
|
---|
55 |
|
---|
56 | DataRepository.init(jdbc_uri,jdbc_db_table);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
61 | */
|
---|
62 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
---|
63 | {
|
---|
64 | final DataTableRequestParam param = DataTablesParamUtility.getParam(request);
|
---|
65 |
|
---|
66 | String sEcho = param.sEcho;
|
---|
67 | int iTotalRecords; // total number of records (unfiltered)
|
---|
68 | int iTotalDisplayRecords; //value will be set when code filters records by keyword
|
---|
69 |
|
---|
70 | JSONArray data = new JSONArray(); //data that will be shown in the table
|
---|
71 |
|
---|
72 | iTotalRecords = DataRepository.GetRecords().size();
|
---|
73 | List<RecordHashmap> records = new LinkedList<RecordHashmap>();
|
---|
74 |
|
---|
75 | List<RecordHashmap> all_records = DataRepository.GetRecords(); // Triggers reading in Database, first time called
|
---|
76 |
|
---|
77 | // Filter records based on value specified in search box (matched only to columns are marked as searchable)
|
---|
78 | for (RecordHashmap rh : all_records) {
|
---|
79 |
|
---|
80 | ArrayList<String> column_headings = DataRepository.getColumnHeadings();
|
---|
81 | int num_columns = column_headings.size();
|
---|
82 |
|
---|
83 | // Don't want search/filter by column 0 (id), so start at index pos 1
|
---|
84 | for (int i=1; i<num_columns; i++) {
|
---|
85 | String field = column_headings.get(i);
|
---|
86 |
|
---|
87 | if (param.bSearchable[i] &&
|
---|
88 | rh.getFieldValue(field).toLowerCase().contains(param.sSearchKeyword.toLowerCase())) {
|
---|
89 | records.add(rh); // Add a record that matches search criterion
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /*
|
---|
95 | if (param.bSearchable[1] &&
|
---|
96 | rh.getFieldValue("Year").toLowerCase().contains(param.sSearchKeyword.toLowerCase())
|
---|
97 | ||
|
---|
98 | param.bSearchable[2] &&
|
---|
99 | rh.getFieldValue("Name").toLowerCase().contains(param.sSearchKeyword.toLowerCase())
|
---|
100 | ||
|
---|
101 | param.bSearchable[3] &&
|
---|
102 | rh.getFieldValue("Address").toLowerCase().contains(param.sSearchKeyword.toLowerCase()))
|
---|
103 | {
|
---|
104 | records.add(rh); // Add a record that matches search criterion
|
---|
105 | }
|
---|
106 | */
|
---|
107 | }
|
---|
108 |
|
---|
109 | iTotalDisplayRecords = records.size();//Number of records that matches search criterion should be returned
|
---|
110 |
|
---|
111 | //
|
---|
112 | // Move on to sorting results
|
---|
113 | //
|
---|
114 | Collections.sort(records, new Comparator<RecordHashmap>() {
|
---|
115 | @Override
|
---|
116 | public int compare(RecordHashmap rh1, RecordHashmap rh2) {
|
---|
117 | int result = 0;
|
---|
118 |
|
---|
119 | ArrayList<String> column_headings = DataRepository.getColumnHeadings();
|
---|
120 |
|
---|
121 | for (int i=0; i<param.iSortingCols; i++) {
|
---|
122 | int sortBy = param.iSortCol[i];
|
---|
123 |
|
---|
124 | if (param.bSortable[sortBy]) {
|
---|
125 | if (sortBy == 0) {
|
---|
126 | result = 0; //sort by id is not allowed
|
---|
127 | }
|
---|
128 | else {
|
---|
129 | String field = column_headings.get(sortBy);
|
---|
130 | result = rh1.getFieldValue(field).compareToIgnoreCase(rh2.getFieldValue(field)) *
|
---|
131 | (param.sSortDir[i].equals("asc") ? -1 : 1);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /*
|
---|
135 | switch(sortBy) {
|
---|
136 | case 0:
|
---|
137 | result = 0; //sort by id is not allowed
|
---|
138 | break;
|
---|
139 | default:
|
---|
140 | String field = _columnHeadings.get(sortBy);
|
---|
141 | result = rh1.getFieldValue(field).compareToIgnoreCase(rh2.getFieldValue(field)) *
|
---|
142 | (param.sSortDir[i].equals("asc") ? -1 : 1);
|
---|
143 | break;
|
---|
144 | / *
|
---|
145 | case 1:
|
---|
146 | result = rh1.getFieldValue("Year").compareToIgnoreCase(rh2.getFieldValue("Year")) *
|
---|
147 | (param.sSortDir[i].equals("asc") ? -1 : 1);
|
---|
148 | break;
|
---|
149 | case 2:
|
---|
150 | result = rh1.getFieldValue("Name").compareToIgnoreCase(rh2.getFieldValue("Name")) *
|
---|
151 | (param.sSortDir[i].equals("asc") ? -1 : 1);
|
---|
152 | break;
|
---|
153 | case 3:
|
---|
154 | result = rh1.getFieldValue("Address").compareToIgnoreCase(rh2.getFieldValue("Address")) *
|
---|
155 | (param.sSortDir[i].equals("asc") ? -1 : 1);
|
---|
156 | break;* /
|
---|
157 |
|
---|
158 | } */
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (result != 0) {
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 | else {
|
---|
165 | continue;
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | return result;
|
---|
170 | }
|
---|
171 | });
|
---|
172 |
|
---|
173 | if (records.size() < param.iDisplayStart + param.iDisplayLength) {
|
---|
174 | records = records.subList(param.iDisplayStart, records.size());
|
---|
175 | }
|
---|
176 | else {
|
---|
177 | records = records.subList(param.iDisplayStart, param.iDisplayStart + param.iDisplayLength);
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Convert resulting (filtered & sorted) records into JSON for output
|
---|
181 |
|
---|
182 | try {
|
---|
183 | JSONObject jsonResponse = new JSONObject();
|
---|
184 |
|
---|
185 | jsonResponse.put("sEcho", sEcho);
|
---|
186 | jsonResponse.put("iTotalRecords", iTotalRecords);
|
---|
187 | jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
|
---|
188 |
|
---|
189 | for(RecordHashmap rh : records) {
|
---|
190 | JSONArray row = new JSONArray();
|
---|
191 |
|
---|
192 | row.put(rh.getId());
|
---|
193 | List<String> field_keys = rh.getFields();
|
---|
194 |
|
---|
195 | for (String field : field_keys) {
|
---|
196 | String field_value = rh.getFieldValue(field);
|
---|
197 | row.put(field_value);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /*
|
---|
201 | row.put(rh.getId()).put(rh.getFieldValue("Year")).put(rh.getFieldValue("Name")).put(rh.getFieldValue("Address")).put(rh.getFieldValue("Field4")).put(rh.getFieldValue("Field5"));
|
---|
202 | */
|
---|
203 |
|
---|
204 | data.put(row);
|
---|
205 | }
|
---|
206 |
|
---|
207 | jsonResponse.put("aaData", data);
|
---|
208 |
|
---|
209 | response.setContentType("application/json");
|
---|
210 | response.getWriter().print(jsonResponse.toString());
|
---|
211 | }
|
---|
212 | catch (JSONException e) {
|
---|
213 | e.printStackTrace();
|
---|
214 | response.setContentType("text/html");
|
---|
215 | response.getWriter().print(e.getMessage());
|
---|
216 | }
|
---|
217 |
|
---|
218 | }
|
---|
219 | }
|
---|