1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
package bean;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import service.ServiceContact;
@ManagedBean(name = "contact") // Concentré d'actionForm et action
@SessionScoped
public class Contact implements Serializable {
// login et password du User
private static final long serialVersionUID = 1L;
private int contactId;
private String firstName;
private String lastName;
private String email;
private String street;
private String city;
private String zip;
private String country;
private Address contactAddress;
public int getContactId() {
return contactId;
}
public void setContactId(int contactId) {
this.contactId = contactId;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String name) {
this.firstName = name;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String name) {
this.lastName = name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Address getContactAddress() {
return contactAddress;
}
public void setContactAddress(Address contactAddress) {
this.contactAddress = contactAddress;
}
public Contact(){
}
public Contact(int contactId, String firstName, String lastName, String email, Address contactAddress){
this.contactId = contactId;
this.firstName= firstName;
this.lastName = lastName;
this.email = email;
this.contactAddress = contactAddress;
}
public String checkContact() throws NamingException, SQLException {
// Gestion des messages d'erreurs
FacesContext context = FacesContext.getCurrentInstance();
if (isMissing(firstName) || isMissing(lastName) || isMissing(email) || isMissing(street) || isMissing(city)
|| isMissing(zip) || isMissing(country)) {
if (isMissing(firstName)) {
context.addMessage(null, new FacesMessage("Prénom requis"));
}
if (isMissing(lastName)) {
context.addMessage(null, new FacesMessage("Nom requis"));
}
if (isMissing(email)) {
context.addMessage(null, new FacesMessage("Mail requis"));
}
if (isMissing(street)) {
context.addMessage(null, new FacesMessage("Rue requis"));
}
if (isMissing(city)) {
context.addMessage(null, new FacesMessage("Ville requis"));
}
if (isMissing(zip)) {
context.addMessage(null, new FacesMessage("Code postal requis"));
}
if (isMissing(country)) {
context.addMessage(null, new FacesMessage("Pays requis"));
}
return null;
} else {
final ServiceContact Service = new ServiceContact();
//Ajout de l'adresse du contact
final String addressError = Service.addAddress(street, city, zip, country);
//Récupère l'identifiant de l'adresse pour pouvoir ajouter l'adresse au contact
final int idAddress = Service.getAddressId(street, city, zip, country);
//Ajout du contact
final String contactError = Service.addContact(firstName, lastName, email, idAddress);
//Redirection en fonction des erreurs de traitement
if (addressError==null|| idAddress != 0 || contactError==null) {
return ("menu");
} else {
return ("login");
}
}
}
@PostConstruct
public ArrayList<Contact> displayContacts(){
//Show contacts
final ServiceContact lDAOContact = new ServiceContact();
final ArrayList<Contact> Contacts = lDAOContact.getContacts();
return Contacts;
}
// Contrôle que les champs sont non-vides
private boolean isMissing(String value) {
return ((value == null) || (value.trim().isEmpty()));
}
} |
Partager