Skip to content

Commit

Permalink
javaee-projets - commandes
Browse files Browse the repository at this point in the history
  • Loading branch information
ltearno committed Nov 17, 2017
1 parent f0a92d9 commit d55189f
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 21 deletions.
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);
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
@WebFilter(filterName = "EncodingFilter", urlPatterns = "*")
public class EncodingFilter implements Filter {
public void init(FilterConfig config) throws ServletException {

}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
Expand All @@ -19,7 +18,6 @@ public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain
chain.doFilter(req, resp);
}


public void destroy() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.lteconsulting.training.moviedb.outil;

import fr.lteconsulting.training.moviedb.model.Categorie;
import fr.lteconsulting.training.moviedb.model.Commande;
import fr.lteconsulting.training.moviedb.model.Fabricant;
import fr.lteconsulting.training.moviedb.model.Produit;

Expand Down Expand Up @@ -70,6 +71,20 @@ public static void afficherResultatImportation(HttpServletRequest req, HttpServl
afficherPage(req, resp, "Résultat importation", "resultatImportation");
}

public static void afficherCommandes(HttpServletRequest req, HttpServletResponse resp, List<Commande> commandes) throws ServletException, IOException {
req.setAttribute("commandes", commandes);

afficherPage(req, resp, "Liste des commandes", "commandes");
}

public static void afficherEditionCommande(HttpServletRequest req, HttpServletResponse resp, Commande commande, List<Produit> produits, String message) throws ServletException, IOException {
req.setAttribute("commande", commande);
req.setAttribute("produits", produits);
req.setAttribute("message", message);

afficherPage(req, resp, "Edition commande", "editionCommande");
}

public static void afficherPage(HttpServletRequest req, HttpServletResponse resp, String title, String pageToDisplay) throws ServletException, IOException {
req.setAttribute("title", title);
req.setAttribute("pageToDisplay", pageToDisplay + ".jsp");
Expand Down
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);
}
}
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "SecurityFilter", urlPatterns = {
"/produits",
"/editionProduit",
"/suppressionProduit",
"/categories",
"/editionCategorie",
"/suppressionCategorie",
"/fabricants",
"/editionFabricant",
"/suppressionFabricant",
"/export.xls",
"/importation"
})
@WebFilter(filterName = "SecurityFilter", urlPatterns = "*")
public class SecurityFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}

public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
if (req instanceof HttpServletRequest && resp instanceof HttpServletResponse) {
if (!Session.estConnecte((HttpServletRequest) req)) {
((HttpServletResponse) resp).sendRedirect("login");
return;
}
}

chain.doFilter(req, resp);
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;

if (request.getRequestURI().endsWith(".css")
|| request.getRequestURI().endsWith("/login")
|| request.getRequestURI().endsWith("/inscription")
|| Session.estConnecte(request))
chain.doFilter(req, resp);
else
response.sendRedirect("login");
} else {
chain.doFilter(req, resp);
}
}

public void destroy() {
Expand Down
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>
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>

0 comments on commit d55189f

Please sign in to comment.