新增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 { public class ProjectBean implements Serializable {
private final DatabaseService databaseService = new DatabaseService(); private final DatabaseService databaseService = new DatabaseService();
private List<Project> projects; // List to store all projects private List<Project> projects; // List to store all projects
private int projectIdx;
private Project project; // Current project being worked on private Project project; // Current project being worked on
private UserInfo userInfo; // User info for the current user private UserInfo userInfo; // User info for the current user
private String username; private String username;
private String password; private String password;
public String projectKeywords = ""; // String to hold project keywords public String projectKeywords = ""; // String to hold project keywords
public String newKeyword = "";
public String projectCollaborators = ""; // String to hold project collaborators public String projectCollaborators = ""; // String to hold project collaborators
public List<String> allProjectsKeywords; // List of all unique keywords from all projects public List<String> allProjectsKeywords; // List of all unique keywords from all projects
@ -76,6 +78,13 @@ public class ProjectBean implements Serializable {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public void addKeyword() {
if (!newKeyword.isEmpty() && !allProjectsKeywords.contains(newKeyword)) {
selectedKeywords.add(newKeyword);
allProjectsKeywords.add(newKeyword);
}
}
public List<Project> getProjects() { public List<Project> getProjects() {
return projects; return projects;
} }
@ -89,6 +98,7 @@ public class ProjectBean implements Serializable {
} }
public void setProject(Project project) { public void setProject(Project project) {
this.projectIdx = projects.indexOf(project);
this.project = project; this.project = project;
this.projectKeywords = getKeywordsAsString(); // Update keywords string this.projectKeywords = getKeywordsAsString(); // Update keywords string
this.projectCollaborators = getCollaboratorsAsString(); this.projectCollaborators = getCollaboratorsAsString();
@ -113,8 +123,12 @@ public class ProjectBean implements Serializable {
project.setKeywords(new ArrayList<>(selectedKeywords)); project.setKeywords(new ArrayList<>(selectedKeywords));
try { 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)); context.addMessage("saveButton", new FacesMessage(FacesMessage.SEVERITY_INFO, "Submission Succeeded", null));
loadAllKeywords();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -217,4 +231,12 @@ public class ProjectBean implements Serializable {
public void setSelectedKeywords(List<String> selectedKeywords) { public void setSelectedKeywords(List<String> selectedKeywords) {
this.selectedKeywords = selectedKeywords; this.selectedKeywords = selectedKeywords;
} }
public String getNewKeyword() {
return newKeyword;
}
public void setNewKeyword(String newKeyword) {
this.newKeyword = newKeyword;
}
} }

View File

@ -55,15 +55,27 @@
</div> </div>
<div class="form-group-container"> <div class="form-group-container">
<h:form id="keywords-form">
<div class="form-group form-group-keywords"> <div class="form-group form-group-keywords">
<h:outputLabel for="keywords" value="Keywords: (Hold Ctrl and click for multi-select.)"/> <h:outputLabel for="keywords"
<h:selectManyListbox id="keywords" value="#{projectBean.selectedKeywords}" styleClass="keywords-listbox"> 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" <f:selectItems value="#{projectBean.allProjectsKeywords}" var="keyword"
itemLabel="#{keyword}" itemValue="#{keyword}"/> itemLabel="#{keyword}" itemValue="#{keyword}"/>
</h:selectManyListbox> </h:selectManyListbox>
<h:message for="keywords" style="color:red"/> <h:message for="keywords" style="color:red"/>
</div>
<!-- 新增的关键词输入框和按钮 -->
<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 form-group-right">
<div class="form-group"> <div class="form-group">
@ -77,7 +89,8 @@
<div class="form-group"> <div class="form-group">
<h:outputLabel for="collaborators" value="Collaborators:"/> <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"> requiredMessage="Collaborators are required">
<f:validateRegex pattern="^([^,]+(, )?)*$"/> <f:validateRegex pattern="^([^,]+(, )?)*$"/>
</h:inputText> </h:inputText>