新增keyword添加按钮;优化保存逻辑,只修改修改了的而不是整个projects列表,减少数据库操作量

This commit is contained in:
高子兴 2024-07-14 14:51:47 +08:00
parent 3aa1f9444e
commit 69b48c30ec
2 changed files with 47 additions and 12 deletions

View File

@ -19,11 +19,13 @@ import java.util.stream.Collectors;
public class ProjectBean implements Serializable {
private final DatabaseService databaseService = new DatabaseService();
private List<Project> projects; // List to store all projects
private int projectIdx;
private Project project; // Current project being worked on
private UserInfo userInfo; // User info for the current user
private String username;
private String password;
public String projectKeywords = ""; // String to hold project keywords
public String newKeyword = "";
public String projectCollaborators = ""; // String to hold project collaborators
public List<String> allProjectsKeywords; // List of all unique keywords from all projects
@ -39,7 +41,7 @@ public class ProjectBean implements Serializable {
loadProjects(); // Load projects from the database
System.out.println("Projects loaded: " + projects.size());
loadAllKeywords(); // Load all unique keywords from all projects
if (projects.isEmpty()){
if (projects.isEmpty()) {
Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
}
setProject(projects.get(0)); // Set the first project as the current project
@ -76,6 +78,13 @@ public class ProjectBean implements Serializable {
.collect(Collectors.toList());
}
public void addKeyword() {
if (!newKeyword.isEmpty() && !allProjectsKeywords.contains(newKeyword)) {
selectedKeywords.add(newKeyword);
allProjectsKeywords.add(newKeyword);
}
}
public List<Project> getProjects() {
return projects;
}
@ -89,6 +98,7 @@ public class ProjectBean implements Serializable {
}
public void setProject(Project project) {
this.projectIdx = projects.indexOf(project);
this.project = project;
this.projectKeywords = getKeywordsAsString(); // Update keywords string
this.projectCollaborators = getCollaboratorsAsString();
@ -113,8 +123,12 @@ public class ProjectBean implements Serializable {
project.setKeywords(new ArrayList<>(selectedKeywords));
try {
databaseService.saveProjects(username, projects);
ArrayList<Project> newProject = new ArrayList<>();
newProject.add(project);
projects.set(projectIdx,project);
databaseService.saveProjects(username, newProject);
context.addMessage("saveButton", new FacesMessage(FacesMessage.SEVERITY_INFO, "Submission Succeeded", null));
loadAllKeywords();
} catch (SQLException e) {
e.printStackTrace();
}
@ -217,4 +231,12 @@ public class ProjectBean implements Serializable {
public void setSelectedKeywords(List<String> selectedKeywords) {
this.selectedKeywords = selectedKeywords;
}
public String getNewKeyword() {
return newKeyword;
}
public void setNewKeyword(String newKeyword) {
this.newKeyword = newKeyword;
}
}

View File

@ -11,7 +11,7 @@
<h:body>
<ui:include src="header.xhtml"/>
<main>
<!-- <h:form id="form" styleClass="project-form">-->
<!-- <h:form id="form" styleClass="project-form">-->
<h:form id="form">
<section id="project-list">
<h1>Select Project to Edit</h1>
@ -55,15 +55,27 @@
</div>
<div class="form-group-container">
<div class="form-group form-group-keywords">
<h:outputLabel for="keywords" value="Keywords: (Hold Ctrl and click for multi-select.)"/>
<h:selectManyListbox id="keywords" value="#{projectBean.selectedKeywords}" styleClass="keywords-listbox">
<f:selectItems value="#{projectBean.allProjectsKeywords}" var="keyword"
itemLabel="#{keyword}" itemValue="#{keyword}"/>
</h:selectManyListbox>
<h:message for="keywords" style="color:red"/>
</div>
<h:form id="keywords-form">
<div class="form-group form-group-keywords">
<h:outputLabel for="keywords"
value="Keywords: (Hold Ctrl and click for multi-select.)"/>
<h:selectManyListbox id="keywords" value="#{projectBean.selectedKeywords}"
styleClass="keywords-listbox">
<f:selectItems value="#{projectBean.allProjectsKeywords}" var="keyword"
itemLabel="#{keyword}" itemValue="#{keyword}"/>
</h:selectManyListbox>
<h:message for="keywords" style="color:red"/>
<!-- 新增的关键词输入框和按钮 -->
<h:panelGrid columns="2">
<h:outputLabel for="newKeyword" value="add a keyword"/>
<h:inputText id="newKeyword" value="#{projectBean.newKeyword}"/>
<h:commandButton value="Add" action="#{projectBean.addKeyword}">
<f:ajax execute="newKeyword" render="keywords @keywords-form"/>
</h:commandButton>
</h:panelGrid>
</div>
</h:form>
<div class="form-group form-group-right">
<div class="form-group">
@ -77,7 +89,8 @@
<div class="form-group">
<h:outputLabel for="collaborators" value="Collaborators:"/>
<h:inputText id="collaborators" value="#{projectBean.projectCollaborators}" required="true"
<h:inputText id="collaborators" value="#{projectBean.projectCollaborators}"
required="true"
requiredMessage="Collaborators are required">
<f:validateRegex pattern="^([^,]+(, )?)*$"/>
</h:inputText>