Commit 13ab4131 authored by Lefebvre michael's avatar Lefebvre michael

JAV

parent 71eca17b
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 10:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Untitled document</title>
</head>
<body>
<a href="process?action=logout">Déconnection</a> | <a href="process?action=showCatalogue">Parcourir le catalogue</a> |
<a href="process?action=showDetails">Ma commande</a>
<hr>
<div>
@POP School, Cours JSP.
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 12:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Accusé de reception de la commande</title>
</head>
<body>
<h2><font face="Verdana, Arial">Merci</font></h2>
<p>
${client.nom} votre commande à été prise en compte.
</p>
<a href="process/action=showCatalogue">continuer a parcourir le catalogue</a>
<br>
<%@include file="footer.jsp"%>
</body>
</html>
......@@ -32,16 +32,18 @@
<td>${myitem.value.stock}</td>
</tr>
</c:forEach>
<tfoot>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tfoot>
</tbody>
</table>
</form>
<%@include file="footer.jsp"%>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 11:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Details</title>
</head>
<body>
<form method="post" action="process?action=validateCommande">
<table align="center">
<thead>
<th>id</th>
<th>descriptif</th>
<th>prix</th>
<th>disponible</th>
<th>stock</th>
</thead>
<tbody>
<c:forEach items="${anOrder.itemIds}" var="cle">
<tr>
<td> <input type="checkbox" checked="checked" name="itemId" value="${catalogue.produits[cle].nProd}"/>
</td>
<td>${catalogue.produits[cle].descript}</td>
<td>${catalogue.produits[cle].prix}</td>
<td>${catalogue.produits[cle].dispo}</td>
<td>${catalogue.produits[cle].stock}</td>
</tr>
</c:forEach>
<tfoot>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tfoot>
</tbody>
</table>
</form>
<%@include file="footer.jsp"%>
</body>
</html>
......@@ -22,5 +22,6 @@
</fieldset>
<input type="submit" value="envoie">
</form>
<%@include file="footer.jsp"%>
</body>
</html>
......@@ -2,6 +2,7 @@ package control;
import dto.Catalogue;
import dto.ClientDTO;
import dto.Panier;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
......@@ -9,7 +10,10 @@ import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import static dao.EcommerceDAO.*;
@WebServlet("/process")
......@@ -40,14 +44,88 @@ public class EcommerceServlet extends HttpServlet {
doInit(request, response);
}else if (action.equals("authenticate")){
doAuthenticate(request, response);
}else if (action.equals("addItems")){
doAddItems(request, response);
}else if (action.equals("validateCommande")){
doValidateCommande(request, response);
}else if (action.equals("logout")){
doLogout(request, response);
}else if (action.equals("showCatalogue")){
doShowCatalogue(request, response);
}else if (action.equals("showDetails")){
doShowDetails(request, response);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
private void doAuthenticate(HttpServletRequest request, HttpServletResponse response) {
private void doShowDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String url = "pages/showDetails.jsp";
forward(url, request,response);
}
private void doShowCatalogue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String url = "pages/showCatalogue.jsp";
forward(url, request,response);
}
private void doLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
HttpSession session = request.getSession();
if (session != null){
ClientDTO client = (ClientDTO) session.getAttribute("client");
if (client != null){
session.invalidate();
}
}
session = request.getSession(true);
response.sendRedirect("index.jsp");
}
private void doValidateCommande(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
Panier panier = (Panier) request.getSession().getAttribute("anOrder");
Catalogue catalogue = (Catalogue) getServletContext().getAttribute("catalogue");
System.out.println("panier : " + panier);
try {
//enregistrement de la commande
//et mise à jour du cache
INSTANCE.createPanier(panier, catalogue);
}catch (SQLException ex){
ex.printStackTrace();
}
String url = "pages/receipt.jsp";
forward(url, request,response);
}
private void doAddItems(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
//il s'agit soit d'une nouvelle commande vue show catalogue
//ou d'une commande modifier vue showDetails
//recuperation, des produit choisi par le clietn
String itemIds[] = request.getParameterValues("itemId");
//Récuperation du scope sesseion du client
ClientDTO client = (ClientDTO) request.getSession().getAttribute("client");
String url="";
//traitement du panier cvréation de la commande et sauvegarde de celle ci dans la session
if (itemIds != null && itemIds.length != 0){
Panier panier = new Panier(client);
panier.setItemIds(itemIds);
request.getSession().setAttribute("anOrder", panier);
url = "pages/showDetails.jsp";
}else {
//panier vide
url = "pages/emptyCart.jsp";
}
//cinematique
forward(url,request, response);
}
private void doAuthenticate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//recuperation des donnée du formauliare
String nom = request.getParameter("nom");
String motDePasse = request.getParameter("motDePasse");
......@@ -73,10 +151,12 @@ public class EcommerceServlet extends HttpServlet {
private void doInit(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
//gestion du cache application
if (this.getServletContext().getAttribute("catalogue")==null){
//si pas de catalogue on le creer
Catalogue catalogue = INSTANCE.getCatalogue();
//sauvegarde du catalogue dans l'appilication
this.getServletContext().setAttribute("catalogue", catalogue);
}
//prochaine vue
String url = "pages/showLogin.jsp";
forward(url, request, response);
......
......@@ -32,6 +32,19 @@ public enum EcommerceDAO{
private static String SQLFindClientByNom =
"SELECT motDePasse from ECOMM_CLIENT WHERE nom = ? ";
private static String SQLInsertStatementStr =
"INSERT INTO ECOMM_COMMANDE " +
" VALUES(?, current_timestamp, ?)";
private static String SQLDetailInsertStatementStr =
"INSERT INTO ECOMM_DETAIL_CDE " +
" VALUES(?, ?, ?)";
private static String SQLUpdateProduit =
"UPDATE ECOMM_PRODUIT " +
" SET QTEENSTOCK = QTEENSTOCK-1 " +
" WHERE NPRODUIT = ?";
public Catalogue getCatalogue(){
Map<String, ProduitDTO> items = new HashMap<>();
try{
......@@ -70,10 +83,41 @@ public enum EcommerceDAO{
//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{
connection.setAutoCommit(false);
//mise en place d'une transaction
PreparedStatement insertStatement = connection.prepareStatement(SQLInsertStatementStr);
PreparedStatement detailInsertStatement = connection.prepareStatement(SQLDetailInsertStatementStr);
PreparedStatement updateProduitStatement = connection.prepareStatement(SQLUpdateProduit);
//enregistrement de l'entete de la commande
insertStatement.setLong(1,panier.getnCommande());
insertStatement.setString(2, panier.getClient().getNom());
int nb = insertStatement.executeUpdate();
//renregistrement des details de la commande et mise a jour stock
for (String refItem : panier.getItemIds()){
//creation d'un detail
detailInsertStatement.setLong(1, panier.getnCommande());
detailInsertStatement.setLong(2, 1);
detailInsertStatement.setString(3, refItem);
nb = detailInsertStatement.executeUpdate();
updateProduitStatement.setString(1, refItem);
nb = updateProduitStatement.executeUpdate();
ProduitDTO produit = catalogue.getProduit(refItem);
produit.setStock(produit.getStock()-1);
catalogue.setProduit(refItem, produit);
}
connection.commit();
}
public static void main(String[] args) {
}
}
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;
}
}
......@@ -8,7 +8,7 @@ public class Panier implements Serializable {
private String[] itemIds; //panier tableau de code produit
private long nCommande;
private ClientDTO client;
private boolean processed;
private boolean processed = false;
public Panier(ClientDTO client){
//creation de la cle primaire
......
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 10:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Untitled document</title>
</head>
<body>
<a href="process?action=logout">Déconnection</a> | <a href="process?action=showCatalogue">Parcourir le catalogue</a> |
<a href="process?action=showDetails">Ma commande</a>
<hr>
<div>
@POP School, Cours JSP.
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 12:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Accusé de reception de la commande</title>
</head>
<body>
<h2><font face="Verdana, Arial">Merci</font></h2>
<p>
${client.nom} votre commande à été prise en compte.
</p>
<a href="process/action=showCatalogue">continuer a parcourir le catalogue</a>
<br>
<%@include file="footer.jsp"%>
</body>
</html>
......@@ -32,16 +32,18 @@
<td>${myitem.value.stock}</td>
</tr>
</c:forEach>
<tfoot>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tfoot>
</tbody>
</table>
</form>
<%@include file="footer.jsp"%>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: michael
Date: 26/02/2020
Time: 11:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Details</title>
</head>
<body>
<form method="post" action="process?action=validateCommande">
<table align="center">
<thead>
<th>id</th>
<th>descriptif</th>
<th>prix</th>
<th>disponible</th>
<th>stock</th>
</thead>
<tbody>
<c:forEach items="${anOrder.itemIds}" var="cle">
<tr>
<td> <input type="checkbox" checked="checked" name="itemId" value="${catalogue.produits[cle].nProd}"/>
</td>
<td>${catalogue.produits[cle].descript}</td>
<td>${catalogue.produits[cle].prix}</td>
<td>${catalogue.produits[cle].dispo}</td>
<td>${catalogue.produits[cle].stock}</td>
</tr>
</c:forEach>
<tfoot>
<tr>
<td colspan="5">
<input type="submit" value="envoie">
</td>
</tr>
</tfoot>
</tbody>
</table>
</form>
<%@include file="footer.jsp"%>
</body>
</html>
......@@ -22,5 +22,6 @@
</fieldset>
<input type="submit" value="envoie">
</form>
<%@include file="footer.jsp"%>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="Hopital_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>modele.Service</class>
<class>modele.Medecin</class>
<class>modele.Malade</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hopital"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="michael"/>
<property name="javax.persistence.jdbc.password" value="mdppopmichael"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="jpa" name="JPA">
<configuration>
<setting name="validation-enabled" value="true" />
<setting name="provider-name" value="EclipseLink" />
<datasource-mapping>
<factory-entry name="Hopital_PU" />
</datasource-mapping>
<naming-strategy-map />
<deploymentDescriptor name="persistence.xml" url="file://$MODULE_DIR$/src/META-INF/persistence.xml" />
</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" name="JPA 2.0" level="project" />
<orderEntry type="library" name="mysql-connector-java-5.1.6-bin" level="project" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.1">
<persistence-unit name="Hopital_PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>modele.Service</class>
<class>modele.Medecin</class>
<class>modele.Malade</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hopital"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="michael"/>
<property name="javax.persistence.jdbc.password" value="mdppopmichael"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
\ No newline at end of file
package modele;
import javax.persistence.Embeddable;
import java.io.Serializable;
@Embeddable
public class Adresse implements Serializable {
private int numero;
private String rue;
private String ville;
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
public String getRue() {
return rue;
}
public void setRue(String rue) {
this.rue = rue;
}
public String getVille() {
return ville;
}
public void setVille(String ville) {
this.ville = ville;
}
protected Adresse(){}
public Adresse(int numero, String rue, String ville) {
this.numero = numero;
this.rue = rue;
this.ville = ville;
}
@Override
public String toString() {
return "Adresse{" +
"numero=" + numero +
", rue='" + rue + '\'' +
", ville='" + ville + '\'' +
'}';
}
}
package modele;
import javax.persistence.*;
import java.io.Serializable;
@Entity
public class Malade implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(nullable = false, length = 30)
private String nom;
@Column(nullable = false, length = 30)
private String prenom;
@Embedded
private Adresse adresse = new Adresse(10,"rue des Tulipes", "Lille");
protected Malade() {
}
public Malade(String nom, String prenom, Adresse adresse) {
this.nom = nom;
this.prenom = prenom;
this.adresse = adresse;
}
}
package modele;
import javax.persistence.*;
import java.io.Serializable;
import java.util.*;
@Entity
@Table(name = "Medecin", uniqueConstraints = {@UniqueConstraint(columnNames = {"nom", "prenom"})})
public class Medecin implements Serializable {
private static final long serialVersion = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
private int id;
@Column(nullable = false, length = 30)
private String nom;
@Column(nullable = false, length = 30)
private String prenom;
private float salaire;
@ManyToOne
@JoinColumn(name="service_id")
private Service service;
@ManyToOne
@JoinColumn(name = "manager_id")
private Medecin mgr;
@OneToMany(mappedBy = "mgr")
private Set<Medecin> subs = new HashSet<>();
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 float getSalaire()