com.evasion.common.controler.Person Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.common.controler;
import com.evasion.entity.Civilite;
import com.evasion.entity.Individual;
import com.evasion.entity.Corporation;
import com.evasion.ejb.local.PersonManagerLocal;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
/**
*
* @author sebastien.glon
*/
@ManagedBean
@RequestScoped
public class Person implements Serializable {
/**
* Type de personne (indiv={@link Individual}; corpo={@link Corporation}}.
*/
private String type = "";
/**
* Champ civilite du formulaire.
*/
private String civilite = "";
/**
* Champ nom du formulaire.
*/
private String nom = "";
/**
* Champ prenom du formulaire.
*/
private String prenom = "";
private Date naissance;
private String email = "";
private String description = "";
public static final String INDIV = "indiv";
public static final String CORPO = "corpo";
private HtmlPanelGroup formIndiv;
private HtmlPanelGroup formCorpo;
private com.evasion.entity.Person personEntity;
@EJB
private PersonManagerLocal ejb;
public com.evasion.entity.Person getPersonEntity() {
return personEntity;
}
public List valueList() {
List list = new ArrayList();
for (Enum e : Civilite.values()) {
String name = e.name();
list.add(name);
}
return list;
}
public void initTitreCivil(String value) {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException();
}
type = value;
formIndiv.setRendered(INDIV.equals(type));
formCorpo.setRendered(CORPO.equals(type));
FacesContext contexte = FacesContext.getCurrentInstance();
formIndiv.saveState(contexte);
formCorpo.saveState(contexte);
}
public void actChangeTitreCivil(ValueChangeEvent evt) {
if (evt.getNewValue() instanceof String && !((String) evt.getNewValue()).isEmpty()) {
initTitreCivil((String) evt.getNewValue());
}
}
public String validPerson() {
if ((type != null && type.equals("indiv")) || !prenom.isEmpty()) {
personEntity = new Individual(email, convertCivilite(civilite), nom, prenom, naissance);
} else if ((type != null && type.equals("corpo")) || !description.isEmpty()) {
personEntity = new Corporation(nom, email, description);
}
return null;
}
public String savePerson() {
validPerson();
personEntity = ejb.createPerson(personEntity);
return "";
}
private Civilite convertCivilite(String u) {
return Civilite.valueOf(u);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCivilite() {
return civilite;
}
public void setCivilite(String civilite) {
this.civilite = civilite;
}
public Date getNaissance() {
return naissance;
}
public void setNaissance(Date naissance) {
this.naissance = naissance;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public HtmlPanelGroup getFormCorpo() {
return formCorpo;
}
public void setFormCorpo(HtmlPanelGroup formCorpo) {
this.formCorpo = formCorpo;
}
public HtmlPanelGroup getFormIndiv() {
return formIndiv;
}
public void setFormIndiv(HtmlPanelGroup formIndiv) {
this.formIndiv = formIndiv;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}