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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| public class TomodachiForm {
// Attribus :
private String champ_nom = "nomT";
private String champ_prenom = "prenomT";
private String champ_age = "AgeT";
private String champ_passe = "passeT";
private String champ_confirmation = "confirmationT";
private String champ_email = "emailT";
private String champ_adresse = "adresseT";
private String champ_ville = "villesT";
private String champ_apropos = "aproposT";
private String forma_date = "YYYY/MM/DD HH:MM:SS";
private static Map<String, String> erreurs = new HashMap<String, String>();
private static String resultats;
public Map<String, String> getErreurs() {
return erreurs;
}
public String getResultats() {
return resultats;
}
// Methode principal :
public Tomodachi formTomodachi( HttpServletRequest request ) {
String nom = getvalueChamp( request, champ_nom );
String prenom = getvalueChamp( request, champ_prenom );
String age = getvalueChamp( request, champ_age );
String passe = getvalueChamp( request, champ_passe );
String confirmation = getvalueChamp( request, champ_confirmation );
String email = getvalueChamp( request, champ_email );
String adresse = getvalueChamp( request, champ_adresse );
String ville = getvalueChamp( request, champ_ville );
String apropos = getvalueChamp( request, champ_apropos );
Tomodachi tomodachi = new Tomodachi();
traiterNom( nom, tomodachi );
traiterPrenom( prenom, tomodachi );
traiterAge( age, tomodachi );
traiterPasse( passe, confirmation, tomodachi );
traiterEmail( email, tomodachi );
traiterVille( ville, tomodachi );
tomodachi.setAdresse( adresse );
tomodachi.setApropos( apropos );
DateTime dt = new DateTime();
DateTimeFormatter forma = DateTimeFormat.forPattern( forma_date );
String date = dt.toString( forma );
tomodachi.setDate_inscription( date );
if ( erreurs.isEmpty() ) {
resultats = "Inscription réussite";
} else {
resultats = "Inscription échoué";
}
return tomodachi;
}
// methodes utilitaires :
public void traiterVille( String ville, Tomodachi tomodachi ) {
try {
validationVille( ville );
} catch ( FormsException e ) {
setErreurs( champ_ville, e.getMessage() );
}
tomodachi.setVille( ville );
}
public void traiterEmail( String email, Tomodachi tomodachi ) {
try {
validationEmail( email );
} catch ( FormsException e ) {
setErreurs( champ_email, e.getMessage() );
}
tomodachi.setEmail( email );
}
public void traiterPasse( String motpasse, String confirm, Tomodachi tomodachi ) {
try {
validationPasse( motpasse, confirm );
} catch ( FormsException e ) {
setErreurs( champ_passe, e.getMessage() );
setErreurs( champ_confirmation, null );
}
tomodachi.setMot_de_passe( motpasse );
}
public void traiterAge( String age, Tomodachi tomodachi ) {
try {
validationAge( age );
} catch ( FormsException e ) {
setErreurs( champ_age, e.getMessage() );
}
tomodachi.setAge( age );
}
public void traiterPrenom( String prenom, Tomodachi tomodachi ) {
try {
validationPrenom( prenom );
} catch ( FormsException e ) {
setErreurs( champ_prenom, e.getMessage() );
}
tomodachi.setPrenom( prenom );
}
public void traiterNom( String nom, Tomodachi tomodachi ) {
try {
validationNom( nom );
} catch ( FormsException e ) {
setErreurs( champ_nom, e.getMessage() );
}
tomodachi.setNom( nom );
}
public void validationVille( String ville ) throws FormsException {
if ( ville == null ) {
throw new FormsException( "Veuillez choisir votre ville" );
}
}
public void validationEmail( String email ) throws FormsException {
if ( email != null ) {
if ( !email.matches( "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" ) ) {
throw new FormsException( "L'adresse email n'est pas valide" );
}
} else {
throw new FormsException( "Veuillez entrer votre adresse email" );
}
}
public void validationPasse( String motpasse, String confir ) throws FormsException {
if ( motpasse != null && confir != null ) {
if ( !motpasse.equals( confir ) ) {
throw new FormsException( "Le mot de passe et la confirmation sont differents" );
}
if ( motpasse.length() < 3 ) {
throw new FormsException( "Le mot de passe doit contenir plus de 3 caractères" );
}
} else {
throw new FormsException( "Veuillez entrer un mot de passe et sa confirmation" );
}
}
public void validationAge( String age ) throws FormsException {
if ( age != null ) {
if ( !age.matches( "^\\d+$" ) ) {
throw new FormsException( "L'age doit etre numérique" );
}
if ( age.length() > 3 ) {
throw new FormsException( "L'age doit contenir 3 chiffres maximum" );
}
} else {
throw new FormsException( "Veuillez entrer votre age" );
}
}
public void validationPrenom( String prenom ) throws FormsException {
if ( prenom == null ) {
throw new FormsException( "Veuillez entrer un prenom" );
}
}
public void validationNom( String nom ) throws FormsException {
if ( nom == null ) {
throw new FormsException( "Veuillez entrer un nom" );
}
}
public static String getvalueChamp( HttpServletRequest request, String champ ) {
String value = request.getParameter( champ );
if ( value == null || value.trim().length() == 0 ) {
return null;
} else {
return value.trim();
}
}
public void setErreurs( String champ, String message ) {
erreurs.put( champ, message );
}
} |
Partager