For days I have been struggling with an issue that, to me, seems quite basic: how to set the value of a select or combobox containing
all
departments to the
current
department of a an employee.
I have an Employee class and a Department class. One of the properties of an employee is his/her Department. Employees are bundled in a JPAContainer as are the Departments. I use Hibernate as the persistence provider. All instances have autogenerated Id’s of the type long.
A form contains the properties of an employee (the employee selected in a table).
Without using a formfieldfactory all properties are shown, including his/her department. For now I even have two ways in which the department is shown:
- the department itself (for instance ‘Department@7ab2f38f’, so the internal key), and
- a nested property ‘department.name’ (for instance ‘Logistics’).
However I want a select or combobox containing all departments and showing the current department of the employee (giving the user the possibility to change the department the employee is assigned to). So I try to construct a formfieldfactory to accomplish this result. So far without succes.
I tried many different options (all in the listener connected to the employeetable). Depending on the option I tried, the selected department remains empty or I encounter errors (illegalArgument exceptions because java.lang.Long was expected, but com.vaadin.addon.jpacontainer.JPAContainerItem was given). Stripped to its core, my code now looks like this.
@Entity
public class Department {
@Id @GeneratedValue
Long id;
public String name;
@OneToMany (mappedBy = "department")
Set<Employee> employees;
@Entity
public class Employee {
@Id @GeneratedValue
Long id;
public String name;
@ManyToOne
@JoinColumn
Department department;
}
employeeTable.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
layout.addComponent(createForm());
}
});
public Form createForm(){
employeeForm = new Form();
employeeForm.setFormFieldFactory(this);
employeeForm.setItemDataSource(employeeTable.getItem(humanResourceTable.getValue()));
return employeeForm;
}
public Field createField(Item item, Object propertyId, Component uiContext) {
String pid = (String) propertyId;
if ("name".equals(pid)) {
final TextField employeeNameField = new TextField();
return employeeNameField;
}
else if ("department".equals(pid)) {
departments = JPAContainerFactory.make(Department.class, "PersistenceUnit");
departmentSelector = new Select();
departmentSelector.setContainerDataSource(departments);
Item selectedEmployee = employeeTable.getItem(employeeTable.getValue());
departmentSelector.setValue(resourceGroups.getItem(selectedHumanResource).getEntity().getId());
return departmentSelector;
}
return null;
}
Can anyone point me in the right direction?