package com.ebpm.webdemo.zk;

import java.util.Collection;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Window;

import com.ebpm.webdemo.common.PersonBean;
import com.ebpm.webdemo.common.PersonDAO;

/**
 * SimpleWindow2.java
 * 1.) Handle submit-button with the onOK-Event --> returnkey = submit button
 * 2.) Handle cancel-button with onCancel-Event --> esc-Key = Cancel button
 
 * Note: reload always the whole list (like jsf and struts)
 */
public class SimpleWindow2 extends Window {

  private static final long serialVersionUID = 3975852223657115755L;
  public PersonBean personBean = new PersonBean();
  private AnnotateDataBinder binder;

  public PersonBean getPersonBean() {
    return personBean;
  }

  public Collection getAllPersons() {
    return  PersonDAO.getInstance().getAllPersons();
  }

  public void onCreate() {
    // get the databinder for later extra load and save
    binder = (AnnotateDataBinderthis.getVariable("binder"false);
  }

  public void onOK() {
    binder.saveAll()//load Inputfields from Form, Constraints will be performed
    PersonDAO.getInstance().addPerson(personBean);  // add the person (from inputfields) to model
    this.personBean = new PersonBean()// prepare the next input
    /* load the empty (new) person in the input formular
     * load the whole model into the UI  
     */
    binder.loadAll()
  }
  public void onCancel() {
    Executions.sendRedirect("/index.jsp");
  }
}