package actions;
import interfaces.exceptions.MetierException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
//import metier.clients.MetierClients;
import interfaces.clients.*;
import ressources.Message;
import base.Client;
public class LoginAction implements Action {
private String suite, erreur,suiteValidationPanier;
public LoginAction(String suite, String erreur,String suiteValidationPanier) {
this.suite = suite;
this.suiteValidationPanier = suiteValidationPanier;
this.erreur = erreur;
}
/**
*
*/
public String execute(HttpServletRequest req) throws Exception {
IMetierClients mc = (IMetierClients)req.getSession().getServletContext().getAttribute("metierClients");
//MetierClients mc = new MetierClients();
String actionLogin = req.getParameter(Message.ACTION_LOGIN);
String actionValidation = req.getParameter(Message.ACTION_VALIDATION);
// On est sur une validation panier
if(actionValidation != null){
// Si erreur on revient avec le bon message
req.setAttribute(Message.ACTION_VALIDATION, Message.VALIDATION);
req.setAttribute(Message.ACTION_VALIDATION, Message.AFF_VALIDATION);
//suite = suiteValidationPanier;
}
if(actionLogin != null){
String id = req.getParameter("id");
if (!verifierChaine(id)) {
req.setAttribute(Message.MESSAGE_ERREUR, Message.SAISIE_ID);
return erreur;
}
String mdp = req.getParameter("mdp");
try {
System.out.println("ID = "+id);
System.out.println("MDP = "+mdp);
Client client = mc.rechercherClient(id);
System.out.println("Client = "+client);
if(!client.getMdp().equals(mdp)){
req.setAttribute(Message.MESSAGE_ERREUR, Message.MDP_INCORRECTE);
return erreur;
}
HttpSession session = req.getSession(true);
session.setAttribute("client", client);
} catch (MetierException me) {
me.printStackTrace();
req.setAttribute(Message.MESSAGE_ERREUR, Message.CLIENT_INEXISTANT);
return erreur;
}
}
return suite;
}
/**
* Si aucun problème, on renvoit true
*
* @param chaine
* La chaine à vérifier
* @return
*/
private boolean verifierChaine(String chaine) {
if (chaine == null || chaine.trim().equals("")) {
return false;
}
return true;
}
}