新增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 {
|
||||
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
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,15 +55,27 @@
|
||||
</div>
|
||||
|
||||
<div class="form-group-container">
|
||||
<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">
|
||||
<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: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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user