Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.82 KB | None | 0 0
  1. index.jsp
  2. 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  3. 2 pageEncoding="UTF-8"%>
  4. 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  5. 4 <!DOCTYPE html>
  6. 5
  7. 6 <html>
  8. 7
  9. 8 <head>
  10. 9
  11. 10 <title>CO2 Readings</title>
  12. 11 <meta name="viewport" content="width=device-width, initial-scale=1">
  13. 12 <link rel="stylesheet" href="http://localhost:8080/project/jquery/
  14. jquery.mobile-1.4.5.min.css" />
  15. 13 <script src="http://localhost:8080/project/jquery/
  16. jquery-1.12.4.min.js"></script>
  17. 14 <script src="http://localhost:8080/project/jquery/
  18. jquery.mobile-1.4.5.min.js"></script>
  19. 15
  20. 16 <style>
  21. 17
  22. 18 table {
  23. 19 font-family: arial, sans-serif;
  24. 20 border-collapse: collapse;
  25. 21 width: 50%;
  26. 22 }
  27. 23
  28. 24 td, th {
  29. 25 border: 1px solid #000000;
  30. 26 padding: 10px 15px 15px 10px;
  31. 27 text-align: left;
  32. 28 }
  33. 29
  34. 30 tr:nth-child(even) {
  35. 31 background-color: #dddddd;
  36. 32 }
  37. 33
  38. 34 </style>
  39. 35
  40. 36 </head>
  41. 37
  42. 38 <body>
  43. 39
  44. 40 <div data-role="header" data-theme="b">
  45. 41 <h1>CO2 Readings</h1>
  46. 42 </div>
  47. 43
  48. 44 <div style="padding:30px;"> <!-- Padding div -->
  49. 45
  50. 46
  51. 47 <div>
  52. 48 <c:if test="${ sort == 'new' }">
  53. 49 Showing Newest First
  54. 50 </c:if>
  55. Page 1
  56. index.jsp
  57. 51
  58. 52 <c:if test="${ sort == 'old' }">
  59. 53 Showing Oldest First
  60. 54 </c:if>
  61. 55
  62. 56 <c:if test="${ sort != 'old' }">
  63. 57 <c:if test="${ sort != 'new' }">
  64. 58 Showing Oldest First
  65. 59 </c:if>
  66. 60 </c:if>
  67. 61
  68. 62 </div>
  69. 63 <br/>
  70. 64 <br/>
  71. 65
  72. 66
  73. 67 <div > <!-- Table div -->
  74. 68
  75. 69 <c:if test="${ enteries_in_last_x > 0 }">
  76. 70 <table>
  77. 71 <tr>
  78. 72 <th style="font-size:150%"> Time </th>
  79. 73 <th style="font-size:150%"> CO2 Reading</th>
  80. 74 <th style="font-size:150%"> Location ID</th>
  81. 75 </tr>
  82. 76
  83. 77 <c:forEach var="co2" items="${ test }">
  84. 78 <tr>
  85. 79 <td> ${ co2.getTime() }</td>
  86. 80 <td> ${ co2.getCo2_reading() }</td>
  87. 81 <td> ${ co2.getSensor_id() }</td>
  88. 82 </tr>
  89. 83 </c:forEach>
  90. 84
  91. 85 </table>
  92. 86 </c:if>
  93. 87 </div> <!-- Table div -->
  94. 88
  95. 89 <div>
  96. 90 <c:if test="${ enteries_in_last_x == 0 }">There were no reading in
  97. the last ${ x_in_min } minutes.</c:if>
  98. 91 </div>
  99. 92
  100. 93 <br>
  101. 94
  102. 95 <div>
  103. 96 <c:if test="${ enteries_not_in_last_x == 0 }">
  104. 97 There are no more values to be shown.
  105. 98
  106. 99 </c:if>
  107. 100 </div>
  108. 101
  109. 102 <div>
  110. Page 2
  111. index.jsp
  112. 103 <c:if test="${ enteries_not_in_last_x > 0 }">
  113. 104 Show ${ enteries_not_in_last_x }
  114. 105 <c:if test="${ enteries_in_last_x != 0 }">
  115. 106 more
  116. 107 </c:if>
  117. 108 values.
  118. 109 </c:if>
  119. 110 </div>
  120. 111
  121. 112 <br/>
  122. 113 <br/>
  123. 114
  124. 115 <div class="ui-bar ui-bar-a ui-grid-c">
  125. 116 <div class="ui-block-a" style="padding:10px;">
  126. 117 <form method="get">
  127. 118 <input type="submit" name="sort" value="new">
  128. 119 </form>
  129. 120 </div>
  130. 121 <div class="ui-block-b" style="padding:10px;">
  131. 122 <form method="get">
  132. 123 <input type="submit" name="sort" value="old">
  133. 124 </form>
  134. 125 </div>
  135. 126 <div class="ui-block-c" style="padding:10px;">
  136. 127 <form method="get">
  137. 128 <input type="submit" name="sort" value="small">
  138. 129 </form>
  139. 130 </div>
  140. 131 <div class="ui-block-d" style="padding:10px;">
  141. 132 <form method="get">
  142. 133 <input type="submit" name="sort" value="big">
  143. 134 </form>
  144. 135 </div>
  145. 136 </div>
  146. 137 </div> <!-- Padding div -->
  147. 138
  148. 139 </body>
  149. 140
  150. 141 </html>
  151. Page 3
  152. DatabaseConnectorTest.java
  153. 1 package uk.org.kelsohighschool.ps.program.tests;
  154. 2
  155. 3 import static org.junit.Assert.*;
  156. 4
  157. 5 import java.sql.SQLException;
  158. 6
  159. 7 import org.junit.Test;
  160. 8
  161. 9 import uk.org.kelsohighschool.ps.program.db.DatabaseConnector;
  162. 10
  163. 11 public class DatabaseConnectorTest {
  164. 12 /**
  165. 13 * URL to connect to
  166. 14 */
  167. 15 private String url = "jdbc:postgresql://localhost/outcometest";
  168. 16 /**
  169. 17 * Username to login as
  170. 18 */
  171. 19 private String username = "postgres";
  172. 20 /**
  173. 21 * Password for login
  174. 22 */
  175. 23 private String password = "postgres";
  176. 24
  177. 25 /**
  178. 26 * tests the database connection
  179. 27 * @throws SQLException
  180. 28 */
  181. 29 @Test
  182. 30 public void testDatabaseConnector() throws SQLException {
  183. 31 DatabaseConnector dc = new DatabaseConnector
  184. (url,username,password);
  185. 32 dc.close();
  186. 33 }
  187. 34
  188. 35 /**
  189. 36 * gets the sql version then checks if it is 9.5.4
  190. 37 * @throws SQLException
  191. 38 */
  192. 39 @Test
  193. 40 public void testGetVer() throws SQLException {
  194. 41 DatabaseConnector dc = new DatabaseConnector
  195. (url,username,password);
  196. 42 assertEquals("9.5.6",dc.GetVer());
  197. 43 dc.close();
  198. 44 }
  199. 45
  200. 46 /**
  201. 47 * tests the getco2 function
  202. 48 * @throws SQLException
  203. 49 */
  204. 50 @Test
  205. 51 public void testGetCo2() throws SQLException {
  206. Page 1
  207. DatabaseConnectorTest.java
  208. 52 DatabaseConnector dc = new DatabaseConnector
  209. (url,username,password);
  210. 53 dc.getCo2();
  211. 54 dc.close();
  212. 55 }
  213. 56 /**
  214. 57 * tests the getlocation function
  215. 58 * @throws SQLException
  216. 59 */
  217. 60 @Test
  218. 61 public void testGetLocation() throws SQLException {
  219. 62 DatabaseConnector dc = new DatabaseConnector
  220. (url,username,password);
  221. 63 dc.getLocation();
  222. 64 dc.close();
  223. 65 }
  224. 66 /**
  225. 67 * tests the temp function
  226. 68 * @throws SQLException
  227. 69 */
  228. 70 @Test
  229. 71 public void testGetTemp() throws SQLException {
  230. 72 DatabaseConnector dc = new DatabaseConnector
  231. (url,username,password);
  232. 73 dc.getTemp();
  233. 74 dc.close();
  234. 75 }
  235. 76
  236. 77 }
  237. 78
  238. Page 2
  239. SortTest.java
  240. 1 package uk.org.kelsohighschool.ps.program.tests;
  241. 2
  242. 3 import static org.junit.Assert.*;
  243. 11
  244. 12 public class SortTest {
  245. 13
  246. 14
  247. 15 /**
  248. 16 * Tests sorting newest to oldest
  249. 17 */
  250. 18 @Test
  251. 19 public void testSortNew(){
  252. 20
  253. 21 // id time Value Location
  254. 22 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
  255. 23 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
  256. 24 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
  257. 25 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
  258. 26 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
  259. 27 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
  260. 28 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
  261. 29 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
  262. 30 Co2 List9 = new Co2(9, 167, (float) 2, 2);
  263. 31 Co2 List0 = new Co2(0, 14, (float) 72, 2);
  264. 32
  265. 33 ArrayList<Co2> presort = new ArrayList<Co2>();
  266. 34 //System.out.println(List1);
  267. 35 presort.add(List1);
  268. 36 presort.add(List2);
  269. 37 presort.add(List3);
  270. 38 presort.add(List4);
  271. 39 presort.add(List5);
  272. 40 presort.add(List6);
  273. 41 presort.add(List7);
  274. 42 presort.add(List8);
  275. 43 presort.add(List9);
  276. 44 presort.add(List0);
  277. 45
  278. 46 //System.out.println(list2);
  279. 47 Sort sort = new Sort();
  280. 48 System.out.print("Sorting new");
  281. 49
  282. 50 ArrayList<Co2> sortedlist = sort.MySort(presort, "new");
  283. 51 System.out.println(sortedlist);
  284. 52 String stringlist = "" + sortedlist;
  285. 53 assertEquals("[ Co2 [id1=8, time=1987, co2_reading=42.0,
  286. sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0, sensor_id=2], Co2
  287. [id1=4, time=165, co2_reading=1.05, sensor_id=2], Co2 [id1=1, time=65,
  288. co2_reading=1.01, sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5,
  289. sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04, sensor_id=2], Co2
  290. [id1=0, time=14, co2_reading=72.0, sensor_id=2], Co2 [id1=3, time=13,
  291. co2_reading=1.03, sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02,
  292. sensor_id=2], Co2 [id1=7, time=11, co2_reading=10.01, sensor_id=2]]",
  293. Page 1
  294. SortTest.java
  295. sensor_id=2], Co2 [id1=7, time=11, co2_reading=10.01, sensor_id=2]]"
  296. stringlist);
  297. 54 }
  298. 55
  299. 56 /**
  300. 57 * Tests sorting biggest to smallest
  301. 58 */
  302. 59 @Test
  303. 60 public void testSortBig(){
  304. 61
  305. 62 // id time Value Location
  306. 63 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
  307. 64 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
  308. 65 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
  309. 66 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
  310. 67 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
  311. 68 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
  312. 69 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
  313. 70 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
  314. 71 Co2 List9 = new Co2(9, 167, (float) 2, 2);
  315. 72 Co2 List0 = new Co2(0, 14, (float) 72, 2);
  316. 73
  317. 74 ArrayList<Co2> presort = new ArrayList<Co2>();
  318. 75 //System.out.println(List1);
  319. 76 presort.add(List1);
  320. 77 presort.add(List2);
  321. 78 presort.add(List3);
  322. 79 presort.add(List4);
  323. 80 presort.add(List5);
  324. 81 presort.add(List6);
  325. 82 presort.add(List7);
  326. 83 presort.add(List8);
  327. 84 presort.add(List9);
  328. 85 presort.add(List0);
  329. 86
  330. 87 //System.out.println(list2);
  331. 88 Sort sort = new Sort();
  332. 89 System.out.print("Sorting big");
  333. 90
  334. 91 ArrayList<Co2> sortedlist = sort.MySort(presort, "big");
  335. 92 System.out.println(sortedlist);
  336. 93 String stringlist = "" + sortedlist;
  337. 94 assertEquals("[ Co2 [id1=0, time=14, co2_reading=72.0,
  338. sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0, sensor_id=2], Co2
  339. [id1=7, time=11, co2_reading=10.01, sensor_id=2], Co2 [id1=9, time=167,
  340. co2_reading=2.0, sensor_id=2], Co2 [id1=4, time=165, co2_reading=1.05,
  341. sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04, sensor_id=2], Co2
  342. [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=2, time=12,
  343. co2_reading=1.02, sensor_id=2], Co2 [id1=1, time=65, co2_reading=1.01,
  344. sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2]]",
  345. stringlist);
  346. 95 }
  347. 96 /**
  348. 97 * Tests sorting oldest to newest
  349. Page 2
  350. SortTest.java
  351. 98 */
  352. 99 @Test
  353. 100 public void testSortOld(){
  354. 101
  355. 102 // id time Value Location
  356. 103 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
  357. 104 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
  358. 105 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
  359. 106 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
  360. 107 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
  361. 108 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
  362. 109 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
  363. 110 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
  364. 111 Co2 List9 = new Co2(9, 167, (float) 2, 2);
  365. 112 Co2 List0 = new Co2(0, 14, (float) 72, 2);
  366. 113
  367. 114 ArrayList<Co2> presort = new ArrayList<Co2>();
  368. 115 //System.out.println(List1);
  369. 116 presort.add(List1);
  370. 117 presort.add(List2);
  371. 118 presort.add(List3);
  372. 119 presort.add(List4);
  373. 120 presort.add(List5);
  374. 121 presort.add(List6);
  375. 122 presort.add(List7);
  376. 123 presort.add(List8);
  377. 124 presort.add(List9);
  378. 125 presort.add(List0);
  379. 126
  380. 127 //System.out.println(list2);
  381. 128 Sort sort = new Sort();
  382. 129 System.out.print("Sorting old");
  383. 130
  384. 131 ArrayList<Co2> sortedlist = sort.MySort(presort, "old");
  385. 132 System.out.println(sortedlist);
  386. 133 String stringlist = "" + sortedlist;
  387. 134 assertEquals("[ Co2 [id1=7, time=11, co2_reading=10.01,
  388. sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2
  389. [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=0, time=14,
  390. co2_reading=72.0, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
  391. sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2], Co2
  392. [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2 [id1=4, time=165,
  393. co2_reading=1.05, sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0,
  394. sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
  395. sensor_id=2]]",stringlist);
  396. 135 }
  397. 136
  398. 137 /**
  399. 138 * Tests sorting smallest to biggest
  400. 139 */
  401. 140 @Test
  402. 141 public void testSortSmall(){
  403. 142
  404. Page 3
  405. SortTest.java
  406. 143 // id time Value Location
  407. 144 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
  408. 145 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
  409. 146 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
  410. 147 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
  411. 148 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
  412. 149 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
  413. 150 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
  414. 151 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
  415. 152 Co2 List9 = new Co2(9, 167, (float) 2, 2);
  416. 153 Co2 List0 = new Co2(0, 14, (float) 72, 2);
  417. 154
  418. 155 ArrayList<Co2> presort = new ArrayList<Co2>();
  419. 156 //System.out.println(List1);
  420. 157 presort.add(List1);
  421. 158 presort.add(List2);
  422. 159 presort.add(List3);
  423. 160 presort.add(List4);
  424. 161 presort.add(List5);
  425. 162 presort.add(List6);
  426. 163 presort.add(List7);
  427. 164 presort.add(List8);
  428. 165 presort.add(List9);
  429. 166 presort.add(List0);
  430. 167
  431. 168 //System.out.println(list2);
  432. 169 Sort sort = new Sort();
  433. 170 System.out.print("Sorting small");
  434. 171
  435. 172 ArrayList<Co2> sortedlist = sort.MySort(presort, "small");
  436. 173 System.out.println(sortedlist);
  437. 174 String stringlist = "" + sortedlist;
  438. 175 assertEquals("[ Co2 [id1=6, time=19, co2_reading=0.5,
  439. sensor_id=2], Co2 [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2
  440. [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2 [id1=3, time=13,
  441. co2_reading=1.03, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
  442. sensor_id=2], Co2 [id1=4, time=165, co2_reading=1.05, sensor_id=2], Co2
  443. [id1=9, time=167, co2_reading=2.0, sensor_id=2], Co2 [id1=7, time=11,
  444. co2_reading=10.01, sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
  445. sensor_id=2], Co2 [id1=0, time=14, co2_reading=72.0, sensor_id=2]]",
  446. stringlist);
  447. 176 }
  448. 177
  449. 178 /**
  450. 179 * Test sorting with no sort value passed, should sort oldest to
  451. newest
  452. 180 */
  453. 181 @Test
  454. 182 public void testSortNone(){
  455. 183
  456. 184 // id time Value Location
  457. 185 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
  458. 186 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
  459. Page 4
  460. SortTest.java
  461. 187 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
  462. 188 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
  463. 189 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
  464. 190 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
  465. 191 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
  466. 192 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
  467. 193 Co2 List9 = new Co2(9, 167, (float) 2, 2);
  468. 194 Co2 List0 = new Co2(0, 14, (float) 72, 2);
  469. 195
  470. 196 ArrayList<Co2> presort = new ArrayList<Co2>();
  471. 197 //System.out.println(List1);
  472. 198 presort.add(List1);
  473. 199 presort.add(List2);
  474. 200 presort.add(List3);
  475. 201 presort.add(List4);
  476. 202 presort.add(List5);
  477. 203 presort.add(List6);
  478. 204 presort.add(List7);
  479. 205 presort.add(List8);
  480. 206 presort.add(List9);
  481. 207 presort.add(List0);
  482. 208
  483. 209 //System.out.println(list2);
  484. 210 Sort sort = new Sort();
  485. 211 System.out.print("Sorting None");
  486. 212
  487. 213 ArrayList<Co2> sortedlist = sort.MySort(presort, "");
  488. 214 System.out.println(sortedlist);
  489. 215 String stringlist = "" + sortedlist;
  490. 216 assertEquals("[ Co2 [id1=7, time=11, co2_reading=10.01,
  491. sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2
  492. [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=0, time=14,
  493. co2_reading=72.0, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
  494. sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2], Co2
  495. [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2 [id1=4, time=165,
  496. co2_reading=1.05, sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0,
  497. sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
  498. sensor_id=2]]",stringlist);
  499. 217 }
  500. 218 }
  501. 219
  502. Page 5
  503. Sort.java
  504. 1 package uk.org.kelsohighschool.ps.program.stuff;
  505. 2
  506. 3 import java.util.ArrayList;
  507. 8
  508. 9 public class Sort {
  509. 10 /**
  510. 11 * ArrayList to sort
  511. 12 */
  512. 13 private ArrayList<Co2> List1;
  513. 14 /**
  514. 15 * string to store the sort type
  515. 16 */
  516. 17 private String sorttype;
  517. 18
  518. 19 /**
  519. 20 * Sorts in different ways by passing values on to other functions
  520. 21 * @param ListIn ArrayList to be sorted
  521. 22 * @param sorttype The way list needs to be sorted, Possible values,
  522. new, big, small and other (default)
  523. 23 * @return The sorted list
  524. 24 */
  525. 25 public ArrayList<Co2> MySort(ArrayList<Co2> ListIn,String sorttype){
  526. 26 ArrayList<Co2> ListOut;
  527. 27
  528. 28 if (sorttype.equals("new")){
  529. 29 ListOut = sortnew(ListIn);
  530. 30 return ListOut;
  531. 31 }
  532. 32 if (sorttype.equals("big")){
  533. 33 ListOut = sortbig(ListIn);
  534. 34 return ListOut;
  535. 35 }
  536. 36 if (sorttype.equals("small")){
  537. 37 ListOut = sortsmall(ListIn);
  538. 38 return ListOut;
  539. 39 }
  540. 40 else {
  541. 41 ListOut = sortold(ListIn);
  542. 42 return ListOut;
  543. 43 }
  544. 44
  545. 45 }
  546. 46
  547. 47
  548. 48 /**
  549. 49 * Sorts the list oldest to newest
  550. 50 * @param ListSorted The list to be sorted
  551. 51 * @return The List sorted oldest to newest
  552. 52 */
  553. 53 public ArrayList<Co2> sortold(ArrayList<Co2> ListSorted){
  554. 54 for(Integer out = ListSorted.size()-1; out>1; out--) {
  555. 55 for(Integer i = 0; i < out;i ++){
  556. 56 int n = i+1;
  557. Page 1
  558. Sort.java
  559. 57 Co2 resulti = ListSorted.get(i);
  560. 58 Co2 resultn = ListSorted.get(n);
  561. 59 if(resulti.getTime() > resultn.getTime()) {
  562. 60 Co2 temp = ListSorted.get(i);
  563. 61 ListSorted.set(i, ListSorted.get(n));
  564. 62 ListSorted.set(n, temp);
  565. 63 }
  566. 64 }
  567. 65 }
  568. 66 return ListSorted;
  569. 67 }
  570. 68
  571. 69
  572. 70 /**
  573. 71 * Runs the Oldest to Newest sort then reverses it
  574. 72 * @param ListSorted The list to be sorted
  575. 73 * @return The List sorted newest to oldest
  576. 74 */
  577. 75 public ArrayList<Co2> sortnew(ArrayList<Co2> ListSorted){
  578. 76 ListSorted = sortold(ListSorted);
  579. 77 Collections.reverse(ListSorted);
  580. 78 return ListSorted;
  581. 79 }
  582. 80
  583. 81
  584. 82
  585. 83 /**
  586. 84 * Runs the Lowest to Highest sort then reverses it
  587. 85 * @param ListSorted The list to be sorted
  588. 86 * @return The List sorted Highest to Lowest
  589. 87 */
  590. 88 public ArrayList<Co2> sortbig(ArrayList<Co2> ListSorted){
  591. 89 ListSorted = sortsmall(ListSorted);
  592. 90 Collections.reverse(ListSorted);
  593. 91 return ListSorted;
  594. 92
  595. 93 }
  596. 94
  597. 95
  598. 96 /**
  599. 97 * Sorts the list Lowest to Highest
  600. 98 * @param ListSorted The list to be sorted
  601. 99 * @return The List sorted Lowest to Highest
  602. 100 */
  603. 101 public ArrayList<Co2> sortsmall(ArrayList<Co2> ListSorted){
  604. 102 for(Integer out = ListSorted.size()-1; out>1; out--) {
  605. 103 for(Integer i = 0; i < out;i ++){
  606. 104 int n = i+1;
  607. 105 Co2 resulti = ListSorted.get(i);
  608. 106 Co2 resultn = ListSorted.get(n);
  609. 107 if(resulti.getCo2_reading() > resultn.getCo2_reading
  610. ()) {
  611. 108 Co2 temp = ListSorted.get(i);
  612. Page 2
  613. Sort.java
  614. 109 ListSorted.set(i, ListSorted.get(n));
  615. 110 ListSorted.set(n, temp);
  616. 111 }
  617. 112 }
  618. 113 }
  619. 114 return ListSorted;
  620. 115 }
  621. 116
  622. 117
  623. 118 /**
  624. 119 * Autogenerated getter
  625. 120 * @return List1
  626. 121 */
  627. 122 public ArrayList<Co2> getList1() {
  628. 123 return List1;
  629. 124 }
  630. 125
  631. 126 /**
  632. 127 * Autogenerated setter
  633. 128 * @param list1 list to set list1 to
  634. 129 */
  635. 130 public void setList1(ArrayList<Co2> list1) {
  636. 131 List1 = list1;
  637. 132 }
  638. 133
  639. 134 /**
  640. 135 * Autogenerated getter
  641. 136 * @return sorttype
  642. 137 */
  643. 138 public String getSorttype() {
  644. 139 return sorttype;
  645. 140 }
  646. 141
  647. 142 /**
  648. 143 * Autogenerated setter
  649. 144 * @param sorttype string to set sorttype to
  650. 145 */
  651. 146 public void setSorttype(String sorttype) {
  652. 147 this.sorttype = sorttype;
  653. 148 }
  654. 149 }
  655. 150
  656. Page 3
  657. co2.java
  658. 1 package uk.org.kelsohighschool.ps.program.servlets;
  659. 2
  660. 3 import java.io.IOException;
  661. 18
  662. 19 /**
  663. 20 * Servlet implementation class test
  664. 21 */
  665. 22 @WebServlet(description = "Thing", urlPatterns = { "/co2" })
  666. 23 public class co2 extends HttpServlet {
  667. 24
  668. 25 /**
  669. 26 * URL to connect to
  670. 27 */
  671. 28 private String url = "jdbc:postgresql://localhost/outcometest";
  672. 29 /**
  673. 30 * Username to login as
  674. 31 */
  675. 32 private String username = "postgres";
  676. 33 /**
  677. 34 * Password for login
  678. 35 */
  679. 36 private String password = "postgres";
  680. 37 private static final long serialVersionUID = 1L;
  681. 38
  682. 39 /**
  683. 40 * @see HttpServlet#HttpServlet()
  684. 41 */
  685. 42 public co2() {
  686. 43 super();
  687. 44 // TODO Auto-generated constructor stub
  688. 45 }
  689. 46
  690. 47 /**
  691. 48 * @see javax.servlet.http.HttpServlet#doGet
  692. (javax.servlet.http.HttpServletRequest,
  693. javax.servlet.http.HttpServletResponse)
  694. 49 */
  695. 50 protected void doGet(HttpServletRequest request, HttpServletResponse
  696. response) throws ServletException, IOException {
  697. 51 // TODO Auto-generated method stub
  698. 52
  699. 53 try {
  700. 54 Class.forName("org.postgresql.Driver"); //TO COMMENT
  701. 55 } catch (ClassNotFoundException e) {
  702. 56 // TODO Auto-generated catch block
  703. 57 e.printStackTrace();
  704. 58 }
  705. 59
  706. 60 response.getWriter().append("Served at: ").append
  707. (request.getContextPath());
  708. 61 String sort = request.getParameter("sort"); //Sets sort to the
  709. part ?sort="" of the URL
  710. 62
  711. Page 1
  712. co2.java
  713. 63 if (sort == null){ //Defaults to old if there is no value
  714. 64 sort = "old";
  715. 65 }
  716. 66
  717. 67 request.setAttribute("sort", sort); //TO COMMENT
  718. 68
  719. 69 try { //autogenerated try catch to surround the DatabaseConnector
  720. 70 DatabaseConnector dc = new DatabaseConnector
  721. (url,username,password); //TO COMMENT
  722. 71 ArrayList<Co2> List1 = dc.getCo2(); //TO COMMENT
  723. 72 //Getting size of db and passing to jsp
  724. 73 Integer number_of_enteries = List1.size(); //TO COMMENT
  725. 74 request.setAttribute("number_of_enteries",
  726. number_of_enteries); //TO COMMENT
  727. 75
  728. 76 Integer x_in_min = 30000000; //Debug
  729. 77 request.setAttribute("x_in_min", x_in_min); //TO COMMENT
  730. 78 long x_in_secs = x_in_min * 60;
  731. 79
  732. 80 long time1 = System.currentTimeMillis()/1000; //TO COMMENT
  733. 81 request.setAttribute("time1", time1); //TO COMMENT
  734. 82
  735. 83 ArrayList<Co2> results = dc.getCo2(); //TO COMMENT
  736. 84
  737. 85 int enteries_in_last_x = 0; //TO COMMENT
  738. 86
  739. 87 for(int i=0;i< results.size();i++){ //TO COMMENT
  740. 88 Co2 result = results.get(i);
  741. 89 int ts = result.getTime();
  742. 90
  743. 91
  744. 92 if(ts > time1 - x_in_secs){ //TO COMMENT
  745. 93 enteries_in_last_x++;
  746. 94 }
  747. 95 }
  748. 96
  749. 97 request.setAttribute("enteries_in_last_x",
  750. enteries_in_last_x); //TO COMMENT
  751. 98
  752. 99 Integer enteries_not_in_last_x = number_of_enteries -
  753. enteries_in_last_x; //TO COMMENT
  754. 100 request.setAttribute("enteries_not_in_last_x",
  755. enteries_not_in_last_x); //TO COMMENT
  756. 101 dc.close();
  757. 102
  758. 103
  759. 104
  760. 105 } catch (SQLException e) {
  761. 106 // TODO Auto-generated catch block
  762. 107 e.printStackTrace();
  763. 108 }
  764. 109
  765. 110 try { //autogenerated try catch to surround the DatabaseConnector
  766. Page 2
  767. co2.java
  768. 111 DatabaseConnector dc = new DatabaseConnector
  769. (url,username,password);
  770. 112 ArrayList<Co2> this_working = dc.getCo2(); //TO COMMENT
  771. 113 Sort ms = new Sort();
  772. 114 ArrayList<Co2> sorted_list = ms.MySort(this_working,sort);
  773. 115 request.setAttribute("test", sorted_list);
  774. 116 dc.close();
  775. 117 } catch (SQLException e) {
  776. 118 // TODO Auto-generated catch block
  777. 119 e.printStackTrace();
  778. 120 } //TO COMMENT
  779. 121
  780. 122 getServletContext().getRequestDispatcher("/jsp/index.jsp").forward
  781. (request,response); //TO COMMENT
  782. 123 }
  783. 124
  784. 125 /**
  785. 126 * @see HttpServlet#doPost(HttpServletRequest request,
  786. HttpServletResponse response)
  787. 127 */
  788. 128 protected void doPost(HttpServletRequest request, HttpServletResponse
  789. response) throws ServletException, IOException {
  790. 129 // TODO Auto-generated method stub
  791. 130 doGet(request, response);
  792. 131 }
  793. 132
  794. 133 }
  795. 134
  796. Page 3
  797. Co2.java
  798. 1 package uk.org.kelsohighschool.ps.program.db;
  799. 2
  800. 3 public class Co2 {
  801. 4 private int id1;
  802. 5 private int time;
  803. 6 private float co2_reading;
  804. 7 private int sensor_id;
  805. 8
  806. 9 /**
  807. 10 * Object which represents the Co2 table
  808. 11 * @param id1 Unique Int
  809. 12 * @param time Time as an Int
  810. 13 * @param co2_reading co2 reading as a float
  811. 14 * @param sensor_id sensor id as Int
  812. 15 */
  813. 16 public Co2(int id1, int time, float co2_reading, int sensor_id) {
  814. 17 super();
  815. 18 this.id1 = id1;
  816. 19 this.time = time;
  817. 20 this.co2_reading = co2_reading;
  818. 21 this.sensor_id = sensor_id;
  819. 22 }
  820. 23
  821. 24 /**
  822. 25 * gets and returns id1
  823. 26 * @return id1
  824. 27 */
  825. 28 public int getId1() {
  826. 29 return id1;
  827. 30 }
  828. 31
  829. 32 /**
  830. 33 * sets id1
  831. 34 * @param id1 Value to set id1 to
  832. 35 */
  833. 36 public void setId1(int id1) {
  834. 37 this.id1 = id1;
  835. 38 }
  836. 39
  837. 40 /**
  838. 41 * gets and returns time
  839. 42 * @return time
  840. 43 */
  841. 44 public int getTime() {
  842. 45 return time;
  843. 46 }
  844. 47
  845. 48 /**
  846. 49 * sets time
  847. 50 * @param time Value to set time to
  848. 51 */
  849. 52 public void setTime(int time) {
  850. 53 this.time = time;
  851. Page 1
  852. Co2.java
  853. 54 }
  854. 55
  855. 56 /**
  856. 57 * Gets and returns co2_reading
  857. 58 * @return co2_reading
  858. 59 */
  859. 60 public float getCo2_reading() {
  860. 61 return co2_reading;
  861. 62 }
  862. 63
  863. 64 /**
  864. 65 * sets co2_reading
  865. 66 * @param co2_reading Value to set co2_reading to
  866. 67 */
  867. 68 public void setCo2_reading(float co2_reading) {
  868. 69 this.co2_reading = co2_reading;
  869. 70 }
  870. 71
  871. 72 /**
  872. 73 * Gets and returns sensor_id
  873. 74 * @return sensor_id
  874. 75 */
  875. 76 public int getSensor_id() {
  876. 77 return sensor_id;
  877. 78 }
  878. 79
  879. 80 /**
  880. 81 * sets sensor_id
  881. 82 * @param sensor_id Value to set sensor_id to
  882. 83 */
  883. 84 public void setSensor_id(int sensor_id) {
  884. 85 this.sensor_id = sensor_id;
  885. 86 }
  886. 87
  887. 88
  888. 89
  889. 90 /* (non-Javadoc)
  890. 91 * @see java.lang.Object#toString()
  891. 92 */
  892. 93 @Override
  893. 94 public String toString() {
  894. 95 return " Co2 [id1=" + id1 + ", time=" + time + ", co2_reading=" +
  895. co2_reading + ", sensor_id=" + sensor_id + "]";
  896. 96 }
  897. 97
  898. 98
  899. 99 }
  900. Page 2
  901. Location.java
  902. 1 package uk.org.kelsohighschool.ps.program.db;
  903. 2
  904. 3 public class Location {
  905. 4 private int location_id;
  906. 5 private String location_name;
  907. 6 private String location_desc;
  908. 7
  909. 8 /**
  910. 9 * Object which represents the location table
  911. 10 * @param location_id id for each location Int
  912. 11 * @param location_name Name of each location String
  913. 12 * @param location_desc Description of each location String
  914. 13 */
  915. 14 public Location(int location_id, String location_name, String
  916. location_desc) {
  917. 15 super();
  918. 16 this.location_id = location_id;
  919. 17 this.location_name = location_name;
  920. 18 this.location_desc = location_desc;
  921. 19 }
  922. 20
  923. 21
  924. 22
  925. 23 /**
  926. 24 * gets and returns location_id
  927. 25 * @return location_id
  928. 26 */
  929. 27 public int getLocation_id() {
  930. 28 return location_id;
  931. 29 }
  932. 30
  933. 31
  934. 32
  935. 33 /**
  936. 34 * sets location_id
  937. 35 * @param location_id Value to set location_id to
  938. 36 */
  939. 37 public void setLocation_id(int location_id) {
  940. 38 this.location_id = location_id;
  941. 39 }
  942. 40
  943. 41
  944. 42
  945. 43 /**
  946. 44 * gets and returns location_name
  947. 45 * @return location_name
  948. 46 */
  949. 47 public String getLocation_name() {
  950. 48 return location_name;
  951. 49 }
  952. 50
  953. 51
  954. 52
  955. Page 1
  956. Location.java
  957. 53 /**
  958. 54 * sets location_name
  959. 55 * @param location_name Value to set location_name to
  960. 56 */
  961. 57 public void setLocation_name(String location_name) {
  962. 58 this.location_name = location_name;
  963. 59 }
  964. 60
  965. 61
  966. 62
  967. 63 /**
  968. 64 * gets and returns location_desc
  969. 65 * @return location_desc
  970. 66 */
  971. 67 public String getLocation_desc() {
  972. 68 return location_desc;
  973. 69 }
  974. 70
  975. 71
  976. 72
  977. 73 /**
  978. 74 * sets location_desc
  979. 75 * @param location_desc location_id Value to set to
  980. 76 */
  981. 77 public void setLocation_desc(String location_desc) {
  982. 78 this.location_desc = location_desc;
  983. 79 }
  984. 80
  985. 81
  986. 82
  987. 83 /* (non-Javadoc)
  988. 84 * @see java.lang.Object#toString()
  989. 85 */
  990. 86 @Override
  991. 87 public String toString() {
  992. 88 return "Location [location_id=" + location_id + ", location_name="
  993. + location_name + ", location_desc="
  994. 89 + location_desc + "]";
  995. 90 }
  996. 91
  997. 92 }
  998. 93
  999. Page 2
  1000. Temp.java
  1001. 1 package uk.org.kelsohighschool.ps.program.db;
  1002. 2
  1003. 3 import java.sql.Timestamp;
  1004. 4
  1005. 5 public class Temp {
  1006. 6
  1007. 7 private int id2;
  1008. 8 private Timestamp time;
  1009. 9 private float temp_reading;
  1010. 10 private int sensor_id;
  1011. 11
  1012. 12
  1013. 13 /**
  1014. 14 * Object which represents the temp table
  1015. 15 * @param id2 Unique Int
  1016. 16 * @param time Time as an Int
  1017. 17 * @param temp_reading temp reading as a float
  1018. 18 * @param sensor_id sensor id as Int
  1019. 19 */
  1020. 20 public Temp(int id2, Timestamp time, float temp_reading, int sensor_id)
  1021. {
  1022. 21 super();
  1023. 22 this.id2 = id2;
  1024. 23 this.time = time;
  1025. 24 this.temp_reading = temp_reading;
  1026. 25 this.sensor_id = sensor_id;
  1027. 26 }
  1028. 27 /**
  1029. 28 * gets and returns id2
  1030. 29 * @return id2
  1031. 30 */
  1032. 31 public int getId2() {
  1033. 32 return id2;
  1034. 33 }
  1035. 34 /**
  1036. 35 * sets Id2
  1037. 36 * @param id2 Value to set id2 to
  1038. 37 */
  1039. 38 public void setId2(int id2) {
  1040. 39 this.id2 = id2;
  1041. 40 }
  1042. 41 /**
  1043. 42 * gets and returns time
  1044. 43 * @return time
  1045. 44 */
  1046. 45 public Timestamp getTime() {
  1047. 46 return time;
  1048. 47 }
  1049. 48 /**
  1050. 49 * sets time
  1051. 50 * @param time Value to set time to
  1052. 51 */
  1053. 52 public void setTime(Timestamp time) {
  1054. Page 1
  1055. Temp.java
  1056. 53 this.time = time;
  1057. 54 }
  1058. 55 /**
  1059. 56 * gets and returns temp_reading
  1060. 57 * @return temp_reading
  1061. 58 */
  1062. 59 public float getTemp_reading() {
  1063. 60 return temp_reading;
  1064. 61 }
  1065. 62 /**
  1066. 63 * sets temp_reading
  1067. 64 * @param temp_reading Value to set temp_reading to
  1068. 65 */
  1069. 66 public void setTemp_reading(float temp_reading) {
  1070. 67 this.temp_reading = temp_reading;
  1071. 68 }
  1072. 69 public int getSensor_id() {
  1073. 70 return sensor_id;
  1074. 71 }
  1075. 72 /**
  1076. 73 * sets sensor_id
  1077. 74 * @param sensor_id Value to set sensor_id to
  1078. 75 */
  1079. 76 public void setSensor_id(int sensor_id) {
  1080. 77 this.sensor_id = sensor_id;
  1081. 78
  1082. 79 }
  1083. 80
  1084. 81 @Override
  1085. 82 public String toString() {
  1086. 83 return "Temp [id2=" + id2 + ", time=" + time + ", temp_reading=" +
  1087. temp_reading + ", sensor_id=" + sensor_id
  1088. 84 + "]";
  1089. 85 }
  1090. 86
  1091. 87
  1092. 88 }
  1093. 89
  1094. Page 2
  1095. DatabaseConnector.java
  1096. 1 package uk.org.kelsohighschool.ps.program.db;
  1097. 2
  1098. 3 import java.sql.*;
  1099. 5
  1100. 6 public class DatabaseConnector {
  1101. 7 private Connection conn;
  1102. 8 private Statement statement;
  1103. 9 private ResultSet resultSet;
  1104. 10
  1105. 11
  1106. 12 /*private static boolean testingMode = false;
  1107. 13
  1108. 14 public static void enableTesting() {
  1109. 15 testingMode = true;
  1110. 16 }
  1111. 17
  1112. 18 */
  1113. 19 /**
  1114. 20 * For connecting to the database
  1115. 21 * @param url URL to connect to
  1116. 22 * @param username Username to use
  1117. 23 * @param password Password to use
  1118. 24 * @throws SQLException
  1119. 25 */
  1120. 26 public DatabaseConnector(String url,String username,String password)
  1121. throws SQLException {
  1122. 27 conn = DriverManager.getConnection(url,username,password);
  1123. 28 }
  1124. 29
  1125. 30 /**
  1126. 31 * @return Version of psql currently running
  1127. 32 * @throws SQLException
  1128. 33 */
  1129. 34 public String GetVer() throws SQLException {
  1130. 35 statement = conn.createStatement();
  1131. 36 resultSet = statement.executeQuery("show server_version");
  1132. 37 String rtn = "";
  1133. 38 if (resultSet.next()) {
  1134. 39 rtn = resultSet.getString(1);
  1135. 40 }
  1136. 41 return rtn;
  1137. 42 }
  1138. 43 /**
  1139. 44 * gets and returns a full arraylist with all the values in the co2
  1140. table
  1141. 45 * @return full arraylist with all the values in the co2 table
  1142. 46 * @throws SQLException
  1143. 47 */
  1144. 48 public ArrayList<Co2> getCo2() throws SQLException {
  1145. 49 statement = conn.createStatement();
  1146. 50 resultSet = statement.executeQuery("select * from co2");
  1147. 51 ArrayList<Co2> rtnVal = new ArrayList<Co2>();
  1148. 52 while (resultSet.next()) {
  1149. Page 1
  1150. DatabaseConnector.java
  1151. 53 Co2 co2= new Co2(resultSet.getInt(1),resultSet.getInt
  1152. (4),resultSet.getFloat(2),resultSet.getInt(3));
  1153. 54 rtnVal.add(co2);
  1154. 55 //System.out.print(co2);
  1155. 56 }
  1156. 57 return rtnVal;
  1157. 58 }
  1158. 59
  1159. 60 /**
  1160. 61 * gets and returns a full arraylist with all the values in the
  1161. location table
  1162. 62 * @return full arraylist with all the values in the location table
  1163. 63 * @throws SQLException
  1164. 64 */
  1165. 65 public ArrayList<Location> getLocation() throws SQLException {
  1166. 66 statement = conn.createStatement();
  1167. 67 resultSet = statement.executeQuery("select * from location");
  1168. 68 ArrayList<Location> rtnVal = new ArrayList<Location>();
  1169. 69 while (resultSet.next()) {
  1170. 70 Location location= new Location(resultSet.getInt
  1171. (1),resultSet.getString(2),resultSet.getString(3));
  1172. 71 rtnVal.add(location);
  1173. 72 System.out.print(location);
  1174. 73 }
  1175. 74 return rtnVal;
  1176. 75 }
  1177. 76
  1178. 77 /**
  1179. 78 * gets and returns a full arraylist with all the values in the temp
  1180. table
  1181. 79 * @return full arraylist with all the values in the temp table
  1182. 80 * @throws SQLException
  1183. 81 */
  1184. 82 public ArrayList<Temp> getTemp() throws SQLException {
  1185. 83 statement = conn.createStatement();
  1186. 84 resultSet = statement.executeQuery("select * from temp");
  1187. 85 ArrayList<Temp> rtnVal = new ArrayList<Temp>();
  1188. 86 while (resultSet.next()) {
  1189. 87 Temp temp= new Temp(resultSet.getInt(1),resultSet.getTimestamp
  1190. (2),resultSet.getFloat(3),resultSet.getInt(4));
  1191. 88 rtnVal.add(temp);
  1192. 89 System.out.print(temp);
  1193. 90 }
  1194. 91 return rtnVal;
  1195. 92 }
  1196. 93
  1197. 94 /**
  1198. 95 * Closes the database connection
  1199. 96 */
  1200. 97 public void close() {
  1201. 98 // TODO Auto-generated method stub
  1202. 99 try {
  1203. 100 if (resultSet != null) {
  1204. Page 2
  1205. DatabaseConnector.java
  1206. 101 resultSet.close();
  1207. 102 }
  1208. 103 if (statement != null) {
  1209. 104 statement.close();
  1210. 105 }
  1211. 106 if (conn != null) {
  1212. 107 conn.close();
  1213. 108 }
  1214. 109 } catch (SQLException e) {
  1215. 110 e.printStackTrace();
  1216. 111 }
  1217. 112 }
  1218. 113 }
  1219. 114
  1220. Page 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement