I essentially have parent / child groups and I want to be able to add a new new child group. The groups are database tables which are basically just a name and are mapped as simple POJO via JPA.
The one slight level of complexity is the mast source of parent groups is from a different source which returns List<String> of names.
The problem I have is that no matter what I seem to try when addChildGroup gets called the parent group is null and this fails.
My JSF is
<h:form id="form">
<p:selectOneListbox id="groupSelector" value="#{myBean.groupName}">
<p:ajax listener="#{myBean.loadChildGroups}" update="selectedGroup,:form:childGroupSelector" />
<f:selectItems value="#{myOtherBean.groupNames}" />
</p:selectOneListbox>
<h2>Selected Group is:
<h:outputText id="selectedGroup" value="#{myBean.group.groupName}" />
</h2>
<p:selectOneListbox id="childGroupSelector" value="#{myBean.childGroup}">
<f:selectItems value="#{myBean.childGroups}" />
</p:selectOneListbox>
<p:commandButton value="Add" onclick="PF('groupNameDlg').show()" />
<p:dialog id="inputGroupName" widgetVar="groupNameDlg" header="Enter Child Group Name" modal="true">
<p:ajax event="close" listener="#{myBean.loadChildGroups}" update=":form:childGroupSelector" />
<p>Enter a group name</p>
<p:outputLabel for="groupNameText" value="Enter Group Name" styleClass="screen-reader-offscreen" />
<p:inputText id="groupNameText" value="#{myBean.childGroupName}" />
<br /> <br />
<p:commandButton value="Add" action="#{myBean.addChildGroup}" icon="pi pi-check"
styleClass="ui-button-success p-mr-2" oncomplete="PF('groupNameDlg').hide()" />
<p:commandButton value="Cancel" onclick="PF('groupNameDlg').hide()" />
</p:dialog>
</h:form>
and the bean behind that is
@ManagedBean(name = "myBean", eager = true)
@ViewScoped
public class MyBean implements Serializable {
private String groupName; // Name of selected group
private Group group; // Selected Group
private List<ChildGroup> childGroups; // list of child groups
private ChildGroup childGroup; //Selected Child Group
private String childGroupName; //Child Group to be added
@Inject
private MyService myService;
public void loadChildGroups() {
group = myService.findGroupByName(groupName);
if (group == null) {
group = new Group(groupName);
myService.saveGroup(group); // Just calls persist
}
childGroups = group.getChildGroups(); // Get children via JPA OneToMany
if (approvalGroups.isEmpty()) {
//No existing groups so seed default approver group record
ChildGroup cg = new ChildGroup(group, "default");
myService.saveChildGroup(cg); // Just calls persist
childGroups.add(cg);
}
}
public void addChildGroup() {
ChildGroup cg = new ChildGroup(group, childGroupName);
myService.saveChildGroup(cg); // Just calls persist
childGroups.add(cg);
}
}
When I open the page I can select items from the parent list and this correctly updates the text and the list of child groups fine (so group is set there) but when I try to add a new child group then call to addChildGroup fails as group is null
I thought that as this was all on the same page that ViewScoped should keep everything but I am obviously missing something.