From 98f49e07ce76c52f2e266600bf9c2b3a6305327b Mon Sep 17 00:00:00 2001 From: heshunme Date: Sun, 14 Jul 2024 13:26:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=80=A7=E7=A1=AC=E7=BC=96=E7=A0=81=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=90=8D;=20=E7=94=A8=E6=88=B7=E5=90=8D=E5=AF=86?= =?UTF-8?q?=E7=A0=81=E7=9B=B8=E5=85=B3getter,setter;=20=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E4=BA=86=E9=83=A8=E5=88=86=E6=96=B9=E6=B3=95=E7=9A=84=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE;=20=E7=99=BB=E5=BD=95=E6=B3=A8=E5=86=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/web/web_assignment/ProjectBean.java | 139 ++++++------------ 1 file changed, 45 insertions(+), 94 deletions(-) diff --git a/src/main/java/com/web/web_assignment/ProjectBean.java b/src/main/java/com/web/web_assignment/ProjectBean.java index 30f9d90..5e66427 100644 --- a/src/main/java/com/web/web_assignment/ProjectBean.java +++ b/src/main/java/com/web/web_assignment/ProjectBean.java @@ -7,7 +7,7 @@ import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import java.io.Serializable; -import java.sql.*; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -20,7 +20,7 @@ public class ProjectBean implements Serializable { private final DatabaseService databaseService = new DatabaseService(); private List projects; // List to store all projects private Project project; // Current project being worked on - private String username = "admin"; + private String username; private String password; public String projectKeywords = ""; // String to hold project keywords public String projectCollaborators = ""; // String to hold project collaborators @@ -45,11 +45,10 @@ public class ProjectBean implements Serializable { public void init() { loadProjects(); // Load projects from the database System.out.println("Projects loaded: " + projects.size()); - if (projects.isEmpty()) { -// loadFakeProjects(); // Load fake projects if no projects are found - saveProjects(); // Save the fake projects to the database - } loadAllKeywords(); // Load all unique keywords from all projects + if (projects.isEmpty()){ + Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", ""); + } setProject(projects.get(0)); // Set the first project as the current project } @@ -103,7 +102,6 @@ public class ProjectBean implements Serializable { // Check for validation errors if (context.getMessages().hasNext()) { // Validation errors exist, do not save data -// context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation errors exist. Please fix them before saving.", null)); return "edit"; } @@ -111,108 +109,61 @@ public class ProjectBean implements Serializable { project.setKeywords(new ArrayList<>(selectedKeywords)); try { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); - Connection conn = DriverManager.getConnection(getJDBCurl()); - - // Iterate over all projects and save to the database - for (Project project : projects) { - // Check if the project already exists - String checkSql = "SELECT COUNT(*) FROM projects WHERE title = ?"; - PreparedStatement checkStmt = conn.prepareStatement(checkSql); - checkStmt.setString(1, project.getTitle()); - ResultSet rs = checkStmt.executeQuery(); - rs.next(); - int count = rs.getInt(1); - - if (count > 0) { - // Project exists, perform update - String updateSql = "UPDATE projects SET summary = ?, description = ?, keywords = ?, type = ?, collaborators = ?, link = ?, time = ? WHERE title = ?"; - PreparedStatement updateStmt = conn.prepareStatement(updateSql); - updateStmt.setString(1, project.getSummary()); - updateStmt.setString(2, project.getDescription()); - updateStmt.setString(3, String.join(", ", project.getKeywords())); - updateStmt.setString(4, project.getType()); - updateStmt.setString(5, String.join(", ", project.getCollaborators())); - updateStmt.setString(6, project.getLink()); - updateStmt.setString(7, project.getTime()); - updateStmt.setString(8, project.getTitle()); - updateStmt.executeUpdate(); - } else { - // Project does not exist, perform insert - String insertSql = "INSERT INTO projects (title, summary, description, keywords, type, collaborators, link, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; - PreparedStatement insertStmt = conn.prepareStatement(insertSql); - insertStmt.setString(1, project.getTitle()); - insertStmt.setString(2, project.getSummary()); - insertStmt.setString(3, project.getDescription()); - insertStmt.setString(4, String.join(", ", project.getKeywords())); - insertStmt.setString(5, project.getType()); - insertStmt.setString(6, String.join(", ", project.getCollaborators())); - insertStmt.setString(7, project.getLink()); - insertStmt.setString(8, project.getTime()); - insertStmt.executeUpdate(); - } - } - conn.close(); + databaseService.saveProjects(username, projects); context.addMessage("saveButton", new FacesMessage(FacesMessage.SEVERITY_INFO, "Submission Succeeded", null)); } catch (SQLException e) { e.printStackTrace(); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); } return "project"; } - public void saveProjects() { + public String register() { try { - Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); - Connection conn = DriverManager.getConnection(getJDBCurl()); + databaseService.registerUser(username, password); + } catch (SQLException e) { + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Registration failed", null)); + return "register"; + } + return "login"; + } - // Iterate over all projects and save to the database - for (Project project : projects) { - // Check if the project already exists - String checkSql = "SELECT COUNT(*) FROM projects WHERE title = ?"; - PreparedStatement checkStmt = conn.prepareStatement(checkSql); - checkStmt.setString(1, project.getTitle()); - ResultSet rs = checkStmt.executeQuery(); - rs.next(); - int count = rs.getInt(1); - - if (count > 0) { - // Project exist, perform update - String updateSql = "UPDATE projects SET summary = ?, description = ?, keywords = ?, type = ?, collaborators = ?, link = ?, time = ? WHERE title = ?"; - PreparedStatement updateStmt = conn.prepareStatement(updateSql); - updateStmt.setString(1, project.getSummary()); - updateStmt.setString(2, project.getDescription()); - updateStmt.setString(3, String.join(", ", project.getKeywords())); - updateStmt.setString(4, project.getType()); - updateStmt.setString(5, String.join(", ", project.getCollaborators())); - updateStmt.setString(6, project.getLink()); - updateStmt.setString(7, project.getTime()); - updateStmt.setString(8, project.getTitle()); - updateStmt.executeUpdate(); - } else { - // Project does not exist, perform insert - String insertSql = "INSERT INTO projects (title, summary, description, keywords, type, collaborators, link, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; - PreparedStatement insertStmt = conn.prepareStatement(insertSql); - insertStmt.setString(1, project.getTitle()); - insertStmt.setString(2, project.getSummary()); - insertStmt.setString(3, project.getDescription()); - insertStmt.setString(4, String.join(", ", project.getKeywords())); - insertStmt.setString(5, project.getType()); - insertStmt.setString(6, String.join(", ", project.getCollaborators())); - insertStmt.setString(7, project.getLink()); - insertStmt.setString(8, project.getTime()); - insertStmt.executeUpdate(); - } + public String login() { + try { + if (databaseService.validateUser(username, password)) { + FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("username", username); + loadProjects(); + return "project"; + } else { + FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Login failed", null)); + return "login"; } - conn.close(); } catch (SQLException e) { e.printStackTrace(); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); + return "login"; } } + public String logout() { + FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); + return "index"; + } + + // Getters and setters for username and password + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } public String getKeywordsAsString() { return project != null ? String.join(", ", project.getKeywords()) : "";