From 73c60d9c528540ea533eb0f6ce5ab093064afe5c Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 15 Feb 2024 13:28:33 +0100 Subject: [PATCH] New servlet containing all survey results --- Makefile | 1 + database.sql | 49 +++++++++++------ src/main/java/servlet/Database.java | 30 ++++++++++- src/main/java/servlet/FormGenerator.java | 13 +++++ src/main/java/servlet/Results.java | 67 ++++++++++++++++++++++++ src/main/java/servlet/Survey.java | 6 ++- src/main/java/servlet/SurveyResult.java | 36 +++++++++++++ 7 files changed, 183 insertions(+), 19 deletions(-) create mode 100644 src/main/java/servlet/Results.java create mode 100644 src/main/java/servlet/SurveyResult.java diff --git a/Makefile b/Makefile index b69a45b..34899d3 100644 --- a/Makefile +++ b/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 diff --git a/database.sql b/database.sql index 0f34e15..5339b1a 100644 --- a/database.sql +++ b/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 ( - answerOne INT, - answerTwo INT, - answerThree INT, - answerFour INT, - name VARCHAR(255), - FOREIGN KEY (name) REFERENCES Respondents(name) -); +CREATE TABLE + IF NOT EXISTS Answers ( + answerOne INT, + answerTwo INT, + answerThree INT, + answerFour INT, + 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), - projectDescription TEXT -); \ No newline at end of file +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; \ No newline at end of file diff --git a/src/main/java/servlet/Database.java b/src/main/java/servlet/Database.java index 1a3c4a5..104d3c2 100644 --- a/src/main/java/servlet/Database.java +++ b/src/main/java/servlet/Database.java @@ -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) { diff --git a/src/main/java/servlet/FormGenerator.java b/src/main/java/servlet/FormGenerator.java index dc704d8..eb4bd07 100644 --- a/src/main/java/servlet/FormGenerator.java +++ b/src/main/java/servlet/FormGenerator.java @@ -56,4 +56,17 @@ public class FormGenerator { return html; } + public String resultsTable(SurveyResult[] results) { + String html = ""; + html += ""; + for (SurveyResult result : results) { + if (result == null) + break; + html += ""; + } + html += "
RespondentProjectDescriptionFactor 1Factor 2Factor 3Factor 4
" + result.getName() + "" + result.getProjectName() + "" + + result.getProjectDesc() + "" + result.getData()[0] + "" + result.getData()[1] + + "" + result.getData()[2] + "" + result.getData()[3] + "
"; + return html; + } } diff --git a/src/main/java/servlet/Results.java b/src/main/java/servlet/Results.java new file mode 100644 index 0000000..c38220a --- /dev/null +++ b/src/main/java/servlet/Results.java @@ -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); + } +} diff --git a/src/main/java/servlet/Survey.java b/src/main/java/servlet/Survey.java index 4e46a7b..782d144 100644 --- a/src/main/java/servlet/Survey.java +++ b/src/main/java/servlet/Survey.java @@ -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("

Hello " + name); out.println("

The sum of the values you entered is " + sum); + out.println("

To view other results, click here"); sessionShouldBeEnded = true; } else { out.println("The values you entered were not OK"); diff --git a/src/main/java/servlet/SurveyResult.java b/src/main/java/servlet/SurveyResult.java new file mode 100644 index 0000000..84b4c3d --- /dev/null +++ b/src/main/java/servlet/SurveyResult.java @@ -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]); + } +}