Commit 71eca17b authored by Lefebvre michael's avatar Lefebvre michael

projet ecommerce

parent fc60ee30
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
\ No newline at end of file
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 14:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<meta http-equiv="Refresh" content="0; URL=process?action=init"/>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 17:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Catalogue</title>
</head>
<body>
<form method="post" action="process?action=addItems">
<table align="center">
<thead>
<th>id</th>
<th>descriptif</th>
<th>prix</th>
<th>disponible</th>
<th>stock</th>
</thead>
<tbody>
<c:forEach items="${catalogue.produits}" var="myitem">
<tr>
<td> <input type="checkbox" name="itemId" value="${myitem.value.nProd}">
</td>
<td>${myitem.value.descript}</td>
<td>${myitem.value.prix}</td>
<td>${myitem.value.dispo}</td>
<td>${myitem.value.stock}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 16:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Connection</title>
</head>
<body>
<form method="post" action="process?action=authenticate">
<fieldset>
<legend>login</legend>
<label>Nom</label>
<input type="text" name="nom">
<br>
<label>mot de passe</label>
<input type="password" name="motDePasse">
</fieldset>
<input type="submit" value="envoie">
</form>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/web" relative="/" />
</webroots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Tomcat 9.0.311" level="application_server_libraries" />
<orderEntry type="library" name="mysql-connector-java-5.1.6-bin" level="project" />
<orderEntry type="library" name="jstl-api-1.2" level="project" />
</component>
</module>
\ No newline at end of file
package control;
import dto.Catalogue;
import dto.ClientDTO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import static dao.EcommerceDAO.*;
@WebServlet("/process")
public class EcommerceServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRquest(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRquest(request, response);
}
//navigation
private void forward(String url, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);
}
//routage
private void processRquest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
try {
//recupertaion de l'action
String action = request.getParameter("action");
//execution du traitement
if (action.equals("init")){
doInit(request, response);
}else if (action.equals("authenticate")){
doAuthenticate(request, response);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
private void doAuthenticate(HttpServletRequest request, HttpServletResponse response) {
try {
String nom = request.getParameter("nom");
String motDePasse = request.getParameter("motDePasse");
//1 recherche du client
ClientDTO client = INSTANCE.findClientsByNom(nom);
if (client != null){
if (client.getPass().equals(motDePasse)){
request.getSession().setAttribute("client", client);
}else {
request.getSession().removeAttribute("client");
client=null;
}
}
// cinematique
String url = (client == null ? "pages/showLogin.jsp":"pages/showCatalogue.jsp");
forward(url, request, response);
}catch (Exception e){
e.printStackTrace();
}
}
private void doInit(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
//gestion du cache application
if (this.getServletContext().getAttribute("catalogue")==null){
Catalogue catalogue = INSTANCE.getCatalogue();
this.getServletContext().setAttribute("catalogue", catalogue);
}
String url = "pages/showLogin.jsp";
forward(url, request, response);
}
}
package dao;
import dto.*;
import java.sql.*;
import java.util.*;
public enum EcommerceDAO{
INSTANCE;
//IDENTIFICATION BASE DE DONNEES
private final static String URL = "jdbc:mysql://localhost:3306/ecommerce";
private final static String USER = "michael";
private final static String PW = "mdppopmichael";
//SINGLETON : DECLARATION
private Connection connection = null;
private EcommerceDAO(){
try{
connection = DriverManager.getConnection(URL, USER, PW);
}catch (Exception e){
System.out.println(e);;
}
}
private static String SQLFindAllItems =
"SELECT nproduit, descriptif, prix, disponible, qteEnStock FROM ECOMM_PRODUIT";
private static String SQLFindClientByNom =
"SELECT motDePasse from ECOMM_CLIENT WHERE nom = ? ";
public Catalogue getCatalogue(){
Map<String, ProduitDTO> items = new HashMap<>();
try{
PreparedStatement selectStatement =
connection.prepareStatement(SQLFindAllItems);
ResultSet rs = selectStatement.executeQuery();
while (rs.next()){
String nproduit = (rs.getString("nproduit")).trim();
String descript = rs.getString("descriptif");
float prix = rs.getFloat("prix");
boolean disponible = (rs.getString("disponible")).equals("vrai");
int stock = rs.getInt("qteEnStock");
items.put(nproduit, new ProduitDTO(nproduit, descript, prix, disponible, stock));
}
}catch (SQLException ex){
ex.printStackTrace();
}
return new Catalogue(items);
}
public ClientDTO findClientsByNom(String nom) throws SQLException {
String motDePasse = null;
PreparedStatement selectStatement = connection.prepareStatement(SQLFindClientByNom);
selectStatement.setString(1, nom);
ResultSet rs = selectStatement.executeQuery();
if (rs.next()){
motDePasse = rs.getString("motDePasse");
return new ClientDTO(nom, motDePasse);
}
return null;
}
//le cache catalogue doit etre mis a jour donc il doit etre passé en parametre
//les tables commande, detail_cmde
public void createPanier(Panier panier, Catalogue catalogue) throws SQLException{
}
public static void main(String[] args) {
}
}
package dto;
import dto.ProduitDTO;
import java.io.Serializable;
import java.util.*;
public class Catalogue implements Serializable {
private Map<String, ProduitDTO> produits= new HashMap<>();
synchronized public Map<String, ProduitDTO> getProduits(){
//renvoie une copie en lecture seule
return Collections.unmodifiableMap(produits);
}
synchronized public ProduitDTO getProduit(String nProd){
return produits.get(nProd);
}
synchronized public void setProduit(String nProd, ProduitDTO produit){
produits.put(nProd, produit);
}
public Catalogue(Map<String, ProduitDTO> produits){
this.produits=produits;
}
}
package dto;
import java.io.Serializable;
public class ClientDTO implements Serializable {
private String nom;
private String pass;
//GETTER SETTER
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPass() {
return pass;
}
public void setPrenom(String pass) {
this.pass = pass;
}
//Constructeur
public ClientDTO(String nom, String pass) {
this.nom = nom;
this.pass = pass;
}
//to string
@Override
public String toString() {
return "ClientDTO{" +
"nom='" + nom + '\'' +
", prenom='" + pass + '\'' +
'}';
}
}
package dto;
import java.io.Serializable;
import java.sql.Date;
public class CommandeDTO implements Serializable {
private long nCommande;
private Date dateCommande;
private String nomClient;
//GETTER SETTER
public long getnCommande() {
return nCommande;
}
public void setnCommande(long nCommande) {
this.nCommande = nCommande;
}
public Date getDateCommande() {
return dateCommande;
}
public void setDateCommande(Date dateCommande) {
this.dateCommande = dateCommande;
}
public String getNomClient() {
return nomClient;
}
public void setNomClient(String nomClient) {
this.nomClient = nomClient;
}
//constructeur
public CommandeDTO(long nCommande, Date dateCommande, String nomClient) {
this.nCommande = nCommande;
this.dateCommande = dateCommande;
this.nomClient = nomClient;
}
//tostring
@Override
public String toString() {
return "CommandeDTO{" +
"nCommande=" + nCommande +
", dateCommande=" + dateCommande +
", nomClient='" + nomClient + '\'' +
'}';
}
}
package dto;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectEcomm {
//url de connection
private final static String URL = "jdbc:mysql://localhost:3306/cinema";
//utilisatuer de connection
private final static String USER = "michael";
//mot de passe de connection
private final static String PW = "mdppopmichael";
//SINGLETON
private static Connection INSTANCE;
//constructeur privé
private ConnectEcomm() {
try{
INSTANCE = DriverManager.getConnection(URL,USER,PW);
}catch(SQLException e){
e.printStackTrace();
}
}
public static Connection getInstance(){
if (INSTANCE == null){
new ConnectEcomm();
}
return INSTANCE;
}
}
package dto;
import java.io.Serializable;
import java.sql.Date;
import java.util.Arrays;
public class Panier implements Serializable {
private String[] itemIds; //panier tableau de code produit
private long nCommande;
private ClientDTO client;
private boolean processed;
public Panier(ClientDTO client){
//creation de la cle primaire
this.nCommande = System.currentTimeMillis();
this.client= client;
}
public boolean isProcessed(){
return processed;
}
public void setProcessed(boolean processed){
this.processed = processed;
}
//getter setter
public long getnCommande() {
return nCommande;
}
public void setnCommande(long nCommande) {
this.nCommande = nCommande;
}
public String[] getItemIds() {
return itemIds;
}
public void setItemIds(String[] itemIds) {
this.itemIds = itemIds;
}
public ClientDTO getClient() {
return client;
}
public void setClient(ClientDTO client) {
this.client = client;
}
//constructeur
public Panier(String[] itemIds, long nCommande, ClientDTO client) {
this.itemIds = itemIds;
this.nCommande = nCommande;
this.client = client;
}
//tostring
@Override
public String toString() {
return "Panier{" +
"itemIds=" + Arrays.toString(itemIds) +
", nCommande=" + nCommande +
", client=" + client +
'}';
}
}
package dto;
import java.io.Serializable;
public class ProduitDTO implements Serializable {
private String nProd;
private String descript;
private float prix;
private boolean dispo;
private int stock;
//GETTER SETTER
public String getnProd() {
return nProd;
}
public void setnProd(String nProd) {
this.nProd = nProd;
}
public String getDescript() {
return descript;
}
public void setDescript(String descript) {
this.descript = descript;
}
public float getPrix() {
return prix;
}
public void setPrix(float prix) {
this.prix = prix;
}
public boolean getDispo() {
return dispo;
}
public void setDispo(boolean dispo) {
this.dispo = dispo;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
//constructeur
public ProduitDTO(String nProd, String descript, float prix,boolean dispo, int stock) {
this.nProd = nProd;
this.descript = descript;
this.prix = prix;
this.dispo = dispo;
this.stock = stock;
}
//tostring
@Override
public String toString() {
return "produitDTO{" +
"nProd=" + nProd +
", descript='" + descript + '\'' +
", prix=" + prix +
", dispo='" + dispo + '\'' +
", stock=" + stock +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
\ No newline at end of file
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 14:30
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<meta http-equiv="Refresh" content="0; URL=process?action=init"/>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 17:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Catalogue</title>
</head>
<body>
<form method="post" action="process?action=addItems">
<table align="center">
<thead>
<th>id</th>
<th>descriptif</th>
<th>prix</th>
<th>disponible</th>
<th>stock</th>
</thead>
<tbody>
<c:forEach items="${catalogue.produits}" var="myitem">
<tr>
<td> <input type="checkbox" name="itemId" value="${myitem.value.nProd}">
</td>
<td>${myitem.value.descript}</td>
<td>${myitem.value.prix}</td>
<td>${myitem.value.dispo}</td>
<td>${myitem.value.stock}</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 25/02/2020
Time: 16:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Connection</title>
</head>
<body>
<form method="post" action="process?action=authenticate">
<fieldset>
<legend>login</legend>
<label>Nom</label>
<input type="text" name="nom">
<br>
<label>mot de passe</label>