Package com.example.jsfdemo.web

Source Code of com.example.jsfdemo.web.PersonFormBean

package com.example.jsfdemo.web;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIForm;
import javax.faces.component.UIInput;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.model.ListDataModel;
import javax.faces.validator.ValidatorException;
import javax.inject.Inject;
import javax.inject.Named;

import com.example.jsfdemo.domain.Person;
import com.example.jsfdemo.service.PersonManager;

@SessionScoped
@Named("personBean")
public class PersonFormBean implements Serializable {

  private static final long serialVersionUID = 1L;

  private Person person = new Person();

  private ListDataModel<Person> persons = new ListDataModel<Person>();

  @Inject
  private PersonManager pm;

  public Person getPerson() {
    return person;
  }
 
  public Person getPersonForName(String firstName){
   
    return pm.getPerson(firstName);
  }

 
  public void setPerson(Person person) {
    this.person = person;
  }

  public ListDataModel<Person> getAllPersons() {
    persons.setWrappedData(pm.getAllPersons());
    return persons;
  }

  // Actions
  public String addPerson() {
    pm.addPerson(person);
    return "showPersons";
    //return null;
  }

  public String deletePerson() {
    Person personToDelete = persons.getRowData();
    pm.deletePerson(personToDelete);
    return null;
  }

  // Validators

  // Business logic validation
//  public void uniquePin(FacesContext context, UIComponent component,
//      Object value) {
//
//    String pin = (String) value;
//
//    for (Person person : pm.getAllPersons()) {
//      if (person.getPin().equalsIgnoreCase(pin)) {
//        FacesMessage message = new FacesMessage(
//            "Person with this PIN already exists in database");
//        message.setSeverity(FacesMessage.SEVERITY_ERROR);
//        throw new ValidatorException(message);
//      }
//    }
//  }
 
 
  // Unique person name
//  public void uniquePerson(FacesContext context, UIComponent component,
//      Object value) {
//
//    String firstName = (String) value;
//
//    for (Person person : pm.getAllPersons()) {
//      if (person.getFirstName().equalsIgnoreCase(firstName)) {
//        FacesMessage message = new FacesMessage(
//            "Person with this Name already exists in database");
//        message.setSeverity(FacesMessage.SEVERITY_ERROR);
//        throw new ValidatorException(message);
//      }
//    }
//  }

  // Multi field validation with <f:event>
  // Rule: first two digits of PIN must match last two digits of the year of
  // birth
  public void validatePinDob(ComponentSystemEvent event) {

    UIForm form = (UIForm) event.getComponent();
    UIInput pin = (UIInput) form.findComponent("pesel");
    UIInput dob = (UIInput) form.findComponent("dob");
   
    if (pin.getValue() != null && dob.getValue() != null
        && pin.getValue().toString().length() >= 2) {
      String twoDigitsOfPin = pin.getValue().toString().substring(0, 2);
      Calendar cal = Calendar.getInstance();
      cal.setTime(((Date) dob.getValue()));

      String lastDigitsOfDob = ((Integer) cal.get(Calendar.YEAR))
          .toString().substring(2);

      if (!twoDigitsOfPin.equals(lastDigitsOfDob)) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(form.getClientId(), new FacesMessage(
            "PIN doesn't match date of birth"));
        context.renderResponse();
      }
    }
  }
}
TOP

Related Classes of com.example.jsfdemo.web.PersonFormBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.