diff --git a/src/main/java/dgpena/siexample/webapp/DepartmentsVM.java b/src/main/java/dgpena/siexample/webapp/DepartmentsVM.java index ca8cfe929eeb8555d4621f403cc4c0d9c1571666..883084f5a37b41e700d11eda663d19aff18769d4 100644 --- a/src/main/java/dgpena/siexample/webapp/DepartmentsVM.java +++ b/src/main/java/dgpena/siexample/webapp/DepartmentsVM.java @@ -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 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; + } } diff --git a/src/main/webapp/edit_departments.zul b/src/main/webapp/edit_departments.zul index 4d27fce3572402261208c76e79fe70f30cac0cdd..b3500512af13bdd2aaa14b8e6fcb7e588cdaf41d 100644 --- a/src/main/webapp/edit_departments.zul +++ b/src/main/webapp/edit_departments.zul @@ -1,23 +1,41 @@ - - - - - - - - + + Name: + + + + - + + + + + + + + + + + + + + + \ No newline at end of file