Created
March 25, 2013 10:46
-
-
Save senthilmuthiah/5236320 to your computer and use it in GitHub Desktop.
Address Book Using JPA +ZK + Spring.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package zkoss.vm; | |
import java.util.HashMap; | |
import java.util.List; | |
import org.zkoss.bind.BindUtils; | |
import org.zkoss.bind.annotation.AfterCompose; | |
import org.zkoss.bind.annotation.BindingParam; | |
import org.zkoss.bind.annotation.Command; | |
import org.zkoss.bind.annotation.ContextParam; | |
import org.zkoss.bind.annotation.ContextType; | |
import org.zkoss.bind.annotation.GlobalCommand; | |
import org.zkoss.bind.annotation.NotifyChange; | |
import org.zkoss.zk.ui.Component; | |
import org.zkoss.zk.ui.Executions; | |
import org.zkoss.zk.ui.event.Event; | |
import org.zkoss.zk.ui.event.EventListener; | |
import org.zkoss.zk.ui.select.Selectors; | |
import org.zkoss.zk.ui.select.annotation.WireVariable; | |
import org.zkoss.zkplus.spring.SpringUtil; | |
import org.zkoss.zul.Messagebox; | |
import addressbook.domain.AddressBook; | |
import addressbook.service.CRUDService; | |
public class AddressListVM { | |
@WireVariable | |
private CRUDService CRUDService; | |
private AddressBook selectedItem; | |
private List<AddressBook> allReordsInDB = null; | |
private Integer selectedItemIndex; | |
public AddressBook getSelectedItem() { | |
return selectedItem; | |
} | |
/*This method will be called when user select a record in the list*/ | |
public void setSelectedItem(AddressBook selectedItem) { | |
this.selectedItem = selectedItem; | |
} | |
public Integer getSelectedItemIndex() { | |
return selectedItemIndex; | |
} | |
public void setSelectedItemIndex(Integer selectedItemIndex) { | |
this.selectedItemIndex = selectedItemIndex; | |
} | |
/*Only one @AfterCompose-annotated method is allowed at the most in a ViewModel class*/ | |
/* Marker annotation to identify a life-cycle method in View Model. | |
this method will be called after host component composition has been done (AfterCompose). | |
*/ | |
@AfterCompose | |
public void initSetup(@ContextParam(ContextType.VIEW) Component view) { | |
Selectors.wireComponents(view, this, false); | |
CRUDService = (CRUDService) SpringUtil.getBean("CRUDService"); | |
allReordsInDB = CRUDService.getAll(AddressBook.class); | |
} | |
/* Using this method, the list box will be populated with records | |
Please note that, we have already initialized this list in the aftercompose. | |
Whenever, the list of the records has changed, then we will annotate with @NotifyChange("dataSet"), so that, | |
again this will be called to update the UI | |
*/ | |
public List<AddressBook> getDataSet() { | |
return allReordsInDB; | |
} | |
/*This method will be called when user press the new button in the Listing screen*/ | |
@Command | |
public void onAddNew() { | |
final HashMap<String, Object> map = new HashMap<String, Object>(); | |
map.put("selectedRecord", null); | |
map.put("recordMode", "NEW"); | |
Executions.createComponents("AddressCRUD.zul", null, map); | |
} | |
@Command | |
public void onEdit(@BindingParam("addressRecord") AddressBook addressBook) { | |
final HashMap<String, Object> map = new HashMap<String, Object>(); | |
this.selectedItem = addressBook; | |
map.put("selectedRecord", addressBook); | |
map.put("recordMode", "EDIT"); | |
setSelectedItemIndex(allReordsInDB.indexOf(selectedItem)); | |
Executions.createComponents("AddressCRUD.zul", null, map); | |
} | |
@Command | |
public void openAsReadOnly( | |
@BindingParam("addressRecord") AddressBook addressBook) { | |
final HashMap<String, Object> map = new HashMap<String, Object>(); | |
this.selectedItem = addressBook; | |
map.put("selectedRecord", addressBook); | |
map.put("recordMode", "READ"); | |
Executions.createComponents("AddressCRUD.zul", null, map); | |
} | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
@Command | |
public void onDelete(@BindingParam("addressRecord") AddressBook addressBook) { | |
int OkCancel; | |
this.selectedItem = addressBook; | |
String str = "The Selected \"" + addressBook.getFullName() | |
+ "\" will be deleted."; | |
OkCancel = Messagebox.show(str, "Confirm", Messagebox.OK | |
| Messagebox.CANCEL, Messagebox.QUESTION); | |
if (OkCancel == Messagebox.CANCEL) { | |
return; | |
} | |
str = "The \"" | |
+ addressBook.getFullName() | |
+ "\" will be permanently deleted and the action cannot be undone."; | |
Messagebox.show(str, "Confirm", Messagebox.OK | Messagebox.CANCEL, | |
Messagebox.QUESTION, new EventListener() { | |
public void onEvent(Event event) throws Exception { | |
if (((Integer) event.getData()).intValue() == Messagebox.OK) { | |
CRUDService.delete(selectedItem); | |
allReordsInDB.remove(allReordsInDB | |
.indexOf(selectedItem)); | |
BindUtils.postNotifyChange(null, null, | |
AddressListVM.this, "dataSet"); | |
} | |
} | |
}); | |
} | |
// note this will be executed from AddressListCRUDVM.java | |
@GlobalCommand | |
@NotifyChange("dataSet") | |
public void refreshList( | |
@BindingParam("selectedRecord") AddressBook addressbook, | |
@BindingParam("recordMode") String recordMode) { | |
if (recordMode.equals("NEW")) { | |
allReordsInDB.add(addressbook); | |
} | |
if (recordMode.equals("EDIT")) { | |
allReordsInDB.set(this.selectedItemIndex, addressbook); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment