Adds Employees management

parent ae5c168e
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<Employee> getEmployees() {
return this.employees.findAll();
}
public List<Department> 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;
}
}
<zk>
<window title="Employees" border="normal"
apply="org.zkoss.bind.BindComposer"
viewModel="@id('vm') @init('dgpena.siexample.webapp.EmployeesVM')"
>
<window title="Employee Editor" mode="modal" visible="@load(vm.currentEmployee ne null)">
Name: <textbox value="@load(vm.currentEmployee.name) @save(vm.currentEmployee.name, before='saveEmployee')"></textbox>
Department:
<combobox model="@bind(vm.departments)" selectedItem="@load(vm.currentEmployee.department) @save(vm.currentEmployee.department, before='saveEmployee')">
<template name="model">
<comboitem label="@bind(each.name)"></comboitem>
</template>
</combobox>
<hbox>
<button label="accept" onClick="@command('saveEmployee')"></button>
<button label="cancel" onClick="@command('resetEditing')"></button>
</hbox>
</window>
<groupbox mold="3d" closable="false">
<caption label="Employees list">
<button label="new" onClick="@command('newEmployee')"></button>
</caption>
<listbox model="@bind(vm.employees)">
<listhead>
<listheader label="name"></listheader>
<listheader label="department"></listheader>
<listheader label="actions"></listheader>
</listhead>
<template name="model">
<listitem>
<listcell><label value="@bind(each.name)"></label></listcell>
<listcell><label value="@bind(each.department.name)"></label></listcell>
<listcell>
<button label="edit" onClick="@command('edit', e=each)"></button>
<button label="delete" onClick="@command('delete', e=each)"></button>
</listcell>
</listitem>
</template>
</listbox>
</groupbox>
</window>
</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