From a71de1e548ebb781765693ba66b130cc23e5ed71 Mon Sep 17 00:00:00 2001 From: dgpena Date: Thu, 30 Nov 2017 19:54:26 +0100 Subject: [PATCH] Adds Projects management --- .../dgpena/siexample/webapp/ProjectsVM.java | 95 +++++++++++++++++++ src/main/webapp/edit_projects.zul | 53 +++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/main/java/dgpena/siexample/webapp/ProjectsVM.java create mode 100644 src/main/webapp/edit_projects.zul diff --git a/src/main/java/dgpena/siexample/webapp/ProjectsVM.java b/src/main/java/dgpena/siexample/webapp/ProjectsVM.java new file mode 100644 index 0000000..cb2887b --- /dev/null +++ b/src/main/java/dgpena/siexample/webapp/ProjectsVM.java @@ -0,0 +1,95 @@ +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.Employee; +import dgpena.siexample.persistence.Employees; +import dgpena.siexample.persistence.Project; +import dgpena.siexample.persistence.Projects; + +public class ProjectsVM { + + private EntityManager em; + private Projects projects; + private Employees employees; + + private boolean isEditing = false; + + // department under edition... + private Project currentProject; + + @Init + public void init() { + this.em = DesktopEntityManagerManager.getDesktopEntityManager(); + this.projects = new Projects(em); + this.employees = new Employees(em); + } + + public int getProjectParticipants(@BindingParam("p") Project p) { + return p.getEmployees().size(); + } + public List getProjects() { + return this.projects.findAll(); + } + + public List getEmployees() { + return this.employees.findAll(); + } + + public Project getCurrentProject() { + return currentProject; + } + + public void setCurrentProject(Project currentProject) { + this.currentProject = currentProject; + } + + @Command + @NotifyChange("currentProject") + public void newProject() { + this.isEditing = false; + this.currentProject = new Project(); + } + + @Command + @NotifyChange("currentProject") + public void resetEditing() { + System.out.println(this.currentProject.getName()); + this.currentProject = null; + } + + @Command + @NotifyChange({"currentProject", "projects"}) + public void saveProject() { + + this.em.getTransaction().begin(); + if (!isEditing) { + this.projects.addNewProject(this.currentProject); + } + this.em.getTransaction().commit(); + + this.currentProject = null; + } + + @Command + @NotifyChange("projects") + public void delete(@BindingParam("p") Project department) { + this.em.getTransaction().begin(); + this.projects.deleteProject(department); + this.em.getTransaction().commit(); + } + + @Command + @NotifyChange("currentProject") + public void edit(@BindingParam("p") Project department) { + this.isEditing = true; + this.currentProject = department; + } +} diff --git a/src/main/webapp/edit_projects.zul b/src/main/webapp/edit_projects.zul new file mode 100644 index 0000000..cc1adc5 --- /dev/null +++ b/src/main/webapp/edit_projects.zul @@ -0,0 +1,53 @@ + + + + + Name: + Employees: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.18.1