Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index.jsp
- 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
- 2 pageEncoding="UTF-8"%>
- 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- 4 <!DOCTYPE html>
- 5
- 6 <html>
- 7
- 8 <head>
- 9
- 10 <title>CO2 Readings</title>
- 11 <meta name="viewport" content="width=device-width, initial-scale=1">
- 12 <link rel="stylesheet" href="http://localhost:8080/project/jquery/
- jquery.mobile-1.4.5.min.css" />
- 13 <script src="http://localhost:8080/project/jquery/
- jquery-1.12.4.min.js"></script>
- 14 <script src="http://localhost:8080/project/jquery/
- jquery.mobile-1.4.5.min.js"></script>
- 15
- 16 <style>
- 17
- 18 table {
- 19 font-family: arial, sans-serif;
- 20 border-collapse: collapse;
- 21 width: 50%;
- 22 }
- 23
- 24 td, th {
- 25 border: 1px solid #000000;
- 26 padding: 10px 15px 15px 10px;
- 27 text-align: left;
- 28 }
- 29
- 30 tr:nth-child(even) {
- 31 background-color: #dddddd;
- 32 }
- 33
- 34 </style>
- 35
- 36 </head>
- 37
- 38 <body>
- 39
- 40 <div data-role="header" data-theme="b">
- 41 <h1>CO2 Readings</h1>
- 42 </div>
- 43
- 44 <div style="padding:30px;"> <!-- Padding div -->
- 45
- 46
- 47 <div>
- 48 <c:if test="${ sort == 'new' }">
- 49 Showing Newest First
- 50 </c:if>
- Page 1
- index.jsp
- 51
- 52 <c:if test="${ sort == 'old' }">
- 53 Showing Oldest First
- 54 </c:if>
- 55
- 56 <c:if test="${ sort != 'old' }">
- 57 <c:if test="${ sort != 'new' }">
- 58 Showing Oldest First
- 59 </c:if>
- 60 </c:if>
- 61
- 62 </div>
- 63 <br/>
- 64 <br/>
- 65
- 66
- 67 <div > <!-- Table div -->
- 68
- 69 <c:if test="${ enteries_in_last_x > 0 }">
- 70 <table>
- 71 <tr>
- 72 <th style="font-size:150%"> Time </th>
- 73 <th style="font-size:150%"> CO2 Reading</th>
- 74 <th style="font-size:150%"> Location ID</th>
- 75 </tr>
- 76
- 77 <c:forEach var="co2" items="${ test }">
- 78 <tr>
- 79 <td> ${ co2.getTime() }</td>
- 80 <td> ${ co2.getCo2_reading() }</td>
- 81 <td> ${ co2.getSensor_id() }</td>
- 82 </tr>
- 83 </c:forEach>
- 84
- 85 </table>
- 86 </c:if>
- 87 </div> <!-- Table div -->
- 88
- 89 <div>
- 90 <c:if test="${ enteries_in_last_x == 0 }">There were no reading in
- the last ${ x_in_min } minutes.</c:if>
- 91 </div>
- 92
- 93 <br>
- 94
- 95 <div>
- 96 <c:if test="${ enteries_not_in_last_x == 0 }">
- 97 There are no more values to be shown.
- 98
- 99 </c:if>
- 100 </div>
- 101
- 102 <div>
- Page 2
- index.jsp
- 103 <c:if test="${ enteries_not_in_last_x > 0 }">
- 104 Show ${ enteries_not_in_last_x }
- 105 <c:if test="${ enteries_in_last_x != 0 }">
- 106 more
- 107 </c:if>
- 108 values.
- 109 </c:if>
- 110 </div>
- 111
- 112 <br/>
- 113 <br/>
- 114
- 115 <div class="ui-bar ui-bar-a ui-grid-c">
- 116 <div class="ui-block-a" style="padding:10px;">
- 117 <form method="get">
- 118 <input type="submit" name="sort" value="new">
- 119 </form>
- 120 </div>
- 121 <div class="ui-block-b" style="padding:10px;">
- 122 <form method="get">
- 123 <input type="submit" name="sort" value="old">
- 124 </form>
- 125 </div>
- 126 <div class="ui-block-c" style="padding:10px;">
- 127 <form method="get">
- 128 <input type="submit" name="sort" value="small">
- 129 </form>
- 130 </div>
- 131 <div class="ui-block-d" style="padding:10px;">
- 132 <form method="get">
- 133 <input type="submit" name="sort" value="big">
- 134 </form>
- 135 </div>
- 136 </div>
- 137 </div> <!-- Padding div -->
- 138
- 139 </body>
- 140
- 141 </html>
- Page 3
- DatabaseConnectorTest.java
- 1 package uk.org.kelsohighschool.ps.program.tests;
- 2
- 3 import static org.junit.Assert.*;
- 4
- 5 import java.sql.SQLException;
- 6
- 7 import org.junit.Test;
- 8
- 9 import uk.org.kelsohighschool.ps.program.db.DatabaseConnector;
- 10
- 11 public class DatabaseConnectorTest {
- 12 /**
- 13 * URL to connect to
- 14 */
- 15 private String url = "jdbc:postgresql://localhost/outcometest";
- 16 /**
- 17 * Username to login as
- 18 */
- 19 private String username = "postgres";
- 20 /**
- 21 * Password for login
- 22 */
- 23 private String password = "postgres";
- 24
- 25 /**
- 26 * tests the database connection
- 27 * @throws SQLException
- 28 */
- 29 @Test
- 30 public void testDatabaseConnector() throws SQLException {
- 31 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 32 dc.close();
- 33 }
- 34
- 35 /**
- 36 * gets the sql version then checks if it is 9.5.4
- 37 * @throws SQLException
- 38 */
- 39 @Test
- 40 public void testGetVer() throws SQLException {
- 41 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 42 assertEquals("9.5.6",dc.GetVer());
- 43 dc.close();
- 44 }
- 45
- 46 /**
- 47 * tests the getco2 function
- 48 * @throws SQLException
- 49 */
- 50 @Test
- 51 public void testGetCo2() throws SQLException {
- Page 1
- DatabaseConnectorTest.java
- 52 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 53 dc.getCo2();
- 54 dc.close();
- 55 }
- 56 /**
- 57 * tests the getlocation function
- 58 * @throws SQLException
- 59 */
- 60 @Test
- 61 public void testGetLocation() throws SQLException {
- 62 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 63 dc.getLocation();
- 64 dc.close();
- 65 }
- 66 /**
- 67 * tests the temp function
- 68 * @throws SQLException
- 69 */
- 70 @Test
- 71 public void testGetTemp() throws SQLException {
- 72 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 73 dc.getTemp();
- 74 dc.close();
- 75 }
- 76
- 77 }
- 78
- Page 2
- SortTest.java
- 1 package uk.org.kelsohighschool.ps.program.tests;
- 2
- 3 import static org.junit.Assert.*;
- 11
- 12 public class SortTest {
- 13
- 14
- 15 /**
- 16 * Tests sorting newest to oldest
- 17 */
- 18 @Test
- 19 public void testSortNew(){
- 20
- 21 // id time Value Location
- 22 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
- 23 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
- 24 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
- 25 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
- 26 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
- 27 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
- 28 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
- 29 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
- 30 Co2 List9 = new Co2(9, 167, (float) 2, 2);
- 31 Co2 List0 = new Co2(0, 14, (float) 72, 2);
- 32
- 33 ArrayList<Co2> presort = new ArrayList<Co2>();
- 34 //System.out.println(List1);
- 35 presort.add(List1);
- 36 presort.add(List2);
- 37 presort.add(List3);
- 38 presort.add(List4);
- 39 presort.add(List5);
- 40 presort.add(List6);
- 41 presort.add(List7);
- 42 presort.add(List8);
- 43 presort.add(List9);
- 44 presort.add(List0);
- 45
- 46 //System.out.println(list2);
- 47 Sort sort = new Sort();
- 48 System.out.print("Sorting new");
- 49
- 50 ArrayList<Co2> sortedlist = sort.MySort(presort, "new");
- 51 System.out.println(sortedlist);
- 52 String stringlist = "" + sortedlist;
- 53 assertEquals("[ Co2 [id1=8, time=1987, co2_reading=42.0,
- sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0, sensor_id=2], Co2
- [id1=4, time=165, co2_reading=1.05, sensor_id=2], Co2 [id1=1, time=65,
- co2_reading=1.01, sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5,
- sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04, sensor_id=2], Co2
- [id1=0, time=14, co2_reading=72.0, sensor_id=2], Co2 [id1=3, time=13,
- co2_reading=1.03, sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02,
- sensor_id=2], Co2 [id1=7, time=11, co2_reading=10.01, sensor_id=2]]",
- Page 1
- SortTest.java
- sensor_id=2], Co2 [id1=7, time=11, co2_reading=10.01, sensor_id=2]]"
- stringlist);
- 54 }
- 55
- 56 /**
- 57 * Tests sorting biggest to smallest
- 58 */
- 59 @Test
- 60 public void testSortBig(){
- 61
- 62 // id time Value Location
- 63 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
- 64 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
- 65 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
- 66 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
- 67 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
- 68 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
- 69 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
- 70 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
- 71 Co2 List9 = new Co2(9, 167, (float) 2, 2);
- 72 Co2 List0 = new Co2(0, 14, (float) 72, 2);
- 73
- 74 ArrayList<Co2> presort = new ArrayList<Co2>();
- 75 //System.out.println(List1);
- 76 presort.add(List1);
- 77 presort.add(List2);
- 78 presort.add(List3);
- 79 presort.add(List4);
- 80 presort.add(List5);
- 81 presort.add(List6);
- 82 presort.add(List7);
- 83 presort.add(List8);
- 84 presort.add(List9);
- 85 presort.add(List0);
- 86
- 87 //System.out.println(list2);
- 88 Sort sort = new Sort();
- 89 System.out.print("Sorting big");
- 90
- 91 ArrayList<Co2> sortedlist = sort.MySort(presort, "big");
- 92 System.out.println(sortedlist);
- 93 String stringlist = "" + sortedlist;
- 94 assertEquals("[ Co2 [id1=0, time=14, co2_reading=72.0,
- sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0, sensor_id=2], Co2
- [id1=7, time=11, co2_reading=10.01, sensor_id=2], Co2 [id1=9, time=167,
- co2_reading=2.0, sensor_id=2], Co2 [id1=4, time=165, co2_reading=1.05,
- sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04, sensor_id=2], Co2
- [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=2, time=12,
- co2_reading=1.02, sensor_id=2], Co2 [id1=1, time=65, co2_reading=1.01,
- sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2]]",
- stringlist);
- 95 }
- 96 /**
- 97 * Tests sorting oldest to newest
- Page 2
- SortTest.java
- 98 */
- 99 @Test
- 100 public void testSortOld(){
- 101
- 102 // id time Value Location
- 103 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
- 104 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
- 105 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
- 106 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
- 107 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
- 108 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
- 109 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
- 110 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
- 111 Co2 List9 = new Co2(9, 167, (float) 2, 2);
- 112 Co2 List0 = new Co2(0, 14, (float) 72, 2);
- 113
- 114 ArrayList<Co2> presort = new ArrayList<Co2>();
- 115 //System.out.println(List1);
- 116 presort.add(List1);
- 117 presort.add(List2);
- 118 presort.add(List3);
- 119 presort.add(List4);
- 120 presort.add(List5);
- 121 presort.add(List6);
- 122 presort.add(List7);
- 123 presort.add(List8);
- 124 presort.add(List9);
- 125 presort.add(List0);
- 126
- 127 //System.out.println(list2);
- 128 Sort sort = new Sort();
- 129 System.out.print("Sorting old");
- 130
- 131 ArrayList<Co2> sortedlist = sort.MySort(presort, "old");
- 132 System.out.println(sortedlist);
- 133 String stringlist = "" + sortedlist;
- 134 assertEquals("[ Co2 [id1=7, time=11, co2_reading=10.01,
- sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2
- [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=0, time=14,
- co2_reading=72.0, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
- sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2], Co2
- [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2 [id1=4, time=165,
- co2_reading=1.05, sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0,
- sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
- sensor_id=2]]",stringlist);
- 135 }
- 136
- 137 /**
- 138 * Tests sorting smallest to biggest
- 139 */
- 140 @Test
- 141 public void testSortSmall(){
- 142
- Page 3
- SortTest.java
- 143 // id time Value Location
- 144 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
- 145 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
- 146 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
- 147 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
- 148 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
- 149 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
- 150 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
- 151 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
- 152 Co2 List9 = new Co2(9, 167, (float) 2, 2);
- 153 Co2 List0 = new Co2(0, 14, (float) 72, 2);
- 154
- 155 ArrayList<Co2> presort = new ArrayList<Co2>();
- 156 //System.out.println(List1);
- 157 presort.add(List1);
- 158 presort.add(List2);
- 159 presort.add(List3);
- 160 presort.add(List4);
- 161 presort.add(List5);
- 162 presort.add(List6);
- 163 presort.add(List7);
- 164 presort.add(List8);
- 165 presort.add(List9);
- 166 presort.add(List0);
- 167
- 168 //System.out.println(list2);
- 169 Sort sort = new Sort();
- 170 System.out.print("Sorting small");
- 171
- 172 ArrayList<Co2> sortedlist = sort.MySort(presort, "small");
- 173 System.out.println(sortedlist);
- 174 String stringlist = "" + sortedlist;
- 175 assertEquals("[ Co2 [id1=6, time=19, co2_reading=0.5,
- sensor_id=2], Co2 [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2
- [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2 [id1=3, time=13,
- co2_reading=1.03, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
- sensor_id=2], Co2 [id1=4, time=165, co2_reading=1.05, sensor_id=2], Co2
- [id1=9, time=167, co2_reading=2.0, sensor_id=2], Co2 [id1=7, time=11,
- co2_reading=10.01, sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
- sensor_id=2], Co2 [id1=0, time=14, co2_reading=72.0, sensor_id=2]]",
- stringlist);
- 176 }
- 177
- 178 /**
- 179 * Test sorting with no sort value passed, should sort oldest to
- newest
- 180 */
- 181 @Test
- 182 public void testSortNone(){
- 183
- 184 // id time Value Location
- 185 Co2 List1 = new Co2(1, 65, (float) 1.01, 2);
- 186 Co2 List2 = new Co2(2, 12, (float) 1.02, 2);
- Page 4
- SortTest.java
- 187 Co2 List3 = new Co2(3, 13, (float) 1.03, 2);
- 188 Co2 List4 = new Co2(4, 165, (float) 1.05, 2);
- 189 Co2 List5 = new Co2(5, 16, (float) 1.04, 2);
- 190 Co2 List6 = new Co2(6, 19, (float) 0.5, 2);
- 191 Co2 List7 = new Co2(7, 11, (float) 10.01, 2);
- 192 Co2 List8 = new Co2(8, 1987, (float) 42, 2);
- 193 Co2 List9 = new Co2(9, 167, (float) 2, 2);
- 194 Co2 List0 = new Co2(0, 14, (float) 72, 2);
- 195
- 196 ArrayList<Co2> presort = new ArrayList<Co2>();
- 197 //System.out.println(List1);
- 198 presort.add(List1);
- 199 presort.add(List2);
- 200 presort.add(List3);
- 201 presort.add(List4);
- 202 presort.add(List5);
- 203 presort.add(List6);
- 204 presort.add(List7);
- 205 presort.add(List8);
- 206 presort.add(List9);
- 207 presort.add(List0);
- 208
- 209 //System.out.println(list2);
- 210 Sort sort = new Sort();
- 211 System.out.print("Sorting None");
- 212
- 213 ArrayList<Co2> sortedlist = sort.MySort(presort, "");
- 214 System.out.println(sortedlist);
- 215 String stringlist = "" + sortedlist;
- 216 assertEquals("[ Co2 [id1=7, time=11, co2_reading=10.01,
- sensor_id=2], Co2 [id1=2, time=12, co2_reading=1.02, sensor_id=2], Co2
- [id1=3, time=13, co2_reading=1.03, sensor_id=2], Co2 [id1=0, time=14,
- co2_reading=72.0, sensor_id=2], Co2 [id1=5, time=16, co2_reading=1.04,
- sensor_id=2], Co2 [id1=6, time=19, co2_reading=0.5, sensor_id=2], Co2
- [id1=1, time=65, co2_reading=1.01, sensor_id=2], Co2 [id1=4, time=165,
- co2_reading=1.05, sensor_id=2], Co2 [id1=9, time=167, co2_reading=2.0,
- sensor_id=2], Co2 [id1=8, time=1987, co2_reading=42.0,
- sensor_id=2]]",stringlist);
- 217 }
- 218 }
- 219
- Page 5
- Sort.java
- 1 package uk.org.kelsohighschool.ps.program.stuff;
- 2
- 3 import java.util.ArrayList;
- 8
- 9 public class Sort {
- 10 /**
- 11 * ArrayList to sort
- 12 */
- 13 private ArrayList<Co2> List1;
- 14 /**
- 15 * string to store the sort type
- 16 */
- 17 private String sorttype;
- 18
- 19 /**
- 20 * Sorts in different ways by passing values on to other functions
- 21 * @param ListIn ArrayList to be sorted
- 22 * @param sorttype The way list needs to be sorted, Possible values,
- new, big, small and other (default)
- 23 * @return The sorted list
- 24 */
- 25 public ArrayList<Co2> MySort(ArrayList<Co2> ListIn,String sorttype){
- 26 ArrayList<Co2> ListOut;
- 27
- 28 if (sorttype.equals("new")){
- 29 ListOut = sortnew(ListIn);
- 30 return ListOut;
- 31 }
- 32 if (sorttype.equals("big")){
- 33 ListOut = sortbig(ListIn);
- 34 return ListOut;
- 35 }
- 36 if (sorttype.equals("small")){
- 37 ListOut = sortsmall(ListIn);
- 38 return ListOut;
- 39 }
- 40 else {
- 41 ListOut = sortold(ListIn);
- 42 return ListOut;
- 43 }
- 44
- 45 }
- 46
- 47
- 48 /**
- 49 * Sorts the list oldest to newest
- 50 * @param ListSorted The list to be sorted
- 51 * @return The List sorted oldest to newest
- 52 */
- 53 public ArrayList<Co2> sortold(ArrayList<Co2> ListSorted){
- 54 for(Integer out = ListSorted.size()-1; out>1; out--) {
- 55 for(Integer i = 0; i < out;i ++){
- 56 int n = i+1;
- Page 1
- Sort.java
- 57 Co2 resulti = ListSorted.get(i);
- 58 Co2 resultn = ListSorted.get(n);
- 59 if(resulti.getTime() > resultn.getTime()) {
- 60 Co2 temp = ListSorted.get(i);
- 61 ListSorted.set(i, ListSorted.get(n));
- 62 ListSorted.set(n, temp);
- 63 }
- 64 }
- 65 }
- 66 return ListSorted;
- 67 }
- 68
- 69
- 70 /**
- 71 * Runs the Oldest to Newest sort then reverses it
- 72 * @param ListSorted The list to be sorted
- 73 * @return The List sorted newest to oldest
- 74 */
- 75 public ArrayList<Co2> sortnew(ArrayList<Co2> ListSorted){
- 76 ListSorted = sortold(ListSorted);
- 77 Collections.reverse(ListSorted);
- 78 return ListSorted;
- 79 }
- 80
- 81
- 82
- 83 /**
- 84 * Runs the Lowest to Highest sort then reverses it
- 85 * @param ListSorted The list to be sorted
- 86 * @return The List sorted Highest to Lowest
- 87 */
- 88 public ArrayList<Co2> sortbig(ArrayList<Co2> ListSorted){
- 89 ListSorted = sortsmall(ListSorted);
- 90 Collections.reverse(ListSorted);
- 91 return ListSorted;
- 92
- 93 }
- 94
- 95
- 96 /**
- 97 * Sorts the list Lowest to Highest
- 98 * @param ListSorted The list to be sorted
- 99 * @return The List sorted Lowest to Highest
- 100 */
- 101 public ArrayList<Co2> sortsmall(ArrayList<Co2> ListSorted){
- 102 for(Integer out = ListSorted.size()-1; out>1; out--) {
- 103 for(Integer i = 0; i < out;i ++){
- 104 int n = i+1;
- 105 Co2 resulti = ListSorted.get(i);
- 106 Co2 resultn = ListSorted.get(n);
- 107 if(resulti.getCo2_reading() > resultn.getCo2_reading
- ()) {
- 108 Co2 temp = ListSorted.get(i);
- Page 2
- Sort.java
- 109 ListSorted.set(i, ListSorted.get(n));
- 110 ListSorted.set(n, temp);
- 111 }
- 112 }
- 113 }
- 114 return ListSorted;
- 115 }
- 116
- 117
- 118 /**
- 119 * Autogenerated getter
- 120 * @return List1
- 121 */
- 122 public ArrayList<Co2> getList1() {
- 123 return List1;
- 124 }
- 125
- 126 /**
- 127 * Autogenerated setter
- 128 * @param list1 list to set list1 to
- 129 */
- 130 public void setList1(ArrayList<Co2> list1) {
- 131 List1 = list1;
- 132 }
- 133
- 134 /**
- 135 * Autogenerated getter
- 136 * @return sorttype
- 137 */
- 138 public String getSorttype() {
- 139 return sorttype;
- 140 }
- 141
- 142 /**
- 143 * Autogenerated setter
- 144 * @param sorttype string to set sorttype to
- 145 */
- 146 public void setSorttype(String sorttype) {
- 147 this.sorttype = sorttype;
- 148 }
- 149 }
- 150
- Page 3
- co2.java
- 1 package uk.org.kelsohighschool.ps.program.servlets;
- 2
- 3 import java.io.IOException;
- 18
- 19 /**
- 20 * Servlet implementation class test
- 21 */
- 22 @WebServlet(description = "Thing", urlPatterns = { "/co2" })
- 23 public class co2 extends HttpServlet {
- 24
- 25 /**
- 26 * URL to connect to
- 27 */
- 28 private String url = "jdbc:postgresql://localhost/outcometest";
- 29 /**
- 30 * Username to login as
- 31 */
- 32 private String username = "postgres";
- 33 /**
- 34 * Password for login
- 35 */
- 36 private String password = "postgres";
- 37 private static final long serialVersionUID = 1L;
- 38
- 39 /**
- 40 * @see HttpServlet#HttpServlet()
- 41 */
- 42 public co2() {
- 43 super();
- 44 // TODO Auto-generated constructor stub
- 45 }
- 46
- 47 /**
- 48 * @see javax.servlet.http.HttpServlet#doGet
- (javax.servlet.http.HttpServletRequest,
- javax.servlet.http.HttpServletResponse)
- 49 */
- 50 protected void doGet(HttpServletRequest request, HttpServletResponse
- response) throws ServletException, IOException {
- 51 // TODO Auto-generated method stub
- 52
- 53 try {
- 54 Class.forName("org.postgresql.Driver"); //TO COMMENT
- 55 } catch (ClassNotFoundException e) {
- 56 // TODO Auto-generated catch block
- 57 e.printStackTrace();
- 58 }
- 59
- 60 response.getWriter().append("Served at: ").append
- (request.getContextPath());
- 61 String sort = request.getParameter("sort"); //Sets sort to the
- part ?sort="" of the URL
- 62
- Page 1
- co2.java
- 63 if (sort == null){ //Defaults to old if there is no value
- 64 sort = "old";
- 65 }
- 66
- 67 request.setAttribute("sort", sort); //TO COMMENT
- 68
- 69 try { //autogenerated try catch to surround the DatabaseConnector
- 70 DatabaseConnector dc = new DatabaseConnector
- (url,username,password); //TO COMMENT
- 71 ArrayList<Co2> List1 = dc.getCo2(); //TO COMMENT
- 72 //Getting size of db and passing to jsp
- 73 Integer number_of_enteries = List1.size(); //TO COMMENT
- 74 request.setAttribute("number_of_enteries",
- number_of_enteries); //TO COMMENT
- 75
- 76 Integer x_in_min = 30000000; //Debug
- 77 request.setAttribute("x_in_min", x_in_min); //TO COMMENT
- 78 long x_in_secs = x_in_min * 60;
- 79
- 80 long time1 = System.currentTimeMillis()/1000; //TO COMMENT
- 81 request.setAttribute("time1", time1); //TO COMMENT
- 82
- 83 ArrayList<Co2> results = dc.getCo2(); //TO COMMENT
- 84
- 85 int enteries_in_last_x = 0; //TO COMMENT
- 86
- 87 for(int i=0;i< results.size();i++){ //TO COMMENT
- 88 Co2 result = results.get(i);
- 89 int ts = result.getTime();
- 90
- 91
- 92 if(ts > time1 - x_in_secs){ //TO COMMENT
- 93 enteries_in_last_x++;
- 94 }
- 95 }
- 96
- 97 request.setAttribute("enteries_in_last_x",
- enteries_in_last_x); //TO COMMENT
- 98
- 99 Integer enteries_not_in_last_x = number_of_enteries -
- enteries_in_last_x; //TO COMMENT
- 100 request.setAttribute("enteries_not_in_last_x",
- enteries_not_in_last_x); //TO COMMENT
- 101 dc.close();
- 102
- 103
- 104
- 105 } catch (SQLException e) {
- 106 // TODO Auto-generated catch block
- 107 e.printStackTrace();
- 108 }
- 109
- 110 try { //autogenerated try catch to surround the DatabaseConnector
- Page 2
- co2.java
- 111 DatabaseConnector dc = new DatabaseConnector
- (url,username,password);
- 112 ArrayList<Co2> this_working = dc.getCo2(); //TO COMMENT
- 113 Sort ms = new Sort();
- 114 ArrayList<Co2> sorted_list = ms.MySort(this_working,sort);
- 115 request.setAttribute("test", sorted_list);
- 116 dc.close();
- 117 } catch (SQLException e) {
- 118 // TODO Auto-generated catch block
- 119 e.printStackTrace();
- 120 } //TO COMMENT
- 121
- 122 getServletContext().getRequestDispatcher("/jsp/index.jsp").forward
- (request,response); //TO COMMENT
- 123 }
- 124
- 125 /**
- 126 * @see HttpServlet#doPost(HttpServletRequest request,
- HttpServletResponse response)
- 127 */
- 128 protected void doPost(HttpServletRequest request, HttpServletResponse
- response) throws ServletException, IOException {
- 129 // TODO Auto-generated method stub
- 130 doGet(request, response);
- 131 }
- 132
- 133 }
- 134
- Page 3
- Co2.java
- 1 package uk.org.kelsohighschool.ps.program.db;
- 2
- 3 public class Co2 {
- 4 private int id1;
- 5 private int time;
- 6 private float co2_reading;
- 7 private int sensor_id;
- 8
- 9 /**
- 10 * Object which represents the Co2 table
- 11 * @param id1 Unique Int
- 12 * @param time Time as an Int
- 13 * @param co2_reading co2 reading as a float
- 14 * @param sensor_id sensor id as Int
- 15 */
- 16 public Co2(int id1, int time, float co2_reading, int sensor_id) {
- 17 super();
- 18 this.id1 = id1;
- 19 this.time = time;
- 20 this.co2_reading = co2_reading;
- 21 this.sensor_id = sensor_id;
- 22 }
- 23
- 24 /**
- 25 * gets and returns id1
- 26 * @return id1
- 27 */
- 28 public int getId1() {
- 29 return id1;
- 30 }
- 31
- 32 /**
- 33 * sets id1
- 34 * @param id1 Value to set id1 to
- 35 */
- 36 public void setId1(int id1) {
- 37 this.id1 = id1;
- 38 }
- 39
- 40 /**
- 41 * gets and returns time
- 42 * @return time
- 43 */
- 44 public int getTime() {
- 45 return time;
- 46 }
- 47
- 48 /**
- 49 * sets time
- 50 * @param time Value to set time to
- 51 */
- 52 public void setTime(int time) {
- 53 this.time = time;
- Page 1
- Co2.java
- 54 }
- 55
- 56 /**
- 57 * Gets and returns co2_reading
- 58 * @return co2_reading
- 59 */
- 60 public float getCo2_reading() {
- 61 return co2_reading;
- 62 }
- 63
- 64 /**
- 65 * sets co2_reading
- 66 * @param co2_reading Value to set co2_reading to
- 67 */
- 68 public void setCo2_reading(float co2_reading) {
- 69 this.co2_reading = co2_reading;
- 70 }
- 71
- 72 /**
- 73 * Gets and returns sensor_id
- 74 * @return sensor_id
- 75 */
- 76 public int getSensor_id() {
- 77 return sensor_id;
- 78 }
- 79
- 80 /**
- 81 * sets sensor_id
- 82 * @param sensor_id Value to set sensor_id to
- 83 */
- 84 public void setSensor_id(int sensor_id) {
- 85 this.sensor_id = sensor_id;
- 86 }
- 87
- 88
- 89
- 90 /* (non-Javadoc)
- 91 * @see java.lang.Object#toString()
- 92 */
- 93 @Override
- 94 public String toString() {
- 95 return " Co2 [id1=" + id1 + ", time=" + time + ", co2_reading=" +
- co2_reading + ", sensor_id=" + sensor_id + "]";
- 96 }
- 97
- 98
- 99 }
- Page 2
- Location.java
- 1 package uk.org.kelsohighschool.ps.program.db;
- 2
- 3 public class Location {
- 4 private int location_id;
- 5 private String location_name;
- 6 private String location_desc;
- 7
- 8 /**
- 9 * Object which represents the location table
- 10 * @param location_id id for each location Int
- 11 * @param location_name Name of each location String
- 12 * @param location_desc Description of each location String
- 13 */
- 14 public Location(int location_id, String location_name, String
- location_desc) {
- 15 super();
- 16 this.location_id = location_id;
- 17 this.location_name = location_name;
- 18 this.location_desc = location_desc;
- 19 }
- 20
- 21
- 22
- 23 /**
- 24 * gets and returns location_id
- 25 * @return location_id
- 26 */
- 27 public int getLocation_id() {
- 28 return location_id;
- 29 }
- 30
- 31
- 32
- 33 /**
- 34 * sets location_id
- 35 * @param location_id Value to set location_id to
- 36 */
- 37 public void setLocation_id(int location_id) {
- 38 this.location_id = location_id;
- 39 }
- 40
- 41
- 42
- 43 /**
- 44 * gets and returns location_name
- 45 * @return location_name
- 46 */
- 47 public String getLocation_name() {
- 48 return location_name;
- 49 }
- 50
- 51
- 52
- Page 1
- Location.java
- 53 /**
- 54 * sets location_name
- 55 * @param location_name Value to set location_name to
- 56 */
- 57 public void setLocation_name(String location_name) {
- 58 this.location_name = location_name;
- 59 }
- 60
- 61
- 62
- 63 /**
- 64 * gets and returns location_desc
- 65 * @return location_desc
- 66 */
- 67 public String getLocation_desc() {
- 68 return location_desc;
- 69 }
- 70
- 71
- 72
- 73 /**
- 74 * sets location_desc
- 75 * @param location_desc location_id Value to set to
- 76 */
- 77 public void setLocation_desc(String location_desc) {
- 78 this.location_desc = location_desc;
- 79 }
- 80
- 81
- 82
- 83 /* (non-Javadoc)
- 84 * @see java.lang.Object#toString()
- 85 */
- 86 @Override
- 87 public String toString() {
- 88 return "Location [location_id=" + location_id + ", location_name="
- + location_name + ", location_desc="
- 89 + location_desc + "]";
- 90 }
- 91
- 92 }
- 93
- Page 2
- Temp.java
- 1 package uk.org.kelsohighschool.ps.program.db;
- 2
- 3 import java.sql.Timestamp;
- 4
- 5 public class Temp {
- 6
- 7 private int id2;
- 8 private Timestamp time;
- 9 private float temp_reading;
- 10 private int sensor_id;
- 11
- 12
- 13 /**
- 14 * Object which represents the temp table
- 15 * @param id2 Unique Int
- 16 * @param time Time as an Int
- 17 * @param temp_reading temp reading as a float
- 18 * @param sensor_id sensor id as Int
- 19 */
- 20 public Temp(int id2, Timestamp time, float temp_reading, int sensor_id)
- {
- 21 super();
- 22 this.id2 = id2;
- 23 this.time = time;
- 24 this.temp_reading = temp_reading;
- 25 this.sensor_id = sensor_id;
- 26 }
- 27 /**
- 28 * gets and returns id2
- 29 * @return id2
- 30 */
- 31 public int getId2() {
- 32 return id2;
- 33 }
- 34 /**
- 35 * sets Id2
- 36 * @param id2 Value to set id2 to
- 37 */
- 38 public void setId2(int id2) {
- 39 this.id2 = id2;
- 40 }
- 41 /**
- 42 * gets and returns time
- 43 * @return time
- 44 */
- 45 public Timestamp getTime() {
- 46 return time;
- 47 }
- 48 /**
- 49 * sets time
- 50 * @param time Value to set time to
- 51 */
- 52 public void setTime(Timestamp time) {
- Page 1
- Temp.java
- 53 this.time = time;
- 54 }
- 55 /**
- 56 * gets and returns temp_reading
- 57 * @return temp_reading
- 58 */
- 59 public float getTemp_reading() {
- 60 return temp_reading;
- 61 }
- 62 /**
- 63 * sets temp_reading
- 64 * @param temp_reading Value to set temp_reading to
- 65 */
- 66 public void setTemp_reading(float temp_reading) {
- 67 this.temp_reading = temp_reading;
- 68 }
- 69 public int getSensor_id() {
- 70 return sensor_id;
- 71 }
- 72 /**
- 73 * sets sensor_id
- 74 * @param sensor_id Value to set sensor_id to
- 75 */
- 76 public void setSensor_id(int sensor_id) {
- 77 this.sensor_id = sensor_id;
- 78
- 79 }
- 80
- 81 @Override
- 82 public String toString() {
- 83 return "Temp [id2=" + id2 + ", time=" + time + ", temp_reading=" +
- temp_reading + ", sensor_id=" + sensor_id
- 84 + "]";
- 85 }
- 86
- 87
- 88 }
- 89
- Page 2
- DatabaseConnector.java
- 1 package uk.org.kelsohighschool.ps.program.db;
- 2
- 3 import java.sql.*;
- 5
- 6 public class DatabaseConnector {
- 7 private Connection conn;
- 8 private Statement statement;
- 9 private ResultSet resultSet;
- 10
- 11
- 12 /*private static boolean testingMode = false;
- 13
- 14 public static void enableTesting() {
- 15 testingMode = true;
- 16 }
- 17
- 18 */
- 19 /**
- 20 * For connecting to the database
- 21 * @param url URL to connect to
- 22 * @param username Username to use
- 23 * @param password Password to use
- 24 * @throws SQLException
- 25 */
- 26 public DatabaseConnector(String url,String username,String password)
- throws SQLException {
- 27 conn = DriverManager.getConnection(url,username,password);
- 28 }
- 29
- 30 /**
- 31 * @return Version of psql currently running
- 32 * @throws SQLException
- 33 */
- 34 public String GetVer() throws SQLException {
- 35 statement = conn.createStatement();
- 36 resultSet = statement.executeQuery("show server_version");
- 37 String rtn = "";
- 38 if (resultSet.next()) {
- 39 rtn = resultSet.getString(1);
- 40 }
- 41 return rtn;
- 42 }
- 43 /**
- 44 * gets and returns a full arraylist with all the values in the co2
- table
- 45 * @return full arraylist with all the values in the co2 table
- 46 * @throws SQLException
- 47 */
- 48 public ArrayList<Co2> getCo2() throws SQLException {
- 49 statement = conn.createStatement();
- 50 resultSet = statement.executeQuery("select * from co2");
- 51 ArrayList<Co2> rtnVal = new ArrayList<Co2>();
- 52 while (resultSet.next()) {
- Page 1
- DatabaseConnector.java
- 53 Co2 co2= new Co2(resultSet.getInt(1),resultSet.getInt
- (4),resultSet.getFloat(2),resultSet.getInt(3));
- 54 rtnVal.add(co2);
- 55 //System.out.print(co2);
- 56 }
- 57 return rtnVal;
- 58 }
- 59
- 60 /**
- 61 * gets and returns a full arraylist with all the values in the
- location table
- 62 * @return full arraylist with all the values in the location table
- 63 * @throws SQLException
- 64 */
- 65 public ArrayList<Location> getLocation() throws SQLException {
- 66 statement = conn.createStatement();
- 67 resultSet = statement.executeQuery("select * from location");
- 68 ArrayList<Location> rtnVal = new ArrayList<Location>();
- 69 while (resultSet.next()) {
- 70 Location location= new Location(resultSet.getInt
- (1),resultSet.getString(2),resultSet.getString(3));
- 71 rtnVal.add(location);
- 72 System.out.print(location);
- 73 }
- 74 return rtnVal;
- 75 }
- 76
- 77 /**
- 78 * gets and returns a full arraylist with all the values in the temp
- table
- 79 * @return full arraylist with all the values in the temp table
- 80 * @throws SQLException
- 81 */
- 82 public ArrayList<Temp> getTemp() throws SQLException {
- 83 statement = conn.createStatement();
- 84 resultSet = statement.executeQuery("select * from temp");
- 85 ArrayList<Temp> rtnVal = new ArrayList<Temp>();
- 86 while (resultSet.next()) {
- 87 Temp temp= new Temp(resultSet.getInt(1),resultSet.getTimestamp
- (2),resultSet.getFloat(3),resultSet.getInt(4));
- 88 rtnVal.add(temp);
- 89 System.out.print(temp);
- 90 }
- 91 return rtnVal;
- 92 }
- 93
- 94 /**
- 95 * Closes the database connection
- 96 */
- 97 public void close() {
- 98 // TODO Auto-generated method stub
- 99 try {
- 100 if (resultSet != null) {
- Page 2
- DatabaseConnector.java
- 101 resultSet.close();
- 102 }
- 103 if (statement != null) {
- 104 statement.close();
- 105 }
- 106 if (conn != null) {
- 107 conn.close();
- 108 }
- 109 } catch (SQLException e) {
- 110 e.printStackTrace();
- 111 }
- 112 }
- 113 }
- 114
- Page 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement