新增keyword添加按钮;优化保存逻辑,只修改修改了的而不是整个projects列表,减少数据库操作量
This commit is contained in:
parent
3aa1f9444e
commit
69b48c30ec
@ -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
|
||||||
@ -39,7 +41,7 @@ public class ProjectBean implements Serializable {
|
|||||||
loadProjects(); // Load projects from the database
|
loadProjects(); // Load projects from the database
|
||||||
System.out.println("Projects loaded: " + projects.size());
|
System.out.println("Projects loaded: " + projects.size());
|
||||||
loadAllKeywords(); // Load all unique keywords from all projects
|
loadAllKeywords(); // Load all unique keywords from all projects
|
||||||
if (projects.isEmpty()){
|
if (projects.isEmpty()) {
|
||||||
Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
|
Project emptyProject = new Project("", "", "", new ArrayList<>(), "", new ArrayList<>(), "", "");
|
||||||
}
|
}
|
||||||
setProject(projects.get(0)); // Set the first project as the current project
|
setProject(projects.get(0)); // Set the first project as the current project
|
||||||
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
<h:body>
|
<h:body>
|
||||||
<ui:include src="header.xhtml"/>
|
<ui:include src="header.xhtml"/>
|
||||||
<main>
|
<main>
|
||||||
<!-- <h:form id="form" styleClass="project-form">-->
|
<!-- <h:form id="form" styleClass="project-form">-->
|
||||||
<h:form id="form">
|
<h:form id="form">
|
||||||
<section id="project-list">
|
<section id="project-list">
|
||||||
<h1>Select Project to Edit</h1>
|
<h1>Select Project to Edit</h1>
|
||||||
@ -55,15 +55,27 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group-container">
|
<div class="form-group-container">
|
||||||
<div class="form-group form-group-keywords">
|
<h:form id="keywords-form">
|
||||||
<h:outputLabel for="keywords" value="Keywords: (Hold Ctrl and click for multi-select.)"/>
|
<div class="form-group form-group-keywords">
|
||||||
<h:selectManyListbox id="keywords" value="#{projectBean.selectedKeywords}" styleClass="keywords-listbox">
|
<h:outputLabel for="keywords"
|
||||||
<f:selectItems value="#{projectBean.allProjectsKeywords}" var="keyword"
|
value="Keywords: (Hold Ctrl and click for multi-select.)"/>
|
||||||
itemLabel="#{keyword}" itemValue="#{keyword}"/>
|
<h:selectManyListbox id="keywords" value="#{projectBean.selectedKeywords}"
|
||||||
</h:selectManyListbox>
|
styleClass="keywords-listbox">
|
||||||
<h:message for="keywords" style="color:red"/>
|
<f:selectItems value="#{projectBean.allProjectsKeywords}" var="keyword"
|
||||||
</div>
|
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 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>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user