forked from ltearno/formations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
284 additions
and
21 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...s/javaee/jpa-101/src/main/java/fr/lteconsulting/training/moviedb/ejb/GestionCommande.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package fr.lteconsulting.training.moviedb.ejb; | ||
|
||
import fr.lteconsulting.training.moviedb.model.Commande; | ||
|
||
import javax.ejb.Stateless; | ||
|
||
@Stateless | ||
public class GestionCommande extends GestionGenerique<Commande> { | ||
public GestionCommande() throws NoSuchFieldException { | ||
super(Commande.class); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rojets/javaee/jpa-101/src/main/java/fr/lteconsulting/training/moviedb/model/Commande.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package fr.lteconsulting.training.moviedb.model; | ||
|
||
import javax.persistence.*; | ||
import javax.validation.constraints.NotNull; | ||
import java.util.Date; | ||
|
||
@Entity | ||
public class Commande { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer id; | ||
|
||
@ManyToOne | ||
@NotNull | ||
private Utilisateur createur; | ||
|
||
@ManyToOne | ||
@NotNull | ||
private Produit produit; | ||
|
||
//@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") | ||
private Date lastUpdateDate; | ||
|
||
private int quantite; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Integer id) { | ||
this.id = id; | ||
} | ||
|
||
public Utilisateur getCreateur() { | ||
return createur; | ||
} | ||
|
||
public void setCreateur(Utilisateur createur) { | ||
this.createur = createur; | ||
} | ||
|
||
public Produit getProduit() { | ||
return produit; | ||
} | ||
|
||
public void setProduit(Produit produit) { | ||
this.produit = produit; | ||
} | ||
|
||
public Date getLastUpdateDate() { | ||
return lastUpdateDate; | ||
} | ||
|
||
public void setLastUpdateDate(Date lastUpdateDate) { | ||
this.lastUpdateDate = lastUpdateDate; | ||
} | ||
|
||
public int getQuantite() { | ||
return quantite; | ||
} | ||
|
||
public void setQuantite(int quantite) { | ||
this.quantite = quantite; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...aee/jpa-101/src/main/java/fr/lteconsulting/training/moviedb/servlet/CommandesServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package fr.lteconsulting.training.moviedb.servlet; | ||
|
||
import fr.lteconsulting.training.moviedb.ejb.GestionCommande; | ||
import fr.lteconsulting.training.moviedb.model.Commande; | ||
import fr.lteconsulting.training.moviedb.outil.Vues; | ||
|
||
import javax.inject.Inject; | ||
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 java.util.List; | ||
|
||
@WebServlet("/commandes") | ||
public class CommandesServlet extends HttpServlet { | ||
@Inject | ||
private GestionCommande gestionCommande; | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
List<Commande> commandes = gestionCommande.findAll(); | ||
|
||
Vues.afficherCommandes(req, resp, commandes); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...a-101/src/main/java/fr/lteconsulting/training/moviedb/servlet/EditionCommandeServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package fr.lteconsulting.training.moviedb.servlet; | ||
|
||
import fr.lteconsulting.training.moviedb.ejb.GestionCommande; | ||
import fr.lteconsulting.training.moviedb.ejb.GestionProduits; | ||
import fr.lteconsulting.training.moviedb.model.Commande; | ||
import fr.lteconsulting.training.moviedb.outil.Session; | ||
import fr.lteconsulting.training.moviedb.outil.Vues; | ||
|
||
import javax.inject.Inject; | ||
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; | ||
|
||
@WebServlet("/editionCommande") | ||
public class EditionCommandeServlet extends HttpServlet { | ||
@Inject | ||
private GestionCommande gestionCommandes; | ||
|
||
@Inject | ||
private GestionProduits gestionProduits; | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
Commande commande = null; | ||
|
||
try { | ||
int id = Integer.parseInt(req.getParameter("id")); | ||
|
||
commande = gestionCommandes.findById(id); | ||
if (commande == null) { | ||
resp.sendRedirect("commandes"); | ||
return; | ||
} | ||
} catch (Exception e) { | ||
commande = new Commande(); | ||
commande.setQuantite(1); | ||
} | ||
|
||
Vues.afficherEditionCommande(req, resp, commande, gestionProduits.findAll(), "Saisissez votre commande"); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
Commande commande = new Commande(); | ||
try { | ||
commande.setId(Integer.parseInt(req.getParameter("id"))); | ||
} catch (Exception e) { | ||
} | ||
|
||
commande.setCreateur(Session.getUtilisateurConnecte(req.getSession())); | ||
commande.setQuantite(Integer.parseInt(req.getParameter("quantite"))); | ||
commande.setProduit(gestionProduits.findById(Integer.parseInt(req.getParameter("produitId")))); | ||
|
||
try { | ||
gestionCommandes.update(commande); | ||
} catch (Exception e) { | ||
Vues.afficherEditionCommande(req, resp, commande, gestionProduits.findAll(), "Commande erronée : " + e.getMessage()); | ||
} | ||
|
||
resp.sendRedirect("commandes"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
formation-programmation-java/projets/javaee/jpa-101/src/main/webapp/WEB-INF/commandes.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<%@ page import="fr.lteconsulting.training.moviedb.model.Commande" %> | ||
<%@ page import="java.util.List" %> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
|
||
<% | ||
List<Commande> commandes = (List<Commande>) request.getAttribute("commandes"); | ||
%> | ||
|
||
<h1>Commandes</h1> | ||
<table> | ||
<tr> | ||
<th>ID</th> | ||
<th>Créateur</th> | ||
<th>Produit</th> | ||
<th>Date dernière modification</th> | ||
<th>Quantité</th> | ||
</tr> | ||
<% | ||
for (Commande commande : commandes) { | ||
%> | ||
<tr> | ||
<td><%= commande.getId() %> | ||
</td> | ||
<td><%= commande.getCreateur().getNom()%> | ||
</td> | ||
<td><%= commande.getProduit().getNom()%> | ||
</td> | ||
<td><%= commande.getLastUpdateDate() %> | ||
</td> | ||
<td><%= commande.getQuantite() %> | ||
</td> | ||
<td> | ||
<form method="get" action="editionCommande" style="display: inline-block;"> | ||
<input type="hidden" name="id" value="<%= commande.getId()%>"> | ||
<button>éditer</button> | ||
</form> | ||
|
||
<form method="post" action="suppressionCommande" style="display: inline-block;"> | ||
<input type="hidden" name="id" value="<%= commande.getId()%>"> | ||
<button>supprimer</button> | ||
</form> | ||
</td> | ||
</tr> | ||
<% | ||
} | ||
%> | ||
|
||
<tr> | ||
<td></td> | ||
<td> | ||
<form method="get" action="editionCommande"> | ||
<button>ajouter</button> | ||
</form> | ||
</td> | ||
<td></td> | ||
</tr> | ||
</table> |
29 changes: 29 additions & 0 deletions
29
...ion-programmation-java/projets/javaee/jpa-101/src/main/webapp/WEB-INF/editionCommande.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<%@ page import="fr.lteconsulting.training.moviedb.model.Commande" %> | ||
<%@ page import="fr.lteconsulting.training.moviedb.model.Produit" %> | ||
<%@ page import="java.util.List" %> | ||
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | ||
|
||
<% | ||
Commande commande = (Commande) request.getAttribute("commande"); | ||
List<Produit> produits = (List<Produit>) request.getAttribute("produits"); | ||
int selectedProduitId = commande.getProduit() != null ? commande.getProduit().getId() : -1; | ||
%> | ||
|
||
<h1>Entrez votre commande</h1> | ||
|
||
<h2>${message}</h2> | ||
|
||
<form method="post"> | ||
<input type="hidden" name="id" value="<%=commande.getId()!=null?commande.getId():"" %>"> | ||
<label>Quantité: <input type="text" name="quantite" value="<%=commande.getQuantite()%>" autofocus></label> | ||
<label>Produit : <select name="produitId"> | ||
<% for (Produit produit : produits) { | ||
%> | ||
<option value="<%= produit.getId()%>" <%= produit.getId() == selectedProduitId ? "selected" : "" %>><%= produit.getNom()%> | ||
</option> | ||
<% | ||
}%> | ||
</select></label> | ||
<button>Valider</button> | ||
</form> |