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 0000000000000000000000000000000000000000..cb2887b118fd7a952b0d50b44f1dec4901d23221 --- /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 0000000000000000000000000000000000000000..cc1adc5b1121cb8a55a0cc2b5ad9da19bdb7956c --- /dev/null +++ b/src/main/webapp/edit_projects.zul @@ -0,0 +1,53 @@ + + + + + Name: + Employees: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file