Completes Departments management

parent 911c9d76
...@@ -4,7 +4,10 @@ import java.util.List; ...@@ -4,7 +4,10 @@ import java.util.List;
import javax.persistence.EntityManager; 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.Init;
import org.zkoss.bind.annotation.NotifyChange;
import dgpena.siexample.persistence.Department; import dgpena.siexample.persistence.Department;
import dgpena.siexample.persistence.Departments; import dgpena.siexample.persistence.Departments;
...@@ -14,6 +17,11 @@ public class DepartmentsVM { ...@@ -14,6 +17,11 @@ public class DepartmentsVM {
private EntityManager em; private EntityManager em;
private Departments departments; private Departments departments;
private boolean isEditing = false;
// department under edition...
private Department currentDepartment;
@Init @Init
public void init() { public void init() {
this.em = DesktopEntityManagerManager.getDesktopEntityManager(); this.em = DesktopEntityManagerManager.getDesktopEntityManager();
...@@ -23,4 +31,54 @@ public class DepartmentsVM { ...@@ -23,4 +31,54 @@ public class DepartmentsVM {
public List<Department> getDepartments() { public List<Department> getDepartments() {
return this.departments.findAll(); 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> <zk>
<window title="Hello World" border="normal" <window title="Departments" border="normal"
apply="org.zkoss.bind.BindComposer" apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('dgpena.siexample.webapp.DepartmentsVM')" viewModel="@id('vm') @init('dgpena.siexample.webapp.DepartmentsVM')"
> >
<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>
</window>
<groupbox mold="3d" closable="false">
<caption label="Departments list">
<button label="new" onClick="@command('newDepartment')"></button>
</caption>
<listbox model="@bind(vm.departments)"> <listbox model="@bind(vm.departments)">
<listhead> <listhead>
<listheader label="name"></listheader> <listheader label="name"></listheader>
...@@ -13,11 +25,17 @@ ...@@ -13,11 +25,17 @@
<template name="model"> <template name="model">
<listitem> <listitem>
<listcell><label value="@bind(each.name)"></label></listcell> <listcell><label value="@bind(each.name)"></label></listcell>
<listcell>work in progress</listcell> <listcell>
<button label="edit" onClick="@command('edit', d=each)"></button>
<button label="delete" onClick="@command('delete', d=each)"></button>
</listcell>
</listitem> </listitem>
</template> </template>
</listbox> </listbox>
</groupbox>
</window> </window>
</zk> </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