ZK MVVM CRUD Without DB Connection - Part 4


        

In Part 3,
we have seen how we can add new customer and update the customer list. In this part, we will see how we can do Edit and delete customer.

Project Structure


image

First we will modify domain customer Class by adding implements clone Here is the code

package domain;

public class Customer implements Cloneable {

private String lastName;
private String firstName;
private String email;

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}



And also we will modify CustomerListVM.java as follows


package appVM;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Messagebox;
import domain.Customer;
import appData.CustomerData;

public class CustomerListVM {

private List<Customer> customerList = new ArrayList<Customer>();
private Customer curSelectedCustomer;
private Integer curSelectedCustomerIndex;

public Customer getCurSelectedCustomer() {
return curSelectedCustomer;
}

public void setCurSelectedCustomer(Customer curSelectedCustomer) {
this.curSelectedCustomer = curSelectedCustomer;
}

public Integer getCurSelectedCustomerIndex() {
return curSelectedCustomerIndex;
}

public void setCurSelectedCustomerIndex(Integer curSelectedCustomerIndex) {
this.curSelectedCustomerIndex = curSelectedCustomerIndex;
}

@Init
public void initSetup() {
customerList = new CustomerData().getAllCustomers();
}

public List<Customer> getallCustomers() {
return customerList;
}

@Command
public void addNewCustomer() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("sCustomer", null);
map.put("recordMode", "NEW");
Executions.createComponents("CustomerCRUD.zul", null, map);
}

@Command
public void editThisCustomer() {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("sCustomer", this.curSelectedCustomer);
map.put("recordMode", "EDIT");
setCurSelectedCustomerIndex(customerList.indexOf(curSelectedCustomer));
Executions.createComponents("CustomerCRUD.zul", null, map);
}

// The following method will be called from CustomerCRUDVM after the save
// When we say Notifychange("allcustomers), then ZUL list items will be
// updated
@GlobalCommand
@NotifyChange("allCustomers")
public void updateCustomerList(@BindingParam("pCustomer") Customer cust1,
@BindingParam("recordMode") String recordMode) {
if (recordMode.equals("NEW")) {
customerList.add(cust1);
}
if (recordMode.equals("EDIT")) {
customerList.set(this.curSelectedCustomerIndex, cust1);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Command
public void deleteThisCustomer() {
int OkCancel;

String str = "The Selected Customer LastName \""
+ curSelectedCustomer.getLastName() + "\" will be deleted.";
OkCancel = Messagebox.show(str, "Confirm", Messagebox.OK
| Messagebox.CANCEL, Messagebox.QUESTION);
if (OkCancel == Messagebox.CANCEL) {
return;
}

str = "The Selected Customer LastName \""
+ curSelectedCustomer.getLastName()
+ "\" will be permanently deleted and the action cannot be undone.";

Messagebox.show(str, "Confirm", Messagebox.OK | Messagebox.CANCEL,
Messagebox.QUESTION, new EventListener() {
@Override
public void onEvent(Event event) throws Exception {
if (((Integer) event.getData()).intValue() == Messagebox.OK) {
customerList.remove(customerList
.indexOf(curSelectedCustomer));
BindUtils.postNotifyChange(null, null,
CustomerListVM.this, "allCustomers");
}
}
});
}
}



And also, we will modify CustomerCRUDVM.java as follows


package appVM;

import java.util.HashMap;
import java.util.Map;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.ExecutionArgParam;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Window;
import domain.Customer;

public class CustomerCRUDVM {

/*
* This is the window ID used in CustomerCRUD.Zul File. Using this only, we
* can close the model window after save and cancel button
*/
@Wire("#CustomerCRUD")
private Window win;

private Customer selectedCustomer;
private String recordMode;

public String getRecordMode() {
return recordMode;
}

public void setRecordMode(String recordMode) {
this.recordMode = recordMode;
}

public Customer getSelectedCustomer() {
return selectedCustomer;
}

public void setSelectedCustomer(Customer selectedCustomer) {
this.selectedCustomer = selectedCustomer;
}

@Init
public void initSetup(@ContextParam(ContextType.VIEW) Component view,
@ExecutionArgParam("sCustomer") Customer c1,
@ExecutionArgParam("recordMode") String recordMode)
throws CloneNotSupportedException {
Selectors.wireComponents(view, this, false);

setRecordMode(recordMode);
if (recordMode.equals("NEW")) {
this.selectedCustomer = new Customer();
}

if (recordMode.equals("EDIT")) {
this.selectedCustomer = (Customer) c1.clone();
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Command
public void save() {
Map args = new HashMap();
args.put("pCustomer", this.selectedCustomer);
args.put("recordMode", this.recordMode);
BindUtils.postGlobalCommand(null, null, "updateCustomerList", args);
win.detach();
}

@Command
public void closeThis() {
win.detach();
}
}

Now you can run customerList.zul and test the Edit customer and Delete customer Option.

Download Source code