New servlet containing all survey results
This commit is contained in:
parent
0043e3d9c0
commit
73c60d9c52
7 changed files with 183 additions and 19 deletions
1
Makefile
1
Makefile
|
@ -25,6 +25,7 @@ dumptables:
|
|||
sqlite3 $(DB_NAME) "SELECT * FROM Respondents;"
|
||||
sqlite3 $(DB_NAME) "SELECT * FROM Answers;"
|
||||
sqlite3 $(DB_NAME) "SELECT * FROM ProjectDetails;"
|
||||
sqlite3 $(DB_NAME) "SELECT * FROM SurveyResults;"
|
||||
|
||||
# Phony targets
|
||||
.PHONY: all init_db clean
|
||||
|
|
35
database.sql
35
database.sql
|
@ -1,18 +1,35 @@
|
|||
CREATE TABLE IF NOT EXISTS Respondents (
|
||||
name VARCHAR(255) PRIMARY KEY
|
||||
);
|
||||
CREATE TABLE
|
||||
IF NOT EXISTS Respondents (name VARCHAR(255) PRIMARY KEY);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Answers (
|
||||
CREATE TABLE
|
||||
IF NOT EXISTS Answers (
|
||||
answerOne INT,
|
||||
answerTwo INT,
|
||||
answerThree INT,
|
||||
answerFour INT,
|
||||
name VARCHAR(255),
|
||||
FOREIGN KEY (name) REFERENCES Respondents(name)
|
||||
name VARCHAR(255) NOT NULL,
|
||||
projectName VARCHAR(255) NOT NULL,
|
||||
FOREIGN KEY (name) REFERENCES Respondents (name),
|
||||
FOREIGN KEY (projectName) REFERENCES ProjectDetails (projectName)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ProjectDetails (
|
||||
projectID INT PRIMARY KEY,
|
||||
projectName VARCHAR(255),
|
||||
CREATE TABLE
|
||||
IF NOT EXISTS ProjectDetails (
|
||||
projectName VARCHAR(255) PRIMARY KEY,
|
||||
projectDescription TEXT
|
||||
);
|
||||
|
||||
CREATE VIEW
|
||||
IF NOT EXISTS SurveyResults AS
|
||||
SELECT
|
||||
Respondents.name,
|
||||
Answers.answerOne,
|
||||
Answers.answerTwo,
|
||||
Answers.answerThree,
|
||||
Answers.answerFour,
|
||||
Answers.projectName,
|
||||
ProjectDetails.projectDescription
|
||||
FROM
|
||||
Respondents
|
||||
JOIN Answers ON Respondents.name = Answers.name
|
||||
JOIN ProjectDetails ON Answers.projectName = ProjectDetails.projectName;
|
|
@ -66,8 +66,8 @@ public class Database implements AutoCloseable {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean addAnswer(String name, String answerOne, String answerTwo, String answerThree, String answerFour) {
|
||||
String sql = "insert into Answers (answerOne, answerTwo, answerThree, answerFour, name) values(?,?,?,?,?)";
|
||||
public boolean addAnswer(String name, String projectName, String answerOne, String answerTwo, String answerThree, String answerFour) {
|
||||
String sql = "insert into Answers (answerOne, answerTwo, answerThree, answerFour, name, projectName) values(?,?,?,?,?,?)";
|
||||
|
||||
try (PreparedStatement ps = conn.prepareStatement(sql)) {
|
||||
ps.setString(1, answerOne);
|
||||
|
@ -75,6 +75,7 @@ public class Database implements AutoCloseable {
|
|||
ps.setString(3, answerThree);
|
||||
ps.setString(4, answerFour);
|
||||
ps.setString(5, name);
|
||||
ps.setString(6, projectName);
|
||||
ps.executeUpdate();
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
|
@ -86,7 +87,32 @@ public class Database implements AutoCloseable {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the survey results from the database
|
||||
*/
|
||||
public SurveyResult[] getSurveyResults() {
|
||||
String sql = "select * from SurveyResults";
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
SurveyResult[] results = new SurveyResult[100];
|
||||
int i = 0;
|
||||
while (rs.next()) {
|
||||
String respName = rs.getString("name");
|
||||
String projectName = rs.getString("projectName");
|
||||
String projectDescription = rs.getString("projectDescription");
|
||||
|
||||
int[] data = { rs.getInt("answerOne"), rs.getInt("answerTwo"), rs.getInt("answerThree"),
|
||||
rs.getInt("answerFour") };
|
||||
results[i] = new SurveyResult(respName, projectName, projectDescription, data);
|
||||
i++;
|
||||
}
|
||||
return results;
|
||||
} catch (SQLException e) {
|
||||
printSqlError(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void printSqlError(SQLException e) {
|
||||
|
|
|
@ -56,4 +56,17 @@ public class FormGenerator {
|
|||
return html;
|
||||
}
|
||||
|
||||
public String resultsTable(SurveyResult[] results) {
|
||||
String html = "<table border=" + formElement("1") + ">";
|
||||
html += "<tr><th>Respondent</th><th>Project</th><th>Description</th><th>Factor 1</th><th>Factor 2</th><th>Factor 3</th><th>Factor 4</th></tr>";
|
||||
for (SurveyResult result : results) {
|
||||
if (result == null)
|
||||
break;
|
||||
html += "<tr><td>" + result.getName() + "</td><td>" + result.getProjectName() + "</td><td>"
|
||||
+ result.getProjectDesc() + "</td><td>" + result.getData()[0] + "</td><td>" + result.getData()[1]
|
||||
+ "</td><td>" + result.getData()[2] + "</td><td>" + result.getData()[3] + "</td></tr>";
|
||||
}
|
||||
html += "</table>";
|
||||
return html;
|
||||
}
|
||||
}
|
||||
|
|
67
src/main/java/servlet/Results.java
Normal file
67
src/main/java/servlet/Results.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
package servlet;
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* Servlet implementation class Results.
|
||||
*
|
||||
* This class is responsible for displaying the results of the survey.
|
||||
* It should be able to display the results of the survey in a table.
|
||||
*/
|
||||
@WebServlet("/Results")
|
||||
public class Results extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private Database db = new Database();
|
||||
private FormGenerator formGenerator = new FormGenerator();
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public Results() {
|
||||
}
|
||||
|
||||
/**
|
||||
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
|
||||
* response)
|
||||
*/
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
SurveyResult[] results = db.getSurveyResults();
|
||||
|
||||
// SurveyResult[] results = {
|
||||
// new SurveyResult("John", "Project 1", new int[] { 1, 2, 3, 4 }),
|
||||
// new SurveyResult("Jane", "Project 2", new int[] { 5, 6, 7, 8 }),
|
||||
// new SurveyResult("Joe", "Project 3", new int[] { 9, 10, 1, 2 }),
|
||||
// new SurveyResult("Jill", "Project 4", new int[] { 3, 4, 5, 6 }),
|
||||
// new SurveyResult("Jack", "Project 5", new int[] { 7, 8, 9, 10 }),
|
||||
// };
|
||||
|
||||
for (SurveyResult result : results) {
|
||||
if (result == null)
|
||||
break;
|
||||
System.out.println(result.toString());
|
||||
}
|
||||
|
||||
// Get a writer
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(formGenerator.resultsTable(results));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
|
||||
* response)
|
||||
*/
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
doGet(request, response);
|
||||
}
|
||||
}
|
|
@ -105,6 +105,7 @@ public class Survey extends HttpServlet {
|
|||
name = (String) session.getAttribute("name");
|
||||
String description = request.getParameter("proj_desc");
|
||||
String proj_name = request.getParameter("proj_name");
|
||||
session.setAttribute("proj_name", proj_name);
|
||||
System.out.println("description: " + description);
|
||||
if (description != null) {
|
||||
state = NEED_PROJECT_DATA;
|
||||
|
@ -138,11 +139,14 @@ public class Survey extends HttpServlet {
|
|||
// display the next page
|
||||
if (valuesOk) {
|
||||
int sum = s11 + s12 + s13 + s14;
|
||||
db.addAnswer(name, s11String, s12String, s13String, s14String);
|
||||
// db.addAnswer(name, s11String, s12String, s13String, s14String);
|
||||
db.addAnswer(name, (String) session.getAttribute("proj_name"), s11String, s12String, s13String,
|
||||
s14String);
|
||||
// This is only to show that it is possible to do something with the values.
|
||||
// It is of course meaningless to calculate the sum.
|
||||
out.println("<p> Hello " + name);
|
||||
out.println("<p> The sum of the values you entered is " + sum);
|
||||
out.println("<p>To view other results, click <a href=\"Results\">here</a>");
|
||||
sessionShouldBeEnded = true;
|
||||
} else {
|
||||
out.println("The values you entered were not OK");
|
||||
|
|
36
src/main/java/servlet/SurveyResult.java
Normal file
36
src/main/java/servlet/SurveyResult.java
Normal file
|
@ -0,0 +1,36 @@
|
|||
package servlet;
|
||||
|
||||
public class SurveyResult {
|
||||
private String respName;
|
||||
private String projectName;
|
||||
private String projectDesc;
|
||||
private int[] data;
|
||||
|
||||
public SurveyResult(String respName, String projectName, String projectDesc, int[] data) {
|
||||
this.respName = respName;
|
||||
this.projectName = projectName;
|
||||
this.projectDesc = projectDesc;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return respName;
|
||||
}
|
||||
|
||||
public String getProjectName() {
|
||||
return projectName;
|
||||
}
|
||||
|
||||
public String getProjectDesc() {
|
||||
return projectDesc;
|
||||
}
|
||||
|
||||
public int[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("Name: %s, Project: %s, Project Description: %s, Data: %d, %d, %d, %d", respName,
|
||||
projectName, projectDesc, data[0], data[1], data[2], data[3]);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue