Project details stage half implemented

This commit is contained in:
Imbus 2024-02-14 19:55:50 +01:00
parent ef6adf71e7
commit dae126f721
4 changed files with 53 additions and 4 deletions

View file

@ -11,4 +11,8 @@ CREATE TABLE IF NOT EXISTS Answers (
FOREIGN KEY (name) REFERENCES Respondents(name)
);
INSERT INTO Respondents (name) VALUES ('user1');
CREATE TABLE IF NOT EXISTS ProjectDetails (
projectID INT PRIMARY KEY,
projectName VARCHAR(255),
projectDescription TEXT
);

View file

@ -55,6 +55,25 @@ public class Database implements AutoCloseable {
// Using the try-with-resources syntax, this will call ps.close() automatically
}
public boolean addProjectDetails(String name, String description) {
String sql = "insert into ProjectDetails (projectName, projectDescription) values(?,?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, name);
ps.setString(2, description);
ps.executeUpdate();
return true;
} catch (SQLException e) {
if (e.getErrorCode() == 1062 && e.getSQLState().equals("23000")) {
// duplicate key error
System.out.println(name + " already exists in the database");
} else {
printSqlError(e);
}
return false;
}
}
public boolean addAnswer(String name, String answerOne, String answerTwo, String answerThree, String answerFour) {
String sql = "insert into Answers (answerOne, answerTwo, answerThree, answerFour, name) values(?,?,?,?,?)";

View file

@ -23,6 +23,18 @@ public class FormGenerator {
return html;
}
public String projectDetailsRequestForm() {
String[][] variables = {{"Project Name", "proj_name"}, {"Project Description", "proj_desc"}};
String html = "<p>Please give us some information about your project";
html += "<p> <form name=" + formElement("input");
html += " method=" + formElement("get");
for (int i=0; i<2; i++) {
html += "<p> " + variables[i][0];
html += ": <input type="+ formElement("text") + "name =" + variables[i][1] + '>';
}
html += "<p> <input type=" + formElement("submit") + "value=" + formElement("Submit") + '>';
return html;
}
/*
* Form for requesting success with respect to four factors

View file

@ -21,6 +21,7 @@ public class Survey extends HttpServlet {
// Define states
private static final int NEED_NAME = 0;
private static final int NEED_PROJECT_INFO = 1;
private static final int NEED_PROJECT_DATA = 2;
private FormGenerator formGenerator = new FormGenerator();
@ -86,8 +87,8 @@ public class Survey extends HttpServlet {
if (name != null) {
if (nameOk(db, name)) {
session.setAttribute("name", name); // save the name in the session
state = NEED_PROJECT_DATA;
out.println(formGenerator.projectDataRequestForm());
state = NEED_PROJECT_INFO;
out.println(formGenerator.projectDetailsRequestForm());
}
else {
out.println("That was not a valid name. Maybe it is already taken by someone else.");
@ -97,6 +98,19 @@ public class Survey extends HttpServlet {
out.println(formGenerator.nameRequestForm());
}
break;
case NEED_PROJECT_INFO:
name = (String) session.getAttribute("name");
String description = request.getParameter("proj_desc");
String proj_name = request.getParameter("proj_name");
System.out.println("description: " + description);
if (description != null) {
state = NEED_PROJECT_DATA;
db.addProjectDetails(proj_name, description);
out.println(formGenerator.projectDataRequestForm());
} else {
out.println(formGenerator.projectDetailsRequestForm());
}
break;
case NEED_PROJECT_DATA:
int s11 = 0, s12 = 0, s13 = 0, s14 = 0;
name = (String) session.getAttribute("name");