From e926d3ad5c34f34c54cf4dc11b7f98c59fe83b6f Mon Sep 17 00:00:00 2001 From: dgpena Date: Thu, 30 Nov 2017 19:54:10 +0100 Subject: [PATCH] Adds Employees management --- .../dgpena/siexample/webapp/EmployeesVM.java | 91 +++++++++++++++++++ src/main/webapp/edit_employees.zul | 50 ++++++++++ 2 files changed, 141 insertions(+) create mode 100644 src/main/java/dgpena/siexample/webapp/EmployeesVM.java create mode 100644 src/main/webapp/edit_employees.zul diff --git a/src/main/java/dgpena/siexample/webapp/EmployeesVM.java b/src/main/java/dgpena/siexample/webapp/EmployeesVM.java new file mode 100644 index 0000000..bd6e852 --- /dev/null +++ b/src/main/java/dgpena/siexample/webapp/EmployeesVM.java @@ -0,0 +1,91 @@ +package dgpena.siexample.webapp; + +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; +import dgpena.siexample.persistence.Employee; +import dgpena.siexample.persistence.Employees; + +public class EmployeesVM { + + private EntityManager em; + private Employees employees; + private Departments departments; + + private boolean isEditing = false; + + // Employee under edition... + private Employee currentEmployee; + + @Init + public void init() { + this.em = DesktopEntityManagerManager.getDesktopEntityManager(); + this.employees = new Employees(em); + this.departments = new Departments(em); + } + + public List getEmployees() { + return this.employees.findAll(); + } + + public List getDepartments() { + return this.departments.findAll(); + } + + public Employee getCurrentEmployee() { + return currentEmployee; + } + + public void setCurrentEmployee(Employee currentEmployee) { + this.currentEmployee = currentEmployee; + } + + @Command + @NotifyChange("currentEmployee") + public void newEmployee() { + this.isEditing = false; + this.currentEmployee = new Employee(); + } + + @Command + @NotifyChange("currentEmployee") + public void resetEditing() { + this.currentEmployee = null; + } + + @Command + @NotifyChange({"currentEmployee", "employees"}) + public void saveEmployee() { + + this.em.getTransaction().begin(); + if (!isEditing) { + this.employees.addNewEmployee(this.currentEmployee); + } + this.em.getTransaction().commit(); + + this.currentEmployee = null; + } + + @Command + @NotifyChange("employees") + public void delete(@BindingParam("e") Employee Employee) { + this.em.getTransaction().begin(); + this.employees.deleteEmployee(Employee); + this.em.getTransaction().commit(); + } + + @Command + @NotifyChange("currentEmployee") + public void edit(@BindingParam("e") Employee Employee) { + this.isEditing = true; + this.currentEmployee = Employee; + } +} diff --git a/src/main/webapp/edit_employees.zul b/src/main/webapp/edit_employees.zul new file mode 100644 index 0000000..b267efe --- /dev/null +++ b/src/main/webapp/edit_employees.zul @@ -0,0 +1,50 @@ + + + + + Name: + Department: + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.18.1