去除了部分临时性硬编码用户名;
用户名密码相关getter,setter; 移动了部分方法的位置; 登录注册支持;
This commit is contained in:
parent
f063c5056d
commit
98f49e07ce
@ -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<Project> 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()) : "";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user