Completes Departments management

parent 911c9d76
......@@ -4,7 +4,10 @@ import java.util.List;
import javax.persistence.EntityManager;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import dgpena.siexample.persistence.Department;
import dgpena.siexample.persistence.Departments;
......@@ -14,6 +17,11 @@ public class DepartmentsVM {
private EntityManager em;
private Departments departments;
private boolean isEditing = false;
// department under edition...
private Department currentDepartment;
@Init
public void init() {
this.em = DesktopEntityManagerManager.getDesktopEntityManager();
......@@ -23,4 +31,54 @@ public class DepartmentsVM {
public List<Department> getDepartments() {
return this.departments.findAll();
}
public Department getCurrentDepartment() {
return currentDepartment;
}
public void setCurrentDepartment(Department currentDepartment) {
this.currentDepartment = currentDepartment;
}
@Command
@NotifyChange("currentDepartment")
public void newDepartment() {
this.isEditing = false;
this.currentDepartment = new Department();
}
@Command
@NotifyChange("currentDepartment")
public void resetEditing() {
System.out.println(this.currentDepartment.getName());
this.currentDepartment = null;
}
@Command
@NotifyChange({"currentDepartment", "departments"})
public void saveDepartment() {
this.em.getTransaction().begin();
if (!isEditing) {
this.departments.addNewDepartment(this.currentDepartment);
}
this.em.getTransaction().commit();
this.currentDepartment = null;
}
@Command
@NotifyChange("departments")
public void delete(@BindingParam("d") Department department) {
this.em.getTransaction().begin();
this.departments.deleteDepartment(department);
this.em.getTransaction().commit();
}
@Command
@NotifyChange("currentDepartment")
public void edit(@BindingParam("d") Department department) {
this.isEditing = true;
this.currentDepartment = department;
}
}
<zk>
<window title="Hello World" border="normal"
<window title="Departments" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('dgpena.siexample.webapp.DepartmentsVM')"
>
<listbox model="@bind(vm.departments)">
<listhead>
<listheader label="name"></listheader>
<listheader label="actions"></listheader>
</listhead>
<template name="model">
<listitem>
<listcell><label value="@bind(each.name)"></label></listcell>
<listcell>work in progress</listcell>
</listitem>
</template>
<window title="Department Editor" mode="modal" visible="@load(vm.currentDepartment ne null)">
Name: <textbox value="@load(vm.currentDepartment.name) @save(vm.currentDepartment.name, before='saveDepartment')"></textbox>
<hbox>
<button label="accept" onClick="@command('saveDepartment')"></button>
<button label="cancel" onClick="@command('resetEditing')"></button>
</hbox>
</listbox>
</window>
<groupbox mold="3d" closable="false">
<caption label="Departments list">
<button label="new" onClick="@command('newDepartment')"></button>
</caption>
<listbox model="@bind(vm.departments)">
<listhead>
<listheader label="name"></listheader>
<listheader label="actions"></listheader>
</listhead>
<template name="model">
<listitem>
<listcell><label value="@bind(each.name)"></label></listcell>
<listcell>
<button label="edit" onClick="@command('edit', d=each)"></button>
<button label="delete" onClick="@command('delete', d=each)"></button>
</listcell>
</listitem>
</template>
</listbox>
</groupbox>
</window>
</zk>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment